提交 47a12f27 编写于 作者: P Peter Krempa

qemu: block: Use simple backing stores string format if possible

In case when the backing store can be represented with something
simpler such as a 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: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 4f11a1ea
...@@ -1923,15 +1923,80 @@ qemuBlockStorageGetCopyOnReadProps(virDomainDiskDefPtr disk) ...@@ -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 static int
qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr backing, qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr backing,
virJSONValuePtr props, virJSONValuePtr props,
bool format) bool format)
{ {
VIR_AUTOPTR(virJSONValue) backingProps = NULL; VIR_AUTOFREE(char *) backingFileStr = NULL;
VIR_AUTOFREE(char *) backingJSON = NULL;
VIR_AUTOFREE(char *) backingPseudoprotocol = NULL;
const char *backingFileStr = NULL;
const char *backingFormatStr = NULL; const char *backingFormatStr = NULL;
if (!virStorageSourceIsBacking(backing)) if (!virStorageSourceIsBacking(backing))
...@@ -1945,24 +2010,8 @@ qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr backing, ...@@ -1945,24 +2010,8 @@ qemuBlockStorageSourceCreateAddBacking(virStorageSourcePtr backing,
backingFormatStr = virStorageFileFormatTypeToString(backing->format); backingFormatStr = virStorageFileFormatTypeToString(backing->format);
} }
if (virStorageSourceIsLocalStorage(backing)) { if (!(backingFileStr = qemuBlockGetBackingStoreString(backing)))
backingFileStr = backing->path; return -1;
} 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 (virJSONValueObjectAdd(props, if (virJSONValueObjectAdd(props,
"S:backing-file", backingFileStr, "S:backing-file", backingFileStr,
......
...@@ -168,6 +168,10 @@ qemuBlockSnapshotAddBlockdev(virJSONValuePtr actions, ...@@ -168,6 +168,10 @@ qemuBlockSnapshotAddBlockdev(virJSONValuePtr actions,
virDomainDiskDefPtr disk, virDomainDiskDefPtr disk,
virStorageSourcePtr newsrc); virStorageSourcePtr newsrc);
char *
qemuBlockGetBackingStoreString(virStorageSourcePtr src)
ATTRIBUTE_NONNULL(1);
int int
qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src, qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src,
virStorageSourcePtr backing, virStorageSourcePtr backing,
......
...@@ -10,6 +10,6 @@ format: ...@@ -10,6 +10,6 @@ format:
"driver": "qcow2", "driver": "qcow2",
"file": "0123456789ABCDEF0123456789ABCDE", "file": "0123456789ABCDEF0123456789ABCDE",
"size": 1337, "size": 1337,
"backing-file": "json:{\"driver\":\"nbd\",\"server\":{\"type\":\"inet\",\"host\":\"example.com\",\"port\":\"1234\"}}", "backing-file": "nbd://example.com:1234",
"backing-fmt": "raw" "backing-fmt": "raw"
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册