qemu: block: Allow skipping non-target related data when formating disk JSON

When formatting new qcow2 images we need to provide the backing store
string which should not contain any authentication or irrelevant data.

Add a flag for qemuBlockStorageSourceGetBackendProps which allows to
skip the irrelevant data.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2018-09-04 18:52:02 +02:00
parent aa7d73134f
commit 423b4f6625
4 changed files with 79 additions and 51 deletions

View File

@ -624,7 +624,8 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src, qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
bool legacy) bool legacy,
bool onlytarget)
{ {
VIR_AUTOPTR(virJSONValue) servers = NULL; VIR_AUTOPTR(virJSONValue) servers = NULL;
VIR_AUTOPTR(virJSONValue) props = NULL; VIR_AUTOPTR(virJSONValue) props = NULL;
@ -645,7 +646,8 @@ qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
"a:server", &servers, NULL) < 0) "a:server", &servers, NULL) < 0)
return NULL; return NULL;
if (src->debug && if (!onlytarget &&
src->debug &&
virJSONValueObjectAdd(props, "u:debug", src->debugLevel, NULL) < 0) virJSONValueObjectAdd(props, "u:debug", src->debugLevel, NULL) < 0)
return NULL; return NULL;
@ -654,10 +656,12 @@ qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src) qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src,
bool onlytarget)
{ {
const char *protocol = virStorageNetProtocolTypeToString(src->protocol); const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
VIR_AUTOPTR(virJSONValue) server = NULL; VIR_AUTOPTR(virJSONValue) server = NULL;
const char *tlsAlias = src->tlsAlias;
virJSONValuePtr ret = NULL; virJSONValuePtr ret = NULL;
if (src->nhosts != 1) { if (src->nhosts != 1) {
@ -669,6 +673,9 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(&src->hosts[0]))) if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(&src->hosts[0])))
return NULL; return NULL;
if (onlytarget)
tlsAlias = NULL;
/* VxHS disk specification example: /* VxHS disk specification example:
* { driver:"vxhs", * { driver:"vxhs",
* tls-creds:"objvirtio-disk0_tls0", * tls-creds:"objvirtio-disk0_tls0",
@ -677,7 +684,7 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
*/ */
ignore_value(virJSONValueObjectCreate(&ret, ignore_value(virJSONValueObjectCreate(&ret,
"s:driver", protocol, "s:driver", protocol,
"S:tls-creds", src->tlsAlias, "S:tls-creds", tlsAlias,
"s:vdisk-id", src->path, "s:vdisk-id", src->path,
"a:server", &server, NULL)); "a:server", &server, NULL));
@ -686,7 +693,8 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src) qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src,
bool onlytarget)
{ {
qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src); qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
const char *passwordalias = NULL; const char *passwordalias = NULL;
@ -716,7 +724,7 @@ qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src)
if (!(uristr = virURIFormat(uri))) if (!(uristr = virURIFormat(uri)))
return NULL; return NULL;
if (src->auth) { if (!onlytarget && src->auth) {
username = src->auth->username; username = src->auth->username;
passwordalias = srcPriv->secinfo->s.aes.alias; passwordalias = srcPriv->secinfo->s.aes.alias;
} }
@ -733,7 +741,8 @@ qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src) qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src,
bool onlytarget)
{ {
qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src); qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
const char *protocol = virStorageNetProtocolTypeToString(src->protocol); const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
@ -781,7 +790,7 @@ qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src)
return NULL; return NULL;
} }
if (src->auth) { if (!onlytarget && src->auth) {
username = src->auth->username; username = src->auth->username;
objalias = srcPriv->secinfo->s.aes.alias; objalias = srcPriv->secinfo->s.aes.alias;
} }
@ -801,9 +810,11 @@ qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src) qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src,
bool onlytarget)
{ {
VIR_AUTOPTR(virJSONValue) serverprops = NULL; VIR_AUTOPTR(virJSONValue) serverprops = NULL;
const char *tlsAlias = src->tlsAlias;
virJSONValuePtr ret = NULL; virJSONValuePtr ret = NULL;
if (src->nhosts != 1) { if (src->nhosts != 1) {
@ -817,11 +828,14 @@ qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src)
if (!serverprops) if (!serverprops)
return NULL; return NULL;
if (onlytarget)
tlsAlias = NULL;
if (virJSONValueObjectCreate(&ret, if (virJSONValueObjectCreate(&ret,
"s:driver", "nbd", "s:driver", "nbd",
"a:server", &serverprops, "a:server", &serverprops,
"S:export", src->path, "S:export", src->path,
"S:tls-creds", src->tlsAlias, "S:tls-creds", tlsAlias,
NULL) < 0) NULL) < 0)
return NULL; return NULL;
@ -830,7 +844,8 @@ qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src) qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src,
bool onlytarget)
{ {
qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src); qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
VIR_AUTOPTR(virJSONValue) servers = NULL; VIR_AUTOPTR(virJSONValue) servers = NULL;
@ -844,7 +859,7 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src)
!(servers = qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(src))) !(servers = qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(src)))
return NULL; return NULL;
if (src->auth) { if (!onlytarget && src->auth) {
username = srcPriv->secinfo->s.aes.username; username = srcPriv->secinfo->s.aes.username;
keysecret = srcPriv->secinfo->s.aes.alias; keysecret = srcPriv->secinfo->s.aes.alias;
/* the auth modes are modelled after our old command line generator */ /* the auth modes are modelled after our old command line generator */
@ -943,16 +958,14 @@ qemuBlockStorageSourceGetSshProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src) qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src,
bool onlytarget)
{ {
const char *driver = "file"; const char *driver = "file";
const char *iomode = NULL; const char *iomode = NULL;
const char *prManagerAlias = NULL; const char *prManagerAlias = NULL;
virJSONValuePtr ret = NULL; virJSONValuePtr ret = NULL;
if (src->iomode != VIR_DOMAIN_DISK_IO_DEFAULT)
iomode = virDomainDiskIoTypeToString(src->iomode);
if (virStorageSourceIsBlockLocal(src)) { if (virStorageSourceIsBlockLocal(src)) {
if (src->hostcdrom) if (src->hostcdrom)
driver = "host_cdrom"; driver = "host_cdrom";
@ -960,8 +973,13 @@ qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src)
driver = "host_device"; driver = "host_device";
} }
if (src->pr) if (!onlytarget) {
prManagerAlias = src->pr->mgralias; if (src->pr)
prManagerAlias = src->pr->mgralias;
if (src->iomode != VIR_DOMAIN_DISK_IO_DEFAULT)
iomode = virDomainDiskIoTypeToString(src->iomode);
}
ignore_value(virJSONValueObjectCreate(&ret, ignore_value(virJSONValueObjectCreate(&ret,
"s:driver", driver, "s:driver", driver,
@ -974,21 +992,26 @@ qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src)
static virJSONValuePtr static virJSONValuePtr
qemuBlockStorageSourceGetVvfatProps(virStorageSourcePtr src) qemuBlockStorageSourceGetVvfatProps(virStorageSourcePtr src,
bool onlytarget)
{ {
virJSONValuePtr ret = NULL; VIR_AUTOPTR(virJSONValue) ret = NULL;
/* libvirt currently does not handle the following attributes: /* libvirt currently does not handle the following attributes:
* '*fat-type': 'int' * '*fat-type': 'int'
* '*label': 'str' * '*label': 'str'
*/ */
ignore_value(virJSONValueObjectCreate(&ret, if (virJSONValueObjectCreate(&ret,
"s:driver", "vvfat", "s:driver", "vvfat",
"s:dir", src->path, "s:dir", src->path,
"b:floppy", src->floppyimg, "b:floppy", src->floppyimg, NULL) < 0)
"b:rw", !src->readonly, NULL)); return NULL;
return ret; if (!onlytarget &&
virJSONValueObjectAdd(ret, "b:rw", !src->readonly, NULL) < 0)
return NULL;
VIR_RETURN_PTR(ret);
} }
@ -1024,13 +1047,15 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSourcePtr src,
* qemuBlockStorageSourceGetBackendProps: * qemuBlockStorageSourceGetBackendProps:
* @src: disk source * @src: disk source
* @legacy: use legacy formatting of attributes (for -drive / old qemus) * @legacy: use legacy formatting of attributes (for -drive / old qemus)
* @onlytarget: omit any data which does not identify the image itself
* *
* Creates a JSON object describing the underlying storage or protocol of a * Creates a JSON object describing the underlying storage or protocol of a
* storage source. Returns NULL on error and reports an appropriate error message. * storage source. Returns NULL on error and reports an appropriate error message.
*/ */
virJSONValuePtr virJSONValuePtr
qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src, qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
bool legacy) bool legacy,
bool onlytarget)
{ {
int actualType = virStorageSourceGetActualType(src); int actualType = virStorageSourceGetActualType(src);
VIR_AUTOPTR(virJSONValue) fileprops = NULL; VIR_AUTOPTR(virJSONValue) fileprops = NULL;
@ -1038,14 +1063,14 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
switch ((virStorageType)actualType) { switch ((virStorageType)actualType) {
case VIR_STORAGE_TYPE_BLOCK: case VIR_STORAGE_TYPE_BLOCK:
case VIR_STORAGE_TYPE_FILE: case VIR_STORAGE_TYPE_FILE:
if (!(fileprops = qemuBlockStorageSourceGetFileProps(src))) if (!(fileprops = qemuBlockStorageSourceGetFileProps(src, onlytarget)))
return NULL; return NULL;
break; break;
case VIR_STORAGE_TYPE_DIR: case VIR_STORAGE_TYPE_DIR:
/* qemu handles directories by exposing them as a device with emulated /* qemu handles directories by exposing them as a device with emulated
* FAT filesystem */ * FAT filesystem */
if (!(fileprops = qemuBlockStorageSourceGetVvfatProps(src))) if (!(fileprops = qemuBlockStorageSourceGetVvfatProps(src, onlytarget)))
return NULL; return NULL;
break; break;
@ -1057,12 +1082,12 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
case VIR_STORAGE_TYPE_NETWORK: case VIR_STORAGE_TYPE_NETWORK:
switch ((virStorageNetProtocol) src->protocol) { switch ((virStorageNetProtocol) src->protocol) {
case VIR_STORAGE_NET_PROTOCOL_GLUSTER: case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
if (!(fileprops = qemuBlockStorageSourceGetGlusterProps(src, legacy))) if (!(fileprops = qemuBlockStorageSourceGetGlusterProps(src, legacy, onlytarget)))
return NULL; return NULL;
break; break;
case VIR_STORAGE_NET_PROTOCOL_VXHS: case VIR_STORAGE_NET_PROTOCOL_VXHS:
if (!(fileprops = qemuBlockStorageSourceGetVxHSProps(src))) if (!(fileprops = qemuBlockStorageSourceGetVxHSProps(src, onlytarget)))
return NULL; return NULL;
break; break;
@ -1071,22 +1096,22 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
case VIR_STORAGE_NET_PROTOCOL_FTP: case VIR_STORAGE_NET_PROTOCOL_FTP:
case VIR_STORAGE_NET_PROTOCOL_FTPS: case VIR_STORAGE_NET_PROTOCOL_FTPS:
case VIR_STORAGE_NET_PROTOCOL_TFTP: case VIR_STORAGE_NET_PROTOCOL_TFTP:
if (!(fileprops = qemuBlockStorageSourceGetCURLProps(src))) if (!(fileprops = qemuBlockStorageSourceGetCURLProps(src, onlytarget)))
return NULL; return NULL;
break; break;
case VIR_STORAGE_NET_PROTOCOL_ISCSI: case VIR_STORAGE_NET_PROTOCOL_ISCSI:
if (!(fileprops = qemuBlockStorageSourceGetISCSIProps(src))) if (!(fileprops = qemuBlockStorageSourceGetISCSIProps(src, onlytarget)))
return NULL; return NULL;
break; break;
case VIR_STORAGE_NET_PROTOCOL_NBD: case VIR_STORAGE_NET_PROTOCOL_NBD:
if (!(fileprops = qemuBlockStorageSourceGetNBDProps(src))) if (!(fileprops = qemuBlockStorageSourceGetNBDProps(src, onlytarget)))
return NULL; return NULL;
break; break;
case VIR_STORAGE_NET_PROTOCOL_RBD: case VIR_STORAGE_NET_PROTOCOL_RBD:
if (!(fileprops = qemuBlockStorageSourceGetRBDProps(src))) if (!(fileprops = qemuBlockStorageSourceGetRBDProps(src, onlytarget)))
return NULL; return NULL;
break; break;
@ -1107,19 +1132,21 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
break; break;
} }
if (qemuBlockNodeNameValidate(src->nodestorage) < 0 || if (!onlytarget) {
virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0) if (qemuBlockNodeNameValidate(src->nodestorage) < 0 ||
return NULL; virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0)
if (!legacy) {
if (qemuBlockStorageSourceGetBlockdevGetCacheProps(src, fileprops) < 0)
return NULL; return NULL;
if (virJSONValueObjectAdd(fileprops, if (!legacy) {
"b:read-only", src->readonly, if (qemuBlockStorageSourceGetBlockdevGetCacheProps(src, fileprops) < 0)
"s:discard", "unmap", return NULL;
NULL) < 0)
return NULL; if (virJSONValueObjectAdd(fileprops,
"b:read-only", src->readonly,
"s:discard", "unmap",
NULL) < 0)
return NULL;
}
} }
VIR_RETURN_PTR(fileprops); VIR_RETURN_PTR(fileprops);
@ -1440,7 +1467,7 @@ qemuBlockStorageSourceAttachPrepareBlockdev(virStorageSourcePtr src)
return NULL; return NULL;
if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src)) || if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src)) ||
!(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false))) !(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false, false)))
return NULL; return NULL;
data->storageNodeName = src->nodestorage; data->storageNodeName = src->nodestorage;

