[PATCH v8 2/3] conf: Support to parse rbd namespace from source name

Han Han hhan at redhat.com
Wed May 26 13:35:11 UTC 2021


Signed-off-by: Han Han <hhan at redhat.com>
---
 docs/formatdomain.rst          | 16 ++++++++++++
 src/conf/domain_conf.c         | 47 +++++++++++++++++++++++++++++++---
 src/conf/storage_source_conf.c |  2 ++
 src/conf/storage_source_conf.h |  1 +
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/docs/formatdomain.rst b/docs/formatdomain.rst
index aa7bb8da14..cb3914d1bb 100644
--- a/docs/formatdomain.rst
+++ b/docs/formatdomain.rst
@@ -2479,6 +2479,16 @@ paravirtualized driver is specified via the ``disk`` element.
        </source>
        <target dev='vdf' bus='virtio'/>
      </disk>
+     <disk type='network'>
+       <driver name="qemu" type="raw"/>
+       <source protocol="rbd" name="pool/namespace/image">
+         <host name="hostname" port="7000"/>
+         <auth username='myuser'>
+           <secret type='ceph' usage='mypassid'/>
+         </auth>
+       </source>
+       <target dev="vdg" bus="virtio"/>
+     </disk>
    </devices>
    ...
 
@@ -2570,6 +2580,12 @@ paravirtualized driver is specified via the ``disk`` element.
       the host by the ``nbd_tls`` and ``nbd_tls_x509_cert_dir`` in
       /etc/libvirt/qemu.conf. ('tls' :since:`Since 4.5.0` )
 
+      For "rbd", the ``name`` attribute could be two formats: the format of
+      ``pool_name/image_name`` includes the rbd pool name and image name with
+      default rbd pool namespace; for the customized namespace, the format is
+      ``pool_name/namespace/image_name`` ( :since:`Since 6.9.0 and QEMU 5.0` ).
+      The pool name, namespace and image are separated by slash.
+
       For protocols ``http`` and ``https`` an optional attribute ``query``
       specifies the query string. ( :since:`Since 6.2.0` )
 
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6d90041bf8..eac59dd00e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8197,6 +8197,7 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node,
 {
     virStorageNetProtocol protocol;
     xmlNodePtr tmpnode;
+    char **tmp_split_paths;
 
     if (virXMLPropEnum(node, "protocol", virStorageNetProtocolTypeFromString,
                        VIR_XML_PROP_REQUIRED, &protocol) < 0)
@@ -8226,8 +8227,7 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node,
     /* for historical reasons we store the volume and image name in one XML
      * element although it complicates thing when attempting to access them. */
     if (src->path &&
-        (src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER ||
-         src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD)) {
+        src->protocol == VIR_STORAGE_NET_PROTOCOL_GLUSTER) {
         char *tmp;
         if (!(tmp = strchr(src->path, '/')) ||
             tmp == src->path) {
@@ -8244,6 +8244,41 @@ virDomainDiskSourceNetworkParse(xmlNodePtr node,
         tmp[0] = '\0';
     }
 
+    /* the name of rbd could be <pool>/<image> or <pool>/<namespace>/<image> */
+    if (src->path &&
+        src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD &&
+        (tmp_split_paths = g_strsplit(src->path, "/", 3))) {
+        if (virStringIsEmpty(tmp_split_paths[0]) ||
+            !tmp_split_paths[1] ||
+            STREQ_NULLABLE(tmp_split_paths[2], "") ||
+            (virStringIsEmpty(tmp_split_paths[1]) &&
+             !tmp_split_paths[2])) {
+            virStringListFreeCount(tmp_split_paths, 3);
+            virReportError(VIR_ERR_XML_ERROR,
+                           _("can't split path '%s' into pool name, pool "
+                             "namespace, image name OR pool name, image name"),
+                             src->path);
+            return -1;
+        }
+
+        VIR_FREE(src->path);
+        src->volume = g_strdup(tmp_split_paths[0]);
+        /* the format of <pool>/<image> */
+        if (!tmp_split_paths[2])
+            src->path = g_strdup(tmp_split_paths[1]);
+
+        if (tmp_split_paths[2]) {
+            /* the format of <pool>/<ns>/<image> */
+            if (STRNEQ_NULLABLE(tmp_split_paths[1], ""))
+                src->ns = g_strdup(tmp_split_paths[1]);
+
+            /* the format of <pool>//<image> */
+            src->path = g_strdup(tmp_split_paths[2]);
+        }
+
+        virStringListFreeCount(tmp_split_paths, 3);
+    }
+
     /* snapshot currently works only for remote disks */
     src->snapshot = virXPathString("string(./snapshot/@name)", ctxt);
 
@@ -22948,8 +22983,12 @@ virDomainDiskSourceFormatNetwork(virBuffer *attrBuf,
     virBufferAsprintf(attrBuf, " protocol='%s'",
                       virStorageNetProtocolTypeToString(src->protocol));
 
-    if (src->volume)
-        path = g_strdup_printf("%s/%s", src->volume, src->path);
+    if (src->volume) {
+        if (src->ns)
+            path = g_strdup_printf("%s/%s/%s", src->volume, src->ns, src->path);
+        else
+            path = g_strdup_printf("%s/%s", src->volume, src->path);
+    }
 
     virBufferEscapeString(attrBuf, " name='%s'", path ? path : src->path);
     virBufferEscapeString(attrBuf, " query='%s'", src->query);
diff --git a/src/conf/storage_source_conf.c b/src/conf/storage_source_conf.c
index 5ca06fa30a..7b765d0d81 100644
--- a/src/conf/storage_source_conf.c
+++ b/src/conf/storage_source_conf.c
@@ -829,6 +829,7 @@ virStorageSourceCopy(const virStorageSource *src,
 
     def->path = g_strdup(src->path);
     def->volume = g_strdup(src->volume);
+    def->ns = g_strdup(src->ns);
     def->relPath = g_strdup(src->relPath);
     def->backingStoreRaw = g_strdup(src->backingStoreRaw);
     def->backingStoreRawFormat = src->backingStoreRawFormat;
@@ -1115,6 +1116,7 @@ virStorageSourceClear(virStorageSource *def)
 
     VIR_FREE(def->path);
     VIR_FREE(def->volume);
+    VIR_FREE(def->ns);
     VIR_FREE(def->snapshot);
     VIR_FREE(def->configFile);
     VIR_FREE(def->query);
diff --git a/src/conf/storage_source_conf.h b/src/conf/storage_source_conf.h
index 389c7b56d1..dcc643ceba 100644
--- a/src/conf/storage_source_conf.h
+++ b/src/conf/storage_source_conf.h
@@ -275,6 +275,7 @@ struct _virStorageSource {
     char *snapshot; /* for storage systems supporting internal snapshots */
     char *configFile; /* some storage systems use config file as part of
                          the source definition */
+    char *ns; /* for the storage systems supporting namespace */
     char *query; /* query string for HTTP based protocols */
     size_t nhosts;
     virStorageNetHostDef *hosts;
-- 
2.31.1




More information about the libvir-list mailing list