[Libguestfs] [PATCH] lib: Autodetect backing format and specify it explicitly.

Richard W.M. Jones rjones at redhat.com
Thu Feb 6 17:20:19 UTC 2020


In the guestfs_disk_create API we have traditionally allowed you to
set backingfile without setting backingformat.  The meaning of this is
to let qemu autodetect the backing format when opening the overlay
disk.

However libvirt >= 6.0 refuses to even pass such disks to qemu (see
https://bugzilla.redhat.com/show_bug.cgi?id=1798148).

For this reason, move the autodetection earlier and make it explicit.
We now autodetect the format of the backing disk at the time of
creation of the overlay, and set that as the backing format in the
overlay disk itself, allowing libvirt to open the disk later.
---
 lib/create.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/create.c b/lib/create.c
index e2a59b88d..e30286c39 100644
--- a/lib/create.c
+++ b/lib/create.c
@@ -255,6 +255,7 @@ disk_create_qcow2 (guestfs_h *g, const char *filename, int64_t size,
                    const struct guestfs_disk_create_argv *optargs)
 {
   const char *backingformat = NULL;
+  CLEANUP_FREE char *backingformat_free = NULL;
   const char *preallocation = NULL;
   const char *compat = NULL;
   int clustersize = -1;
@@ -302,6 +303,18 @@ disk_create_qcow2 (guestfs_h *g, const char *filename, int64_t size,
     }
   }
 
+  /* With libvirt >= 6.0 the backing format must be specified. */
+  if (backingfile != NULL && backingformat == NULL) {
+    backingformat = backingformat_free = guestfs_disk_format (g, backingfile);
+    if (!backingformat)
+      return -1;
+    if (STREQ (backingformat, "unknown")) {
+      error (g, _("could not auto-detect the format of the backing file %s"),
+             backingfile);
+      return -1;
+    }
+  }
+
   /* Assemble the qemu-img command line. */
   guestfs_int_cmd_add_arg (cmd, "qemu-img");
   guestfs_int_cmd_add_arg (cmd, "create");
-- 
2.25.0




More information about the Libguestfs mailing list