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>
50 #include "guestmount.h"
53 /* See <attr/xattr.h> */
55 #define ENOATTR ENODATA
58 static guestfs_h *g = NULL;
59 static int read_only = 0;
61 int dir_cache_timeout = 60;
63 /* This is ugly: guestfs errors are strings, FUSE wants -errno. We
64 * have to do the conversion as best we can.
72 const char *err = guestfs_last_error (g);
78 fprintf (stderr, "%s\n", err);
80 /* Add a few of our own ... */
82 /* This indicates guestfsd died. Translate into a hard EIO error.
83 * Arguably we could relaunch the guest if we hit this error.
85 if (strstr (err, "call launch before using this function"))
88 /* See if it matches an errno string in the host. */
89 for (i = 0; i < MAX_ERRNO; ++i) {
90 const char *e = strerror (i);
91 if (e && strstr (err, e) != NULL)
95 /* Too bad, return a generic error. */
99 static struct guestfs_xattr_list *
100 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
102 struct guestfs_xattr_list *xattrs;
104 xattrs = malloc (sizeof *xattrs);
105 if (xattrs == NULL) {
111 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
112 if (xattrs->val == NULL) {
119 for (i = 0; i < num; ++i) {
120 xattrs->val[i].attrname = strdup (first[i].attrname);
121 xattrs->val[i].attrval_len = first[i].attrval_len;
122 xattrs->val[i].attrval = malloc (first[i].attrval_len);
123 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
130 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
131 off_t offset, struct fuse_file_info *fi)
136 dir_cache_remove_all_expired (now);
138 struct guestfs_dirent_list *ents;
140 ents = guestfs_readdir (g, path);
145 for (i = 0; i < ents->len; ++i) {
147 memset (&stat, 0, sizeof stat);
149 stat.st_ino = ents->val[i].ino;
150 switch (ents->val[i].ftyp) {
151 case 'b': stat.st_mode = S_IFBLK; break;
152 case 'c': stat.st_mode = S_IFCHR; break;
153 case 'd': stat.st_mode = S_IFDIR; break;
154 case 'f': stat.st_mode = S_IFIFO; break;
155 case 'l': stat.st_mode = S_IFLNK; break;
156 case 'r': stat.st_mode = S_IFREG; break;
157 case 's': stat.st_mode = S_IFSOCK; break;
160 default: stat.st_mode = 0;
163 /* Copied from the example, which also ignores 'offset'. I'm
164 * not quite sure how this is ever supposed to work on large
167 if (filler (buf, ents->val[i].name, &stat, 0))
171 /* Now prepopulate the directory caches. This step is just an
172 * optimization, don't worry if it fails.
174 char **names = malloc ((ents->len + 1) * sizeof (char *));
176 for (i = 0; i < ents->len; ++i)
177 names[i] = ents->val[i].name;
180 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
182 for (i = 0; i < ss->len; ++i) {
183 if (ss->val[i].ino >= 0) {
186 statbuf.st_dev = ss->val[i].dev;
187 statbuf.st_ino = ss->val[i].ino;
188 statbuf.st_mode = ss->val[i].mode;
189 statbuf.st_nlink = ss->val[i].nlink;
190 statbuf.st_uid = ss->val[i].uid;
191 statbuf.st_gid = ss->val[i].gid;
192 statbuf.st_rdev = ss->val[i].rdev;
193 statbuf.st_size = ss->val[i].size;
194 statbuf.st_blksize = ss->val[i].blksize;
195 statbuf.st_blocks = ss->val[i].blocks;
196 statbuf.st_atime = ss->val[i].atime;
197 statbuf.st_mtime = ss->val[i].mtime;
198 statbuf.st_ctime = ss->val[i].ctime;
200 lsc_insert (path, names[i], now, &statbuf);
203 guestfs_free_stat_list (ss);
206 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
209 struct guestfs_xattr *first;
210 struct guestfs_xattr_list *copy;
211 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
212 assert (strlen (xattrs->val[i].attrname) == 0);
213 if (xattrs->val[i].attrval_len > 0) {
215 first = &xattrs->val[i];
217 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
220 copy = copy_xattr_list (first, num);
222 xac_insert (path, names[ni], now, copy);
227 guestfs_free_xattr_list (xattrs);
230 char **links = guestfs_readlinklist (g, path, names);
232 for (i = 0; names[i] != NULL; ++i) {
234 /* Note that rlc_insert owns the string links[i] after this, */
235 rlc_insert (path, names[i], now, links[i]);
237 /* which is why we have to free links[i] here. */
240 free (links); /* free the array, not the strings */
246 guestfs_free_dirent_list (ents);
252 fg_getattr (const char *path, struct stat *statbuf)
254 const struct stat *buf;
256 buf = lsc_lookup (path);
258 memcpy (statbuf, buf, sizeof *statbuf);
262 struct guestfs_stat *r;
264 r = guestfs_lstat (g, path);
268 statbuf->st_dev = r->dev;
269 statbuf->st_ino = r->ino;
270 statbuf->st_mode = r->mode;
271 statbuf->st_nlink = r->nlink;
272 statbuf->st_uid = r->uid;
273 statbuf->st_gid = r->gid;
274 statbuf->st_rdev = r->rdev;
275 statbuf->st_size = r->size;
276 statbuf->st_blksize = r->blksize;
277 statbuf->st_blocks = r->blocks;
278 statbuf->st_atime = r->atime;
279 statbuf->st_mtime = r->mtime;
280 statbuf->st_ctime = r->ctime;
282 guestfs_free_stat (r);
287 /* Nautilus loves to use access(2) to test everything about a file,
288 * such as whether it's executable. Therefore treat this a lot like
292 fg_access (const char *path, int mask)
297 if (read_only && (mask & W_OK))
300 r = fg_getattr (path, &statbuf);
301 if (r < 0 || mask == F_OK)
304 struct fuse_context *fuse = fuse_get_context ();
309 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
310 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
311 : statbuf.st_mode & S_IROTH);
314 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
315 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
316 : statbuf.st_mode & S_IWOTH);
319 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
320 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
321 : statbuf.st_mode & S_IXOTH);
323 return ok ? 0 : -EACCES;
327 fg_readlink (const char *path, char *buf, size_t size)
332 r = rlc_lookup (path);
334 r = guestfs_readlink (g, path);
340 /* Note this is different from the real readlink(2) syscall. FUSE wants
341 * the string to be always nul-terminated, even if truncated.
343 size_t len = strlen (r);
347 memcpy (buf, r, len);
351 char *tmp = (char *) r;
359 fg_mknod (const char *path, mode_t mode, dev_t rdev)
363 if (read_only) return -EROFS;
365 dir_cache_invalidate (path);
367 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
375 fg_mkdir (const char *path, mode_t mode)
379 if (read_only) return -EROFS;
381 dir_cache_invalidate (path);
383 r = guestfs_mkdir_mode (g, path, mode);
391 fg_unlink (const char *path)
395 if (read_only) return -EROFS;
397 dir_cache_invalidate (path);
399 r = guestfs_rm (g, path);
407 fg_rmdir (const char *path)
411 if (read_only) return -EROFS;
413 dir_cache_invalidate (path);
415 r = guestfs_rmdir (g, path);
423 fg_symlink (const char *from, const char *to)
427 if (read_only) return -EROFS;
429 dir_cache_invalidate (to);
431 r = guestfs_ln_s (g, from, to);
439 fg_rename (const char *from, const char *to)
443 if (read_only) return -EROFS;
445 dir_cache_invalidate (from);
446 dir_cache_invalidate (to);
448 /* XXX It's not clear how close the 'mv' command is to the
449 * rename syscall. We might need to add the rename syscall
450 * to the guestfs(3) API.
452 r = guestfs_mv (g, from, to);
460 fg_link (const char *from, const char *to)
464 if (read_only) return -EROFS;
466 dir_cache_invalidate (from);
467 dir_cache_invalidate (to);
469 r = guestfs_ln (g, from, to);
477 fg_chmod (const char *path, mode_t mode)
481 if (read_only) return -EROFS;
483 dir_cache_invalidate (path);
485 r = guestfs_chmod (g, mode, path);
493 fg_chown (const char *path, uid_t uid, gid_t gid)
497 if (read_only) return -EROFS;
499 dir_cache_invalidate (path);
501 r = guestfs_lchown (g, uid, gid, path);
509 fg_truncate (const char *path, off_t size)
513 if (read_only) return -EROFS;
515 dir_cache_invalidate (path);
517 r = guestfs_truncate_size (g, path, size);
525 fg_utimens (const char *path, const struct timespec ts[2])
529 if (read_only) return -EROFS;
531 dir_cache_invalidate (path);
533 time_t atsecs = ts[0].tv_sec;
534 long atnsecs = ts[0].tv_nsec;
535 time_t mtsecs = ts[1].tv_sec;
536 long mtnsecs = ts[1].tv_nsec;
539 if (atnsecs == UTIME_NOW)
543 if (atnsecs == UTIME_OMIT)
547 if (mtnsecs == UTIME_NOW)
551 if (mtnsecs == UTIME_OMIT)
555 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
562 /* This call is quite hard to emulate through the guestfs(3) API. In
563 * one sense it's a little like access (see above) because it tests
564 * whether opening a file would succeed given the flags. But it also
565 * has side effects such as truncating the file if O_TRUNC is given.
566 * Therefore we need to emulate it ... painfully.
569 fg_open (const char *path, struct fuse_file_info *fi)
573 if (fi->flags & O_WRONLY) {
578 exists = guestfs_exists (g, path);
582 if (fi->flags & O_CREAT) {
586 dir_cache_invalidate (path);
588 /* Exclusive? File must not exist already. */
589 if (fi->flags & O_EXCL) {
594 /* Create? Touch it and optionally truncate it. */
595 r = guestfs_touch (g, path);
599 if (fi->flags & O_TRUNC) {
600 r = guestfs_truncate (g, path);
605 /* Not create, just check it exists. */
614 fg_read (const char *path, char *buf, size_t size, off_t offset,
615 struct fuse_file_info *fi)
621 fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
624 /* The guestfs protocol limits size to somewhere over 2MB. We just
625 * reduce the requested size here accordingly and push the problem
626 * up to every user. http://www.jwz.org/doc/worse-is-better.html
628 const size_t limit = 2 * 1024 * 1024;
632 r = guestfs_pread (g, path, size, offset, &rsize);
636 /* This should never happen, but at least it stops us overflowing
637 * the output buffer if it does happen.
642 memcpy (buf, r, rsize);
649 fg_write (const char *path, const char *buf, size_t size,
650 off_t offset, struct fuse_file_info *fi)
652 if (read_only) return -EROFS;
654 dir_cache_invalidate (path);
656 return -ENOSYS; /* XXX */
660 fg_statfs (const char *path, struct statvfs *stbuf)
662 struct guestfs_statvfs *r;
664 r = guestfs_statvfs (g, path);
668 stbuf->f_bsize = r->bsize;
669 stbuf->f_frsize = r->frsize;
670 stbuf->f_blocks = r->blocks;
671 stbuf->f_bfree = r->bfree;
672 stbuf->f_bavail = r->bavail;
673 stbuf->f_files = r->files;
674 stbuf->f_ffree = r->ffree;
675 stbuf->f_favail = r->favail;
676 stbuf->f_fsid = r->fsid;
677 stbuf->f_flag = r->flag;
678 stbuf->f_namemax = r->namemax;
680 guestfs_free_statvfs (r);
686 fg_release (const char *path, struct fuse_file_info *fi)
688 /* Just a stub. This method is optional and can safely be left
694 /* Emulate this by calling sync. */
695 static int fg_fsync(const char *path, int isdatasync,
696 struct fuse_file_info *fi)
700 r = guestfs_sync (g);
708 fg_setxattr (const char *path, const char *name, const char *value,
709 size_t size, int flags)
713 if (read_only) return -EROFS;
715 dir_cache_invalidate (path);
717 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
718 r = guestfs_lsetxattr (g, name, value, size, path);
725 /* The guestfs(3) API for getting xattrs is much easier to use
726 * than the real syscall. Unfortunately we now have to emulate
727 * the real syscall using that API :-(
730 fg_getxattr (const char *path, const char *name, char *value,
733 const struct guestfs_xattr_list *xattrs;
736 xattrs = xac_lookup (path);
737 if (xattrs == NULL) {
738 xattrs = guestfs_lgetxattrs (g, path);
746 for (i = 0; i < xattrs->len; ++i) {
747 if (STREQ (xattrs->val[i].attrname, name)) {
748 size_t sz = xattrs->val[i].attrval_len;
751 memcpy (value, xattrs->val[i].attrval, sz);
758 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
763 /* Ditto as above. */
765 fg_listxattr (const char *path, char *list, size_t size)
767 const struct guestfs_xattr_list *xattrs;
770 xattrs = xac_lookup (path);
771 if (xattrs == NULL) {
772 xattrs = guestfs_lgetxattrs (g, path);
780 for (i = 0; i < xattrs->len; ++i) {
781 size_t len = strlen (xattrs->val[i].attrname) + 1;
783 memcpy (list, xattrs->val[i].attrname, len);
794 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
800 fg_removexattr(const char *path, const char *name)
804 if (read_only) return -EROFS;
806 dir_cache_invalidate (path);
808 r = guestfs_lremovexattr (g, name, path);
815 static struct fuse_operations fg_operations = {
816 .getattr = fg_getattr,
818 .readlink = fg_readlink,
819 .readdir = fg_readdir,
822 .symlink = fg_symlink,
829 .truncate = fg_truncate,
830 .utimens = fg_utimens,
835 .release = fg_release,
837 .setxattr = fg_setxattr,
838 .getxattr = fg_getxattr,
839 .listxattr = fg_listxattr,
840 .removexattr = fg_removexattr,
854 static void add_drives (struct drv *);
855 static void mount_mps (struct mp *);
857 static void __attribute__((noreturn))
860 const char *tmp_argv[] = { program_name, "--help", NULL };
861 fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
865 static void __attribute__((noreturn))
868 if (status != EXIT_SUCCESS)
869 fprintf (stderr, _("Try `%s --help' for more information.\n"),
873 _("%s: FUSE module for libguestfs\n"
874 "%s lets you mount a virtual machine filesystem\n"
875 "Copyright (C) 2009 Red Hat Inc.\n"
877 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
879 " -a|--add image Add image\n"
880 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
881 " --fuse-help Display extra FUSE options\n"
882 " --help Display help message and exit\n"
883 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
884 " -n|--no-sync Don't autosync\n"
885 " -o|--option opt Pass extra option to FUSE\n"
886 " -r|--ro Mount read-only\n"
887 " --selinux Enable SELinux support\n"
888 " --trace Trace guestfs API calls (to stderr)\n"
889 " -v|--verbose Verbose messages\n"
890 " -V|--version Display version and exit\n"
892 program_name, program_name, program_name);
898 main (int argc, char *argv[])
900 setlocale (LC_ALL, "");
901 bindtextdomain (PACKAGE, LOCALEBASEDIR);
902 textdomain (PACKAGE);
904 enum { HELP_OPTION = CHAR_MAX + 1 };
906 /* The command line arguments are broadly compatible with (a subset
907 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
909 static const char *options = "a:m:no:rv?V";
910 static const struct option long_options[] = {
911 { "add", 1, 0, 'a' },
912 { "dir-cache-timeout", 1, 0, 0 },
913 { "fuse-help", 0, 0, 0 },
914 { "help", 0, 0, HELP_OPTION },
915 { "mount", 1, 0, 'm' },
916 { "no-sync", 0, 0, 'n' },
917 { "option", 1, 0, 'o' },
919 { "selinux", 0, 0, 0 },
920 { "trace", 0, 0, 0 },
921 { "verbose", 0, 0, 'v' },
922 { "version", 0, 0, 'V' },
926 struct drv *drvs = NULL;
928 struct mp *mps = NULL;
936 const char **fuse_argv = NULL;
938 #define ADD_FUSE_ARG(str) \
941 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
943 perror ("realloc"); \
944 exit (EXIT_FAILURE); \
946 fuse_argv[fuse_argc-1] = (str); \
947 fuse_argv[fuse_argc] = NULL; \
950 /* LC_ALL=C is required so we can parse error messages. */
951 setenv ("LC_ALL", "C", 1);
953 /* Set global program name that is not polluted with libtool artifacts. */
954 set_program_name (argv[0]);
956 memset (&sa, 0, sizeof sa);
957 sa.sa_handler = SIG_IGN;
958 sa.sa_flags = SA_RESTART;
959 sigaction (SIGPIPE, &sa, NULL);
961 /* Various initialization. */
964 g = guestfs_create ();
966 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
970 guestfs_set_autosync (g, 1);
971 guestfs_set_recovery_proc (g, 0);
973 ADD_FUSE_ARG (program_name);
974 /* MUST be single-threaded. You cannot have two threads accessing the
975 * same libguestfs handle, and opening more than one handle is likely
976 * to be very expensive.
980 /* If developing, add ./appliance to the path. Note that libtools
981 * interferes with this because uninstalled guestfish is a shell
982 * script that runs the real program with an absolute path. Detect
985 * BUT if LIBGUESTFS_PATH environment variable is already set by
986 * the user, then don't override it.
988 if (getenv ("LIBGUESTFS_PATH") == NULL &&
990 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
991 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
994 c = getopt_long (argc, argv, options, long_options, &option_index);
998 case 0: /* options which are long only */
999 if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
1000 dir_cache_timeout = atoi (optarg);
1001 else if (STREQ (long_options[option_index].name, "fuse-help"))
1003 else if (STREQ (long_options[option_index].name, "selinux"))
1004 guestfs_set_selinux (g, 1);
1005 else if (STREQ (long_options[option_index].name, "trace")) {
1006 ADD_FUSE_ARG ("-f");
1007 guestfs_set_trace (g, 1);
1008 guestfs_set_recovery_proc (g, 1);
1011 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
1012 program_name, long_options[option_index].name, option_index);
1013 exit (EXIT_FAILURE);
1018 if (access (optarg, R_OK) != 0) {
1020 exit (EXIT_FAILURE);
1022 drv = malloc (sizeof (struct drv));
1025 exit (EXIT_FAILURE);
1027 drv->filename = optarg;
1033 mp = malloc (sizeof (struct mp));
1036 exit (EXIT_FAILURE);
1038 p = strchr (optarg, ':');
1041 mp->mountpoint = p+1;
1043 mp->mountpoint = bad_cast ("/");
1044 mp->device = optarg;
1050 guestfs_set_autosync (g, 0);
1054 ADD_FUSE_ARG ("-o");
1055 ADD_FUSE_ARG (optarg);
1064 guestfs_set_verbose (g, verbose);
1068 printf ("%s %s\n", program_name, PACKAGE_VERSION);
1069 exit (EXIT_SUCCESS);
1072 usage (EXIT_SUCCESS);
1075 usage (EXIT_FAILURE);
1079 /* We must have at least one -a and at least one -m. */
1080 if (!drvs || !mps) {
1082 _("%s: must have at least one -a and at least one -m option\n"),
1084 exit (EXIT_FAILURE);
1087 /* We'd better have a mountpoint. */
1088 if (optind+1 != argc) {
1090 _("%s: you must specify a mountpoint in the host filesystem\n"),
1092 exit (EXIT_FAILURE);
1095 /* Do the guest drives and mountpoints. */
1097 if (guestfs_launch (g) == -1)
1098 exit (EXIT_FAILURE);
1101 /* FUSE example does this, not clear if it's necessary, but ... */
1102 if (guestfs_umask (g, 0) == -1)
1103 exit (EXIT_FAILURE);
1105 /* At the last minute, remove the libguestfs error handler. In code
1106 * above this point, the default error handler has been used which
1107 * sends all errors to stderr. Now before entering FUSE itself we
1108 * want to silence errors so we can convert them (see error()
1111 guestfs_set_error_handler (g, NULL, NULL);
1113 /* Finish off FUSE args. */
1114 ADD_FUSE_ARG (argv[optind]);
1117 It says about the line containing the for-statement:
1118 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1121 fprintf (stderr, "guestmount: invoking FUSE with args [");
1122 for (i = 0; i < fuse_argc; ++i) {
1123 if (i > 0) fprintf (stderr, ", ");
1124 fprintf (stderr, "%s", fuse_argv[i]);
1126 fprintf (stderr, "]\n");
1130 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1136 exit (r == -1 ? 1 : 0);
1139 /* List is built in reverse order, so add them in reverse order. */
1141 add_drives (struct drv *drv)
1146 add_drives (drv->next);
1148 r = guestfs_add_drive (g, drv->filename);
1150 r = guestfs_add_drive_ro (g, drv->filename);
1152 exit (EXIT_FAILURE);
1156 /* List is built in reverse order, so mount them in reverse order. */
1158 mount_mps (struct mp *mp)
1163 mount_mps (mp->next);
1165 /* Don't use guestfs_mount here because that will default to mount
1166 * options -o sync,noatime. For more information, see guestfs(3)
1167 * section "LIBGUESTFS GOTCHAS".
1169 const char *options = read_only ? "ro" : "";
1170 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
1172 exit (EXIT_FAILURE);