Blob Blame History Raw
From d86f8ca314a4209b19d2ea425c2447b006afc84e Mon Sep 17 00:00:00 2001
Message-Id: <d86f8ca314a4209b19d2ea425c2447b006afc84e.1378475168.git.jdenemar@redhat.com>
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
Date: Wed, 4 Sep 2013 12:46:10 +0200
Subject: [PATCH] Always specify qcow2 compat level on qemu-img command line

qemu-img is going to switch the default for QCOW2
to QCOW2v3 (compat=1.1) [1]

Extend the probing for qemu-img command line options to check
if -o compat is supported. If the volume definition specifies
the qcow2 format but no compat level and -o compat is supported,
specify -o compat=0.10 to create a QCOW2v2 image.

https://bugzilla.redhat.com/show_bug.cgi?id=997977
(cherry picked from commit bab2eda6ada1bf832decdc64cc5062e54eb105df)
---
 src/storage/storage_backend.c                      | 47 +++++++++++++++++++---
 tests/storagevolxml2argvdata/qcow2-compat.argv     |  3 ++
 .../qcow2-from-logical-compat.argv                 |  3 ++
 .../qcow2-nobacking-convert-prealloc-compat.argv   |  3 ++
 .../qcow2-nobacking-prealloc-compat.argv           |  3 ++
 tests/storagevolxml2argvtest.c                     | 26 ++++++++++++
 6 files changed, 79 insertions(+), 6 deletions(-)
 create mode 100644 tests/storagevolxml2argvdata/qcow2-compat.argv
 create mode 100644 tests/storagevolxml2argvdata/qcow2-from-logical-compat.argv
 create mode 100644 tests/storagevolxml2argvdata/qcow2-nobacking-convert-prealloc-compat.argv
 create mode 100644 tests/storagevolxml2argvdata/qcow2-nobacking-prealloc-compat.argv

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 8d5880e..4ebe11b 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -581,8 +581,34 @@ enum {
     QEMU_IMG_BACKING_FORMAT_NONE = 0,
     QEMU_IMG_BACKING_FORMAT_FLAG,
     QEMU_IMG_BACKING_FORMAT_OPTIONS,
+    QEMU_IMG_BACKING_FORMAT_OPTIONS_COMPAT,
 };
 
+static bool
+virStorageBackendQemuImgSupportsCompat(const char *qemuimg)
+{
+    bool ret = false;
+    char *output;
+    virCommandPtr cmd = NULL;
+
+    cmd = virCommandNewArgList(qemuimg, "create", "-o", "?", "-f", "qcow2",
+                               "/dev/null", NULL);
+
+    virCommandAddEnvString(cmd, "LC_ALL=C");
+    virCommandSetOutputBuffer(cmd, &output);
+
+    if (virCommandRun(cmd, NULL) < 0)
+        goto cleanup;
+
+    if (strstr(output, "\ncompat "))
+        ret = true;
+
+cleanup:
+    virCommandFree(cmd);
+    VIR_FREE(output);
+    return ret;
+}
+
 static int
 virStorageBackendQEMUImgBackingFormat(const char *qemuimg)
 {
@@ -612,12 +638,16 @@ virStorageBackendQEMUImgBackingFormat(const char *qemuimg)
         goto cleanup;
     }
     if (((tmp = strstr(start, "-F fmt")) && tmp < end) ||
-        ((tmp = strstr(start, "-F backing_fmt")) && tmp < end))
+        ((tmp = strstr(start, "-F backing_fmt")) && tmp < end)) {
         ret = QEMU_IMG_BACKING_FORMAT_FLAG;
-    else if ((tmp = strstr(start, "[-o options]")) && tmp < end)
-        ret = QEMU_IMG_BACKING_FORMAT_OPTIONS;
-    else
+    } else if ((tmp = strstr(start, "[-o options]")) && tmp < end) {
+        if (virStorageBackendQemuImgSupportsCompat(qemuimg))
+            ret = QEMU_IMG_BACKING_FORMAT_OPTIONS_COMPAT;
+        else
+            ret = QEMU_IMG_BACKING_FORMAT_OPTIONS;
+    } else {
         ret = QEMU_IMG_BACKING_FORMAT_NONE;
+    }
 
 cleanup:
     virCommandFree(cmd);
@@ -705,6 +735,7 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
     const char *backingType = NULL;
     const char *inputPath = NULL;
     const char *inputType = NULL;
+    const char *compat = vol->target.compat;
     char *opts = NULL;
     bool convert = false;
     bool backing = false;
@@ -854,12 +885,16 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
     if (backing)
         virCommandAddArgList(cmd, "-b", vol->backingStore.path, NULL);
 
