1 /* libguestfs - guestfish and guestmount shared option parsing
2 * Copyright (C) 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 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.
29 add_drives (struct drv *drv, char next_drive)
32 struct guestfs_add_drive_opts_argv ad_optargs;
34 if (next_drive > 'z') {
36 _("%s: too many drives added on the command line\n"),
42 next_drive = add_drives (drv->next, next_drive);
46 ad_optargs.bitmask = 0;
48 ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
49 ad_optargs.readonly = 1;
52 ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
53 ad_optargs.format = drv->a.format;
55 r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
63 r = add_libvirt_drives (drv->d.guest);
71 /* guestfs_add_drive (ie. autodetecting) should be safe here
72 * since we have just created the prepared disk. At the moment
73 * it will always be "raw" but in a theoretical future we might
74 * create other formats.
76 /* -N option is not affected by --ro */
77 r = guestfs_add_drive (g, drv->N.filename);
81 if (asprintf (&drv->N.device, "/dev/sd%c", next_drive) == -1) {
89 default: /* keep GCC happy */
97 /* List is built in reverse order, so mount them in reverse order. */
99 mount_mps (struct mp *mp)
104 mount_mps (mp->next);
106 /* Don't use guestfs_mount here because that will default to mount
107 * options -o sync,noatime. For more information, see guestfs(3)
108 * section "LIBGUESTFS GOTCHAS".
110 const char *options = read_only ? "ro" : "";
111 r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
113 /* Display possible mountpoints before exiting. */
114 char **fses = guestfs_list_filesystems (g);
115 if (fses == NULL || fses[0] == NULL)
118 _("%s: '%s' could not be mounted. Did you mean one of these?\n"),
119 program_name, mp->device);
121 for (i = 0; fses[i] != NULL; i += 2)
122 fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
131 free_drives (struct drv *drv)
134 free_drives (drv->next);
137 case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
138 case drv_d: /* d.filename is optarg, don't free it */ break;
140 free (drv->N.filename);
141 free (drv->N.device);
142 drv->N.data_free (drv->N.data);
144 default: ; /* keep GCC happy */
150 free_mps (struct mp *mp)
155 /* The drive and mountpoint fields are not allocated
156 * from the heap, so we should not free them here.