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;
70 return -guestfs_last_errno (g);
73 static struct guestfs_xattr_list *
74 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
76 struct guestfs_xattr_list *xattrs;
78 xattrs = malloc (sizeof *xattrs);
85 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
86 if (xattrs->val == NULL) {
93 for (i = 0; i < num; ++i) {
94 xattrs->val[i].attrname = strdup (first[i].attrname);
95 xattrs->val[i].attrval_len = first[i].attrval_len;
96 xattrs->val[i].attrval = malloc (first[i].attrval_len);
97 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
104 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
105 off_t offset, struct fuse_file_info *fi)
110 dir_cache_remove_all_expired (now);
112 struct guestfs_dirent_list *ents;
114 ents = guestfs_readdir (g, path);
119 for (i = 0; i < ents->len; ++i) {
121 memset (&stat, 0, sizeof stat);
123 stat.st_ino = ents->val[i].ino;
124 switch (ents->val[i].ftyp) {
125 case 'b': stat.st_mode = S_IFBLK; break;
126 case 'c': stat.st_mode = S_IFCHR; break;
127 case 'd': stat.st_mode = S_IFDIR; break;
128 case 'f': stat.st_mode = S_IFIFO; break;
129 case 'l': stat.st_mode = S_IFLNK; break;
130 case 'r': stat.st_mode = S_IFREG; break;
131 case 's': stat.st_mode = S_IFSOCK; break;
134 default: stat.st_mode = 0;
137 /* Copied from the example, which also ignores 'offset'. I'm
138 * not quite sure how this is ever supposed to work on large
141 if (filler (buf, ents->val[i].name, &stat, 0))
145 /* Now prepopulate the directory caches. This step is just an
146 * optimization, don't worry if it fails.
148 char **names = malloc ((ents->len + 1) * sizeof (char *));
150 for (i = 0; i < ents->len; ++i)
151 names[i] = ents->val[i].name;
154 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
156 for (i = 0; i < ss->len; ++i) {
157 if (ss->val[i].ino >= 0) {
160 statbuf.st_dev = ss->val[i].dev;
161 statbuf.st_ino = ss->val[i].ino;
162 statbuf.st_mode = ss->val[i].mode;
163 statbuf.st_nlink = ss->val[i].nlink;
164 statbuf.st_uid = ss->val[i].uid;
165 statbuf.st_gid = ss->val[i].gid;
166 statbuf.st_rdev = ss->val[i].rdev;
167 statbuf.st_size = ss->val[i].size;
168 statbuf.st_blksize = ss->val[i].blksize;
169 statbuf.st_blocks = ss->val[i].blocks;
170 statbuf.st_atime = ss->val[i].atime;
171 statbuf.st_mtime = ss->val[i].mtime;
172 statbuf.st_ctime = ss->val[i].ctime;
174 lsc_insert (path, names[i], now, &statbuf);
177 guestfs_free_stat_list (ss);
180 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
183 struct guestfs_xattr *first;
184 struct guestfs_xattr_list *copy;
185 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
186 assert (strlen (xattrs->val[i].attrname) == 0);
187 if (xattrs->val[i].attrval_len > 0) {
189 first = &xattrs->val[i];
191 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
194 copy = copy_xattr_list (first, num);
196 xac_insert (path, names[ni], now, copy);
201 guestfs_free_xattr_list (xattrs);
204 char **links = guestfs_readlinklist (g, path, names);
206 for (i = 0; names[i] != NULL; ++i) {
208 /* Note that rlc_insert owns the string links[i] after this, */
209 rlc_insert (path, names[i], now, links[i]);
211 /* which is why we have to free links[i] here. */
214 free (links); /* free the array, not the strings */
220 guestfs_free_dirent_list (ents);
226 fg_getattr (const char *path, struct stat *statbuf)
228 const struct stat *buf;
230 buf = lsc_lookup (path);
232 memcpy (statbuf, buf, sizeof *statbuf);
236 struct guestfs_stat *r;
238 r = guestfs_lstat (g, path);
242 statbuf->st_dev = r->dev;
243 statbuf->st_ino = r->ino;
244 statbuf->st_mode = r->mode;
245 statbuf->st_nlink = r->nlink;
246 statbuf->st_uid = r->uid;
247 statbuf->st_gid = r->gid;
248 statbuf->st_rdev = r->rdev;
249 statbuf->st_size = r->size;
250 statbuf->st_blksize = r->blksize;
251 statbuf->st_blocks = r->blocks;
252 statbuf->st_atime = r->atime;
253 statbuf->st_mtime = r->mtime;
254 statbuf->st_ctime = r->ctime;
256 guestfs_free_stat (r);
261 /* Nautilus loves to use access(2) to test everything about a file,
262 * such as whether it's executable. Therefore treat this a lot like
266 fg_access (const char *path, int mask)
271 if (read_only && (mask & W_OK))
274 r = fg_getattr (path, &statbuf);
275 if (r < 0 || mask == F_OK)
278 struct fuse_context *fuse = fuse_get_context ();
283 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
284 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
285 : statbuf.st_mode & S_IROTH);
288 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
289 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
290 : statbuf.st_mode & S_IWOTH);
293 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
294 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
295 : statbuf.st_mode & S_IXOTH);
297 return ok ? 0 : -EACCES;
301 fg_readlink (const char *path, char *buf, size_t size)
306 r = rlc_lookup (path);
308 r = guestfs_readlink (g, path);
314 /* Note this is different from the real readlink(2) syscall. FUSE wants
315 * the string to be always nul-terminated, even if truncated.
317 size_t len = strlen (r);
321 memcpy (buf, r, len);
325 char *tmp = (char *) r;
333 fg_mknod (const char *path, mode_t mode, dev_t rdev)
337 if (read_only) return -EROFS;
339 dir_cache_invalidate (path);
341 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
349 fg_mkdir (const char *path, mode_t mode)
353 if (read_only) return -EROFS;
355 dir_cache_invalidate (path);
357 r = guestfs_mkdir_mode (g, path, mode);
365 fg_unlink (const char *path)
369 if (read_only) return -EROFS;
371 dir_cache_invalidate (path);
373 r = guestfs_rm (g, path);
381 fg_rmdir (const char *path)
385 if (read_only) return -EROFS;
387 dir_cache_invalidate (path);
389 r = guestfs_rmdir (g, path);
397 fg_symlink (const char *from, const char *to)
401 if (read_only) return -EROFS;
403 dir_cache_invalidate (to);
405 r = guestfs_ln_s (g, from, to);
413 fg_rename (const char *from, const char *to)
417 if (read_only) return -EROFS;
419 dir_cache_invalidate (from);
420 dir_cache_invalidate (to);
422 /* XXX It's not clear how close the 'mv' command is to the
423 * rename syscall. We might need to add the rename syscall
424 * to the guestfs(3) API.
426 r = guestfs_mv (g, from, to);
434 fg_link (const char *from, const char *to)
438 if (read_only) return -EROFS;
440 dir_cache_invalidate (from);
441 dir_cache_invalidate (to);
443 r = guestfs_ln (g, from, to);
451 fg_chmod (const char *path, mode_t mode)
455 if (read_only) return -EROFS;
457 dir_cache_invalidate (path);
459 r = guestfs_chmod (g, mode, path);
467 fg_chown (const char *path, uid_t uid, gid_t gid)
471 if (read_only) return -EROFS;
473 dir_cache_invalidate (path);
475 r = guestfs_lchown (g, uid, gid, path);
483 fg_truncate (const char *path, off_t size)
487 if (read_only) return -EROFS;
489 dir_cache_invalidate (path);
491 r = guestfs_truncate_size (g, path, size);
499 fg_utimens (const char *path, const struct timespec ts[2])
503 if (read_only) return -EROFS;
505 dir_cache_invalidate (path);
507 time_t atsecs = ts[0].tv_sec;
508 long atnsecs = ts[0].tv_nsec;
509 time_t mtsecs = ts[1].tv_sec;
510 long mtnsecs = ts[1].tv_nsec;
513 if (atnsecs == UTIME_NOW)
517 if (atnsecs == UTIME_OMIT)
521 if (mtnsecs == UTIME_NOW)
525 if (mtnsecs == UTIME_OMIT)
529 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
536 /* This call is quite hard to emulate through the guestfs(3) API. In
537 * one sense it's a little like access (see above) because it tests
538 * whether opening a file would succeed given the flags. But it also
539 * has side effects such as truncating the file if O_TRUNC is given.
540 * Therefore we need to emulate it ... painfully.
543 fg_open (const char *path, struct fuse_file_info *fi)
547 if (fi->flags & O_WRONLY) {
552 exists = guestfs_exists (g, path);
556 if (fi->flags & O_CREAT) {
560 dir_cache_invalidate (path);
562 /* Exclusive? File must not exist already. */
563 if (fi->flags & O_EXCL) {
568 /* Create? Touch it and optionally truncate it. */
569 r = guestfs_touch (g, path);
573 if (fi->flags & O_TRUNC) {
574 r = guestfs_truncate (g, path);
579 /* Not create, just check it exists. */
588 fg_read (const char *path, char *buf, size_t size, off_t offset,
589 struct fuse_file_info *fi)
595 fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
598 /* The guestfs protocol limits size to somewhere over 2MB. We just
599 * reduce the requested size here accordingly and push the problem
600 * up to every user. http://www.jwz.org/doc/worse-is-better.html
602 const size_t limit = 2 * 1024 * 1024;
606 r = guestfs_pread (g, path, size, offset, &rsize);
610 /* This should never happen, but at least it stops us overflowing
611 * the output buffer if it does happen.
616 memcpy (buf, r, rsize);
623 fg_write (const char *path, const char *buf, size_t size,
624 off_t offset, struct fuse_file_info *fi)
626 if (read_only) return -EROFS;
628 dir_cache_invalidate (path);
631 const size_t limit = 2 * 1024 * 1024;
636 r = guestfs_pwrite (g, path, buf, size, offset);
644 fg_statfs (const char *path, struct statvfs *stbuf)
646 struct guestfs_statvfs *r;
648 r = guestfs_statvfs (g, path);
652 stbuf->f_bsize = r->bsize;
653 stbuf->f_frsize = r->frsize;
654 stbuf->f_blocks = r->blocks;
655 stbuf->f_bfree = r->bfree;
656 stbuf->f_bavail = r->bavail;
657 stbuf->f_files = r->files;
658 stbuf->f_ffree = r->ffree;
659 stbuf->f_favail = r->favail;
660 stbuf->f_fsid = r->fsid;
661 stbuf->f_flag = r->flag;
662 stbuf->f_namemax = r->namemax;
664 guestfs_free_statvfs (r);
670 fg_release (const char *path, struct fuse_file_info *fi)
672 /* Just a stub. This method is optional and can safely be left
678 /* Emulate this by calling sync. */
679 static int fg_fsync(const char *path, int isdatasync,
680 struct fuse_file_info *fi)
684 r = guestfs_sync (g);
692 fg_setxattr (const char *path, const char *name, const char *value,
693 size_t size, int flags)
697 if (read_only) return -EROFS;
699 dir_cache_invalidate (path);
701 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
702 r = guestfs_lsetxattr (g, name, value, size, path);
709 /* The guestfs(3) API for getting xattrs is much easier to use
710 * than the real syscall. Unfortunately we now have to emulate
711 * the real syscall using that API :-(
714 fg_getxattr (const char *path, const char *name, char *value,
717 const struct guestfs_xattr_list *xattrs;
720 xattrs = xac_lookup (path);
721 if (xattrs == NULL) {
722 xattrs = guestfs_lgetxattrs (g, path);
730 for (i = 0; i < xattrs->len; ++i) {
731 if (STREQ (xattrs->val[i].attrname, name)) {
732 size_t sz = xattrs->val[i].attrval_len;
735 memcpy (value, xattrs->val[i].attrval, sz);
742 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
747 /* Ditto as above. */
749 fg_listxattr (const char *path, char *list, size_t size)
751 const struct guestfs_xattr_list *xattrs;
754 xattrs = xac_lookup (path);
755 if (xattrs == NULL) {
756 xattrs = guestfs_lgetxattrs (g, path);
764 for (i = 0; i < xattrs->len; ++i) {
765 size_t len = strlen (xattrs->val[i].attrname) + 1;
767 memcpy (list, xattrs->val[i].attrname, len);
778 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
784 fg_removexattr(const char *path, const char *name)
788 if (read_only) return -EROFS;
790 dir_cache_invalidate (path);
792 r = guestfs_lremovexattr (g, name, path);
799 static struct fuse_operations fg_operations = {
800 .getattr = fg_getattr,
802 .readlink = fg_readlink,
803 .readdir = fg_readdir,
806 .symlink = fg_symlink,
813 .truncate = fg_truncate,
814 .utimens = fg_utimens,
819 .release = fg_release,
821 .setxattr = fg_setxattr,
822 .getxattr = fg_getxattr,
823 .listxattr = fg_listxattr,
824 .removexattr = fg_removexattr,
827 static void __attribute__((noreturn))
830 const char *tmp_argv[] = { program_name, "--help", NULL };
831 fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
835 static void __attribute__((noreturn))
838 if (status != EXIT_SUCCESS)
839 fprintf (stderr, _("Try `%s --help' for more information.\n"),
843 _("%s: FUSE module for libguestfs\n"
844 "%s lets you mount a virtual machine filesystem\n"
845 "Copyright (C) 2009-2010 Red Hat Inc.\n"
847 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
849 " -a|--add image Add image\n"
850 " -c|--connect uri Specify libvirt URI for -d option\n"
851 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
852 " -d|--domain guest Add disks from libvirt guest\n"
853 " --format[=raw|..] Force disk format for -a option\n"
854 " --fuse-help Display extra FUSE options\n"
855 " -i|--inspector Automatically mount filesystems\n"
856 " --help Display help message and exit\n"
857 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
858 " -n|--no-sync Don't autosync\n"
859 " -o|--option opt Pass extra option to FUSE\n"
860 " -r|--ro Mount read-only\n"
861 " --selinux Enable SELinux support\n"
862 " -v|--verbose Verbose messages\n"
863 " -V|--version Display version and exit\n"
864 " -x|--trace Trace guestfs API calls\n"
866 program_name, program_name, program_name);
872 main (int argc, char *argv[])
874 setlocale (LC_ALL, "");
875 bindtextdomain (PACKAGE, LOCALEBASEDIR);
876 textdomain (PACKAGE);
878 enum { HELP_OPTION = CHAR_MAX + 1 };
880 /* The command line arguments are broadly compatible with (a subset
881 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
883 static const char *options = "a:c:d:im:no:rv?Vx";
884 static const struct option long_options[] = {
885 { "add", 1, 0, 'a' },
886 { "connect", 1, 0, 'c' },
887 { "dir-cache-timeout", 1, 0, 0 },
888 { "domain", 1, 0, 'd' },
889 { "format", 2, 0, 0 },
890 { "fuse-help", 0, 0, 0 },
891 { "help", 0, 0, HELP_OPTION },
892 { "inspector", 0, 0, 'i' },
893 { "mount", 1, 0, 'm' },
894 { "no-sync", 0, 0, 'n' },
895 { "option", 1, 0, 'o' },
897 { "selinux", 0, 0, 0 },
898 { "trace", 0, 0, 'x' },
899 { "verbose", 0, 0, 'v' },
900 { "version", 0, 0, 'V' },
904 struct drv *drvs = NULL;
906 struct mp *mps = NULL;
909 const char *format = NULL;
915 const char **fuse_argv = NULL;
917 #define ADD_FUSE_ARG(str) \
920 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
922 perror ("realloc"); \
923 exit (EXIT_FAILURE); \
925 fuse_argv[fuse_argc-1] = (str); \
926 fuse_argv[fuse_argc] = NULL; \
929 /* LC_ALL=C is required so we can parse error messages. */
930 setenv ("LC_ALL", "C", 1);
932 /* Set global program name that is not polluted with libtool artifacts. */
933 set_program_name (argv[0]);
935 memset (&sa, 0, sizeof sa);
936 sa.sa_handler = SIG_IGN;
937 sa.sa_flags = SA_RESTART;
938 sigaction (SIGPIPE, &sa, NULL);
940 /* Various initialization. */
943 g = guestfs_create ();
945 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
949 guestfs_set_recovery_proc (g, 0);
951 ADD_FUSE_ARG (program_name);
952 /* MUST be single-threaded. You cannot have two threads accessing the
953 * same libguestfs handle, and opening more than one handle is likely
954 * to be very expensive.
958 /* If developing, add ./appliance to the path. Note that libtools
959 * interferes with this because uninstalled guestfish is a shell
960 * script that runs the real program with an absolute path. Detect
963 * BUT if LIBGUESTFS_PATH environment variable is already set by
964 * the user, then don't override it.
966 if (getenv ("LIBGUESTFS_PATH") == NULL &&
968 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
969 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
972 c = getopt_long (argc, argv, options, long_options, &option_index);
976 case 0: /* options which are long only */
977 if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
978 dir_cache_timeout = atoi (optarg);
979 else if (STREQ (long_options[option_index].name, "fuse-help"))
981 else if (STREQ (long_options[option_index].name, "selinux"))
982 guestfs_set_selinux (g, 1);
983 else if (STREQ (long_options[option_index].name, "format")) {
984 if (!optarg || STREQ (optarg, ""))
990 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
991 program_name, long_options[option_index].name, option_index);
1021 ADD_FUSE_ARG ("-o");
1022 ADD_FUSE_ARG (optarg);
1039 ADD_FUSE_ARG ("-f");
1040 guestfs_set_recovery_proc (g, 1);
1044 usage (EXIT_SUCCESS);
1047 usage (EXIT_FAILURE);
1051 /* Check we have the right options. */
1052 if (!drvs || !(mps || inspector)) {
1054 _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1056 exit (EXIT_FAILURE);
1059 /* We'd better have a mountpoint. */
1060 if (optind+1 != argc) {
1062 _("%s: you must specify a mountpoint in the host filesystem\n"),
1064 exit (EXIT_FAILURE);
1067 /* Do the guest drives and mountpoints. */
1068 add_drives (drvs, 'a');
1069 if (guestfs_launch (g) == -1)
1070 exit (EXIT_FAILURE);
1078 /* FUSE example does this, not clear if it's necessary, but ... */
1079 if (guestfs_umask (g, 0) == -1)
1080 exit (EXIT_FAILURE);
1082 /* At the last minute, remove the libguestfs error handler. In code
1083 * above this point, the default error handler has been used which
1084 * sends all errors to stderr. Now before entering FUSE itself we
1085 * want to silence errors so we can convert them (see error()
1088 guestfs_set_error_handler (g, NULL, NULL);
1090 /* Finish off FUSE args. */
1091 ADD_FUSE_ARG (argv[optind]);
1094 It says about the line containing the for-statement:
1095 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1098 fprintf (stderr, "guestmount: invoking FUSE with args [");
1099 for (i = 0; i < fuse_argc; ++i) {
1100 if (i > 0) fprintf (stderr, ", ");
1101 fprintf (stderr, "%s", fuse_argv[i]);
1103 fprintf (stderr, "]\n");
1107 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1113 exit (r == -1 ? 1 : 0);