-    if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS) {
+    if (imgformat >= QEMU_IMG_BACKING_FORMAT_OPTIONS) {
+        if (vol->target.format == VIR_STORAGE_FILE_QCOW2 && !compat &&
+            imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS_COMPAT)
+            compat = "0.10";
+
         if (virStorageBackendCreateQemuImgOpts(&opts,
                                                backing ? backingType : NULL,
                                                do_encryption, preallocate,
                                                vol->target.format,
-                                               vol->target.compat,
+                                               compat,
                                                vol->target.features) < 0) {
             virCommandFree(cmd);
             return NULL;
diff --git a/tests/storagevolxml2argvdata/qcow2-compat.argv b/tests/storagevolxml2argvdata/qcow2-compat.argv
new file mode 100644
index 0000000..37ad2c0
--- /dev/null
+++ b/tests/storagevolxml2argvdata/qcow2-compat.argv
@@ -0,0 +1,3 @@
+qemu-img create -f qcow2 -b /dev/null \
+-o backing_fmt=raw,encryption=on,compat=0.10 \
+/var/lib/libvirt/images/OtherDemo.img 5242880K
diff --git a/tests/storagevolxml2argvdata/qcow2-from-logical-compat.argv b/tests/storagevolxml2argvdata/qcow2-from-logical-compat.argv
new file mode 100644
index 0000000..5f365b1
--- /dev/null
+++ b/tests/storagevolxml2argvdata/qcow2-from-logical-compat.argv
@@ -0,0 +1,3 @@
+qemu-img convert -f raw -O qcow2 \
+-o encryption=on,compat=0.10 \
+/dev/HostVG/Swap /var/lib/libvirt/images/OtherDemo.img
diff --git a/tests/storagevolxml2argvdata/qcow2-nobacking-convert-prealloc-compat.argv b/tests/storagevolxml2argvdata/qcow2-nobacking-convert-prealloc-compat.argv
new file mode 100644
index 0000000..3d93ec8
--- /dev/null
+++ b/tests/storagevolxml2argvdata/qcow2-nobacking-convert-prealloc-compat.argv
@@ -0,0 +1,3 @@
+qemu-img convert -f raw -O qcow2 \
+-o encryption=on,preallocation=metadata,compat=0.10 \
+/var/lib/libvirt/images/sparse.img /var/lib/libvirt/images/OtherDemo.img
diff --git a/tests/storagevolxml2argvdata/qcow2-nobacking-prealloc-compat.argv b/tests/storagevolxml2argvdata/qcow2-nobacking-prealloc-compat.argv
new file mode 100644
index 0000000..903c94e
--- /dev/null
+++ b/tests/storagevolxml2argvdata/qcow2-nobacking-prealloc-compat.argv
@@ -0,0 +1,3 @@
+qemu-img create -f qcow2 \
+-o encryption=on,preallocation=metadata,compat=0.10 \
+/var/lib/libvirt/images/OtherDemo.img 5242880K
diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c
index b1cf09f..cafcaad 100644
--- a/tests/storagevolxml2argvtest.c
+++ b/tests/storagevolxml2argvtest.c
@@ -195,6 +195,7 @@ enum {
     FMT_NONE = 0,
     FMT_FLAG,
     FMT_OPTIONS,
+    FMT_COMPAT,
 };
 
 
@@ -271,6 +272,31 @@ mymain(void)
             "pool-dir", "vol-qcow2-nobacking",
             "logical-from-qcow2", 0, FMT_OPTIONS);
 
+    DO_TEST("pool-dir", "vol-qcow2",
+            NULL, NULL,
+            "qcow2-compat", 0, FMT_COMPAT);
+    DO_TEST("pool-dir", "vol-qcow2-nobacking",
+            NULL, NULL,
+            "qcow2-nobacking-prealloc-compat", flags, FMT_COMPAT);
+    DO_TEST("pool-dir", "vol-qcow2-nobacking",
+            "pool-dir", "vol-file",
+            "qcow2-nobacking-convert-prealloc-compat", flags, FMT_COMPAT);
+    DO_TEST("pool-dir", "vol-qcow2-lazy",
+            NULL, NULL,
+            "qcow2-lazy", 0, FMT_COMPAT);
+    DO_TEST("pool-dir", "vol-qcow2-1.1",
+            NULL, NULL,
+            "qcow2-1.1", 0, FMT_COMPAT);
+    DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
+                 NULL, NULL,
+                 "qcow2-0.10-lazy", 0, FMT_COMPAT);
+    DO_TEST("pool-dir", "vol-qcow2-nobacking",
+            "pool-logical", "vol-logical",
+            "qcow2-from-logical-compat", 0, FMT_COMPAT);
+    DO_TEST("pool-logical", "vol-logical",
+            "pool-dir", "vol-qcow2-nobacking",
+            "logical-from-qcow2", 0, FMT_COMPAT);
+
     return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-- 
1.8.3.2