[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/8] storage: rework qemu-img command line generation



Split out option string generation to make adding new options easier
and simplify the code.
---
 src/storage/storage_backend.c | 111 ++++++++++++++++++++++--------------------
 1 file changed, 59 insertions(+), 52 deletions(-)

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 01a404e..ab249b4 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -635,6 +635,32 @@ cleanup:
     return ret;
 }
 
+static int
+virStorageBackendCreateQemuImgOpts(char **opts,
+                                   const char *backingType,
+                                   bool encryption,
+                                   bool preallocate)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    if (backingType)
+        virBufferAsprintf(&buf, "backing_fmt=%s,", backingType);
+    if (encryption)
+        virBufferAddLit(&buf, "encryption=on,");
+    if (preallocate)
+        virBufferAddLit(&buf, "preallocation=metadata,");
+
+    virBufferTrim(&buf, ",", -1);
+
+    if (virBufferError(&buf)) {
+        virBufferFreeAndReset(&buf);
+        virReportOOMError();
+        return -1;
+    }
+
+    *opts = virBufferContentAndReset(&buf);
+    return 0;
+}
+
 virCommandPtr
 virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
                                   virStoragePoolObjPtr pool,
@@ -648,6 +674,9 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
     bool do_encryption = (vol->target.encryption != NULL);
     unsigned long long int size_arg;
     bool preallocate = false;
+    char *opts = NULL;
+    bool convert = false;
+    bool backing = false;
 
     /* Treat output block devices as 'raw' format */
     const char *type =
@@ -776,65 +805,43 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
 
     cmd = virCommandNew(create_tool);
 
-    if (inputvol) {
-        virCommandAddArgList(cmd, "convert", "-f", inputType, "-O", type, NULL);
+    convert = !!inputvol;
+    backing = !inputvol && vol->backingStore.path;
 
-        if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS &&
-            (do_encryption || preallocate)) {
-            virCommandAddArg(cmd, "-o");
-            virCommandAddArgFormat(cmd, "%s%s%s", do_encryption ? "encryption=on" : "",
-                                   (do_encryption && preallocate) ? "," : "",
-                                   preallocate ? "preallocation=metadata" : "");
-        } else if (do_encryption) {
-            virCommandAddArg(cmd, "-e");
-        }
-        virCommandAddArgList(cmd, inputPath, vol->target.path, NULL);
-    } else if (vol->backingStore.path) {
-        virCommandAddArgList(cmd, "create", "-f", type,
-                             "-b", vol->backingStore.path, NULL);
-
-        switch (imgformat) {
-        case QEMU_IMG_BACKING_FORMAT_FLAG:
-            virCommandAddArgList(cmd, "-F", backingType, NULL);
-            if (do_encryption)
-                virCommandAddArg(cmd, "-e");
-            virCommandAddArg(cmd, vol->target.path);
-            virCommandAddArgFormat(cmd, "%lluK", size_arg);
-            break;
-
-        case QEMU_IMG_BACKING_FORMAT_OPTIONS:
-            virCommandAddArg(cmd, "-o");
-            virCommandAddArgFormat(cmd, "backing_fmt=%s%s", backingType,
-                                   do_encryption ? ",encryption=on" : "");
-            virCommandAddArg(cmd, vol->target.path);
-            virCommandAddArgFormat(cmd, "%lluK", size_arg);
-            break;
+    if (convert)
+        virCommandAddArgList(cmd, "convert", "-f", inputType, "-O", type, NULL);
+    else
+        virCommandAddArgList(cmd, "create", "-f", type, NULL);
 
-        default:
-            VIR_DEBUG("Unable to set backing store format for %s with %s",
-                      vol->target.path, create_tool);
+    if (backing)
+        virCommandAddArgList(cmd, "-b", vol->backingStore.path, NULL);
 
-            if (do_encryption)
-                virCommandAddArg(cmd, "-e");
-            virCommandAddArg(cmd, vol->target.path);
-            virCommandAddArgFormat(cmd, "%lluK", size_arg);
-        }
+    if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS) {
+        if (virStorageBackendCreateQemuImgOpts(&opts,
+                                               backing ? backingType : NULL,
+                                               do_encryption, preallocate))
+            return NULL;
+        if (opts)
+            virCommandAddArgList(cmd, "-o", opts, NULL);
+        VIR_FREE(opts);
     } else {
-        virCommandAddArgList(cmd, "create", "-f", type, NULL);
-
-        if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS &&
-            (do_encryption || preallocate)) {
-            virCommandAddArg(cmd, "-o");
-            virCommandAddArgFormat(cmd, "%s%s%s", do_encryption ? "encryption=on" : "",
-                                   (do_encryption && preallocate) ? "," : "",
-                                   preallocate ? "preallocation=metadata" : "");
-        } else if (do_encryption) {
-            virCommandAddArg(cmd, "-e");
+        if (backing) {
+            if (imgformat == QEMU_IMG_BACKING_FORMAT_FLAG)
+                virCommandAddArgList(cmd, "-F", backingType, NULL);
+            else
+                VIR_DEBUG("Unable to set backing store format for %s with %s",
+                          vol->target.path, create_tool);
         }
-        virCommandAddArg(cmd, vol->target.path);
-        virCommandAddArgFormat(cmd, "%lluK", size_arg);
+        if (do_encryption)
+            virCommandAddArg(cmd, "-e");
     }
 
+    if (convert)
+        virCommandAddArg(cmd, inputPath);
+    virCommandAddArg(cmd, vol->target.path);
+    if (!convert)
+        virCommandAddArgFormat(cmd, "%lluK", size_arg);
+
     return cmd;
 }
 
-- 
1.8.1.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]