[libvirt] [PATCH v2 1/1] storage: extend preallocation flags support for qemu-img

Wim Ten Have wim.ten.have at oracle.com
Mon Apr 9 18:14:35 UTC 2018


From: Wim ten Have <wim.ten.have at oracle.com>

This patch adds support to qcow2 formatted filesystem object storage by
instructing qemu-img to build them with preallocation=falloc whenever the
XML described storage <allocation> matches its <capacity>.  For all other
cases the filesystem stored objects are built with preallocation=metadata.

Signed-off-by: Wim ten Have <wim.ten.have at oracle.com>
---
 src/conf/storage_conf.c                                        |  3 +++
 src/storage/storage_util.c                                     | 10 ++++++++--
 .../qcow2-nocapacity-convert-prealloc.argv                     |  2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 5036ab9ef..2bd077237 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1184,6 +1184,9 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
         ret->target.allocation = ret->target.capacity;
     }
 
+    if (ret->target.allocation < ret->target.capacity)
+        ret->target.sparse = true;
+
     ret->target.path = virXPathString("string(./target/path)", ctxt);
     if (options->formatFromString) {
         char *format = virXPathString("string(./target/format/@type)", ctxt);
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
index b4aed0f70..c4f311ded 100644
--- a/src/storage/storage_util.c
+++ b/src/storage/storage_util.c
@@ -856,6 +856,7 @@ struct _virStorageBackendQemuImgInfo {
     const char *compat;
     virBitmapPtr features;
     bool nocow;
+    bool sparse;
 
     const char *backingPath;
     int backingFormat;
@@ -884,8 +885,12 @@ storageBackendCreateQemuImgOpts(virStorageEncryptionInfoDefPtr enc,
                               virStorageFileFormatTypeToString(info.backingFormat));
         if (info.encryption)
             virBufferAddLit(&buf, "encryption=on,");
-        if (info.preallocate)
-            virBufferAddLit(&buf, "preallocation=metadata,");
+        if (info.preallocate) {
+            if (info.sparse)
+                virBufferAddLit(&buf, "preallocation=metadata,");
+            else
+                virBufferAddLit(&buf, "preallocation=falloc,");
+        }
     }
 
     if (info.nocow)
@@ -1187,6 +1192,7 @@ virStorageBackendCreateQemuImgCmdFromVol(virStoragePoolObjPtr pool,
         .compat = vol->target.compat,
         .features = vol->target.features,
         .nocow = vol->target.nocow,
+        .sparse = vol->target.sparse,
         .secretPath = secretPath,
         .secretAlias = NULL,
     };
diff --git a/tests/storagevolxml2argvdata/qcow2-nocapacity-convert-prealloc.argv b/tests/storagevolxml2argvdata/qcow2-nocapacity-convert-prealloc.argv
index 9073b1b16..b151b9401 100644
--- a/tests/storagevolxml2argvdata/qcow2-nocapacity-convert-prealloc.argv
+++ b/tests/storagevolxml2argvdata/qcow2-nocapacity-convert-prealloc.argv
@@ -1,4 +1,4 @@
 qemu-img convert -f raw -O qcow2 \
--o encryption=on,preallocation=metadata \
+-o encryption=on,preallocation=falloc \
 /var/lib/libvirt/images/sparse.img \
 /var/lib/libvirt/images/OtherDemo.img
-- 
2.14.3




More information about the libvir-list mailing list