[Libguestfs] [PATCH v2 RESEND] direct, fish: add command launch_blocksize

Tuan Hoang tmhoang at linux.ibm.com
Tue Nov 5 08:49:08 UTC 2019


Add a new launch command for direct backend, which allows specifying the
blocksize to the disks. The name is `launch_blocksize`, accompanied alias is
`run-blocksize`.

Example:

$ guestfish --listen -a raw.img
$ guestfish --remote -- launch_blocksize 4096

The actual qemu command is:
[...]
    -device virtio-scsi-ccw,id=scsi
    -drive file=raw.img,cache=writeback,id=hd0,if=none
    -device scsi-hd,drive=hd0,physical_block_size=4096,logical_block_size=4096
[...]

Ideally it would be better to invoke optional option to `launch` command
like:

$ guestfish --remote -- launch --blocksize 4096

Signed-off-by: Tuan Hoang <tmhoang at linux.ibm.com>
---
 generator/actions_core.ml | 11 +++++++++++
 lib/guestfs-internal.h    |  3 +++
 lib/launch-direct.c       |  6 ++++++
 lib/launch.c              | 14 ++++++++++++++
 4 files changed, 34 insertions(+)

diff --git a/generator/actions_core.ml b/generator/actions_core.ml
index 7b6568b..cfe735a 100644
--- a/generator/actions_core.ml
+++ b/generator/actions_core.ml
@@ -38,6 +38,17 @@ it will not give an error (for historical reasons), the precise
 behaviour when you do this is not well defined.  Handles are
 very cheap to create, so create a new one for each launch." };
 
+  { defaults with
+    name = "launch_blocksize"; added = (1, 40, 2);
+    style = RErr, [Int "blocksize"], [];
+    fish_alias = ["run-blocksize"]; progress = true; config_only = true;
+    shortdesc = "launch the backend with blocksize for disks";
+    longdesc = "\
+Similar to \"launch\" call, but requires an integer as blocksize for disks.
+
+A call of \"launch_blocksize 512\" is equivalent of \"launch\" since the
+default blocksize for qemu is 512." };
+
   { defaults with
     name = "add_drive_ro"; added = (1, 0, 38);
     style = RErr, [String (PlainString, "filename")], [];
diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
index 75b8a5c..e9b7057 100644
--- a/lib/guestfs-internal.h
+++ b/lib/guestfs-internal.h
@@ -513,6 +513,9 @@ struct guestfs_h {
 
   /* Used by lib/info.c.  -1 = not tested or error; else 0 or 1. */
   int qemu_img_supports_U_option;
+
+  /* Used by guestfish's launch_blocksize call */
+  int blocksize;
 };
 
 /**
diff --git a/lib/launch-direct.c b/lib/launch-direct.c
index ee2dcb8..54cd8c6 100644
--- a/lib/launch-direct.c
+++ b/lib/launch-direct.c
@@ -315,6 +315,12 @@ add_drive (guestfs_h *g, struct backend_direct_data *data,
     start_list ("-device") {
       append_list ("scsi-hd");
       append_list_format ("drive=hd%zu", i);
+      if (g->blocksize >= 512 && g->blocksize <= 32768
+          && (g->blocksize & (g->blocksize-1)) == 0)
+      {
+        append_list_format ("physical_block_size=%d", g->blocksize);
+        append_list_format ("logical_block_size=%d", g->blocksize);
+      }
       if (drv->disk_label)
         append_list_format ("serial=%s", drv->disk_label);
     } end_list ();
diff --git a/lib/launch.c b/lib/launch.c
index eb7f85c..8edf7b1 100644
--- a/lib/launch.c
+++ b/lib/launch.c
@@ -53,6 +53,20 @@ static struct backend {
   const struct backend_ops *ops;
 } *backends = NULL;
 
+int
+guestfs_impl_launch_blocksize (guestfs_h *g, int blocksize)
+{
+  if (blocksize >= 512 && blocksize <= 32768
+      && (blocksize & (blocksize-1)) == 0) {
+    g->blocksize = blocksize;
+    return guestfs_impl_launch (g);
+  }
+  else {
+    error (g, _("blocksize must be a power of 2 between 512 and 32768"));
+    return -1;
+  }
+}
+
 int
 guestfs_impl_launch (guestfs_h *g)
 {
-- 
2.21.0




More information about the Libguestfs mailing list