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 int keys_from_stdin = 0;
66 const char *libvirt_uri;
67 int dir_cache_timeout = 60;
72 return -guestfs_last_errno (g);
75 static struct guestfs_xattr_list *
76 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
78 struct guestfs_xattr_list *xattrs;
80 xattrs = malloc (sizeof *xattrs);
87 xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
88 if (xattrs->val == NULL) {
95 for (i = 0; i < num; ++i) {
96 xattrs->val[i].attrname = strdup (first[i].attrname);
97 xattrs->val[i].attrval_len = first[i].attrval_len;
98 xattrs->val[i].attrval = malloc (first[i].attrval_len);
99 memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
106 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
107 off_t offset, struct fuse_file_info *fi)
112 dir_cache_remove_all_expired (now);
114 struct guestfs_dirent_list *ents;
116 ents = guestfs_readdir (g, path);
121 for (i = 0; i < ents->len; ++i) {
123 memset (&stat, 0, sizeof stat);
125 stat.st_ino = ents->val[i].ino;
126 switch (ents->val[i].ftyp) {
127 case 'b': stat.st_mode = S_IFBLK; break;
128 case 'c': stat.st_mode = S_IFCHR; break;
129 case 'd': stat.st_mode = S_IFDIR; break;
130 case 'f': stat.st_mode = S_IFIFO; break;
131 case 'l': stat.st_mode = S_IFLNK; break;
132 case 'r': stat.st_mode = S_IFREG; break;
133 case 's': stat.st_mode = S_IFSOCK; break;
136 default: stat.st_mode = 0;
139 /* Copied from the example, which also ignores 'offset'. I'm
140 * not quite sure how this is ever supposed to work on large
143 if (filler (buf, ents->val[i].name, &stat, 0))
147 /* Now prepopulate the directory caches. This step is just an
148 * optimization, don't worry if it fails.
150 char **names = malloc ((ents->len + 1) * sizeof (char *));
152 for (i = 0; i < ents->len; ++i)
153 names[i] = ents->val[i].name;
156 struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
158 for (i = 0; i < ss->len; ++i) {
159 if (ss->val[i].ino >= 0) {
162 statbuf.st_dev = ss->val[i].dev;
163 statbuf.st_ino = ss->val[i].ino;
164 statbuf.st_mode = ss->val[i].mode;
165 statbuf.st_nlink = ss->val[i].nlink;
166 statbuf.st_uid = ss->val[i].uid;
167 statbuf.st_gid = ss->val[i].gid;
168 statbuf.st_rdev = ss->val[i].rdev;
169 statbuf.st_size = ss->val[i].size;
170 statbuf.st_blksize = ss->val[i].blksize;
171 statbuf.st_blocks = ss->val[i].blocks;
172 statbuf.st_atime = ss->val[i].atime;
173 statbuf.st_mtime = ss->val[i].mtime;
174 statbuf.st_ctime = ss->val[i].ctime;
176 lsc_insert (path, names[i], now, &statbuf);
179 guestfs_free_stat_list (ss);
182 struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
185 struct guestfs_xattr *first;
186 struct guestfs_xattr_list *copy;
187 for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
188 assert (strlen (xattrs->val[i].attrname) == 0);
189 if (xattrs->val[i].attrval_len > 0) {
191 first = &xattrs->val[i];
193 for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
196 copy = copy_xattr_list (first, num);
198 xac_insert (path, names[ni], now, copy);
203 guestfs_free_xattr_list (xattrs);
206 char **links = guestfs_readlinklist (g, path, names);
208 for (i = 0; names[i] != NULL; ++i) {
210 /* Note that rlc_insert owns the string links[i] after this, */
211 rlc_insert (path, names[i], now, links[i]);
213 /* which is why we have to free links[i] here. */
216 free (links); /* free the array, not the strings */
222 guestfs_free_dirent_list (ents);
228 fg_getattr (const char *path, struct stat *statbuf)
230 const struct stat *buf;
232 buf = lsc_lookup (path);
234 memcpy (statbuf, buf, sizeof *statbuf);
238 struct guestfs_stat *r;
240 r = guestfs_lstat (g, path);
244 statbuf->st_dev = r->dev;
245 statbuf->st_ino = r->ino;
246 statbuf->st_mode = r->mode;
247 statbuf->st_nlink = r->nlink;
248 statbuf->st_uid = r->uid;
249 statbuf->st_gid = r->gid;
250 statbuf->st_rdev = r->rdev;
251 statbuf->st_size = r->size;
252 statbuf->st_blksize = r->blksize;
253 statbuf->st_blocks = r->blocks;
254 statbuf->st_atime = r->atime;
255 statbuf->st_mtime = r->mtime;
256 statbuf->st_ctime = r->ctime;
258 guestfs_free_stat (r);
263 /* Nautilus loves to use access(2) to test everything about a file,
264 * such as whether it's executable. Therefore treat this a lot like
268 fg_access (const char *path, int mask)
273 if (read_only && (mask & W_OK))
276 r = fg_getattr (path, &statbuf);
277 if (r < 0 || mask == F_OK)
280 struct fuse_context *fuse = fuse_get_context ();
285 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
286 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
287 : statbuf.st_mode & S_IROTH);
290 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
291 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
292 : statbuf.st_mode & S_IWOTH);
295 ( fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
296 : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
297 : statbuf.st_mode & S_IXOTH);
299 return ok ? 0 : -EACCES;
303 fg_readlink (const char *path, char *buf, size_t size)
308 r = rlc_lookup (path);
310 r = guestfs_readlink (g, path);
316 /* Note this is different from the real readlink(2) syscall. FUSE wants
317 * the string to be always nul-terminated, even if truncated.
319 size_t len = strlen (r);
323 memcpy (buf, r, len);
327 char *tmp = (char *) r;
335 fg_mknod (const char *path, mode_t mode, dev_t rdev)
339 if (read_only) return -EROFS;
341 dir_cache_invalidate (path);
343 r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
351 fg_mkdir (const char *path, mode_t mode)
355 if (read_only) return -EROFS;
357 dir_cache_invalidate (path);
359 r = guestfs_mkdir_mode (g, path, mode);
367 fg_unlink (const char *path)
371 if (read_only) return -EROFS;
373 dir_cache_invalidate (path);
375 r = guestfs_rm (g, path);
383 fg_rmdir (const char *path)
387 if (read_only) return -EROFS;
389 dir_cache_invalidate (path);
391 r = guestfs_rmdir (g, path);
399 fg_symlink (const char *from, const char *to)
403 if (read_only) return -EROFS;
405 dir_cache_invalidate (to);
407 r = guestfs_ln_s (g, from, to);
415 fg_rename (const char *from, const char *to)
419 if (read_only) return -EROFS;
421 dir_cache_invalidate (from);
422 dir_cache_invalidate (to);
424 /* XXX It's not clear how close the 'mv' command is to the
425 * rename syscall. We might need to add the rename syscall
426 * to the guestfs(3) API.
428 r = guestfs_mv (g, from, to);
436 fg_link (const char *from, const char *to)
440 if (read_only) return -EROFS;
442 dir_cache_invalidate (from);
443 dir_cache_invalidate (to);
445 r = guestfs_ln (g, from, to);
453 fg_chmod (const char *path, mode_t mode)
457 if (read_only) return -EROFS;
459 dir_cache_invalidate (path);
461 r = guestfs_chmod (g, mode, path);
469 fg_chown (const char *path, uid_t uid, gid_t gid)
473 if (read_only) return -EROFS;
475 dir_cache_invalidate (path);
477 r = guestfs_lchown (g, uid, gid, path);
485 fg_truncate (const char *path, off_t size)
489 if (read_only) return -EROFS;
491 dir_cache_invalidate (path);
493 r = guestfs_truncate_size (g, path, size);
501 fg_utimens (const char *path, const struct timespec ts[2])
505 if (read_only) return -EROFS;
507 dir_cache_invalidate (path);
509 time_t atsecs = ts[0].tv_sec;
510 long atnsecs = ts[0].tv_nsec;
511 time_t mtsecs = ts[1].tv_sec;
512 long mtnsecs = ts[1].tv_nsec;
515 if (atnsecs == UTIME_NOW)
519 if (atnsecs == UTIME_OMIT)
523 if (mtnsecs == UTIME_NOW)
527 if (mtnsecs == UTIME_OMIT)
531 r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
538 /* All this function needs to do is to check that the requested open
539 * flags are valid. See the notes in <fuse/fuse.h>.
542 fg_open (const char *path, struct fuse_file_info *fi)
544 int flags = fi->flags & 3;
546 if (read_only && flags != O_RDONLY)
553 fg_read (const char *path, char *buf, size_t size, off_t offset,
554 struct fuse_file_info *fi)
560 fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
563 /* The guestfs protocol limits size to somewhere over 2MB. We just
564 * reduce the requested size here accordingly and push the problem
565 * up to every user. http://www.jwz.org/doc/worse-is-better.html
567 const size_t limit = 2 * 1024 * 1024;
571 r = guestfs_pread (g, path, size, offset, &rsize);
575 /* This should never happen, but at least it stops us overflowing
576 * the output buffer if it does happen.
581 memcpy (buf, r, rsize);
588 fg_write (const char *path, const char *buf, size_t size,
589 off_t offset, struct fuse_file_info *fi)
591 if (read_only) return -EROFS;
593 dir_cache_invalidate (path);
596 const size_t limit = 2 * 1024 * 1024;
601 r = guestfs_pwrite (g, path, buf, size, offset);
609 fg_statfs (const char *path, struct statvfs *stbuf)
611 struct guestfs_statvfs *r;
613 r = guestfs_statvfs (g, path);
617 stbuf->f_bsize = r->bsize;
618 stbuf->f_frsize = r->frsize;
619 stbuf->f_blocks = r->blocks;
620 stbuf->f_bfree = r->bfree;
621 stbuf->f_bavail = r->bavail;
622 stbuf->f_files = r->files;
623 stbuf->f_ffree = r->ffree;
624 stbuf->f_favail = r->favail;
625 stbuf->f_fsid = r->fsid;
626 stbuf->f_flag = r->flag;
627 stbuf->f_namemax = r->namemax;
629 guestfs_free_statvfs (r);
635 fg_release (const char *path, struct fuse_file_info *fi)
637 /* Just a stub. This method is optional and can safely be left
643 /* Emulate this by calling sync. */
644 static int fg_fsync(const char *path, int isdatasync,
645 struct fuse_file_info *fi)
649 r = guestfs_sync (g);
657 fg_setxattr (const char *path, const char *name, const char *value,
658 size_t size, int flags)
662 if (read_only) return -EROFS;
664 dir_cache_invalidate (path);
666 /* XXX Underlying guestfs(3) API doesn't understand the flags. */
667 r = guestfs_lsetxattr (g, name, value, size, path);
674 /* The guestfs(3) API for getting xattrs is much easier to use
675 * than the real syscall. Unfortunately we now have to emulate
676 * the real syscall using that API :-(
679 fg_getxattr (const char *path, const char *name, char *value,
682 const struct guestfs_xattr_list *xattrs;
685 xattrs = xac_lookup (path);
686 if (xattrs == NULL) {
687 xattrs = guestfs_lgetxattrs (g, path);
695 for (i = 0; i < xattrs->len; ++i) {
696 if (STREQ (xattrs->val[i].attrname, name)) {
697 size_t sz = xattrs->val[i].attrval_len;
700 memcpy (value, xattrs->val[i].attrval, sz);
707 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
712 /* Ditto as above. */
714 fg_listxattr (const char *path, char *list, size_t size)
716 const struct guestfs_xattr_list *xattrs;
719 xattrs = xac_lookup (path);
720 if (xattrs == NULL) {
721 xattrs = guestfs_lgetxattrs (g, path);
729 for (i = 0; i < xattrs->len; ++i) {
730 size_t len = strlen (xattrs->val[i].attrname) + 1;
732 memcpy (list, xattrs->val[i].attrname, len);
743 guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
749 fg_removexattr(const char *path, const char *name)
753 if (read_only) return -EROFS;
755 dir_cache_invalidate (path);
757 r = guestfs_lremovexattr (g, name, path);
764 static struct fuse_operations fg_operations = {
765 .getattr = fg_getattr,
767 .readlink = fg_readlink,
768 .readdir = fg_readdir,
771 .symlink = fg_symlink,
778 .truncate = fg_truncate,
779 .utimens = fg_utimens,
784 .release = fg_release,
786 .setxattr = fg_setxattr,
787 .getxattr = fg_getxattr,
788 .listxattr = fg_listxattr,
789 .removexattr = fg_removexattr,
792 static void __attribute__((noreturn))
795 const char *tmp_argv[] = { program_name, "--help", NULL };
796 fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
800 static void __attribute__((noreturn))
803 if (status != EXIT_SUCCESS)
804 fprintf (stderr, _("Try `%s --help' for more information.\n"),
808 _("%s: FUSE module for libguestfs\n"
809 "%s lets you mount a virtual machine filesystem\n"
810 "Copyright (C) 2009-2010 Red Hat Inc.\n"
812 " %s [--options] [-- [--FUSE-options]] mountpoint\n"
814 " -a|--add image Add image\n"
815 " -c|--connect uri Specify libvirt URI for -d option\n"
816 " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n"
817 " -d|--domain guest Add disks from libvirt guest\n"
818 " --echo-keys Don't turn off echo for passphrases\n"
819 " --format[=raw|..] Force disk format for -a option\n"
820 " --fuse-help Display extra FUSE options\n"
821 " -i|--inspector Automatically mount filesystems\n"
822 " --help Display help message and exit\n"
823 " --keys-from-stdin Read passphrases from stdin\n"
824 " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
825 " -n|--no-sync Don't autosync\n"
826 " -o|--option opt Pass extra option to FUSE\n"
827 " -r|--ro Mount read-only\n"
828 " --selinux Enable SELinux support\n"
829 " -v|--verbose Verbose messages\n"
830 " -V|--version Display version and exit\n"
831 " -x|--trace Trace guestfs API calls\n"
833 program_name, program_name, program_name);
839 main (int argc, char *argv[])
841 setlocale (LC_ALL, "");
842 bindtextdomain (PACKAGE, LOCALEBASEDIR);
843 textdomain (PACKAGE);
845 enum { HELP_OPTION = CHAR_MAX + 1 };
847 /* The command line arguments are broadly compatible with (a subset
848 * of) guestfish. Thus we have to deal mainly with -a, -m and --ro.
850 static const char *options = "a:c:d:im:no:rv?Vwx";
851 static const struct option long_options[] = {
852 { "add", 1, 0, 'a' },
853 { "connect", 1, 0, 'c' },
854 { "dir-cache-timeout", 1, 0, 0 },
855 { "domain", 1, 0, 'd' },
856 { "echo-keys", 0, 0, 0 },
857 { "format", 2, 0, 0 },
858 { "fuse-help", 0, 0, 0 },
859 { "help", 0, 0, HELP_OPTION },
860 { "inspector", 0, 0, 'i' },
861 { "keys-from-stdin", 0, 0, 0 },
862 { "mount", 1, 0, 'm' },
863 { "no-sync", 0, 0, 'n' },
864 { "option", 1, 0, 'o' },
867 { "selinux", 0, 0, 0 },
868 { "trace", 0, 0, 'x' },
869 { "verbose", 0, 0, 'v' },
870 { "version", 0, 0, 'V' },
874 struct drv *drvs = NULL;
876 struct mp *mps = NULL;
879 const char *format = NULL;
885 const char **fuse_argv = NULL;
887 #define ADD_FUSE_ARG(str) \
890 fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \
892 perror ("realloc"); \
893 exit (EXIT_FAILURE); \
895 fuse_argv[fuse_argc-1] = (str); \
896 fuse_argv[fuse_argc] = NULL; \
899 /* LC_ALL=C is required so we can parse error messages. */
900 setenv ("LC_ALL", "C", 1);
902 /* Set global program name that is not polluted with libtool artifacts. */
903 set_program_name (argv[0]);
905 memset (&sa, 0, sizeof sa);
906 sa.sa_handler = SIG_IGN;
907 sa.sa_flags = SA_RESTART;
908 sigaction (SIGPIPE, &sa, NULL);
910 /* Various initialization. */
913 g = guestfs_create ();
915 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
919 guestfs_set_recovery_proc (g, 0);
921 ADD_FUSE_ARG (program_name);
922 /* MUST be single-threaded. You cannot have two threads accessing the
923 * same libguestfs handle, and opening more than one handle is likely
924 * to be very expensive.
928 /* If developing, add ./appliance to the path. Note that libtools
929 * interferes with this because uninstalled guestfish is a shell
930 * script that runs the real program with an absolute path. Detect
933 * BUT if LIBGUESTFS_PATH environment variable is already set by
934 * the user, then don't override it.
936 if (getenv ("LIBGUESTFS_PATH") == NULL &&
938 (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
939 guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
942 c = getopt_long (argc, argv, options, long_options, &option_index);
946 case 0: /* options which are long only */
947 if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
948 dir_cache_timeout = atoi (optarg);
949 else if (STREQ (long_options[option_index].name, "fuse-help"))
951 else if (STREQ (long_options[option_index].name, "selinux"))
952 guestfs_set_selinux (g, 1);
953 else if (STREQ (long_options[option_index].name, "format")) {
954 if (!optarg || STREQ (optarg, ""))
958 } else if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
960 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
963 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
964 program_name, long_options[option_index].name, option_index);
995 ADD_FUSE_ARG (optarg);
1016 ADD_FUSE_ARG ("-f");
1017 guestfs_set_recovery_proc (g, 1);
1021 usage (EXIT_SUCCESS);
1024 usage (EXIT_FAILURE);
1028 /* Check we have the right options. */
1029 if (!drvs || !(mps || inspector)) {
1031 _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1033 exit (EXIT_FAILURE);
1036 /* We'd better have a mountpoint. */
1037 if (optind+1 != argc) {
1039 _("%s: you must specify a mountpoint in the host filesystem\n"),
1041 exit (EXIT_FAILURE);
1044 /* Do the guest drives and mountpoints. */
1045 add_drives (drvs, 'a');
1046 if (guestfs_launch (g) == -1)
1047 exit (EXIT_FAILURE);
1055 /* FUSE example does this, not clear if it's necessary, but ... */
1056 if (guestfs_umask (g, 0) == -1)
1057 exit (EXIT_FAILURE);
1059 /* At the last minute, remove the libguestfs error handler. In code
1060 * above this point, the default error handler has been used which
1061 * sends all errors to stderr. Now before entering FUSE itself we
1062 * want to silence errors so we can convert them (see error()
1065 guestfs_set_error_handler (g, NULL, NULL);
1067 /* Finish off FUSE args. */
1068 ADD_FUSE_ARG (argv[optind]);
1071 It says about the line containing the for-statement:
1072 error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1075 fprintf (stderr, "guestmount: invoking FUSE with args [");
1076 for (i = 0; i < fuse_argc; ++i) {
1077 if (i > 0) fprintf (stderr, ", ");
1078 fprintf (stderr, "%s", fuse_argv[i]);
1080 fprintf (stderr, "]\n");
1084 r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1090 exit (r == -1 ? 1 : 0);