1 /* libguestfs - guestfish and guestmount shared option parsing
2 * Copyright (C) 2010-2011 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 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.
31 add_drives (struct drv *drv, char next_drive)
34 struct guestfs_add_drive_opts_argv ad_optargs;
36 if (next_drive > 'z') {
38 _("%s: too many drives added on the command line\n"),
44 next_drive = add_drives (drv->next, next_drive);
46 if (asprintf (&drv->device, "/dev/sd%c", next_drive) == -1) {
53 ad_optargs.bitmask = 0;
55 ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
56 ad_optargs.readonly = 1;
59 ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
60 ad_optargs.format = drv->a.format;
62 r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
71 r = add_libvirt_drives (drv->d.guest);
80 /* guestfs_add_drive (ie. autodetecting) should be safe here
81 * since we have just created the prepared disk. At the moment
82 * it will always be "raw" but in a theoretical future we might
83 * create other formats.
85 /* -N option is not affected by --ro */
86 r = guestfs_add_drive (g, drv->N.filename);
94 default: /* keep GCC happy */
102 static void display_mountpoints_on_failure (const char *mp_device);
103 static void canonical_device_name (char *dev);
105 /* List is built in reverse order, so mount them in reverse order. */
107 mount_mps (struct mp *mp)
112 mount_mps (mp->next);
116 options = mp->options;
122 /* Don't use guestfs_mount here because that will default to mount
123 * options -o sync,noatime. For more information, see guestfs(3)
124 * section "LIBGUESTFS GOTCHAS".
126 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
128 display_mountpoints_on_failure (mp->device);
134 /* If the -m option fails on any command, display a useful error
135 * message listing the mountpoints.
138 display_mountpoints_on_failure (const char *mp_device)
143 fses = guestfs_list_filesystems (g);
146 if (fses[0] == NULL) {
152 _("%s: '%s' could not be mounted. Did you mean one of these?\n"),
153 program_name, mp_device);
155 for (i = 0; fses[i] != NULL; i += 2) {
156 canonical_device_name (fses[i]);
157 fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
166 canonical_device_name (char *dev)
168 if (STRPREFIX (dev, "/dev/") &&
169 (dev[5] == 'h' || dev[5] == 'v') &&
171 c_isalpha (dev[7]) &&
172 (c_isdigit (dev[8]) || dev[8] == '\0'))
177 free_drives (struct drv *drv)
180 free_drives (drv->next);
185 case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
186 case drv_d: /* d.filename is optarg, don't free it */ break;
188 free (drv->N.filename);
189 drv->N.data_free (drv->N.data);
191 default: ; /* keep GCC happy */
197 free_mps (struct mp *mp)
202 /* The drive and mountpoint fields are not allocated
203 * from the heap, so we should not free them here.