fish: fuse: Add -m dev:mnt:opts to allow mount options to be specified.
[libguestfs.git] / fish / options.c
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010-2011 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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "guestfs.h"
25
26 #include "options.h"
27
28 char
29 add_drives (struct drv *drv, char next_drive)
30 {
31   int r;
32   struct guestfs_add_drive_opts_argv ad_optargs;
33
34   if (next_drive > 'z') {
35     fprintf (stderr,
36              _("%s: too many drives added on the command line\n"),
37              program_name);
38     exit (EXIT_FAILURE);
39   }
40
41   if (drv) {
42     next_drive = add_drives (drv->next, next_drive);
43
44     if (asprintf (&drv->device, "/dev/sd%c", next_drive) == -1) {
45       perror ("asprintf");
46       exit (EXIT_FAILURE);
47     }
48
49     switch (drv->type) {
50     case drv_a:
51       ad_optargs.bitmask = 0;
52       if (read_only) {
53         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
54         ad_optargs.readonly = 1;
55       }
56       if (drv->a.format) {
57         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
58         ad_optargs.format = drv->a.format;
59       }
60       r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
61       if (r == -1)
62         exit (EXIT_FAILURE);
63
64       drv->nr_drives = 1;
65       next_drive++;
66       break;
67
68     case drv_d:
69       r = add_libvirt_drives (drv->d.guest);
70       if (r == -1)
71         exit (EXIT_FAILURE);
72
73       drv->nr_drives = r;
74       next_drive += r;
75       break;
76
77     case drv_N:
78       /* guestfs_add_drive (ie. autodetecting) should be safe here
79        * since we have just created the prepared disk.  At the moment
80        * it will always be "raw" but in a theoretical future we might
81        * create other formats.
82        */
83       /* -N option is not affected by --ro */
84       r = guestfs_add_drive (g, drv->N.filename);
85       if (r == -1)
86         exit (EXIT_FAILURE);
87
88       drv->nr_drives = 1;
89       next_drive++;
90       break;
91
92     default: /* keep GCC happy */
93       abort ();
94     }
95   }
96
97   return next_drive;
98 }
99
100 /* List is built in reverse order, so mount them in reverse order. */
101 void
102 mount_mps (struct mp *mp)
103 {
104   int r;
105
106   if (mp) {
107     mount_mps (mp->next);
108
109     const char *options;
110     if (mp->options)
111       options = mp->options;
112     else if (read_only)
113       options = "ro";
114     else
115       options = "";
116
117     /* Don't use guestfs_mount here because that will default to mount
118      * options -o sync,noatime.  For more information, see guestfs(3)
119      * section "LIBGUESTFS GOTCHAS".
120      */
121     r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
122     if (r == -1) {
123       /* Display possible mountpoints before exiting. */
124       char **fses = guestfs_list_filesystems (g);
125       if (fses == NULL || fses[0] == NULL)
126         goto out;
127       fprintf (stderr,
128                _("%s: '%s' could not be mounted.  Did you mean one of these?\n"),
129                program_name, mp->device);
130       size_t i;
131       for (i = 0; fses[i] != NULL; i += 2)
132         fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
133
134     out:
135       exit (EXIT_FAILURE);
136     }
137   }
138 }
139
140 void
141 free_drives (struct drv *drv)
142 {
143   if (!drv) return;
144   free_drives (drv->next);
145
146   free (drv->device);
147
148   switch (drv->type) {
149   case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
150   case drv_d: /* d.filename is optarg, don't free it */ break;
151   case drv_N:
152     free (drv->N.filename);
153     drv->N.data_free (drv->N.data);
154     break;
155   default: ;                    /* keep GCC happy */
156   }
157   free (drv);
158 }
159
160 void
161 free_mps (struct mp *mp)
162 {
163   if (!mp) return;
164   free_mps (mp->next);
165
166   /* The drive and mountpoint fields are not allocated
167    * from the heap, so we should not free them here.
168    */
169
170   free (mp);
171 }