]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: block: Allow skipping non-target related data when formating disk JSON
authorPeter Krempa <pkrempa@redhat.com>
Tue, 4 Sep 2018 16:52:02 +0000 (18:52 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 18 Jul 2019 15:59:34 +0000 (17:59 +0200)
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>
src/qemu/qemu_block.c
src/qemu/qemu_block.h
src/qemu/qemu_command.c
tests/qemublocktest.c

index 8641e2011ccaf4365cff22e8fb40bc504063be77..53ce6576e3a659ba066f58ace75e557cc54781b7 100644 (file)
@@ -624,7 +624,8 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(virStorageSourcePtr src)
 
 static virJSONValuePtr
 qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
-                                      bool legacy)
+                                      bool legacy,
+                                      bool onlytarget)
 {
     VIR_AUTOPTR(virJSONValue) servers = NULL;
     VIR_AUTOPTR(virJSONValue) props = NULL;
@@ -645,7 +646,8 @@ qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
                                  "a:server", &servers, NULL) < 0)
         return NULL;
 
-    if (src->debug &&
+    if (!onlytarget &&
+        src->debug &&
         virJSONValueObjectAdd(props, "u:debug", src->debugLevel, NULL) < 0)
         return NULL;
 
@@ -654,10 +656,12 @@ qemuBlockStorageSourceGetGlusterProps(virStorageSourcePtr src,
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src,
+                                   bool onlytarget)
 {
     const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
     VIR_AUTOPTR(virJSONValue) server = NULL;
+    const char *tlsAlias = src->tlsAlias;
     virJSONValuePtr ret = NULL;
 
     if (src->nhosts != 1) {
@@ -669,6 +673,9 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
     if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(&src->hosts[0])))
         return NULL;
 
+    if (onlytarget)
+        tlsAlias = NULL;
+
     /* VxHS disk specification example:
      * { driver:"vxhs",
      *   tls-creds:"objvirtio-disk0_tls0",
@@ -677,7 +684,7 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
      */
     ignore_value(virJSONValueObjectCreate(&ret,
                                           "s:driver", protocol,
-                                          "S:tls-creds", src->tlsAlias,
+                                          "S:tls-creds", tlsAlias,
                                           "s:vdisk-id", src->path,
                                           "a:server", &server, NULL));
 
@@ -686,7 +693,8 @@ qemuBlockStorageSourceGetVxHSProps(virStorageSourcePtr src)
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src,
+                                   bool onlytarget)
 {
     qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
     const char *passwordalias = NULL;
@@ -716,7 +724,7 @@ qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src)
     if (!(uristr = virURIFormat(uri)))
         return NULL;
 
-    if (src->auth) {
+    if (!onlytarget && src->auth) {
         username = src->auth->username;
         passwordalias = srcPriv->secinfo->s.aes.alias;
     }
@@ -733,7 +741,8 @@ qemuBlockStorageSourceGetCURLProps(virStorageSourcePtr src)
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src,
+                                    bool onlytarget)
 {
     qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
     const char *protocol = virStorageNetProtocolTypeToString(src->protocol);
@@ -781,7 +790,7 @@ qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src)
             return NULL;
     }
 
-    if (src->auth) {
+    if (!onlytarget && src->auth) {
         username = src->auth->username;
         objalias = srcPriv->secinfo->s.aes.alias;
     }
@@ -801,9 +810,11 @@ qemuBlockStorageSourceGetISCSIProps(virStorageSourcePtr src)
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src,
+                                  bool onlytarget)
 {
     VIR_AUTOPTR(virJSONValue) serverprops = NULL;
+    const char *tlsAlias = src->tlsAlias;
     virJSONValuePtr ret = NULL;
 
     if (src->nhosts != 1) {
@@ -817,11 +828,14 @@ qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src)
     if (!serverprops)
         return NULL;
 
+    if (onlytarget)
+        tlsAlias = NULL;
+
     if (virJSONValueObjectCreate(&ret,
                                  "s:driver", "nbd",
                                  "a:server", &serverprops,
                                  "S:export", src->path,
-                                 "S:tls-creds", src->tlsAlias,
+                                 "S:tls-creds", tlsAlias,
                                  NULL) < 0)
         return NULL;
 
@@ -830,7 +844,8 @@ qemuBlockStorageSourceGetNBDProps(virStorageSourcePtr src)
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src,
+                                  bool onlytarget)
 {
     qemuDomainStorageSourcePrivatePtr srcPriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
     VIR_AUTOPTR(virJSONValue) servers = NULL;
@@ -844,7 +859,7 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src)
         !(servers = qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(src)))
         return NULL;
 
