[libvirt] [PATCHv1 09/13] Split out qemu-img command generation

Ján Tomko jtomko at redhat.com
Fri Apr 10 12:59:01 UTC 2015


Do not require the volume definition.
This will allow code reuse when creating snapshots.
---
 src/storage/storage_backend.c | 203 +++++++++++++++++++++++-------------------
 1 file changed, 109 insertions(+), 94 deletions(-)

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index f5b95ec..bfbc193 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -870,38 +870,16 @@ virStorageBackendCreateQemuImgOpts(char **opts,
     return -1;
 }
 
-/* Create a qemu-img virCommand from the supplied binary path,
- * volume definitions and imgformat
- */
-virCommandPtr
-virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
-                                         virStoragePoolObjPtr pool,
-                                         virStorageVolDefPtr vol,
-                                         virStorageVolDefPtr inputvol,
-                                         unsigned int flags,
-                                         const char *create_tool,
-                                         int imgformat)
+static virCommandPtr
+virStorageBackendCreateQemuImgCmd(const char *create_tool,
+                                  int imgformat,
+                                  struct _virStorageBackendQemuImgInfo info)
 {
     virCommandPtr cmd = NULL;
-    const char *type;
+    const char *type = NULL;
     const char *backingType = NULL;
     const char *inputType = NULL;
     char *opts = NULL;
-    struct _virStorageBackendQemuImgInfo info = {
-        .format = vol->target.format,
-        .path = vol->target.path,
-        .encryption = vol->target.encryption != NULL,
-        .preallocate = !!(flags & VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA),
-        .compat = vol->target.compat,
-        .features = vol->target.features,
-        .nocow = vol->target.nocow,
-    };
-
-    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);
-
-    /* Treat output block devices as 'raw' format */
-    if (vol->type == VIR_STORAGE_VOL_BLOCK)
-        info.format = VIR_STORAGE_FILE_RAW;
 
     if (!(type = virStorageFileFormatTypeToString(info.format))) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -926,6 +904,107 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
         return NULL;
     }
 
+    if (info.inputPath &&
+        !(inputType = virStorageFileFormatTypeToString(info.inputFormat))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("unknown storage vol type %d"),
+                       info.inputFormat);
+        return NULL;
+    }
+
+    if (info.backingPath) {
+        if (info.preallocate) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("metadata preallocation conflicts with backing"
+                             " store"));
+            return NULL;
+        }
+
+        if (!(backingType = virStorageFileFormatTypeToString(info.backingFormat))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("unknown storage vol backing store type %d"),
+                           info.backingFormat);
+            return NULL;
+        }
+    }
+
+    /* ignore the backing volume when we're converting a volume */
+    if (info.inputPath) {
+        info.backingPath = NULL;
+        backingType = NULL;
+    }
+
+    cmd = virCommandNew(create_tool);
+
+    if (info.inputPath)
+        virCommandAddArgList(cmd, "convert", "-f", inputType, "-O", type, NULL);
+    else
+        virCommandAddArgList(cmd, "create", "-f", type, NULL);
+
+    if (info.backingPath)
+        virCommandAddArgList(cmd, "-b", info.backingPath, NULL);
+
+    if (imgformat >= QEMU_IMG_BACKING_FORMAT_OPTIONS) {
+        if (info.format == VIR_STORAGE_FILE_QCOW2 && !info.compat &&
+            imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS_COMPAT)
+            info.compat = "0.10";
+
+        if (virStorageBackendCreateQemuImgOpts(&opts, info) < 0) {
+            virCommandFree(cmd);
+            return NULL;
+        }
+        if (opts)
+            virCommandAddArgList(cmd, "-o", opts, NULL);
+        VIR_FREE(opts);
+    } else {
+        if (info.backingPath) {
+            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",
+                          info.path, create_tool);
+        }
+        if (info.encryption)
+            virCommandAddArg(cmd, "-e");
+    }
+
+    if (info.inputPath)
+        virCommandAddArg(cmd, info.inputPath);
+    virCommandAddArg(cmd, info.path);
+    if (!info.inputPath && info.size_arg)
+        virCommandAddArgFormat(cmd, "%lluK", info.size_arg);
+    return cmd;
+}
+
+/* Create a qemu-img virCommand from the supplied binary path,
+ * volume definitions and imgformat
+ */
+virCommandPtr
+virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
+                                         virStoragePoolObjPtr pool,
+                                         virStorageVolDefPtr vol,
+                                         virStorageVolDefPtr inputvol,
+                                         unsigned int flags,
+                                         const char *create_tool,
+                                         int imgformat)
+{
+    virCommandPtr cmd = NULL;
+    struct _virStorageBackendQemuImgInfo info = {
+        .format = vol->target.format,
+        .path = vol->target.path,
+        .encryption = vol->target.encryption != NULL,
+        .preallocate = !!(flags & VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA),
+        .compat = vol->target.compat,
+        .features = vol->target.features,
+        .nocow = vol->target.nocow,
+    };
+
+    virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);
+
+    /* Treat output block devices as 'raw' format */
+    if (vol->type == VIR_STORAGE_VOL_BLOCK)
+        info.format = VIR_STORAGE_FILE_RAW;
+
     if (inputvol) {
         if (!(info.inputPath = inputvol->target.path)) {
             virReportError(VIR_ERR_INVALID_ARG, "%s",
@@ -936,12 +1015,6 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
         info.inputFormat = inputvol->target.format;
         if (inputvol->type == VIR_STORAGE_VOL_BLOCK)
             info.inputFormat = VIR_STORAGE_FILE_RAW;
-        if (!(inputType = virStorageFileFormatTypeToString(info.inputFormat))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("unknown storage vol type %d"),
-                           info.inputFormat);
-            return NULL;
-        }
     }
 
     if (vol->target.backingStore) {
@@ -951,13 +1024,6 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
         info.backingFormat = vol->target.backingStore->format;
         info.backingPath = vol->target.backingStore->path;
 
-        if (info.preallocate) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("metadata preallocation conflicts with backing"
-                             " store"));
-            return NULL;
-        }
-
         /* XXX: Not strictly required: qemu-img has an option a different
          * backing store, not really sure what use it serves though, and it
          * may cause issues with lvm. Untested essentially.
@@ -969,13 +1035,6 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
             return NULL;
         }
 
-        if (!(backingType = virStorageFileFormatTypeToString(info.backingFormat))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("unknown storage vol backing store type %d"),
-                           info.backingFormat);
-            return NULL;
-        }
-
         /* Convert relative backing store paths to absolute paths for access
          * validation.
          */
