common/options: Change drv struct to store drive index instead of device name.

The device name is only used by guestfish (when using the -N option to
prepare drives).  We constructed the device name very naively,
basically ‘sprintf ("/dev/sd%c", next_drive)’.

This stores the device index instead, and only constructs the device
name in guestfish.  Also the device name is constructed properly using
guestfs_int_drive_name so it can cope with #drives > 26.

As a side effect of this change we can remove the extra parameter of
the add_drives macro.

Thanks: Pino Toscano
This commit is contained in:
Richard W.M. Jones
2017-04-28 09:59:23 +01:00
parent b168147834
commit 8c9a4bc84f
17 changed files with 42 additions and 51 deletions

View File

@@ -236,7 +236,7 @@ main (int argc, char *argv[])
error (EXIT_FAILURE, 0, _("--uuid option cannot be used with -a or -d"));
/* Add domains/drives from the command line (for a single guest). */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -236,7 +236,7 @@ main (int argc, char *argv[])
}
/* Add drives, inspect and mount. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -346,7 +346,7 @@ main (int argc, char *argv[])
}
/* Add drives. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -209,7 +209,7 @@ main (int argc, char *argv[])
/* Add drives, inspect and mount. Note that inspector is always true,
* and there is no -m option.
*/
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -359,7 +359,7 @@ main (int argc, char *argv[])
}
/* Add drives, inspect and mount. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -279,7 +279,7 @@ do_tail (int argc, char *argv[], /* list of files in the guest */
int processed;
/* Add drives, inspect and mount. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
return -1;

View File

@@ -67,7 +67,6 @@ option_a (const char *arg, const char *format, struct drv **drvsp)
error (EXIT_FAILURE, errno, "access: %s", uri.path);
drv->type = drv_a;
drv->nr_drives = -1;
drv->a.filename = uri.path;
drv->a.format = format;
@@ -76,7 +75,6 @@ option_a (const char *arg, const char *format, struct drv **drvsp)
else {
/* Remote storage. */
drv->type = drv_uri;
drv->nr_drives = -1;
drv->uri.path = uri.path;
drv->uri.protocol = uri.protocol;
drv->uri.server = uri.server;
@@ -103,7 +101,6 @@ option_d (const char *arg, struct drv **drvsp)
error (EXIT_FAILURE, errno, "calloc");
drv->type = drv_d;
drv->nr_drives = -1;
drv->d.guest = optarg;
drv->next = *drvsp;
@@ -111,22 +108,15 @@ option_d (const char *arg, struct drv **drvsp)
}
char
add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index)
{
int r;
struct guestfs_add_drive_opts_argv ad_optargs;
if (next_drive > 'z')
error (EXIT_FAILURE, 0, _("too many drives added on the command line"));
if (drv) {
next_drive = add_drives (drv->next, next_drive);
drive_index = add_drives_handle (g, drv->next, drive_index);
free (drv->device);
drv->device = NULL;
if (asprintf (&drv->device, "/dev/sd%c", next_drive) == -1)
error (EXIT_FAILURE, errno, "asprintf");
drv->drive_index = drive_index;
switch (drv->type) {
case drv_a:
@@ -153,7 +143,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
next_drive++;
drive_index++;
break;
case drv_uri:
@@ -186,7 +176,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
next_drive++;
drive_index++;
break;
case drv_d:
@@ -195,7 +185,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
exit (EXIT_FAILURE);
drv->nr_drives = r;
next_drive += r;
drive_index += r;
break;
case drv_N:
@@ -208,7 +198,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
next_drive++;
drive_index++;
break;
case drv_scratch:
@@ -218,7 +208,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
exit (EXIT_FAILURE);
drv->nr_drives = 1;
next_drive++;
drive_index++;
break;
default: /* keep GCC happy */
@@ -226,7 +216,7 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
}
}
return next_drive;
return drive_index;
}
static void display_mountpoints_on_failure (const char *mp_device, const char *user_supplied_options);
@@ -320,8 +310,6 @@ free_drives (struct drv *drv)
if (!drv) return;
free_drives (drv->next);
free (drv->device);
switch (drv->type) {
case drv_a:
free (drv->a.filename);

View File

@@ -43,14 +43,14 @@ extern int in_virt_rescue;
struct drv {
struct drv *next;
char *device; /* Device name inside the appliance (eg. /dev/sda).
* This is filled in when we add the drives in
* add_drives. Note that guests (-d option) may
* have multiple drives, in which case this is the
* first drive, and nr_drives is the number of
* drives used.
*/
int nr_drives; /* number of drives for this guest */
/* Drive index. This is filled in by add_drives(). */
size_t drive_index;
/* Number of drives represented by this 'drv' struct. For -d this
* can be != 1 because a guest can have more than one disk. For
* others it is always 1. This is filled in by add_drives().
*/
size_t nr_drives;
enum {
drv_a, /* -a option (without URI) */
@@ -123,8 +123,8 @@ extern char *read_key (const char *param);
/* in options.c */
extern void option_a (const char *arg, const char *format, struct drv **drvsp);
extern void option_d (const char *arg, struct drv **drvsp);
extern char add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive);
#define add_drives(drv, next_drive) add_drives_handle (g, drv, next_drive)
extern char add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index);
#define add_drives(drv) add_drives_handle (g, drv, 0)
extern void mount_mps (struct mp *mp);
extern void free_drives (struct drv *drv);
extern void free_mps (struct mp *mp);

View File

@@ -278,7 +278,7 @@ main (int argc, char *argv[])
CLEANUP_FREE char *name = NULL;
/* Add domains/drives from the command line (for a single guest). */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -362,7 +362,7 @@ main (int argc, char *argv[])
unsigned errors = 0;
/* Mount up first guest. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
@@ -373,7 +373,7 @@ main (int argc, char *argv[])
errors++;
/* Mount up second guest. */
add_drives_handle (g2, drvs2, 'a');
add_drives_handle (g2, drvs2, 0);
if (guestfs_launch (g2) == -1)
exit (EXIT_FAILURE);

View File

@@ -261,7 +261,7 @@ main (int argc, char *argv[])
}
/* Add drives. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -362,7 +362,6 @@ main (int argc, char *argv[])
if (!drv)
error (EXIT_FAILURE, errno, "calloc");
drv->type = drv_N;
drv->nr_drives = -1;
p = strchr (optarg, '=');
if (p != NULL) {
*p = '\0';
@@ -466,7 +465,7 @@ main (int argc, char *argv[])
CHECK_OPTION_format_consumed;
/* If we've got drives to add, add them now. */
add_drives (drvs, 'a');
add_drives (drvs);
/* If we've got mountpoints or prepared drives or -i option, we must
* launch the guest and mount them.
@@ -586,8 +585,13 @@ prepare_drives (struct drv *drv)
{
if (drv) {
prepare_drives (drv->next);
if (drv->type == drv_N)
prepare_drive (drv->N.filename, drv->N.data, drv->device);
if (drv->type == drv_N) {
char device[64];
strcpy (device, "/dev/sd");
guestfs_int_drive_name (drv->drive_index, &device[7]);
prepare_drive (drv->N.filename, drv->N.data, device);
}
}
}

View File

@@ -249,7 +249,7 @@ main (int argc, char *argv[])
const char *wipefs[] = { "wipefs", NULL };
/* Add domains/drives from the command line (for a single guest). */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -360,7 +360,7 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
/* Do the guest drives and mountpoints. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
if (inspector)

View File

@@ -271,7 +271,7 @@ main (int argc, char *argv[])
/* Add drives, inspect and mount. Note that inspector is always true,
* and there is no -m option.
*/
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);

View File

@@ -372,7 +372,7 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE);
/* Do the guest drives and mountpoints. */
add_drives (drvs, 'a');
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
if (inspector)
@@ -673,7 +673,6 @@ add_scratch_disk (struct drv **drvs)
if (!drv)
error (EXIT_FAILURE, errno, "calloc");
drv->type = drv_scratch;
drv->nr_drives = -1;
drv->scratch.size = INT64_C (10737418240);
drv->next = *drvs;
*drvs = drv;

View File

@@ -54,7 +54,7 @@ do_suggestion (struct drv *drvs)
read_only = 1;
/* Add drives. */
add_drives (drvs, 'a');
add_drives (drvs);
/* Free up data structures, no longer needed after this point. */
free_drives (drvs);