1 /* guestmount - mount guests using libguestfs and FUSE
2 * Copyright (C) 2009-2010 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
43 #include <sys/types.h>
51 #include "guestmount.h"
55 /* See <attr/xattr.h> */
57 #define ENOATTR ENODATA
64 const char *libvirt_uri;
65 int dir_cache_timeout = 60;
67 /* This is ugly: guestfs errors are strings, FUSE wants -errno. We
68 * have to do the conversion as best we can.
76 const char *err = guestfs_last_error (g);
82 fprintf (stderr, "%s\n", err);
84 /* Add a few of our own ... */
86 /* This indicates guestfsd died. Translate into a hard EIO error.
87 * Arguably we could relaunch the guest if we hit this error.
89 if (strstr (err, "call launch before using this function"))
92 /* See if it matches an errno string in the host. */
93 for (i = 0; i < MAX_ERRNO; ++i) {
94 const char *e = strerror (i);
95 if (e && strstr (err, e) != NULL)
99 /* Too bad, return a generic error. */
103 static struct guestfs_xattr_list *
104 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
106 struct guestfs_xattr_list *xattrs;
108 xattrs = malloc (sizeof *xattrs);
109 if (xattrs == NULL) {
115 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
116 if (xattrs->val == NULL) {
123 for (i = 0; i < num; ++i) {
124 xattrs->val[i].attrname = strdup (first[i].attrname);
125 xattrs->val[i].attrval_len = first[i].attrval_len;
126 xattrs->val[i].attrval = malloc (first[i].attrval_len);
127 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
134 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
135 off_t offset, struct fuse_file_info *fi)
140 dir_cache_remove_all_expired (now);
142 struct guestfs_dirent_list *ents;
144 ents = guestfs_readdir (g, path);
149 for (i = 0; i < ents->len; ++i) {
151 memset (&stat, 0, sizeof stat);
153 stat.st_ino = ents->val[i].ino;
154 switch (ents->val[i].ftyp) {
155 case 'b': stat.st_mode = S_IFBLK; break;
156 case 'c': stat.st_mode = S_IFCHR; break;
157 case 'd': stat.st_mode = S_IFDIR; break;
158 case 'f': stat.st_mode = S_IFIFO; break;
159 case 'l': stat.st_mode = S_IFLNK; break;
160 case 'r': stat.st_mode = S_IFREG; break;
161 case 's': stat.st_mode = S_IFSOCK; break;
164 default: stat.st_mode = 0;
167 /* Copied from the example, which also ignores 'offset'. I'm
168 * not quite sure how this is ever supposed to work on large
171 if (filler (buf, ents->val[i].name, &stat, 0))
175 /* Now prepopulate the directory caches. This step is just an
176 * optimization, don't worry if it fails.
178 char **names = malloc ((ents->len + 1) * sizeof (char *));
180 for (i = 0; i < ents->len; ++i)
181 names[i] = ents->val[i].name;
184 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
186 for (i = 0; i < ss->len; ++i) {
187 if (ss->val[i].ino >= 0) {
190 statbuf.st_dev = ss->val[i].dev;
191 statbuf.st_ino = ss->val[i].ino;
192 statbuf.st_mode = ss->val[i].mode;
193 statbuf.st_nlink = ss->val[i].nlink;
194 statbuf.st_uid = ss->val[i].uid;
195 statbuf.st_gid = ss->val[i].gid;
196 statbuf.st_rdev = ss->val[i].rdev;
197 statbuf.st_size = ss->val[i].size;
198 statbuf.st_blksize = ss->val[i].blksize;
199 statbuf.st_blocks = ss->val[i].blocks;
200 statbuf.st_atime = ss->val[i].atime;
201 statbuf.st_mtime = ss->val[i].mtime;
202 statbuf.st_ctime = ss->val[i].ctime;
204 lsc_insert (path, names[i], now, &statbuf);
207 guestfs_free_stat_list (ss);
210 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
213 struct guestfs_xattr *first;
214 struct guestfs_xattr_list *copy;
215 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
216 assert (strlen (xattrs->val[i].attrname) == 0);
217 if (xattrs->val[i].attrval_len > 0) {
219 first = &xattrs->val[i];
221 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
224 copy = copy_xattr_list (first, num);
226 xac_insert (path, names[ni], now, copy);
231 guestfs_free_xattr_list (xattrs);
234 char **links = guestfs_readlinklist (g, path, names);
236 for (i = 0; names[i] != NULL; ++i) {
238 /* Note that rlc_insert owns the string links[i] after this, */
239 rlc_insert (path, names[i], now, links[i]);
241 /* which is why we have to free links[i] here. */
244 free (links); /* free the array, not the strings */
250 guestfs_free_dirent_list (ents);
256 fg_getattr (const char *path, struct stat *statbuf)
258 const struct stat *buf;
260 buf = lsc_lookup (path);
262 memcpy (statbuf, buf, sizeof *statbuf);
266 struct guestfs_stat *r;
268 r = guestfs_lstat (g, path);
272 statbuf->st_dev = r->dev;
273 statbuf->st_ino = r->ino;
274 statbuf->st_mode = r->mode;
275 statbuf->st_nlink = r->nlink;
276 statbuf->st_uid = r->uid;
277 statbuf->st_gid = r->gid;
278 statbuf->st_rdev = r->rdev;
279 statbuf->st_size = r->size;
280 statbuf->st_blksize = r->blksize;
281 statbuf->st_blocks = r->blocks;
282 statbuf->st_atime = r->atime;
283 statbuf->st_mtime = r->mtime;
284 statbuf->st_ctime = r->ctime;
286 guestfs_free_stat (r);
291 /* Nautilus loves to use access(2) to test everything about a file,
292 * such as whether it's executable. Therefore treat this a lot like
296 fg_access (const char *path, int mask)
301 if (read_only && (mask & W_OK))
304 r = fg_getattr (path, &statbuf);
305 if (r < 0 || mask == F_OK)
308 struct fuse_context *fuse = fuse_get_context ();
313 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
314 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
315 : statbuf.st_mode & S_IROTH);
318 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
319 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
320 : statbuf.st_mode & S_IWOTH);
323 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
324 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
325 : statbuf.st_mode & S_IXOTH);
327 return ok ? 0 : -EACCES;
331 fg_readlink (const char *path, char *buf, size_t size)
336 r = rlc_lookup (path);
338 r = guestfs_readlink (g, path);
344 /* Note this is different from the real readlink(2) syscall. FUSE wants
345 * the string to be always nul-terminated, even if truncated.
347 size_t len = strlen (r);
351 memcpy (buf, r, len);
355 char *tmp = (char *) r;
363 fg_mknod (const char *path, mode_t mode, dev_t rdev)
367 if (read_only) return -EROFS;
369 dir_cache_invalidate (path);
371 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
379 fg_mkdir (const char *path, mode_t mode)
383 if (read_only) return -EROFS;
385 dir_cache_invalidate (path);
387 r = guestfs_mkdir_mode (g, path, mode);
395 fg_unlink (const char *path)
399 if (read_only) return -EROFS;
401 dir_cache_invalidate (path);
403 r = guestfs_rm (g, path);
411 fg_rmdir (const char *path)
415 if (read_only) return -EROFS;
417 dir_cache_invalidate (path);
419 r = guestfs_rmdir (g, path);
427 fg_symlink (const char *from, const char *to)
431 if (read_only) return -EROFS;
433 dir_cache_invalidate (to);
435 r = guestfs_ln_s (g, from, to);
443 fg_rename (const char *from, const char *to)
447 if (read_only) return -EROFS;
449 dir_cache_invalidate (from);
450 dir_cache_invalidate (to);
452 /* XXX It's not clear how close the 'mv' command is to the
453 * rename syscall. We might need to add the rename syscall
454 * to the guestfs(3) API.
456 r = guestfs_mv (g, from, to);
464 fg_link (const char *from, const char *to)
468 if (read_only) return -EROFS;
470 dir_cache_invalidate (from);
471 dir_cache_invalidate (to);
473 r = guestfs_ln (g, from, to);
481 fg_chmod (const char *path, mode_t mode)
485 if (read_only) return -EROFS;
487 dir_cache_invalidate (path);
489 r = guestfs_chmod (g, mode, path);
497 fg_chown (const char *path, uid_t uid, gid_t gid)
501 if (read_only) return -EROFS;
503 dir_cache_invalidate (path);
505 r = guestfs_lchown (g, uid, gid, path);
513 fg_truncate (const char *path, off_t size)
517 if (read_only) return -EROFS;
519 dir_cache_invalidate (path);
521 r = guestfs_truncate_size (g, path, size);
529 fg_utimens (const char *path, const struct timespec ts[2])
533 if (read_only) return -EROFS;
535 dir_cache_invalidate (path);
537 time_t atsecs = ts[0].tv_sec;
538 long atnsecs = ts[0].tv_nsec;
539 time_t mtsecs = ts[1].tv_sec;
540 long mtnsecs = ts[1].tv_nsec;
543 if (atnsecs == UTIME_NOW)
547 if (atnsecs == UTIME_OMIT)
551 if (mtnsecs == UTIME_NOW)
555 if (mtnsecs == UTIME_OMIT)
559 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
566 /* This call is quite hard to emulate through the guestfs(3) API. In
567 * one sense it's a little like access (see above) because it tests
568 * whether opening a file would succeed given the flags. But it also
569 * has side effects such as truncating the file if O_TRUNC is given.
570 * Therefore we need to emulate it ... painfully.
573 fg_open (const char *path, struct fuse_file_info *fi)
577 if (fi->flags & O_WRONLY) {
582 exists = guestfs_exists (g, path);
586 if (fi->flags & O_CREAT) {
590 dir_cache_invalidate (path);
592 /* Exclusive? File must not exist already. */
593 if (fi->flags & O_EXCL) {
598 /* Create? Touch it and optionally truncate it. */
599 r = guestfs_touch (g, path);
603 if (fi->flags & O_TRUNC) {
604 r = guestfs_truncate (g, path);
609 /* Not create, just check it exists. */
618 fg_read (const char *path, char *buf, size_t size, off_t offset,
619 struct fuse_file_info *fi)
625 fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
628 /* The guestfs protocol limits size to somewhere over 2MB. We just
629 * reduce the requested size here accordingly and push the problem
630 * up to every user. http://www.jwz.org/doc/worse-is-better.html
632 const size_t limit = 2 * 1024 * 1024;
636 r = guestfs_pread (g, path, size, offset, &rsize);
640 /* This should never happen, but at least it stops us overflowing
641 * the output buffer if it does happen.
646 memcpy (buf, r, rsize);
653 fg_write (const char *path, const char *buf, size_t size,
654 off_t offset, struct fuse_file_info *fi)
656 if (read_only) return -EROFS;
658 dir_cache_invalidate (path);
661 const size_t limit = 2 * 1024 * 1024;
666 r = guestfs_pwrite (g, path, buf, size, offset);
674 fg_statfs (const char *path, struct statvfs *stbuf)
676 struct guestfs_statvfs *r;
678 r = guestfs_statvfs (g, path);
682 stbuf->f_bsize = r->bsize;
683 stbuf->f_frsize = r->frsize;
684 stbuf->f_blocks = r->blocks;
685 stbuf->f_bfree = r->bfree;
686 stbuf->f_bavail = r->bavail;
687 stbuf->f_files = r->files;
688 stbuf->f_ffree = r->ffree;
689 stbuf->f_favail = r->favail;
690 stbuf->f_fsid = r->fsid;
691 stbuf->f_flag = r->flag;
692 stbuf->f_namemax = r->namemax;
694 guestfs_free_statvfs (r);
700 fg_release (const char *path, struct fuse_file_info *fi)
702 /* Just a stub. This method is optional and can safely be left
708 /* Emulate this by calling sync. */
709 static int fg_fsync(const char *path, int isdatasync,
710 struct fuse_file_info *fi)
714 r = guestfs_sync (g);
722 fg_setxattr (const char *path, const char *name, const char *value,
723 size_t size, int flags)
727 if (read_only) return -EROFS;
729 dir_cache_invalidate (path);
731 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
732 r = guestfs_lsetxattr (g, name, value, size, path);
739 /* The guestfs(3) API for getting xattrs is much easier to use
740 * than the real syscall. Unfortunately we now have to emulate
741 * the real syscall using that API :-(
744 fg_getxattr (const char *path, const char *name, char *value,
747 const struct guestfs_xattr_list *xattrs;
750 xattrs = xac_lookup (path);
751 if (xattrs == NULL) {
752 xattrs = guestfs_lgetxattrs (g, path);
760 for (i = 0; i < xattrs->len; ++i) {
761 if (STREQ (xattrs->val[i].attrname, name)) {
762 size_t sz = xattrs->val[i].attrval_len;
765 memcpy (value, xattrs->val[i].attrval, sz);
772 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
777 /* Ditto as above. */
779 fg_listxattr (const char *path, char *list, size_t size)
781 const struct guestfs_xattr_list *xattrs;
784 xattrs = xac_lookup (path);
785 if (xattrs == NULL) {
786 xattrs = guestfs_lgetxattrs (g, path);
794 for (i = 0; i < xattrs->len; ++i) {
795 size_t len = strlen (xattrs->val[i].attrname) + 1;
797 memcpy (list, xattrs->val[i].attrname, len);
808 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
814 fg_removexattr(const char *path, const char *name)
818 if (read_only) return -EROFS;
820 dir_cache_invalidate (path);
822 r = guestfs_lremovexattr (g, name, path);
829 static struct fuse_operations fg_operations = {
830 .getattr = fg_getattr,
832 .readlink = fg_readlink,
833 .readdir = fg_readdir,
836 .symlink = fg_symlink,
843 .truncate = fg_truncate,
844 .utimens = fg_utimens,
849 .release = fg_release,
851 .setxattr = fg_setxattr,
852 .getxattr = fg_getxattr,
853 .listxattr = fg_listxattr,
854 .removexattr = fg_removexattr,
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-2010 Red Hat Inc.\n"
877 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
879 " -a|--add image Add image\n"
880 " -c|--connect uri Specify libvirt URI for -d option\n"
881 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
882 " -d|--domain guest Add disks from libvirt guest\n"
883 " --format[=raw|..] Force disk format for -a option\n"
884 " --fuse-help Display extra FUSE options\n"
885 " -i|--inspector Automatically mount filesystems\n"
886 " --help Display help message and exit\n"
887 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
888 " -n|--no-sync Don't autosync\n"
889 " -o|--option opt Pass extra option to FUSE\n"
890 " -r|--ro Mount read-only\n"
891 " --selinux Enable SELinux support\n"
892 " -v|--verbose Verbose messages\n"
893 " -V|--version Display version and exit\n"
894 " -x|--trace Trace guestfs API calls\n"
896 program_name, program_name, program_name);
902 main (int argc, char *argv[])
904 setlocale (LC_ALL, "");
905 bindtextdomain (PACKAGE, LOCALEBASEDIR);
906 textdomain (PACKAGE);
908 enum { HELP_OPTION = CHAR_MAX + 1 };
910 /* The command line arguments are broadly compatible with (a subset
911 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
913 static const char *options = "a:c:d:im:no:rv?Vwx";
914 static const struct option long_options[] = {
915 { "add", 1, 0, 'a' },
916 { "connect", 1, 0, 'c' },
917 { "dir-cache-timeout", 1, 0, 0 },
918 { "domain", 1, 0, 'd' },
919 { "format", 2, 0, 0 },
920 { "fuse-help", 0, 0, 0 },
921 { "help", 0, 0, HELP_OPTION },
922 { "inspector", 0, 0, 'i' },
923 { "mount", 1, 0, 'm' },
924 { "no-sync", 0, 0, 'n' },
925 { "option", 1, 0, 'o' },
928 { "selinux", 0, 0, 0 },
929 { "trace", 0, 0, 'x' },
930 { "verbose", 0, 0, 'v' },
931 { "version", 0, 0, 'V' },
935 struct drv *drvs = NULL;
937 struct mp *mps = NULL;
940 const char *format = NULL;
946 const char **fuse_argv = NULL;
948 #define ADD_FUSE_ARG(str) \
951 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
953 perror ("realloc"); \
954 exit (EXIT_FAILURE); \
956 fuse_argv[fuse_argc-1] = (str); \
957 fuse_argv[fuse_argc] = NULL; \
960 /* LC_ALL=C is required so we can parse error messages. */
961 setenv ("LC_ALL", "C", 1);
963 /* Set global program name that is not polluted with libtool artifacts. */
964 set_program_name (argv[0]);
966 memset (&sa, 0, sizeof sa);
967 sa.sa_handler = SIG_IGN;
968 sa.sa_flags = SA_RESTART;
969 sigaction (SIGPIPE, &sa, NULL);
971 /* Various initialization. */
974 g = guestfs_create ();
976 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
980 guestfs_set_recovery_proc (g, 0);
982 ADD_FUSE_ARG (program_name);
983 /* MUST be single-threaded. You cannot have two threads accessing the
984 * same libguestfs handle, and opening more than one handle is likely
985 * to be very expensive.
989 /* If developing, add ./appliance to the path. Note that libtools
990 * interferes with this because uninstalled guestfish is a shell
991 * script that runs the real program with an absolute path. Detect
994 * BUT if LIBGUESTFS_PATH environment variable is already set by
995 * the user, then don't override it.
997 if (getenv ("LIBGUESTFS_PATH") == NULL &&
999 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
1000 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
1003 c = getopt_long (argc, argv, options, long_options, &option_index);
1007 case 0: /* options which are long only */
1008 if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
1009 dir_cache_timeout = atoi (optarg);
1010 else if (STREQ (long_options[option_index].name, "fuse-help"))
1012 else if (STREQ (long_options[option_index].name, "selinux"))
1013 guestfs_set_selinux (g, 1);
1014 else if (STREQ (long_options[option_index].name, "format")) {
1015 if (!optarg || STREQ (optarg, ""))
1021 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
1022 program_name, long_options[option_index].name, option_index);
1023 exit (EXIT_FAILURE);
1052 ADD_FUSE_ARG ("-o");
1053 ADD_FUSE_ARG (optarg);
1074 ADD_FUSE_ARG ("-f");
1075 guestfs_set_recovery_proc (g, 1);
1079 usage (EXIT_SUCCESS);
1082 usage (EXIT_FAILURE);
1086 /* Check we have the right options. */
1087 if (!drvs || !(mps || inspector)) {
1089 _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1091 exit (EXIT_FAILURE);
1094 /* We'd better have a mountpoint. */
1095 if (optind+1 != argc) {
1097 _("%s: you must specify a mountpoint in the host filesystem\n"),
1099 exit (EXIT_FAILURE);
1102 /* Do the guest drives and mountpoints. */
1103 add_drives (drvs, 'a');
1104 if (guestfs_launch (g) == -1)
1105 exit (EXIT_FAILURE);
1113 /* FUSE example does this, not clear if it's necessary, but ... */
1114 if (guestfs_umask (g, 0) == -1)
1115 exit (EXIT_FAILURE);
1117 /* At the last minute, remove the libguestfs error handler. In code
1118 * above this point, the default error handler has been used which
1119 * sends all errors to stderr. Now before entering FUSE itself we
1120 * want to silence errors so we can convert them (see error()
1123 guestfs_set_error_handler (g, NULL, NULL);
1125 /* Finish off FUSE args. */
1126 ADD_FUSE_ARG (argv[optind]);
1129 It says about the line containing the for-statement:
1130 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1133 fprintf (stderr, "guestmount: invoking FUSE with args [");
1134 for (i = 0; i < fuse_argc; ++i) {
1135 if (i > 0) fprintf (stderr, ", ");
1136 fprintf (stderr, "%s", fuse_argv[i]);
1138 fprintf (stderr, "]\n");
1142 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1148 exit (r == -1 ? 1 : 0);