View File

@ -58,7 +58,8 @@ qemuBlockStorageSourceSupportsConcurrentAccess(virStorageSourcePtr src);
virJSONValuePtr virJSONValuePtr
qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src, qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
bool legacy); bool legacy,
bool onlytarget);
virURIPtr virURIPtr
qemuBlockStorageSourceGetURI(virStorageSourcePtr src); qemuBlockStorageSourceGetURI(virStorageSourcePtr src);

View File

@ -1602,7 +1602,7 @@ qemuDiskSourceGetProps(virStorageSourcePtr src)
virJSONValuePtr props; virJSONValuePtr props;
virJSONValuePtr ret; virJSONValuePtr ret;
if (!(props = qemuBlockStorageSourceGetBackendProps(src, true))) if (!(props = qemuBlockStorageSourceGetBackendProps(src, true, false)))
return NULL; return NULL;
if (virJSONValueObjectCreate(&ret, "a:file", &props, NULL) < 0) { if (virJSONValueObjectCreate(&ret, "a:file", &props, NULL) < 0) {

View File

@ -67,7 +67,7 @@ testBackingXMLjsonXML(const void *args)
return -1; return -1;
} }
if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc, true))) { if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc, true, false))) {
fprintf(stderr, "failed to format disk source json\n"); fprintf(stderr, "failed to format disk source json\n");
return -1; return -1;
} }
@ -213,7 +213,7 @@ testQemuDiskXMLToProps(const void *opaque)
goto cleanup; goto cleanup;
if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n)) || if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n)) ||
!(storageProps = qemuBlockStorageSourceGetBackendProps(n, false))) { !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false))) {
if (!data->fail) { if (!data->fail) {
VIR_TEST_VERBOSE("failed to generate qemu blockdev props\n"); VIR_TEST_VERBOSE("failed to generate qemu blockdev props\n");
goto cleanup; goto cleanup;