From: Nikolay Ivanets <stenavin(a)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 | 41 ++++++++++++++++++++++++++++--------
4 files changed, 57 insertions(+), 11 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..7b96ebf
--- /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 the disk sector size to the
+default value which is 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..4716e6f 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,16 +166,18 @@ 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 \
@@ -194,6 +198,15 @@ extern void free_mps (struct mp *mp);
format_consumed = false; \
} while (0)
+#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
@@ -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