Mykola Ivanets
2020-Feb-12 00:54 UTC
[Libguestfs] [common PATCH v2 0/1] options: add '--blocksize' option for C-based tools
From: Nikolay Ivanets <stenavin@gmail.com> In v2 I've moved '--blocksize' parameter description into the separate file called blocksize-option.pod so we can include it everywhere we need similar to key-option.pod. v1 was here: https://www.redhat.com/archives/libguestfs/2020-February/msg00096.html Nikolay Ivanets (1): options: add '--blocksize' option for C-based tools options/Makefile.am | 3 +- options/blocksize-option.pod | 11 +++ options/options.c | 13 +++- options/options.h | 125 +++++++++++++++++++++-------------- 4 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 options/blocksize-option.pod -- 2.17.2
Mykola Ivanets
2020-Feb-12 00:54 UTC
[Libguestfs] [common PATCH v2 1/1] options: add '--blocksize' option for C-based tools
From: Nikolay Ivanets <stenavin@gmail.com> This patch adds '--blocksize' command line option parsing and handling for guestfish and other C-based tools which share the same code from this sub-module. '--blocksize' will be a common for almost all libguestfs-based tools and thus parameter description will be repeated all the time. Let's move it into blocksize-option.pod and include everywhere we need. --- options/Makefile.am | 3 +- options/blocksize-option.pod | 11 +++ options/options.c | 13 +++- options/options.h | 125 +++++++++++++++++++++-------------- 4 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 options/blocksize-option.pod diff --git a/options/Makefile.am b/options/Makefile.am index 28940f1..394f668 100644 --- a/options/Makefile.am +++ b/options/Makefile.am @@ -18,7 +18,8 @@ include $(top_srcdir)/subdir-rules.mk EXTRA_DIST = \ - key-option.pod + key-option.pod \ + blocksize-option.pod # liboptions.la contains guestfish code which is used in other # C tools for options parsing and a few other things diff --git a/options/blocksize-option.pod b/options/blocksize-option.pod new file mode 100644 index 0000000..ae748be --- /dev/null +++ b/options/blocksize-option.pod @@ -0,0 +1,11 @@ +=item B<--blocksize=512> + +=item B<--blocksize=4096> + +=item B<--blocksize> + +This parameter sets the sector size of the disk image. It affects all +explicitly added subsequent disks after this parameter. Using +I<--blocksize> with no argument switches disk sector size to the +default value which usually 512 bytes. See also +L<guestfs(3)/guestfs_add_drive_opts>. diff --git a/options/options.c b/options/options.c index fe63da9..63221ea 100644 --- a/options/options.c +++ b/options/options.c @@ -49,7 +49,8 @@ * Handle the guestfish I<-a> option on the command line. */ void -option_a (const char *arg, const char *format, struct drv **drvsp) +option_a (const char *arg, const char *format, int blocksize, + struct drv **drvsp) { struct uri uri; struct drv *drv; @@ -69,6 +70,7 @@ option_a (const char *arg, const char *format, struct drv **drvsp) drv->type = drv_a; drv->a.filename = uri.path; drv->a.format = format; + drv->a.blocksize = blocksize; free (uri.protocol); } @@ -82,6 +84,7 @@ option_a (const char *arg, const char *format, struct drv **drvsp) drv->uri.password = uri.password; drv->uri.format = format; drv->uri.orig_uri = arg; + drv->uri.blocksize = blocksize; } drv->next = *drvsp; @@ -137,6 +140,10 @@ add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index) ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_DISCARD_BITMASK; ad_optargs.discard = drv->a.discard; } + if (drv->a.blocksize) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK; + ad_optargs.blocksize = drv->a.blocksize; + } r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs); if (r == -1) @@ -170,6 +177,10 @@ add_drives_handle (guestfs_h *g, struct drv *drv, size_t drive_index) ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_SECRET_BITMASK; ad_optargs.secret = drv->uri.password; } + if (drv->uri.blocksize) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_BLOCKSIZE_BITMASK; + ad_optargs.blocksize = drv->uri.blocksize; + } r = guestfs_add_drive_opts_argv (g, drv->uri.path, &ad_optargs); if (r == -1) diff --git a/options/options.h b/options/options.h index 9b78302..9f3a1e7 100644 --- a/options/options.h +++ b/options/options.h @@ -65,6 +65,7 @@ struct drv { const char *format; /* format (NULL == autodetect) */ const char *cachemode;/* cachemode (NULL == default) */ const char *discard; /* discard (NULL == disable) */ + int blocksize; /* blocksize (0 == default) */ } a; struct { char *path; /* disk path */ @@ -74,6 +75,7 @@ struct drv { char *password; /* password - can be NULL */ const char *format; /* format (NULL == autodetect) */ const char *orig_uri; /* original URI (for error messages etc.) */ + int blocksize; /* blocksize (0 == default) */ } uri; struct { char *guest; /* guest name */ @@ -156,7 +158,7 @@ extern struct key_store *key_store_import_key (struct key_store *ks, const struc extern void free_key_store (struct key_store *ks); /* in options.c */ -extern void option_a (const char *arg, const char *format, struct drv **drvsp); +extern void option_a (const char *arg, const char *format, int blocksize, struct drv **drvsp); extern void option_d (const char *arg, struct drv **drvsp); 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) @@ -164,76 +166,87 @@ extern void mount_mps (struct mp *mp); extern void free_drives (struct drv *drv); extern void free_mps (struct mp *mp); -#define OPTION_a \ - do { \ - option_a (optarg, format, &drvs); \ - format_consumed = true; \ +#define OPTION_a \ + do { \ + option_a (optarg, format, blocksize, &drvs); \ + format_consumed = true; \ + blocksize_consumed = true; \ } while (0) -#define OPTION_A \ - do { \ - option_a (optarg, format, &drvs2); \ - format_consumed = true; \ +#define OPTION_A \ + do { \ + option_a (optarg, format, blocksize, &drvs2); \ + format_consumed = true; \ + blocksize_consumed = true; \ } while (0) -#define OPTION_c \ +#define OPTION_c \ libvirt_uri = optarg -#define OPTION_d \ +#define OPTION_d \ option_d (optarg, &drvs) -#define OPTION_D \ +#define OPTION_D \ option_d (optarg, &drvs2) -#define OPTION_format \ - do { \ - if (!optarg || STREQ (optarg, "")) \ - format = NULL; \ - else \ - format = optarg; \ - format_consumed = false; \ +#define OPTION_format \ + do { \ + if (!optarg || STREQ (optarg, "")) \ + format = NULL; \ + else \ + format = optarg; \ + format_consumed = false; \ } while (0) -#define OPTION_i \ +#define OPTION_blocksize \ + do { \ + if (!optarg || STREQ (optarg, "")) \ + blocksize = 0; \ + else if (sscanf (optarg, "%d", &blocksize) != 1) \ + error (EXIT_FAILURE, 0, _("--blocksize option is not numeric")); \ + blocksize_consumed = false; \ + } while (0) + +#define OPTION_i \ inspector = 1 -#define OPTION_m \ - mp = malloc (sizeof (struct mp)); \ - if (!mp) \ - error (EXIT_FAILURE, errno, "malloc"); \ - mp->fstype = NULL; \ - mp->options = NULL; \ - mp->mountpoint = (char *) "/"; \ - p = strchr (optarg, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->mountpoint = p; \ - p = strchr (p, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->options = p; \ - p = strchr (p, ':'); \ - if (p) { \ - *p = '\0'; \ - p++; \ - mp->fstype = p; \ - } \ - } \ - } \ - mp->device = optarg; \ - mp->next = mps; \ +#define OPTION_m \ + mp = malloc (sizeof (struct mp)); \ + if (!mp) \ + error (EXIT_FAILURE, errno, "malloc"); \ + mp->fstype = NULL; \ + mp->options = NULL; \ + mp->mountpoint = (char *) "/"; \ + p = strchr (optarg, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->mountpoint = p; \ + p = strchr (p, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->options = p; \ + p = strchr (p, ':'); \ + if (p) { \ + *p = '\0'; \ + p++; \ + mp->fstype = p; \ + } \ + } \ + } \ + mp->device = optarg; \ + mp->next = mps; \ mps = mp -#define OPTION_n \ +#define OPTION_n \ guestfs_set_autosync (g, 0) -#define OPTION_r \ +#define OPTION_r \ read_only = 1 -#define OPTION_v \ - verbose++; \ +#define OPTION_v \ + verbose++; \ guestfs_set_verbose (g, verbose) #define OPTION_V \ @@ -267,4 +280,14 @@ extern void free_mps (struct mp *mp); } \ } while (0) +#define CHECK_OPTION_blocksize_consumed \ + do { \ + if (!blocksize_consumed) { \ + fprintf (stderr, \ + _("%s: --blocksize parameter must appear before -a parameter\n"), \ + getprogname ()); \ + exit (EXIT_FAILURE); \ + } \ + } while (0) + #endif /* OPTIONS_H */ -- 2.17.2
Eric Blake
2020-Feb-12 01:35 UTC
Re: [Libguestfs] [common PATCH v2 1/1] options: add '--blocksize' option for C-based tools
On 2/11/20 6:54 PM, Mykola Ivanets wrote:> From: Nikolay Ivanets <stenavin@gmail.com> > > This patch adds '--blocksize' command line option parsing and handling > for guestfish and other C-based tools which share the same code from > this sub-module. > > '--blocksize' will be a common for almost all libguestfs-based tools and > thus parameter description will be repeated all the time. Let's move > it into blocksize-option.pod and include everywhere we need. > ---> +++ b/options/blocksize-option.pod > @@ -0,0 +1,11 @@ > +=item B<--blocksize=512> > + > +=item B<--blocksize=4096> > + > +=item B<--blocksize> > + > +This parameter sets the sector size of the disk image. It affects all > +explicitly added subsequent disks after this parameter. Using > +I<--blocksize> with no argument switches disk sector size to theswitches the disk sector> +default value which usually 512 bytes. See alsowhich is usually> +L<guestfs(3)/guestfs_add_drive_opts>. > diff --git a/options/options.c b/options/options.c > index fe63da9..63221ea 100644 > --- a/options/options.c-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Richard W.M. Jones
2020-Feb-13 09:45 UTC
Re: [Libguestfs] [common PATCH v2 1/1] options: add '--blocksize' option for C-based tools
This version fixes the trailing whitespace problem, and having the "blocksize-option.pod" file makes sense as well. (One day we should do the same for --format and other options). Still a certain amount of whitespace-change-only hunks going on though. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Possibly Parallel Threads
- [common PATCH] options: add '--blocksize' option for C-based tools
- [PATCH] common/options: Change drv struct to store drive index instead of device name.
- [common PATCH v3 0/1] options: add '--blocksize' option for C-based tools
- [common PATCH v2 0/1] options: add '--blocksize' option for C-based tools
- [common PATCH v4 0/1] options: add '--blocksize' option for C-based tools