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>
49 #include "guestmount.h"
52 /* See <attr/xattr.h> */
54 #define ENOATTR ENODATA
57 static guestfs_h *g = NULL;
58 static int read_only = 0;
60 int dir_cache_timeout = 60;
62 /* This is ugly: guestfs errors are strings, FUSE wants -errno. We
63 * have to do the conversion as best we can.
71 const char *err = guestfs_last_error (g);
77 fprintf (stderr, "%s\n", err);
79 /* Add a few of our own ... */
81 /* This indicates guestfsd died. Translate into a hard EIO error.
82 * Arguably we could relaunch the guest if we hit this error.
84 if (strstr (err, "call launch before using this function"))
87 /* See if it matches an errno string in the host. */
88 for (i = 0; i < MAX_ERRNO; ++i) {
89 const char *e = strerror (i);
90 if (e && strstr (err, e) != NULL)
94 /* Too bad, return a generic error. */
98 static struct guestfs_xattr_list *
99 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
101 struct guestfs_xattr_list *xattrs;
103 xattrs = malloc (sizeof *xattrs);
104 if (xattrs == NULL) {
110 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
111 if (xattrs->val == NULL) {
118 for (i = 0; i < num; ++i) {
119 xattrs->val[i].attrname = strdup (first[i].attrname);
120 xattrs->val[i].attrval_len = first[i].attrval_len;
121 xattrs->val[i].attrval = malloc (first[i].attrval_len);
122 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
129 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
130 off_t offset, struct fuse_file_info *fi)
135 dir_cache_remove_all_expired (now);
137 struct guestfs_dirent_list *ents;
139 ents = guestfs_readdir (g, path);
144 for (i = 0; i < ents->len; ++i) {
146 memset (&stat, 0, sizeof stat);
148 stat.st_ino = ents->val[i].ino;
149 switch (ents->val[i].ftyp) {
150 case 'b': stat.st_mode = S_IFBLK; break;
151 case 'c': stat.st_mode = S_IFCHR; break;
152 case 'd': stat.st_mode = S_IFDIR; break;
153 case 'f': stat.st_mode = S_IFIFO; break;
154 case 'l': stat.st_mode = S_IFLNK; break;
155 case 'r': stat.st_mode = S_IFREG; break;
156 case 's': stat.st_mode = S_IFSOCK; break;
159 default: stat.st_mode = 0;
162 /* Copied from the example, which also ignores 'offset'. I'm
163 * not quite sure how this is ever supposed to work on large
166 if (filler (buf, ents->val[i].name, &stat, 0))
170 /* Now prepopulate the directory caches. This step is just an
171 * optimization, don't worry if it fails.
173 char **names = malloc ((ents->len + 1) * sizeof (char *));
175 for (i = 0; i < ents->len; ++i)
176 names[i] = ents->val[i].name;
179 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
181 for (i = 0; i < ss->len; ++i) {
182 if (ss->val[i].ino >= 0) {
185 statbuf.st_dev = ss->val[i].dev;
186 statbuf.st_ino = ss->val[i].ino;
187 statbuf.st_mode = ss->val[i].mode;
188 statbuf.st_nlink = ss->val[i].nlink;
189 statbuf.st_uid = ss->val[i].uid;
190 statbuf.st_gid = ss->val[i].gid;
191 statbuf.st_rdev = ss->val[i].rdev;
192 statbuf.st_size = ss->val[i].size;
193 statbuf.st_blksize = ss->val[i].blksize;
194 statbuf.st_blocks = ss->val[i].blocks;
195 statbuf.st_atime = ss->val[i].atime;
196 statbuf.st_mtime = ss->val[i].mtime;
197 statbuf.st_ctime = ss->val[i].ctime;
199 lsc_insert (path, names[i], now, &statbuf);
202 guestfs_free_stat_list (ss);
205 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
208 struct guestfs_xattr *first;
209 struct guestfs_xattr_list *copy;
210 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
211 assert (strlen (xattrs->val[i].attrname) == 0);
212 if (xattrs->val[i].attrval_len > 0) {
214 first = &xattrs->val[i];
216 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
219 copy = copy_xattr_list (first, num);
221 xac_insert (path, names[ni], now, copy);
226 guestfs_free_xattr_list (xattrs);
229 char **links = guestfs_readlinklist (g, path, names);
231 for (i = 0; names[i] != NULL; ++i) {
233 /* Note that rlc_insert owns the string links[i] after this, */
234 rlc_insert (path, names[i], now, links[i]);
236 /* which is why we have to free links[i] here. */
239 free (links); /* free the array, not the strings */
245 guestfs_free_dirent_list (ents);
251 fg_getattr (const char *path, struct stat *statbuf)
253 const struct stat *buf;
255 buf = lsc_lookup (path);
257 memcpy (statbuf, buf, sizeof *statbuf);
261 struct guestfs_stat *r;
263 r = guestfs_lstat (g, path);
267 statbuf->st_dev = r->dev;
268 statbuf->st_ino = r->ino;
269 statbuf->st_mode = r->mode;
270 statbuf->st_nlink = r->nlink;
271 statbuf->st_uid = r->uid;
272 statbuf->st_gid = r->gid;
273 statbuf->st_rdev = r->rdev;
274 statbuf->st_size = r->size;
275 statbuf->st_blksize = r->blksize;
276 statbuf->st_blocks = r->blocks;
277 statbuf->st_atime = r->atime;
278 statbuf->st_mtime = r->mtime;
279 statbuf->st_ctime = r->ctime;
281 guestfs_free_stat (r);
286 /* Nautilus loves to use access(2) to test everything about a file,
287 * such as whether it's executable. Therefore treat this a lot like
291 fg_access (const char *path, int mask)
296 if (read_only && (mask & W_OK))
299 r = fg_getattr (path, &statbuf);
300 if (r < 0 || mask == F_OK)
303 struct fuse_context *fuse = fuse_get_context ();
308 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
309 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
310 : statbuf.st_mode & S_IROTH);
313 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
314 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
315 : statbuf.st_mode & S_IWOTH);
318 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
319 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
320 : statbuf.st_mode & S_IXOTH);
322 return ok ? 0 : -EACCES;
326 fg_readlink (const char *path, char *buf, size_t size)
331 r = rlc_lookup (path);
333 r = guestfs_readlink (g, path);
339 /* Note this is different from the real readlink(2) syscall. FUSE wants
340 * the string to be always nul-terminated, even if truncated.
342 size_t len = strlen (r);
346 memcpy (buf, r, len);
350 char *tmp = (char *) r;
358 fg_mknod (const char *path, mode_t mode, dev_t rdev)
362 if (read_only) return -EROFS;
364 dir_cache_invalidate (path);
366 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
374 fg_mkdir (const char *path, mode_t mode)
378 if (read_only) return -EROFS;
380 dir_cache_invalidate (path);
382 r = guestfs_mkdir_mode (g, path, mode);
390 fg_unlink (const char *path)
394 if (read_only) return -EROFS;
396 dir_cache_invalidate (path);
398 r = guestfs_rm (g, path);
406 fg_rmdir (const char *path)
410 if (read_only) return -EROFS;
412 dir_cache_invalidate (path);
414 r = guestfs_rmdir (g, path);
422 fg_symlink (const char *from, const char *to)
426 if (read_only) return -EROFS;
428 dir_cache_invalidate (to);
430 r = guestfs_ln_s (g, from, to);
438 fg_rename (const char *from, const char *to)
442 if (read_only) return -EROFS;
444 dir_cache_invalidate (to);
446 /* XXX It's not clear how close the 'mv' command is to the
447 * rename syscall. We might need to add the rename syscall
448 * to the guestfs(3) API.
450 r = guestfs_mv (g, from, to);
458 fg_link (const char *from, const char *to)
462 if (read_only) return -EROFS;
464 dir_cache_invalidate (from);
465 dir_cache_invalidate (to);
467 r = guestfs_ln (g, from, to);
475 fg_chmod (const char *path, mode_t mode)
479 if (read_only) return -EROFS;
481 dir_cache_invalidate (path);
483 r = guestfs_chmod (g, mode, path);
491 fg_chown (const char *path, uid_t uid, gid_t gid)
495 if (read_only) return -EROFS;
497 dir_cache_invalidate (path);
499 r = guestfs_lchown (g, uid, gid, path);
507 fg_truncate (const char *path, off_t size)
511 if (read_only) return -EROFS;
513 dir_cache_invalidate (path);
515 r = guestfs_truncate_size (g, path, size);
523 fg_utimens (const char *path, const struct timespec ts[2])
527 if (read_only) return -EROFS;
529 dir_cache_invalidate (path);
531 time_t atsecs = ts[0].tv_sec;
532 long atnsecs = ts[0].tv_nsec;
533 time_t mtsecs = ts[1].tv_sec;
534 long mtnsecs = ts[1].tv_nsec;
536 if (atnsecs == UTIME_NOW)
538 if (atnsecs == UTIME_OMIT)
540 if (mtnsecs == UTIME_NOW)
542 if (mtnsecs == UTIME_OMIT)
545 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
552 /* This call is quite hard to emulate through the guestfs(3) API. In
553 * one sense it's a little like access (see above) because it tests
554 * whether opening a file would succeed given the flags. But it also
555 * has side effects such as truncating the file if O_TRUNC is given.
556 * Therefore we need to emulate it ... painfully.
559 fg_open (const char *path, struct fuse_file_info *fi)
563 if (fi->flags & O_WRONLY) {
568 exists = guestfs_exists (g, path);
572 if (fi->flags & O_CREAT) {
576 dir_cache_invalidate (path);
578 /* Exclusive? File must not exist already. */
579 if (fi->flags & O_EXCL) {
584 /* Create? Touch it and optionally truncate it. */
585 r = guestfs_touch (g, path);
589 if (fi->flags & O_TRUNC) {
590 r = guestfs_truncate (g, path);
595 /* Not create, just check it exists. */
604 fg_read (const char *path, char *buf, size_t size, off_t offset,
605 struct fuse_file_info *fi)
611 fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
614 /* The guestfs protocol limits size to somewhere over 2MB. We just
615 * reduce the requested size here accordingly and push the problem
616 * up to every user. http://www.jwz.org/doc/worse-is-better.html
618 const size_t limit = 2 * 1024 * 1024;
622 r = guestfs_pread (g, path, size, offset, &rsize);
626 /* This should never happen, but at least it stops us overflowing
627 * the output buffer if it does happen.
632 memcpy (buf, r, rsize);
639 fg_write (const char *path, const char *buf, size_t size,
640 off_t offset, struct fuse_file_info *fi)
642 if (read_only) return -EROFS;
644 dir_cache_invalidate (path);
646 return -ENOSYS; /* XXX */
650 fg_statfs (const char *path, struct statvfs *stbuf)
652 struct guestfs_statvfs *r;
654 r = guestfs_statvfs (g, path);
658 stbuf->f_bsize = r->bsize;
659 stbuf->f_frsize = r->frsize;
660 stbuf->f_blocks = r->blocks;
661 stbuf->f_bfree = r->bfree;
662 stbuf->f_bavail = r->bavail;
663 stbuf->f_files = r->files;
664 stbuf->f_ffree = r->ffree;
665 stbuf->f_favail = r->favail;
666 stbuf->f_fsid = r->fsid;
667 stbuf->f_flag = r->flag;
668 stbuf->f_namemax = r->namemax;
670 guestfs_free_statvfs (r);
676 fg_release (const char *path, struct fuse_file_info *fi)
678 /* Just a stub. This method is optional and can safely be left
684 /* Emulate this by calling sync. */
685 static int fg_fsync(const char *path, int isdatasync,
686 struct fuse_file_info *fi)
690 r = guestfs_sync (g);
698 fg_setxattr (const char *path, const char *name, const char *value,
699 size_t size, int flags)
703 if (read_only) return -EROFS;
705 dir_cache_invalidate (path);
707 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
708 r = guestfs_lsetxattr (g, name, value, size, path);
715 /* The guestfs(3) API for getting xattrs is much easier to use
716 * than the real syscall. Unfortunately we now have to emulate
717 * the real syscall using that API :-(
720 fg_getxattr (const char *path, const char *name, char *value,
723 const struct guestfs_xattr_list *xattrs;
726 xattrs = xac_lookup (path);
727 if (xattrs == NULL) {
728 xattrs = guestfs_lgetxattrs (g, path);
736 for (i = 0; i < xattrs->len; ++i) {
737 if (STREQ (xattrs->val[i].attrname, name)) {
738 size_t sz = xattrs->val[i].attrval_len;
741 memcpy (value, xattrs->val[i].attrval, sz);
748 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
753 /* Ditto as above. */
755 fg_listxattr (const char *path, char *list, size_t size)
757 const struct guestfs_xattr_list *xattrs;
760 xattrs = xac_lookup (path);
761 if (xattrs == NULL) {
762 xattrs = guestfs_lgetxattrs (g, path);
770 for (i = 0; i < xattrs->len; ++i) {
771 size_t len = strlen (xattrs->val[i].attrname) + 1;
773 memcpy (list, xattrs->val[i].attrname, len);
784 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
790 fg_removexattr(const char *path, const char *name)
794 if (read_only) return -EROFS;
796 dir_cache_invalidate (path);
798 r = guestfs_lremovexattr (g, name, path);
805 static struct fuse_operations fg_operations = {
806 .getattr = fg_getattr,
808 .readlink = fg_readlink,
809 .readdir = fg_readdir,
812 .symlink = fg_symlink,
819 .truncate = fg_truncate,
820 .utimens = fg_utimens,
825 .release = fg_release,
827 .setxattr = fg_setxattr,
828 .getxattr = fg_getxattr,
829 .listxattr = fg_listxattr,
830 .removexattr = fg_removexattr,
844 static void add_drives (struct drv *);
845 static void mount_mps (struct mp *);
847 static void __attribute__((noreturn))
850 const char *tmp_argv[] = { program_name, "--help", NULL };
851 fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
855 static void __attribute__((noreturn))
858 if (status != EXIT_SUCCESS)
859 fprintf (stderr, _("Try `%s --help' for more information.\n"),
863 _("%s: FUSE module for libguestfs\n"
864 "%s lets you mount a virtual machine filesystem\n"
865 "Copyright (C) 2009 Red Hat Inc.\n"
867 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
869 " -a|--add image Add image\n"
870 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
871 " --fuse-help Display extra FUSE options\n"
872 " --help Display help message and exit\n"
873 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
874 " -n|--no-sync Don't autosync\n"
875 " -o|--option opt Pass extra option to FUSE\n"
876 " -r|--ro Mount read-only\n"
877 " --selinux Enable SELinux support\n"
878 " --trace Trace guestfs API calls (to stderr)\n"
879 " -v|--verbose Verbose messages\n"
880 " -V|--version Display version and exit\n"
882 program_name, program_name, program_name);
888 main (int argc, char *argv[])
890 enum { HELP_OPTION = CHAR_MAX + 1 };
892 /* The command line arguments are broadly compatible with (a subset
893 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
895 static const char *options = "a:m:no:rv?V";
896 static const struct option long_options[] = {
897 { "add", 1, 0, 'a' },
898 { "dir-cache-timeout", 1, 0, 0 },
899 { "fuse-help", 0, 0, 0 },
900 { "help", 0, 0, HELP_OPTION },
901 { "mount", 1, 0, 'm' },
902 { "no-sync", 0, 0, 'n' },
903 { "option", 1, 0, 'o' },
905 { "selinux", 0, 0, 0 },
906 { "trace", 0, 0, 0 },
907 { "verbose", 0, 0, 'v' },
908 { "version", 0, 0, 'V' },
912 struct drv *drvs = NULL;
914 struct mp *mps = NULL;
922 const char **fuse_argv = NULL;
924 #define ADD_FUSE_ARG(str) \
927 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
929 perror ("realloc"); \
932 fuse_argv[fuse_argc-1] = (str); \
933 fuse_argv[fuse_argc] = NULL; \
936 /* LC_ALL=C is required so we can parse error messages. */
937 setenv ("LC_ALL", "C", 1);
939 /* Set global program name that is not polluted with libtool artifacts. */
940 set_program_name (argv[0]);
942 memset (&sa, 0, sizeof sa);
943 sa.sa_handler = SIG_IGN;
944 sa.sa_flags = SA_RESTART;
945 sigaction (SIGPIPE, &sa, NULL);
947 /* Various initialization. */
950 g = guestfs_create ();
952 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
956 guestfs_set_autosync (g, 1);
957 guestfs_set_recovery_proc (g, 0);
959 ADD_FUSE_ARG (program_name);
960 /* MUST be single-threaded. You cannot have two threads accessing the
961 * same libguestfs handle, and opening more than one handle is likely
962 * to be very expensive.
966 /* If developing, add ./appliance to the path. Note that libtools
967 * interferes with this because uninstalled guestfish is a shell
968 * script that runs the real program with an absolute path. Detect
971 * BUT if LIBGUESTFS_PATH environment variable is already set by
972 * the user, then don't override it.
974 if (getenv ("LIBGUESTFS_PATH") == NULL &&
976 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
977 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
980 c = getopt_long (argc, argv, options, long_options, &option_index);
984 case 0: /* options which are long only */
985 if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
986 dir_cache_timeout = atoi (optarg);
987 else if (STREQ (long_options[option_index].name, "fuse-help"))
989 else if (STREQ (long_options[option_index].name, "selinux"))
990 guestfs_set_selinux (g, 1);
991 else if (STREQ (long_options[option_index].name, "trace")) {
993 guestfs_set_trace (g, 1);
994 guestfs_set_recovery_proc (g, 1);
997 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
998 program_name, long_options[option_index].name, option_index);
1004 if (access (optarg, R_OK) != 0) {
1008 drv = malloc (sizeof (struct drv));
1013 drv->filename = optarg;
1019 mp = malloc (sizeof (struct mp));
1024 p = strchr (optarg, ':');
1027 mp->mountpoint = p+1;
1029 mp->mountpoint = bad_cast ("/");
1030 mp->device = optarg;
1036 guestfs_set_autosync (g, 0);
1040 ADD_FUSE_ARG ("-o");
1041 ADD_FUSE_ARG (optarg);
1050 guestfs_set_verbose (g, verbose);
1054 printf ("%s %s\n", program_name, PACKAGE_VERSION);
1065 /* We must have at least one -a and at least one -m. */
1066 if (!drvs || !mps) {
1068 _("%s: must have at least one -a and at least one -m option\n"),
1073 /* We'd better have a mountpoint. */
1074 if (optind+1 != argc) {
1076 _("%s: you must specify a mountpoint in the host filesystem\n"),
1081 /* Do the guest drives and mountpoints. */
1083 if (guestfs_launch (g) == -1)
1087 /* FUSE example does this, not clear if it's necessary, but ... */
1088 if (guestfs_umask (g, 0) == -1)
1091 /* At the last minute, remove the libguestfs error handler. In code
1092 * above this point, the default error handler has been used which
1093 * sends all errors to stderr. Now before entering FUSE itself we
1094 * want to silence errors so we can convert them (see error()
1097 guestfs_set_error_handler (g, NULL, NULL);
1099 /* Finish off FUSE args. */
1100 ADD_FUSE_ARG (argv[optind]);
1103 It says about the line containing the for-statement:
1104 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1107 fprintf (stderr, "guestmount: invoking FUSE with args [");
1108 for (i = 0; i < fuse_argc; ++i) {
1109 if (i > 0) fprintf (stderr, ", ");
1110 fprintf (stderr, "%s", fuse_argv[i]);
1112 fprintf (stderr, "]\n");
1116 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1122 exit (r == -1 ? 1 : 0);
1125 /* List is built in reverse order, so add them in reverse order. */
1127 add_drives (struct drv *drv)
1132 add_drives (drv->next);
1134 r = guestfs_add_drive (g, drv->filename);
1136 r = guestfs_add_drive_ro (g, drv->filename);
1142 /* List is built in reverse order, so mount them in reverse order. */
1144 mount_mps (struct mp *mp)
1149 mount_mps (mp->next);
1151 r = guestfs_mount (g, mp->device, mp->mountpoint);
1153 r = guestfs_mount_ro (g, mp->device, mp->mountpoint);