1 /* guestmount - mount guests using libguestfs and FUSE
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Derived from the example program 'fusexmp.c':
19 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
21 * This program can be distributed under the terms of the GNU GPL.
22 * See the file COPYING.
25 #define FUSE_USE_VERSION 26
42 #include <sys/types.h>
51 /* See <attr/xattr.h> */
53 #define ENOATTR ENODATA
58 #define _(str) dgettext(PACKAGE, (str))
59 //#define N_(str) dgettext(PACKAGE, (str))
66 bad_cast (char const *s)
71 static guestfs_h *g = NULL;
72 static int read_only = 0;
74 int dir_cache_timeout = 60;
76 /* This is ugly: guestfs errors are strings, FUSE wants -errno. We
77 * have to do the conversion as best we can.
85 const char *err = guestfs_last_error (g);
91 fprintf (stderr, "%s\n", err);
93 /* Add a few of our own ... */
95 /* This indicates guestfsd died. Translate into a hard EIO error.
96 * Arguably we could relaunch the guest if we hit this error.
98 if (strstr (err, "call launch before using this function"))
101 /* See if it matches an errno string in the host. */
102 for (i = 0; i < MAX_ERRNO; ++i) {
103 const char *e = strerror (i);
104 if (e && strstr (err, e) != NULL)
108 /* Too bad, return a generic error. */
112 static struct guestfs_xattr_list *
113 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
115 struct guestfs_xattr_list *xattrs;
117 xattrs = malloc (sizeof *xattrs);
118 if (xattrs == NULL) {
124 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
125 if (xattrs->val == NULL) {
132 for (i = 0; i < num; ++i) {
133 xattrs->val[i].attrname = strdup (first[i].attrname);
134 xattrs->val[i].attrval_len = first[i].attrval_len;
135 xattrs->val[i].attrval = malloc (first[i].attrval_len);
136 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
143 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
144 off_t offset, struct fuse_file_info *fi)
149 dir_cache_remove_all_expired (now);
151 struct guestfs_dirent_list *ents;
153 ents = guestfs_readdir (g, path);
158 for (i = 0; i < ents->len; ++i) {
160 memset (&stat, 0, sizeof stat);
162 stat.st_ino = ents->val[i].ino;
163 switch (ents->val[i].ftyp) {
164 case 'b': stat.st_mode = S_IFBLK; break;
165 case 'c': stat.st_mode = S_IFCHR; break;
166 case 'd': stat.st_mode = S_IFDIR; break;
167 case 'f': stat.st_mode = S_IFIFO; break;
168 case 'l': stat.st_mode = S_IFLNK; break;
169 case 'r': stat.st_mode = S_IFREG; break;
170 case 's': stat.st_mode = S_IFSOCK; break;
173 default: stat.st_mode = 0;
176 /* Copied from the example, which also ignores 'offset'. I'm
177 * not quite sure how this is ever supposed to work on large
180 if (filler (buf, ents->val[i].name, &stat, 0))
184 /* Now prepopulate the directory caches. This step is just an
185 * optimization, don't worry if it fails.
187 char **names = malloc ((ents->len + 1) * sizeof (char *));
189 for (i = 0; i < ents->len; ++i)
190 names[i] = ents->val[i].name;
193 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
195 for (i = 0; i < ss->len; ++i) {
196 if (ss->val[i].ino >= 0) {
199 statbuf.st_dev = ss->val[i].dev;
200 statbuf.st_ino = ss->val[i].ino;
201 statbuf.st_mode = ss->val[i].mode;
202 statbuf.st_nlink = ss->val[i].nlink;
203 statbuf.st_uid = ss->val[i].uid;
204 statbuf.st_gid = ss->val[i].gid;
205 statbuf.st_rdev = ss->val[i].rdev;
206 statbuf.st_size = ss->val[i].size;
207 statbuf.st_blksize = ss->val[i].blksize;
208 statbuf.st_blocks = ss->val[i].blocks;
209 statbuf.st_atime = ss->val[i].atime;
210 statbuf.st_mtime = ss->val[i].mtime;
211 statbuf.st_ctime = ss->val[i].ctime;
213 lsc_insert (path, names[i], now, &statbuf);
216 guestfs_free_stat_list (ss);
219 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
222 struct guestfs_xattr *first;
223 struct guestfs_xattr_list *copy;
224 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
225 assert (strlen (xattrs->val[i].attrname) == 0);
226 if (xattrs->val[i].attrval_len > 0) {
228 first = &xattrs->val[i];
230 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
233 copy = copy_xattr_list (first, num);
235 xac_insert (path, names[ni], now, copy);
240 guestfs_free_xattr_list (xattrs);
243 char **links = guestfs_readlinklist (g, path, names);
245 for (i = 0; names[i] != NULL; ++i) {
247 /* Note that rlc_insert owns the string links[i] after this, */
248 rlc_insert (path, names[i], now, links[i]);
250 /* which is why we have to free links[i] here. */
253 free (links); /* free the array, not the strings */
259 guestfs_free_dirent_list (ents);
265 fg_getattr (const char *path, struct stat *statbuf)
267 const struct stat *buf;
269 buf = lsc_lookup (path);
271 memcpy (statbuf, buf, sizeof *statbuf);
275 struct guestfs_stat *r;
277 r = guestfs_lstat (g, path);
281 statbuf->st_dev = r->dev;
282 statbuf->st_ino = r->ino;
283 statbuf->st_mode = r->mode;
284 statbuf->st_nlink = r->nlink;
285 statbuf->st_uid = r->uid;
286 statbuf->st_gid = r->gid;
287 statbuf->st_rdev = r->rdev;
288 statbuf->st_size = r->size;
289 statbuf->st_blksize = r->blksize;
290 statbuf->st_blocks = r->blocks;
291 statbuf->st_atime = r->atime;
292 statbuf->st_mtime = r->mtime;
293 statbuf->st_ctime = r->ctime;
295 guestfs_free_stat (r);
300 /* Nautilus loves to use access(2) to test everything about a file,
301 * such as whether it's executable. Therefore treat this a lot like
305 fg_access (const char *path, int mask)
310 if (read_only && (mask & W_OK))
313 r = fg_getattr (path, &statbuf);
314 if (r < 0 || mask == F_OK)
317 struct fuse_context *fuse = fuse_get_context ();
322 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
323 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
324 : statbuf.st_mode & S_IROTH);
327 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
328 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
329 : statbuf.st_mode & S_IWOTH);
332 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
333 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
334 : statbuf.st_mode & S_IXOTH);
336 return ok ? 0 : -EACCES;
340 fg_readlink (const char *path, char *buf, size_t size)
345 r = rlc_lookup (path);
347 r = guestfs_readlink (g, path);
353 /* Note this is different from the real readlink(2) syscall. FUSE wants
354 * the string to be always nul-terminated, even if truncated.
356 size_t len = strlen (r);
360 memcpy (buf, r, len);
370 fg_mknod (const char *path, mode_t mode, dev_t rdev)
374 if (read_only) return -EROFS;
376 dir_cache_invalidate (path);
378 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
386 fg_mkdir (const char *path, mode_t mode)
390 if (read_only) return -EROFS;
392 dir_cache_invalidate (path);
394 r = guestfs_mkdir_mode (g, path, mode);
402 fg_unlink (const char *path)
406 if (read_only) return -EROFS;
408 dir_cache_invalidate (path);
410 r = guestfs_rm (g, path);
418 fg_rmdir (const char *path)
422 if (read_only) return -EROFS;
424 dir_cache_invalidate (path);
426 r = guestfs_rmdir (g, path);
434 fg_symlink (const char *from, const char *to)
438 if (read_only) return -EROFS;
440 dir_cache_invalidate (to);
442 r = guestfs_ln_s (g, to, from);
450 fg_rename (const char *from, const char *to)
454 if (read_only) return -EROFS;
456 dir_cache_invalidate (to);
458 /* XXX It's not clear how close the 'mv' command is to the
459 * rename syscall. We might need to add the rename syscall
460 * to the guestfs(3) API.
462 r = guestfs_mv (g, from, to);
470 fg_link (const char *from, const char *to)
474 if (read_only) return -EROFS;
476 dir_cache_invalidate (to);
478 r = guestfs_ln (g, to, from);
486 fg_chmod (const char *path, mode_t mode)
490 if (read_only) return -EROFS;
492 dir_cache_invalidate (path);
494 r = guestfs_chmod (g, mode, path);
502 fg_chown (const char *path, uid_t uid, gid_t gid)
506 if (read_only) return -EROFS;
508 dir_cache_invalidate (path);
510 r = guestfs_lchown (g, uid, gid, path);
518 fg_truncate (const char *path, off_t size)
522 if (read_only) return -EROFS;
524 dir_cache_invalidate (path);
526 r = guestfs_truncate_size (g, path, size);
534 fg_utimens (const char *path, const struct timespec ts[2])
538 if (read_only) return -EROFS;
540 dir_cache_invalidate (path);
542 time_t atsecs = ts[0].tv_sec;
543 long atnsecs = ts[0].tv_nsec;
544 time_t mtsecs = ts[1].tv_sec;
545 long mtnsecs = ts[1].tv_nsec;
547 if (atnsecs == UTIME_NOW)
549 if (atnsecs == UTIME_OMIT)
551 if (mtnsecs == UTIME_NOW)
553 if (mtnsecs == UTIME_OMIT)
556 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
563 /* This call is quite hard to emulate through the guestfs(3) API. In
564 * one sense it's a little like access (see above) because it tests
565 * whether opening a file would succeed given the flags. But it also
566 * has side effects such as truncating the file if O_TRUNC is given.
567 * Therefore we need to emulate it ... painfully.
570 fg_open (const char *path, struct fuse_file_info *fi)
574 if (fi->flags & O_WRONLY) {
579 exists = guestfs_exists (g, path);
583 if (fi->flags & O_CREAT) {
587 dir_cache_invalidate (path);
589 /* Exclusive? File must not exist already. */
590 if (fi->flags & O_EXCL) {
595 /* Create? Touch it and optionally truncate it. */
596 r = guestfs_touch (g, path);
600 if (fi->flags & O_TRUNC) {
601 r = guestfs_truncate (g, path);
606 /* Not create, just check it exists. */
615 fg_read (const char *path, char *buf, size_t size, off_t offset,
616 struct fuse_file_info *fi)
622 fprintf (stderr, "fg_read: %s: size %zu offset %zu\n",
625 /* The guestfs protocol limits size to somewhere over 2MB. We just
626 * reduce the requested size here accordingly and push the problem
627 * up to every user. http://www.jwz.org/doc/worse-is-better.html
629 const size_t limit = 2 * 1024 * 1024;
633 r = guestfs_pread (g, path, size, offset, &rsize);
637 /* This should never happen, but at least it stops us overflowing
638 * the output buffer if it does happen.
643 memcpy (buf, r, rsize);
650 fg_write (const char *path, const char *buf, size_t size,
651 off_t offset, struct fuse_file_info *fi)
653 if (read_only) return -EROFS;
655 dir_cache_invalidate (path);
657 return -ENOSYS; /* XXX */
661 fg_statfs (const char *path, struct statvfs *stbuf)
663 struct guestfs_statvfs *r;
665 r = guestfs_statvfs (g, path);
669 stbuf->f_bsize = r->bsize;
670 stbuf->f_frsize = r->frsize;
671 stbuf->f_blocks = r->blocks;
672 stbuf->f_bfree = r->bfree;
673 stbuf->f_bavail = r->bavail;
674 stbuf->f_files = r->files;
675 stbuf->f_ffree = r->ffree;
676 stbuf->f_favail = r->favail;
677 stbuf->f_fsid = r->fsid;
678 stbuf->f_flag = r->flag;
679 stbuf->f_namemax = r->namemax;
681 guestfs_free_statvfs (r);
687 fg_release (const char *path, struct fuse_file_info *fi)
689 /* Just a stub. This method is optional and can safely be left
695 /* Emulate this by calling sync. */
696 static int fg_fsync(const char *path, int isdatasync,
697 struct fuse_file_info *fi)
701 r = guestfs_sync (g);
709 fg_setxattr (const char *path, const char *name, const char *value,
710 size_t size, int flags)
714 if (read_only) return -EROFS;
716 dir_cache_invalidate (path);
718 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
719 r = guestfs_lsetxattr (g, name, value, size, path);
726 /* The guestfs(3) API for getting xattrs is much easier to use
727 * than the real syscall. Unfortunately we now have to emulate
728 * the real syscall using that API :-(
731 fg_getxattr (const char *path, const char *name, char *value,
734 const struct guestfs_xattr_list *xattrs;
737 xattrs = xac_lookup (path);
738 if (xattrs == NULL) {
739 xattrs = guestfs_lgetxattrs (g, path);
747 for (i = 0; i < xattrs->len; ++i) {
748 if (strcmp (xattrs->val[i].attrname, name) == 0) {
749 size_t sz = xattrs->val[i].attrval_len;
752 memcpy (value, xattrs->val[i].attrval, sz);
759 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
764 /* Ditto as above. */
766 fg_listxattr (const char *path, char *list, size_t size)
768 const struct guestfs_xattr_list *xattrs;
771 xattrs = xac_lookup (path);
772 if (xattrs == NULL) {
773 xattrs = guestfs_lgetxattrs (g, path);
781 for (i = 0; i < xattrs->len; ++i) {
782 size_t len = strlen (xattrs->val[i].attrname) + 1;
784 memcpy (list, xattrs->val[i].attrname, len);
795 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
801 fg_removexattr(const char *path, const char *name)
805 if (read_only) return -EROFS;
807 dir_cache_invalidate (path);
809 r = guestfs_lremovexattr (g, name, path);
816 static struct fuse_operations fg_operations = {
817 .getattr = fg_getattr,
819 .readlink = fg_readlink,
820 .readdir = fg_readdir,
823 .symlink = fg_symlink,
830 .truncate = fg_truncate,
831 .utimens = fg_utimens,
836 .release = fg_release,
838 .setxattr = fg_setxattr,
839 .getxattr = fg_getxattr,
840 .listxattr = fg_listxattr,
841 .removexattr = fg_removexattr,
855 static void add_drives (struct drv *);
856 static void mount_mps (struct mp *);
858 static void __attribute__((noreturn))
861 const char *tmp_argv[] = { program_name, "--help", NULL };
862 fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
866 static void __attribute__((noreturn))
869 if (status != EXIT_SUCCESS)
870 fprintf (stderr, _("Try `%s --help' for more information.\n"),
874 _("%s: FUSE module for libguestfs\n"
875 "%s lets you mount a virtual machine filesystem\n"
876 "Copyright (C) 2009 Red Hat Inc.\n"
878 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
880 " -a|--add image Add image\n"
881 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
882 " --fuse-help Display extra FUSE options\n"
883 " --help Display help message and exit\n"
884 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
885 " -n|--no-sync Don't autosync\n"
886 " -o|--option opt Pass extra option to FUSE\n"
887 " -r|--ro Mount read-only\n"
888 " --selinux Enable SELinux support\n"
889 " --trace Trace guestfs API calls (to stderr)\n"
890 " -v|--verbose Verbose messages\n"
891 " -V|--version Display version and exit\n"
893 program_name, program_name, program_name);
899 main (int argc, char *argv[])
901 enum { HELP_OPTION = CHAR_MAX + 1 };
903 /* The command line arguments are broadly compatible with (a subset
904 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
906 static const char *options = "a:m:no:rv?V";
907 static const struct option long_options[] = {
908 { "add", 1, 0, 'a' },
909 { "dir-cache-timeout", 1, 0, 0 },
910 { "fuse-help", 0, 0, 0 },
911 { "help", 0, 0, HELP_OPTION },
912 { "mount", 1, 0, 'm' },
913 { "no-sync", 0, 0, 'n' },
914 { "option", 1, 0, 'o' },
916 { "selinux", 0, 0, 0 },
917 { "trace", 0, 0, 0 },
918 { "verbose", 0, 0, 'v' },
919 { "version", 0, 0, 'V' },
923 struct drv *drvs = NULL;
925 struct mp *mps = NULL;
933 const char **fuse_argv = NULL;
935 #define ADD_FUSE_ARG(str) \
938 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
940 perror ("realloc"); \
943 fuse_argv[fuse_argc-1] = (str); \
944 fuse_argv[fuse_argc] = NULL; \
947 /* LC_ALL=C is required so we can parse error messages. */
948 setenv ("LC_ALL", "C", 1);
950 /* Set global program name that is not polluted with libtool artifacts. */
951 set_program_name (argv[0]);
953 memset (&sa, 0, sizeof sa);
954 sa.sa_handler = SIG_IGN;
955 sa.sa_flags = SA_RESTART;
956 sigaction (SIGPIPE, &sa, NULL);
958 /* Various initialization. */
961 g = guestfs_create ();
963 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
967 guestfs_set_autosync (g, 1);
968 guestfs_set_recovery_proc (g, 0);
970 ADD_FUSE_ARG (program_name);
971 /* MUST be single-threaded. You cannot have two threads accessing the
972 * same libguestfs handle, and opening more than one handle is likely
973 * to be very expensive.
977 /* If developing, add ./appliance to the path. Note that libtools
978 * interferes with this because uninstalled guestfish is a shell
979 * script that runs the real program with an absolute path. Detect
982 * BUT if LIBGUESTFS_PATH environment variable is already set by
983 * the user, then don't override it.
985 if (getenv ("LIBGUESTFS_PATH") == NULL &&
987 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
988 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
991 c = getopt_long (argc, argv, options, long_options, &option_index);
995 case 0: /* options which are long only */
996 if (strcmp (long_options[option_index].name, "dir-cache-timeout") == 0)
997 dir_cache_timeout = atoi (optarg);
998 else if (strcmp (long_options[option_index].name, "fuse-help") == 0)
1000 else if (strcmp (long_options[option_index].name, "selinux") == 0)
1001 guestfs_set_selinux (g, 1);
1002 else if (strcmp (long_options[option_index].name, "trace") == 0) {
1003 ADD_FUSE_ARG ("-f");
1004 guestfs_set_trace (g, 1);
1005 guestfs_set_recovery_proc (g, 1);
1008 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
1009 program_name, long_options[option_index].name, option_index);
1015 if (access (optarg, R_OK) != 0) {
1019 drv = malloc (sizeof (struct drv));
1024 drv->filename = optarg;
1030 mp = malloc (sizeof (struct mp));
1035 p = strchr (optarg, ':');
1038 mp->mountpoint = p+1;
1040 mp->mountpoint = bad_cast ("/");
1041 mp->device = optarg;
1047 guestfs_set_autosync (g, 0);
1051 ADD_FUSE_ARG ("-o");
1052 ADD_FUSE_ARG (optarg);
1061 guestfs_set_verbose (g, verbose);
1065 printf ("%s %s\n", program_name, PACKAGE_VERSION);
1076 /* We must have at least one -a and at least one -m. */
1077 if (!drvs || !mps) {
1079 _("%s: must have at least one -a and at least one -m option\n"),
1084 /* We'd better have a mountpoint. */
1085 if (optind+1 != argc) {
1087 _("%s: you must specify a mountpoint in the host filesystem\n"),
1092 /* Do the guest drives and mountpoints. */
1094 if (guestfs_launch (g) == -1)
1098 /* FUSE example does this, not clear if it's necessary, but ... */
1099 if (guestfs_umask (g, 0) == -1)
1102 /* At the last minute, remove the libguestfs error handler. In code
1103 * above this point, the default error handler has been used which
1104 * sends all errors to stderr. Now before entering FUSE itself we
1105 * want to silence errors so we can convert them (see error()
1108 guestfs_set_error_handler (g, NULL, NULL);
1110 /* Finish off FUSE args. */
1111 ADD_FUSE_ARG (argv[optind]);
1114 It says about the line containing the for-statement:
1115 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1118 fprintf (stderr, "guestmount: invoking FUSE with args [");
1119 for (i = 0; i < fuse_argc; ++i) {
1120 if (i > 0) fprintf (stderr, ", ");
1121 fprintf (stderr, "%s", fuse_argv[i]);
1123 fprintf (stderr, "]\n");
1127 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1133 exit (r == -1 ? 1 : 0);
1136 /* List is built in reverse order, so add them in reverse order. */
1138 add_drives (struct drv *drv)
1143 add_drives (drv->next);
1145 r = guestfs_add_drive (g, drv->filename);
1147 r = guestfs_add_drive_ro (g, drv->filename);
1153 /* List is built in reverse order, so mount them in reverse order. */
1155 mount_mps (struct mp *mp)
1160 mount_mps (mp->next);
1162 r = guestfs_mount (g, mp->device, mp->mountpoint);
1164 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);