1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/* libguestfs - guestfish and guestmount shared option parsing
* Copyright (C) 2010-2012 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* This file contains common options parsing code used by guestfish
* and many other tools which share a common options syntax.
*
* For example, guestfish, virt-cat, virt-ls etc all support the I<-a>
* option, and that is handled in all of those tools using a macro
* C<OPTION_a> defined in F<fish/options.h>.
*
* There are a lot of common global variables used, C<drvs>
* accumulates the list of drives, C<verbose> for the I<-v> flag, and
* many more.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <libintl.h>
#include "getprogname.h"
#include "guestfs.h"
#include "options.h"
#include "uri.h"
/**
* Handle the guestfish I<-a> option on the command line.
*/
void
option_a (const char *arg, const char *format, int blocksize,
struct drv **drvsp)
{
struct uri uri;
struct drv *drv;
drv = calloc (1, sizeof (struct drv));
if (!drv)
error (EXIT_FAILURE, errno, "calloc");
if (parse_uri (arg, &uri) == -1)
exit (EXIT_FAILURE);
if (STREQ (uri.protocol, "file")) {
/* Ordinary file. */
if (access (uri.path, R_OK) != 0)
error (EXIT_FAILURE, errno, "access: %s", uri.path);
drv->type = drv_a;
drv->a.filename = uri.path;
drv->a.format = format;
drv->a.blocksize = blocksize;
free (uri.protocol);
}
else {
/* Remote storage. */
drv->type = drv_uri;
drv->uri.path = uri.path;
drv->uri.protocol = uri.protocol;
drv->uri.server = uri.server;
drv->uri.username = uri.username;
drv->uri.password = uri.password;
drv->uri.format = format;
drv->uri.orig_uri = arg;
drv->uri.blocksize = blocksize;
}
drv->next = *drvsp;
*drvsp = drv;
}
/**
* Handle the I<-d> option when passed on the command line.
*/
void
option_d (const char *arg, struct drv **drvsp)
{
struct drv *drv;
drv = calloc (1, sizeof (struct drv));
if (!drv)
error (EXIT_FAILURE, errno, "calloc");
drv->type = drv_d;
drv->d.guest = optarg;
drv->next = *drvsp;
*drvsp = drv;
}
char
add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index)
{
int r;
struct guestfs_add_drive_opts_argv ad_optargs;
if (drv) {
drive_index = add_drives_handle (g, drv->next, drive_index);
drv->drive_index = drive_index;
switch (drv->type) {
case drv_a:
ad_optargs.bitmask = 0;
if (read_only) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
ad_optargs.readonly = 1;
}
if (drv->a.format) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
ad_optargs.format = drv->a.format;
}
if (drv->a.cachemode) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_CACHEMODE_BITMASK;
ad_optargs.cachemode = drv->a.cachemode;
}
if (drv->a.discard) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_DISCARD_BITMASK;
ad_optargs.discard = drv->a.discard;
}
#ifdef GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK
if (drv->a.blocksize) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK;
ad_optargs.blocksize = drv->a.blocksize;
}
#endif
r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
if (r == -1)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
drive_index++;
break;
case drv_uri:
ad_optargs.bitmask = 0;
if (read_only) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
ad_optargs.readonly = 1;
}
if (drv->uri.format) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
ad_optargs.format = drv->uri.format;
}
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_PROTOCOL_BITMASK;
ad_optargs.protocol = drv->uri.protocol;
if (drv->uri.server) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_SERVER_BITMASK;
ad_optargs.server = drv->uri.server;
}
if (drv->uri.username) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_USERNAME_BITMASK;
ad_optargs.username = drv->uri.username;
}
if (drv->uri.password) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_SECRET_BITMASK;
ad_optargs.secret = drv->uri.password;
}
#ifdef GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK
if (drv->uri.blocksize) {
ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK;
ad_optargs.blocksize = drv->uri.blocksize;
}
#endif
r = guestfs_add_drive_opts_argv (g, drv->uri.path, &ad_optargs);
if (r == -1)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
drive_index++;
break;
case drv_d:
r = add_libvirt_drives (g, drv->d.guest);
if (r == -1)
exit (EXIT_FAILURE);
drv->nr_drives = r;
drive_index += r;
break;
case drv_N:
if (!in_guestfish) abort ();
/* -N option is not affected by --ro */
r = guestfs_add_drive_opts (g, drv->N.filename,
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
-1);
if (r == -1)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
drive_index++;
break;
case drv_scratch:
if (!in_virt_rescue) abort ();
r = guestfs_add_drive_scratch (g, drv->scratch.size, -1);
if (r == -1)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
drive_index++;
break;
default: /* keep GCC happy */
abort ();
}
}
return drive_index;
}
static void display_mountpoints_on_failure (const char *mp_device, const char *user_supplied_options);
void
mount_mps (struct mp *mp)
{
int r;
if (mp) {
/* List is built in reverse order, so mount them in reverse order. */
mount_mps (mp->next);
const char *options;
if (mp->options)
options = mp->options;
else if (read_only)
options = "ro";
else
options = "";
if (mp->fstype)
r = guestfs_mount_vfs (g, options, mp->fstype, mp->device,
mp->mountpoint);
else
r = guestfs_mount_options (g, options, mp->device,
mp->mountpoint);
if (r == -1) {
display_mountpoints_on_failure (mp->device, mp->options);
exit (EXIT_FAILURE);
}
}
}
/**
* If the I<-m> option fails on any command, display a useful error
* message listing the mountpoints.
*/
static void
display_mountpoints_on_failure (const char *mp_device,
const char *user_supplied_options)
{
CLEANUP_FREE_STRING_LIST char **fses = guestfs_list_filesystems (g);
size_t i;
if (fses == NULL || fses[0] == NULL)
return;
fprintf (stderr, _("%s: ā%sā could not be mounted.\n"),
getprogname (), mp_device);
if (user_supplied_options)
fprintf (stderr, _("%s: Check mount(8) man page to ensure options ā%sā\n"
"%s: are supported by the filesystem that is being mounted.\n"),
getprogname (), user_supplied_options, getprogname ());
fprintf (stderr, _("%s: Did you mean to mount one of these filesystems?\n"),
getprogname ());
for (i = 0; fses[i] != NULL; i += 2) {
CLEANUP_FREE char *p = NULL;
CLEANUP_FREE char *device = guestfs_mountable_device (g, fses[i]);
CLEANUP_FREE char *subvolume = NULL;
guestfs_push_error_handler (g, NULL, NULL);
subvolume = guestfs_mountable_subvolume (g, fses[i]);
if (subvolume == NULL && guestfs_last_errno (g) != EINVAL)
error (EXIT_FAILURE, 0,
_("cannot determine the subvolume for %s: %s (%d)"),
fses[i], guestfs_last_error (g), guestfs_last_errno (g));
guestfs_pop_error_handler (g);
/* Reformat the internal btrfsvol string into a valid mount option */
if (device && subvolume) {
if (asprintf (&p, "%s:/:subvol=%s", device, subvolume) == -1)
error (EXIT_FAILURE, errno, "asprintf");
} else {
p = guestfs_canonical_device_name (g, fses[i]);
}
fprintf (stderr, "%s: \t%s (%s)\n", getprogname (),
p ? p : fses[i], fses[i+1]);
}
}
void
free_drives (struct drv *drv)
{
if (!drv) return;
free_drives (drv->next);
switch (drv->type) {
case drv_a:
free (drv->a.filename);
/* a.format is an optarg, so don't free it */
/* a.cachemode is a static string, so don't free it */
/* a.discard is a static string, so don't free it */
break;
case drv_uri:
free (drv->uri.path);
free (drv->uri.protocol);
guestfs_int_free_string_list (drv->uri.server);
free (drv->uri.username);
free (drv->uri.password);
break;
case drv_d:
/* d.filename is optarg, don't free it */
break;
case drv_N:
if (!in_guestfish) abort ();
free (drv->N.filename);
drv->N.data_free (drv->N.data);
break;
case drv_scratch:
if (!in_virt_rescue) abort ();
/* nothing */
break;
default: ; /* keep GCC happy */
}
free (drv);
}
void
free_mps (struct mp *mp)
{
if (!mp) return;
free_mps (mp->next);
/* The drive and mountpoint fields are not allocated
* from the heap, so we should not free them here.
*/
free (mp);
}
|