1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

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: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-07-22 17:36:05 +02:00
parent 4f11a1eaf2
commit 47a12f2752
3 changed files with 76 additions and 23 deletions

View File

@ -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,

View File

@ -168,6 +168,10 @@ qemuBlockSnapshotAddBlockdev(virJSONValuePtr actions,
virDomainDiskDefPtr disk,
virStorageSourcePtr newsrc);
char *
qemuBlockGetBackingStoreString(virStorageSourcePtr src)
ATTRIBUTE_NONNULL(1);
int
qemuBlockStorageSourceCreateGetFormatProps(virStorageSourcePtr src,
virStorageSourcePtr backing,

View File

@ -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"
}