In case when the backing store can be represented with something
simpler such as an URI we can use it rather than falling back to the
json: pseudo-protocol.
In cases when it's not worth it (e.g. with the old ugly NBD or RBD
strings let's switch to json).
The function is exported as we'll need it when overwriting the ugly
strings qemu would come up with during blockjobs.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_block.c | 93 ++++++++++++++-----
src/qemu/qemu_block.h | 4 +
.../imagecreate/qcow2-backing-raw-nbd.json | 2 +-
3 files changed, 76 insertions(+), 23 deletions(-)
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index e47bc2d8f0..47661fb8bd 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -1923,15 +1923,80 @@ qemuBlockStorageGetCopyOnReadProps(virDomainDiskDefPtr disk)
}
+/**
+ * qemuBlockGetBackingStoreString:
+ * @src: storage source to get the string for
+ *
+ * Formats a string used in the backing store field of a disk image which
+ * supports backing store. Non-local storage may result in use of the json:
+ * pseudo protocol for any complex configuration.
+ */
+char *
+qemuBlockGetBackingStoreString(virStorageSourcePtr src)
+{
+ int actualType = virStorageSourceGetActualType(src);
+ VIR_AUTOPTR(virJSONValue) backingProps = NULL;
+ VIR_AUTOPTR(virURI) uri = NULL;
+ VIR_AUTOFREE(char *) backingJSON = NULL;
+ char *ret = NULL;
+
+ if (virStorageSourceIsLocalStorage(src)) {
+ ignore_value(VIR_STRDUP(ret, src->path));
+ return ret;
+ }
+
+ /* generate simplified URIs for the easy cases */
+ if (actualType == VIR_STORAGE_TYPE_NETWORK &&
+ src->nhosts == 1 &&
+ src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
+
+ switch ((virStorageNetProtocol) src->protocol) {
+ case VIR_STORAGE_NET_PROTOCOL_NBD:
+ case VIR_STORAGE_NET_PROTOCOL_HTTP:
+ case VIR_STORAGE_NET_PROTOCOL_HTTPS:
+ case VIR_STORAGE_NET_PROTOCOL_FTP:
+ case VIR_STORAGE_NET_PROTOCOL_FTPS:
+ case VIR_STORAGE_NET_PROTOCOL_TFTP:
+ case VIR_STORAGE_NET_PROTOCOL_ISCSI:
+ case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
+ if (!(uri = qemuBlockStorageSourceGetURI(src)))
+ return NULL;
+
+ if (!(ret = virURIFormat(uri)))
+ return NULL;
+
+ return ret;
+
+ case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
+ case VIR_STORAGE_NET_PROTOCOL_RBD:
+ case VIR_STORAGE_NET_PROTOCOL_VXHS:
+ case VIR_STORAGE_NET_PROTOCOL_SSH:
+ case VIR_STORAGE_NET_PROTOCOL_LAST:
+ case VIR_STORAGE_NET_PROTOCOL_NONE:
+ break;
+ }
+ }
+
+ /* use json: pseudo protocol otherwise */
+ if (!(backingProps = qemuBlockStorageSourceGetBackendProps(src, false, true,
false)))
+ return NULL;
+
+ if (!(backingJSON = virJSONValueToString(backingProps, false)))
+ return NULL;
+
+ if (virAsprintf(&ret, "json:%s", backingJSON) < 0)
+ return NULL;
+
+ return ret;
+}
+
+
static int
qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr backing,
virJSONValuePtr props,
bool format)
{
- VIR_AUTOPTR(virJSONValue) backingProps = NULL;
- VIR_AUTOFREE(char *) backingJSON = NULL;
- VIR_AUTOFREE(char *) backingPseudoprotocol = NULL;
- const char *backingFileStr = NULL;
+ VIR_AUTOFREE(char *) backingFileStr = NULL;
const char *backingFormatStr = NULL;
if (!virStorageSourceIsBacking(backing))
@@ -1945,24 +2010,8 @@ qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr
backing,
backingFormatStr = virStorageFileFormatTypeToString(backing->format);
}
- if (virStorageSourceIsLocalStorage(backing)) {
- backingFileStr = backing->path;
- } else {
- if (!(backingProps = qemuBlockStorageSourceGetBackendProps(backing, false,
- true, false))) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("failed to generate backing file JSON
properties"));
- return -1;
- }
-
- if (!(backingJSON = virJSONValueToString(backingProps, false)))
- return -1;
-
- if (virAsprintf(&backingPseudoprotocol, "json:%s", backingJSON)
< 0)
- return -1;
-
- backingFileStr = backingPseudoprotocol;
- }
+ if (!(backingFileStr = qemuBlockGetBackingStoreString(backing)))
+ return -1;
if (virJSONValueObjectAdd(props,
"S:backing-file", backingFileStr,
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
index 5fe5319ab9..ab6b9dc791 100644
--- a/src/qemu/qemu_block.h
+++ b/src/qemu/qemu_block.h
@@ -168,6 +168,10 @@ qemuBlockSnapshotAddBlockdev(virJSONValuePtr actions,
virDomainDiskDefPtr disk,
virStorageSourcePtr newsrc);
+char *
+qemuBlockGetBackingStoreString(virStorageSourcePtr src)
+ ATTRIBUTE_NONNULL(1);
+
int
qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src,
virStorageSourcePtr backing,
diff --git a/tests/qemublocktestdata/imagecreate/qcow2-backing-raw-nbd.json
b/tests/qemublocktestdata/imagecreate/qcow2-backing-raw-nbd.json
index 34ce74a548..b9d1d97302 100644
--- a/tests/qemublocktestdata/imagecreate/qcow2-backing-raw-nbd.json
+++ b/tests/qemublocktestdata/imagecreate/qcow2-backing-raw-nbd.json
@@ -10,6 +10,6 @@ format:
"driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337,
- "backing-file":
"json:{\"driver\":\"nbd\",\"server\":{\"type\":\"inet\",\"host\":\"example.com\",\"port\":\"1234\"}}",
+ "backing-file": "nbd://example.com:1234",
"backing-fmt": "raw"
}
--
2.21.0