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 e5c9209..0abc3f3 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -651,6 +651,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,
@@ -668,6 +694,9 @@ virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
const char *backingType = NULL;
const char *inputPath = NULL;
const char *inputType = NULL;
+ char *opts = NULL;
+ bool convert = false;
+ bool backing = false;
virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA, NULL);
@@ -795,65 +824,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