-    if (src->auth) {
+    if (!onlytarget && src->auth) {
         username = srcPriv->secinfo->s.aes.username;
         keysecret = srcPriv->secinfo->s.aes.alias;
         /* the auth modes are modelled after our old command line generator */
@@ -943,16 +958,14 @@ qemuBlockStorageSourceGetSshProps(virStorageSourcePtr src)
 
 
 static virJSONValuePtr
-qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src)
+qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src,
+                                   bool onlytarget)
 {
     const char *driver = "file";
     const char *iomode = NULL;
     const char *prManagerAlias = NULL;
     virJSONValuePtr ret = NULL;
 
-    if (src->iomode != VIR_DOMAIN_DISK_IO_DEFAULT)
-        iomode = virDomainDiskIoTypeToString(src->iomode);
-
     if (virStorageSourceIsBlockLocal(src)) {
         if (src->hostcdrom)
             driver = "host_cdrom";
@@ -960,8 +973,13 @@ qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src)
             driver = "host_device";
     }
 
-    if (src->pr)
-        prManagerAlias = src->pr->mgralias;
+    if (!onlytarget) {
+        if (src->pr)
+            prManagerAlias = src->pr->mgralias;
+
+        if (src->iomode != VIR_DOMAIN_DISK_IO_DEFAULT)
+            iomode = virDomainDiskIoTypeToString(src->iomode);
+    }
 
     ignore_value(virJSONValueObjectCreate(&ret,
                                           "s:driver", driver,
@@ -974,21 +992,26 @@ qemuBlockStorageSourceGetFileProps(virStorageSourcePtr src)
 
 
 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:
      * '*fat-type': 'int'
      * '*label': 'str'
      */
-    ignore_value(virJSONValueObjectCreate(&ret,
-                                          "s:driver", "vvfat",
-                                          "s:dir", src->path,
-                                          "b:floppy", src->floppyimg,
-                                          "b:rw", !src->readonly, NULL));
+    i(virJSONValueObjectCreate(&ret,
+                                 "s:driver", "vvfat",
+                                 "s:dir", src->path,
+                                 "b:floppy", src->floppyimg, NULL) < 0)
+        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:
  * @src: disk source
  * @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
  * storage source. Returns NULL on error and reports an appropriate error message.
  */
 virJSONValuePtr
 qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
-                                      bool legacy)
+                                      bool legacy,
+                                      bool onlytarget)
 {
     int actualType = virStorageSourceGetActualType(src);
     VIR_AUTOPTR(virJSONValue) fileprops = NULL;
@@ -1038,14 +1063,14 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
     switch ((virStorageType)actualType) {
     case VIR_STORAGE_TYPE_BLOCK:
     case VIR_STORAGE_TYPE_FILE:
-        if (!(fileprops = qemuBlockStorageSourceGetFileProps(src)))
+        if (!(fileprops = qemuBlockStorageSourceGetFileProps(src, onlytarget)))
             return NULL;
         break;
 
     case VIR_STORAGE_TYPE_DIR:
         /* qemu handles directories by exposing them as a device with emulated
          * FAT filesystem */
-        if (!(fileprops = qemuBlockStorageSourceGetVvfatProps(src)))
+        if (!(fileprops = qemuBlockStorageSourceGetVvfatProps(src, onlytarget)))
             return NULL;
         break;
 
@@ -1057,12 +1082,12 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
     case VIR_STORAGE_TYPE_NETWORK:
         switch ((virStorageNetProtocol) src->protocol) {
         case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
-            if (!(fileprops = qemuBlockStorageSourceGetGlusterProps(src, legacy)))
+            if (!(fileprops = qemuBlockStorageSourceGetGlusterProps(src, legacy, onlytarget)))
                 return NULL;
             break;
 
         case VIR_STORAGE_NET_PROTOCOL_VXHS:
-            if (!(fileprops = qemuBlockStorageSourceGetVxHSProps(src)))
+            if (!(fileprops = qemuBlockStorageSourceGetVxHSProps(src, onlytarget)))
                 return NULL;
             break;
 
@@ -1071,22 +1096,22 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
         case VIR_STORAGE_NET_PROTOCOL_FTP:
         case VIR_STORAGE_NET_PROTOCOL_FTPS:
         case VIR_STORAGE_NET_PROTOCOL_TFTP:
-            if (!(fileprops = qemuBlockStorageSourceGetCURLProps(src)))
+            if (!(fileprops = qemuBlockStorageSourceGetCURLProps(src, onlytarget)))
                 return NULL;
             break;
 
         case VIR_STORAGE_NET_PROTOCOL_ISCSI:
-            if (!(fileprops = qemuBlockStorageSourceGetISCSIProps(src)))
+            if (!(fileprops = qemuBlockStorageSourceGetISCSIProps(src, onlytarget)))
                 return NULL;
             break;
 
         case VIR_STORAGE_NET_PROTOCOL_NBD:
-            if (!(fileprops = qemuBlockStorageSourceGetNBDProps(src)))
+            if (!(fileprops = qemuBlockStorageSourceGetNBDProps(src, onlytarget)))
                 return NULL;
             break;
 
         case VIR_STORAGE_NET_PROTOCOL_RBD:
-            if (!(fileprops = qemuBlockStorageSourceGetRBDProps(src)))
+            if (!(fileprops = qemuBlockStorageSourceGetRBDProps(src, onlytarget)))
                 return NULL;
             break;
 
@@ -1107,19 +1132,21 @@ qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
         break;
     }
 
-    if (qemuBlockNodeNameValidate(src->nodestorage) < 0 ||
-        virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0)
-        return NULL;
-
-    if (!legacy) {
-        if (qemuBlockStorageSourceGetBlockdevGetCacheProps(src, fileprops) < 0)
+    if (!onlytarget) {
+        if (qemuBlockNodeNameValidate(src->nodestorage) < 0 ||
+            virJSONValueObjectAdd(fileprops, "S:node-name", src->nodestorage, NULL) < 0)
             return NULL;
 
-        if (virJSONValueObjectAdd(fileprops,
-                                  "b:read-only", src->readonly,
-                                  "s:discard", "unmap",
-                                  NULL) < 0)
-            return NULL;
+        if (!legacy) {
+            if (qemuBlockStorageSourceGetBlockdevGetCacheProps(src, fileprops) < 0)
+                return NULL;
+
+            if (virJSONValueObjectAdd(fileprops,
+                                      "b:read-only", src->readonly,
+                                      "s:discard", "unmap",
+                                      NULL) < 0)
+                return NULL;
+        }
     }
 
     VIR_RETURN_PTR(fileprops);
@@ -1440,7 +1467,7 @@ qemuBlockStorageSourceAttachPrepareBlockdev(virStorageSourcePtr src)
         return NULL;
 
     if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src)) ||
-        !(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false)))
+        !(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false, false)))
         return NULL;
 
     data->storageNodeName = src->nodestorage;
