Version 1.7.1.
[libguestfs.git] / fuse / guestmount.c
1 /* guestmount - mount guests using libguestfs and FUSE
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Derived from the example program 'fusexmp.c':
19  * Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
20  *
21  * This program can be distributed under the terms of the GNU GPL.
22  * See the file COPYING.
23  */
24
25 #define FUSE_USE_VERSION 26
26
27 #include <config.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <time.h>
41 #include <assert.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <locale.h>
45
46 #include <fuse.h>
47 #include <guestfs.h>
48
49 #include "progname.h"
50
51 #include "guestmount.h"
52 #include "options.h"
53 #include "dircache.h"
54
55 /* See <attr/xattr.h> */
56 #ifndef ENOATTR
57 #define ENOATTR ENODATA
58 #endif
59
60 guestfs_h *g = NULL;
61 int read_only = 0;
62 int verbose = 0;
63 int inspector = 0;
64 const char *libvirt_uri;
65 int dir_cache_timeout = 60;
66
67 static int
68 error (void)
69 {
70   return -guestfs_last_errno (g);
71 }
72
73 static struct guestfs_xattr_list *
74 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
75 {
76   struct guestfs_xattr_list *xattrs;
77
78   xattrs = malloc (sizeof *xattrs);
79   if (xattrs == NULL) {
80     perror ("malloc");
81     return NULL;
82   }
83
84   xattrs->len = num;
85   xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
86   if (xattrs->val == NULL) {
87     perror ("malloc");
88     free (xattrs);
89     return NULL;
90   }
91
92   size_t i;
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);
98   }
99
100   return xattrs;
101 }
102
103 static int
104 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
105             off_t offset, struct fuse_file_info *fi)
106 {
107   time_t now;
108   time (&now);
109
110   dir_cache_remove_all_expired (now);
111
112   struct guestfs_dirent_list *ents;
113
114   ents = guestfs_readdir (g, path);
115   if (ents == NULL)
116     return error ();
117
118   size_t i;
119   for (i = 0; i < ents->len; ++i) {
120     struct stat stat;
121     memset (&stat, 0, sizeof stat);
122
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;
132     case 'u':
133     case '?':
134     default:  stat.st_mode = 0;
135     }
136
137     /* Copied from the example, which also ignores 'offset'.  I'm
138      * not quite sure how this is ever supposed to work on large
139      * directories. XXX
140      */
141     if (filler (buf, ents->val[i].name, &stat, 0))
142       break;
143   }
144
145   /* Now prepopulate the directory caches.  This step is just an
146    * optimization, don't worry if it fails.
147    */
148   char **names = malloc ((ents->len + 1) * sizeof (char *));
149   if (names) {
150     for (i = 0; i < ents->len; ++i)
151       names[i] = ents->val[i].name;
152     names[i] = NULL;
153
154     struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
155     if (ss) {
156       for (i = 0; i < ss->len; ++i) {
157         if (ss->val[i].ino >= 0) {
158           struct stat statbuf;
159
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;
173
174           lsc_insert (path, names[i], now, &statbuf);
175         }
176       }
177       guestfs_free_stat_list (ss);
178     }
179
180     struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
181     if (xattrs) {
182       size_t ni, num;
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) {
188           ++i;
189           first = &xattrs->val[i];
190           num = 0;
191           for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
192             num++;
193
194           copy = copy_xattr_list (first, num);
195           if (copy)
196             xac_insert (path, names[ni], now, copy);
197
198           i--;
199         }
200       }
201       guestfs_free_xattr_list (xattrs);
202     }
203
204     char **links = guestfs_readlinklist (g, path, names);
205     if (links) {
206       for (i = 0; names[i] != NULL; ++i) {
207         if (links[i][0])
208           /* Note that rlc_insert owns the string links[i] after this, */
209           rlc_insert (path, names[i], now, links[i]);
210         else
211           /* which is why we have to free links[i] here. */
212           free (links[i]);
213       }
214       free (links);             /* free the array, not the strings */
215     }
216
217     free (names);
218   }
219
220   guestfs_free_dirent_list (ents);
221
222   return 0;
223 }
224
225 static int
226 fg_getattr (const char *path, struct stat *statbuf)
227 {
228   const struct stat *buf;
229
230   buf = lsc_lookup (path);
231   if (buf) {
232     memcpy (statbuf, buf, sizeof *statbuf);
233     return 0;
234   }
235
236   struct guestfs_stat *r;
237
238   r = guestfs_lstat (g, path);
239   if (r == NULL)
240     return error ();
241
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;
255
256   guestfs_free_stat (r);
257
258   return 0;
259 }
260
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
263  * fg_getattr.
264  */
265 static int
266 fg_access (const char *path, int mask)
267 {
268   struct stat statbuf;
269   int r;
270
271   if (read_only && (mask & W_OK))
272     return -EROFS;
273
274   r = fg_getattr (path, &statbuf);
275   if (r < 0 || mask == F_OK)
276     return r;
277
278   struct fuse_context *fuse = fuse_get_context ();
279   int ok = 1;
280
281   if (mask & R_OK)
282     ok = ok &&
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);
286   if (mask & W_OK)
287     ok = ok &&
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);
291   if (mask & X_OK)
292     ok = ok &&
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);
296
297   return ok ? 0 : -EACCES;
298 }
299
300 static int
301 fg_readlink (const char *path, char *buf, size_t size)
302 {
303   const char *r;
304   int free_it = 0;
305
306   r = rlc_lookup (path);
307   if (!r) {
308     r = guestfs_readlink (g, path);
309     if (r == NULL)
310       return error ();
311     free_it = 1;
312   }
313
314   /* Note this is different from the real readlink(2) syscall.  FUSE wants
315    * the string to be always nul-terminated, even if truncated.
316    */
317   size_t len = strlen (r);
318   if (len > size - 1)
319     len = size - 1;
320
321   memcpy (buf, r, len);
322   buf[len] = '\0';
323
324   if (free_it) {
325     char *tmp = (char *) r;
326     free (tmp);
327   }
328
329   return 0;
330 }
331
332 static int
333 fg_mknod (const char *path, mode_t mode, dev_t rdev)
334 {
335   int r;
336
337   if (read_only) return -EROFS;
338
339   dir_cache_invalidate (path);
340
341   r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
342   if (r == -1)
343     return error ();
344
345   return 0;
346 }
347
348 static int
349 fg_mkdir (const char *path, mode_t mode)
350 {
351   int r;
352
353   if (read_only) return -EROFS;
354
355   dir_cache_invalidate (path);
356
357   r = guestfs_mkdir_mode (g, path, mode);
358   if (r == -1)
359     return error ();
360
361   return 0;
362 }
363
364 static int
365 fg_unlink (const char *path)
366 {
367   int r;
368
369   if (read_only) return -EROFS;
370
371   dir_cache_invalidate (path);
372
373   r = guestfs_rm (g, path);
374   if (r == -1)
375     return error ();
376
377   return 0;
378 }
379
380 static int
381 fg_rmdir (const char *path)
382 {
383   int r;
384
385   if (read_only) return -EROFS;
386
387   dir_cache_invalidate (path);
388
389   r = guestfs_rmdir (g, path);
390   if (r == -1)
391     return error ();
392
393   return 0;
394 }
395
396 static int
397 fg_symlink (const char *from, const char *to)
398 {
399   int r;
400
401   if (read_only) return -EROFS;
402
403   dir_cache_invalidate (to);
404
405   r = guestfs_ln_s (g, from, to);
406   if (r == -1)
407     return error ();
408
409   return 0;
410 }
411
412 static int
413 fg_rename (const char *from, const char *to)
414 {
415   int r;
416
417   if (read_only) return -EROFS;
418
419   dir_cache_invalidate (from);
420   dir_cache_invalidate (to);
421
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.
425    */
426   r = guestfs_mv (g, from, to);
427   if (r == -1)
428     return error ();
429
430   return 0;
431 }
432
433 static int
434 fg_link (const char *from, const char *to)
435 {
436   int r;
437
438   if (read_only) return -EROFS;
439
440   dir_cache_invalidate (from);
441   dir_cache_invalidate (to);
442
443   r = guestfs_ln (g, from, to);
444   if (r == -1)
445     return error ();
446
447   return 0;
448 }
449
450 static int
451 fg_chmod (const char *path, mode_t mode)
452 {
453   int r;
454
455   if (read_only) return -EROFS;
456
457   dir_cache_invalidate (path);
458
459   r = guestfs_chmod (g, mode, path);
460   if (r == -1)
461     return error ();
462
463   return 0;
464 }
465
466 static int
467 fg_chown (const char *path, uid_t uid, gid_t gid)
468 {
469   int r;
470
471   if (read_only) return -EROFS;
472
473   dir_cache_invalidate (path);
474
475   r = guestfs_lchown (g, uid, gid, path);
476   if (r == -1)
477     return error ();
478
479   return 0;
480 }
481
482 static int
483 fg_truncate (const char *path, off_t size)
484 {
485   int r;
486
487   if (read_only) return -EROFS;
488
489   dir_cache_invalidate (path);
490
491   r = guestfs_truncate_size (g, path, size);
492   if (r == -1)
493     return error ();
494
495   return 0;
496 }
497
498 static int
499 fg_utimens (const char *path, const struct timespec ts[2])
500 {
501   int r;
502
503   if (read_only) return -EROFS;
504
505   dir_cache_invalidate (path);
506
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;
511
512 #ifdef UTIME_NOW
513   if (atnsecs == UTIME_NOW)
514     atnsecs = -1;
515 #endif
516 #ifdef UTIME_OMIT
517   if (atnsecs == UTIME_OMIT)
518     atnsecs = -2;
519 #endif
520 #ifdef UTIME_NOW
521   if (mtnsecs == UTIME_NOW)
522     mtnsecs = -1;
523 #endif
524 #ifdef UTIME_OMIT
525   if (mtnsecs == UTIME_OMIT)
526     mtnsecs = -2;
527 #endif
528
529   r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
530   if (r == -1)
531     return error ();
532
533   return 0;
534 }
535
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.
541  */
542 static int
543 fg_open (const char *path, struct fuse_file_info *fi)
544 {
545   int r, exists;
546
547   if (fi->flags & O_WRONLY) {
548     if (read_only)
549       return -EROFS;
550   }
551
552   exists = guestfs_exists (g, path);
553   if (exists == -1)
554     return error ();
555
556   if (fi->flags & O_CREAT) {
557     if (read_only)
558       return -EROFS;
559
560     dir_cache_invalidate (path);
561
562     /* Exclusive?  File must not exist already. */
563     if (fi->flags & O_EXCL) {
564       if (exists)
565         return -EEXIST;
566     }
567
568     /* Create?  Touch it and optionally truncate it. */
569     r = guestfs_touch (g, path);
570     if (r == -1)
571       return error ();
572
573     if (fi->flags & O_TRUNC) {
574       r = guestfs_truncate (g, path);
575       if (r == -1)
576         return error ();
577     }
578   } else {
579     /* Not create, just check it exists. */
580     if (!exists)
581       return -ENOENT;
582   }
583
584   return 0;
585 }
586
587 static int
588 fg_read (const char *path, char *buf, size_t size, off_t offset,
589          struct fuse_file_info *fi)
590 {
591   char *r;
592   size_t rsize;
593
594   if (verbose)
595     fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
596              path, size, offset);
597
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
601    */
602   const size_t limit = 2 * 1024 * 1024;
603   if (size > limit)
604     size = limit;
605
606   r = guestfs_pread (g, path, size, offset, &rsize);
607   if (r == NULL)
608     return error ();
609
610   /* This should never happen, but at least it stops us overflowing
611    * the output buffer if it does happen.
612    */
613   if (rsize > size)
614     rsize = size;
615
616   memcpy (buf, r, rsize);
617   free (r);
618
619   return rsize;
620 }
621
622 static int
623 fg_write (const char *path, const char *buf, size_t size,
624           off_t offset, struct fuse_file_info *fi)
625 {
626   if (read_only) return -EROFS;
627
628   dir_cache_invalidate (path);
629
630   /* See fg_read. */
631   const size_t limit = 2 * 1024 * 1024;
632   if (size > limit)
633     size = limit;
634
635   int r;
636   r = guestfs_pwrite (g, path, buf, size, offset);
637   if (r == -1)
638     return error ();
639
640   return r;
641 }
642
643 static int
644 fg_statfs (const char *path, struct statvfs *stbuf)
645 {
646   struct guestfs_statvfs *r;
647
648   r = guestfs_statvfs (g, path);
649   if (r == NULL)
650     return error ();
651
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;
663
664   guestfs_free_statvfs (r);
665
666   return 0;
667 }
668
669 static int
670 fg_release (const char *path, struct fuse_file_info *fi)
671 {
672   /* Just a stub. This method is optional and can safely be left
673    * unimplemented.
674    */
675   return 0;
676 }
677
678 /* Emulate this by calling sync. */
679 static int fg_fsync(const char *path, int isdatasync,
680                      struct fuse_file_info *fi)
681 {
682   int r;
683
684   r = guestfs_sync (g);
685   if (r == -1)
686     return error ();
687
688   return 0;
689 }
690
691 static int
692 fg_setxattr (const char *path, const char *name, const char *value,
693              size_t size, int flags)
694 {
695   int r;
696
697   if (read_only) return -EROFS;
698
699   dir_cache_invalidate (path);
700
701   /* XXX Underlying guestfs(3) API doesn't understand the flags. */
702   r = guestfs_lsetxattr (g, name, value, size, path);
703   if (r == -1)
704     return error ();
705
706   return 0;
707 }
708
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 :-(
712  */
713 static int
714 fg_getxattr (const char *path, const char *name, char *value,
715              size_t size)
716 {
717   const struct guestfs_xattr_list *xattrs;
718   int free_attrs = 0;
719
720   xattrs = xac_lookup (path);
721   if (xattrs == NULL) {
722     xattrs = guestfs_lgetxattrs (g, path);
723     if (xattrs == NULL)
724       return error ();
725     free_attrs = 1;
726   }
727
728   size_t i;
729   int r = -ENOATTR;
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;
733       if (sz > size)
734         sz = size;
735       memcpy (value, xattrs->val[i].attrval, sz);
736       r = 0;
737       break;
738     }
739   }
740
741   if (free_attrs)
742     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
743
744   return r;
745 }
746
747 /* Ditto as above. */
748 static int
749 fg_listxattr (const char *path, char *list, size_t size)
750 {
751   const struct guestfs_xattr_list *xattrs;
752   int free_attrs = 0;
753
754   xattrs = xac_lookup (path);
755   if (xattrs == NULL) {
756     xattrs = guestfs_lgetxattrs (g, path);
757     if (xattrs == NULL)
758       return error ();
759     free_attrs = 1;
760   }
761
762   size_t i;
763   ssize_t copied = 0;
764   for (i = 0; i < xattrs->len; ++i) {
765     size_t len = strlen (xattrs->val[i].attrname) + 1;
766     if (size >= len) {
767       memcpy (list, xattrs->val[i].attrname, len);
768       size -= len;
769       list += len;
770       copied += len;
771     } else {
772       copied = -ERANGE;
773       break;
774     }
775   }
776
777   if (free_attrs)
778     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
779
780   return copied;
781 }
782
783 static int
784 fg_removexattr(const char *path, const char *name)
785 {
786   int r;
787
788   if (read_only) return -EROFS;
789
790   dir_cache_invalidate (path);
791
792   r = guestfs_lremovexattr (g, name, path);
793   if (r == -1)
794     return error ();
795
796   return 0;
797 }
798
799 static struct fuse_operations fg_operations = {
800   .getattr      = fg_getattr,
801   .access       = fg_access,
802   .readlink     = fg_readlink,
803   .readdir      = fg_readdir,
804   .mknod        = fg_mknod,
805   .mkdir        = fg_mkdir,
806   .symlink      = fg_symlink,
807   .unlink       = fg_unlink,
808   .rmdir        = fg_rmdir,
809   .rename       = fg_rename,
810   .link         = fg_link,
811   .chmod        = fg_chmod,
812   .chown        = fg_chown,
813   .truncate     = fg_truncate,
814   .utimens      = fg_utimens,
815   .open         = fg_open,
816   .read         = fg_read,
817   .write        = fg_write,
818   .statfs       = fg_statfs,
819   .release      = fg_release,
820   .fsync        = fg_fsync,
821   .setxattr     = fg_setxattr,
822   .getxattr     = fg_getxattr,
823   .listxattr    = fg_listxattr,
824   .removexattr  = fg_removexattr,
825 };
826
827 static void __attribute__((noreturn))
828 fuse_help (void)
829 {
830   const char *tmp_argv[] = { program_name, "--help", NULL };
831   fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
832   exit (EXIT_SUCCESS);
833 }
834
835 static void __attribute__((noreturn))
836 usage (int status)
837 {
838   if (status != EXIT_SUCCESS)
839     fprintf (stderr, _("Try `%s --help' for more information.\n"),
840              program_name);
841   else {
842     fprintf (stdout,
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"
846              "Usage:\n"
847              "  %s [--options] [-- [--FUSE-options]] mountpoint\n"
848              "Options:\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"
865              ),
866              program_name, program_name, program_name);
867   }
868   exit (status);
869 }
870
871 int
872 main (int argc, char *argv[])
873 {
874   setlocale (LC_ALL, "");
875   bindtextdomain (PACKAGE, LOCALEBASEDIR);
876   textdomain (PACKAGE);
877
878   enum { HELP_OPTION = CHAR_MAX + 1 };
879
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.
882    */
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' },
896     { "ro", 0, 0, 'r' },
897     { "selinux", 0, 0, 0 },
898     { "trace", 0, 0, 'x' },
899     { "verbose", 0, 0, 'v' },
900     { "version", 0, 0, 'V' },
901     { 0, 0, 0, 0 }
902   };
903
904   struct drv *drvs = NULL;
905   struct drv *drv;
906   struct mp *mps = NULL;
907   struct mp *mp;
908   char *p;
909   const char *format = NULL;
910   int c, r;
911   int option_index;
912   struct sigaction sa;
913
914   int fuse_argc = 0;
915   const char **fuse_argv = NULL;
916
917 #define ADD_FUSE_ARG(str)                                               \
918   do {                                                                  \
919     fuse_argc ++;                                                       \
920     fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *));   \
921     if (!fuse_argv) {                                                   \
922       perror ("realloc");                                               \
923       exit (EXIT_FAILURE);                                                         \
924     }                                                                   \
925     fuse_argv[fuse_argc-1] = (str);                                     \
926     fuse_argv[fuse_argc] = NULL;                                        \
927   } while (0)
928
929   /* LC_ALL=C is required so we can parse error messages. */
930   setenv ("LC_ALL", "C", 1);
931
932   /* Set global program name that is not polluted with libtool artifacts.  */
933   set_program_name (argv[0]);
934
935   memset (&sa, 0, sizeof sa);
936   sa.sa_handler = SIG_IGN;
937   sa.sa_flags = SA_RESTART;
938   sigaction (SIGPIPE, &sa, NULL);
939
940   /* Various initialization. */
941   init_dir_caches ();
942
943   g = guestfs_create ();
944   if (g == NULL) {
945     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
946     exit (EXIT_FAILURE);
947   }
948
949   guestfs_set_recovery_proc (g, 0);
950
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.
955    */
956   ADD_FUSE_ARG ("-s");
957
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
961    * that too.
962    *
963    * BUT if LIBGUESTFS_PATH environment variable is already set by
964    * the user, then don't override it.
965    */
966   if (getenv ("LIBGUESTFS_PATH") == NULL &&
967       argv[0] &&
968       (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
969     guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
970
971   for (;;) {
972     c = getopt_long (argc, argv, options, long_options, &option_index);
973     if (c == -1) break;
974
975     switch (c) {
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"))
980         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, ""))
985           format = NULL;
986         else
987           format = optarg;
988       }
989       else {
990         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
991                  program_name, long_options[option_index].name, option_index);
992         exit (EXIT_FAILURE);
993       }
994       break;
995
996     case 'a':
997       OPTION_a;
998       break;
999
1000     case 'c':
1001       OPTION_c;
1002       break;
1003
1004     case 'd':
1005       OPTION_d;
1006       break;
1007
1008     case 'i':
1009       OPTION_i;
1010       break;
1011
1012     case 'm':
1013       OPTION_m;
1014       break;
1015
1016     case 'n':
1017       OPTION_n;
1018       break;
1019
1020     case 'o':
1021       ADD_FUSE_ARG ("-o");
1022       ADD_FUSE_ARG (optarg);
1023       break;
1024
1025     case 'r':
1026       OPTION_r;
1027       break;
1028
1029     case 'v':
1030       OPTION_v;
1031       break;
1032
1033     case 'V':
1034       OPTION_V;
1035       break;
1036
1037     case 'x':
1038       OPTION_x;
1039       ADD_FUSE_ARG ("-f");
1040       guestfs_set_recovery_proc (g, 1);
1041       break;
1042
1043     case HELP_OPTION:
1044       usage (EXIT_SUCCESS);
1045
1046     default:
1047       usage (EXIT_FAILURE);
1048     }
1049   }
1050
1051   /* Check we have the right options. */
1052   if (!drvs || !(mps || inspector)) {
1053     fprintf (stderr,
1054              _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1055              program_name);
1056     exit (EXIT_FAILURE);
1057   }
1058
1059   /* We'd better have a mountpoint. */
1060   if (optind+1 != argc) {
1061     fprintf (stderr,
1062              _("%s: you must specify a mountpoint in the host filesystem\n"),
1063              program_name);
1064     exit (EXIT_FAILURE);
1065   }
1066
1067   /* Do the guest drives and mountpoints. */
1068   add_drives (drvs, 'a');
1069   if (guestfs_launch (g) == -1)
1070     exit (EXIT_FAILURE);
1071   if (inspector)
1072     inspect_mount ();
1073   mount_mps (mps);
1074
1075   free_drives (drvs);
1076   free_mps (mps);
1077
1078   /* FUSE example does this, not clear if it's necessary, but ... */
1079   if (guestfs_umask (g, 0) == -1)
1080     exit (EXIT_FAILURE);
1081
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()
1086    * function above).
1087    */
1088   guestfs_set_error_handler (g, NULL, NULL);
1089
1090   /* Finish off FUSE args. */
1091   ADD_FUSE_ARG (argv[optind]);
1092
1093   /*
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]
1096
1097   if (verbose) {
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]);
1102     }
1103     fprintf (stderr, "]\n");
1104   }
1105   */
1106
1107   r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1108
1109   /* Cleanup. */
1110   guestfs_close (g);
1111   free_dir_caches ();
1112
1113   exit (r == -1 ? 1 : 0);
1114 }