@@ -1000,7 +1059,8 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
             info.format != VIR_STORAGE_FILE_QCOW2) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("qcow volume encryption unsupported with "
-                             "volume format %s"), type);
+                             "volume format %s"),
+                           virStorageFileFormatTypeToString(info.format));
             return NULL;
         }
         enc = vol->target.encryption;
@@ -1026,52 +1086,7 @@ virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
     /* Size in KB */
     info.size_arg = VIR_DIV_UP(vol->target.capacity, 1024);
 
-    cmd = virCommandNew(create_tool);
-
-    /* ignore the backing volume when we're converting a volume */
-    if (info.inputPath) {
-        info.backingPath = NULL;
-        backingType = NULL;
-    }
-
-    if (info.inputPath)
-        virCommandAddArgList(cmd, "convert", "-f", inputType, "-O", type, NULL);
-    else
-        virCommandAddArgList(cmd, "create", "-f", type, NULL);
-
-    if (info.backingPath)
-        virCommandAddArgList(cmd, "-b", info.backingPath, NULL);
-
-    if (imgformat >= QEMU_IMG_BACKING_FORMAT_OPTIONS) {
-        if (info.format == VIR_STORAGE_FILE_QCOW2 && !info.compat &&
-            imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS_COMPAT)
-            info.compat = "0.10";
-
-        if (virStorageBackendCreateQemuImgOpts(&opts, info) < 0) {
-            virCommandFree(cmd);
-            return NULL;
-        }
-        if (opts)
-            virCommandAddArgList(cmd, "-o", opts, NULL);
-        VIR_FREE(opts);
-    } else {
-        if (info.backingPath) {
-            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",
-                          info.path, create_tool);
-        }
-        if (info.encryption)
-            virCommandAddArg(cmd, "-e");
-    }
-
-    if (info.inputPath)
-        virCommandAddArg(cmd, info.inputPath);
-    virCommandAddArg(cmd, info.path);
-    if (!info.inputPath && info.size_arg)
-        virCommandAddArgFormat(cmd, "%lluK", info.size_arg);
-
+    cmd = virStorageBackendCreateQemuImgCmd(create_tool, imgformat, info);
     return cmd;
 }
 
-- 
2.0.5




More information about the libvir-list mailing list