index 934a1f125d770cb3e9408419bf5d2f1b5dcd59b3..7b94f4f0ff019fe9554f8b6a8b1f2a06e257f517 100644 (file)
@@ -58,7 +58,8 @@ qemuBlockStorageSourceSupportsConcurrentAccess(virStorageSourcePtr src);
 
 virJSONValuePtr
 qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
-                                      bool legacy);
+                                      bool legacy,
+                                      bool onlytarget);
 
 virURIPtr
 qemuBlockStorageSourceGetURI(virStorageSourcePtr src);
index d8115a674a0cc2049e4468d758775faa4317a489..eb2598119ef08df5d057ee01b43464e77bd925b1 100644 (file)
@@ -1602,7 +1602,7 @@ qemuDiskSourceGetProps(virStorageSourcePtr src)
     virJSONValuePtr props;
     virJSONValuePtr ret;
 
-    if (!(props = qemuBlockStorageSourceGetBackendProps(src, true)))
+    if (!(props = qemuBlockStorageSourceGetBackendProps(src, true, false)))
         return NULL;
 
     if (virJSONValueObjectCreate(&ret, "a:file", &props, NULL) < 0) {
index 21db3e4881f07b7d407ffe9d25e51f5513c2a3a7..29ce27021f69d7d81d0ee540e482658759d05c2a 100644 (file)
@@ -67,7 +67,7 @@ testBackingXMLjsonXML(const void *args)
         return -1;
     }
 
-    if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc, true))) {
+    if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc, true, false))) {
         fprintf(stderr, "failed to format disk source json\n");
         return -1;
     }
@@ -213,7 +213,7 @@ testQemuDiskXMLToProps(const void *opaque)
             goto cleanup;
 
         if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n)) ||
-            !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false))) {
+            !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false))) {
             if (!data->fail) {
                 VIR_TEST_VERBOSE("failed to generate qemu blockdev props\n");
                 goto cleanup;