c401cc
From 3460bd35cb5a9fdd08af1c0cb266f67d4336e494 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <3460bd35cb5a9fdd08af1c0cb266f67d4336e494@dist-git>
c401cc
From: Peter Krempa <pkrempa@redhat.com>
c401cc
Date: Wed, 26 Feb 2014 14:55:00 +0100
c401cc
Subject: [PATCH] qemu: Unify formatting of RBD sources
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1032370
c401cc
c401cc
(cherry picked from commit b384e2b4d71702abb45c749b7d738870cef5d9b7)
c401cc
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 src/qemu/qemu_command.c | 150 ++++++++++++++++++++----------------------------
c401cc
 1 file changed, 61 insertions(+), 89 deletions(-)
c401cc
c401cc
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
c401cc
index c1dab06..e1af654 100644
c401cc
--- a/src/qemu/qemu_command.c
c401cc
+++ b/src/qemu/qemu_command.c
c401cc
@@ -3173,72 +3173,6 @@ cleanup:
c401cc
     return secret;
c401cc
 }
c401cc
 
c401cc
-static int
c401cc
-qemuBuildRBDString(virConnectPtr conn,
c401cc
-                   virDomainDiskDefPtr disk,
c401cc
-                   virBufferPtr opt)
c401cc
-{
c401cc
-    size_t i;
c401cc
-    int ret = 0;
c401cc
-    char *secret = NULL;
c401cc
-
c401cc
-    if (strchr(disk->src, ':')) {
c401cc
-        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
c401cc
-                       _("':' not allowed in RBD source volume name '%s'"),
c401cc
-                       disk->src);
c401cc
-        return -1;
c401cc
-    }
c401cc
-
c401cc
-    virBufferEscape(opt, ',', ",", "rbd:%s", disk->src);
c401cc
-    if (disk->auth.username) {
c401cc
-        virBufferEscape(opt, '\\', ":", ":id=%s", disk->auth.username);
c401cc
-        /* Get the secret string using the virDomainDiskDef
c401cc
-         * NOTE: qemu/librbd wants it base64 encoded
c401cc
-         */
c401cc
-        if (!(secret = qemuGetSecretString(conn, "rbd", true,
c401cc
-                                           disk->auth.secretType,
c401cc
-                                           disk->auth.username,
c401cc
-                                           disk->auth.secret.uuid,
c401cc
-                                           disk->auth.secret.usage,
c401cc
-                                           VIR_SECRET_USAGE_TYPE_CEPH)))
c401cc
-            goto error;
c401cc
-
c401cc
-
c401cc
-        virBufferEscape(opt, '\\', ":",
c401cc
-                        ":key=%s:auth_supported=cephx\\;none",
c401cc
-                        secret);
c401cc
-    } else {
c401cc
-        virBufferAddLit(opt, ":auth_supported=none");
c401cc
-    }
c401cc
-
c401cc
-    if (disk->nhosts > 0) {
c401cc
-        virBufferAddLit(opt, ":mon_host=");
c401cc
-        for (i = 0; i < disk->nhosts; ++i) {
c401cc
-            if (i) {
c401cc
-                virBufferAddLit(opt, "\\;");
c401cc
-            }
c401cc
-
c401cc
-            /* assume host containing : is ipv6 */
c401cc
-            if (strchr(disk->hosts[i].name, ':')) {
c401cc
-                virBufferEscape(opt, '\\', ":", "[%s]", disk->hosts[i].name);
c401cc
-            } else {
c401cc
-                virBufferAsprintf(opt, "%s", disk->hosts[i].name);
c401cc
-            }
c401cc
-            if (disk->hosts[i].port) {
c401cc
-                virBufferAsprintf(opt, "\\:%s", disk->hosts[i].port);
c401cc
-            }
c401cc
-        }
c401cc
-    }
c401cc
-
c401cc
-cleanup:
c401cc
-    VIR_FREE(secret);
c401cc
-
c401cc
-    return ret;
c401cc
-
c401cc
-error:
c401cc
-    ret = -1;
c401cc
-    goto cleanup;
c401cc
-}
c401cc
 
c401cc
 static int qemuAddRBDHost(virDomainDiskDefPtr disk, char *hostport)
c401cc
 {
c401cc
@@ -3599,6 +3533,7 @@ qemuBuildNetworkDriveURI(int protocol,
c401cc
     char *ret = NULL;
c401cc
     virBuffer buf = VIR_BUFFER_INITIALIZER;
c401cc
     virURIPtr uri = NULL;
c401cc
+    size_t i;
c401cc
 
c401cc
     switch ((enum virDomainDiskProtocol) protocol) {
c401cc
         case VIR_DOMAIN_DISK_PROTOCOL_NBD:
c401cc
@@ -3736,10 +3671,51 @@ qemuBuildNetworkDriveURI(int protocol,
c401cc
             break;
c401cc
 
c401cc
         case VIR_DOMAIN_DISK_PROTOCOL_RBD:
c401cc
+            if (strchr(src, ':')) {
c401cc
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
c401cc
+                               _("':' not allowed in RBD source volume name '%s'"),
c401cc
+                               src);
c401cc
+                goto cleanup;
c401cc
+            }
c401cc
+
c401cc
+            virBufferStrcat(&buf, "rbd:", src, NULL);
c401cc
+
c401cc
+            if (username) {
c401cc
+                virBufferEscape(&buf, '\\', ":", ":id=%s", username);
c401cc
+                virBufferEscape(&buf, '\\', ":",
c401cc
+                                ":key=%s:auth_supported=cephx\\;none",
c401cc
+                                secret);
c401cc
+            } else {
c401cc
+                virBufferAddLit(&buf, ":auth_supported=none");
c401cc
+            }
c401cc
+
c401cc
+            if (nhosts > 0) {
c401cc
+                virBufferAddLit(&buf, ":mon_host=");
c401cc
+                for (i = 0; i < nhosts; i++) {
c401cc
+                    if (i)
c401cc
+                        virBufferAddLit(&buf, "\\;");
c401cc
+
c401cc
+                    /* assume host containing : is ipv6 */
c401cc
+                    if (strchr(hosts[i].name, ':'))
c401cc
+                        virBufferEscape(&buf, '\\', ":", "[%s]", hosts[i].name);
c401cc
+                    else
c401cc
+                        virBufferAsprintf(&buf, "%s", hosts[i].name);
c401cc
+
c401cc
+                    if (hosts[i].port)
c401cc
+                        virBufferAsprintf(&buf, "\\:%s", hosts[i].port);
c401cc
+                }
c401cc
+            }
c401cc
+
c401cc
+            if (virBufferError(&buf) < 0) {
c401cc
+                virReportOOMError();
c401cc
+                goto cleanup;
c401cc
+            }
c401cc
+
c401cc
+            ret = virBufferContentAndReset(&buf;;
c401cc
+            break;
c401cc
+
c401cc
+
c401cc
         case VIR_DOMAIN_DISK_PROTOCOL_LAST:
c401cc
-            virReportError(VIR_ERR_INTERNAL_ERROR,
c401cc
-                           _("network disk protocol '%s' not supported"),
c401cc
-                           virDomainDiskProtocolTypeToString(protocol));
c401cc
             goto cleanup;
c401cc
     }
c401cc
 
c401cc
@@ -3762,17 +3738,26 @@ qemuBuildDriveURIString(virConnectPtr conn,
c401cc
 
c401cc
     virBufferAddLit(opt, "file=");
c401cc
 
c401cc
-    if (disk->protocol == VIR_DOMAIN_DISK_PROTOCOL_ISCSI &&
c401cc
+    if ((disk->protocol == VIR_DOMAIN_DISK_PROTOCOL_ISCSI ||
c401cc
+         disk->protocol == VIR_DOMAIN_DISK_PROTOCOL_RBD) &&
c401cc
         disk->auth.username) {
c401cc
-        /* Get the secret string using the virDomainDiskDef */
c401cc
+        bool encode = false;
c401cc
+        int secretType = VIR_SECRET_USAGE_TYPE_ISCSI;
c401cc
+
c401cc
+        if (disk->protocol == VIR_DOMAIN_DISK_PROTOCOL_RBD) {
c401cc
+            /* qemu requires the secret to be encoded for RBD */
c401cc
+            encode = true;
c401cc
+            secretType = VIR_SECRET_USAGE_TYPE_CEPH;
c401cc
+        }
c401cc
+
c401cc
         if (!(secret = qemuGetSecretString(conn,
c401cc
                                            virDomainDiskProtocolTypeToString(disk->protocol),
c401cc
-                                           false,
c401cc
+                                           encode,
c401cc
                                            disk->auth.secretType,
c401cc
                                            disk->auth.username,
c401cc
                                            disk->auth.secret.uuid,
c401cc
                                            disk->auth.secret.usage,
c401cc
-                                           VIR_SECRET_USAGE_TYPE_ISCSI)))
c401cc
+                                           secretType)))
c401cc
             goto cleanup;
c401cc
     }
c401cc
 
c401cc
@@ -3918,23 +3903,10 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
c401cc
                                 disk->src);
c401cc
             else
c401cc
                 virBufferEscape(&opt, ',', ",", "file=fat:%s,", disk->src);
c401cc
-        } else if (actualType == VIR_DOMAIN_DISK_TYPE_NETWORK) {
c401cc
-            switch (disk->protocol) {
c401cc
-            case VIR_DOMAIN_DISK_PROTOCOL_RBD:
c401cc
-                virBufferAddLit(&opt, "file=");
c401cc
-                if (qemuBuildRBDString(conn, disk, &opt) < 0)
c401cc
-                    goto error;
c401cc
-                virBufferAddChar(&opt, ',');
c401cc
-                break;
c401cc
 
c401cc
-            case VIR_DOMAIN_DISK_PROTOCOL_NBD:
c401cc
-            case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
c401cc
-            case VIR_DOMAIN_DISK_PROTOCOL_GLUSTER:
c401cc
-            case VIR_DOMAIN_DISK_PROTOCOL_ISCSI:
c401cc
-                if (qemuBuildDriveURIString(conn, disk, &opt) < 0)
c401cc
-                    goto error;
c401cc
-                break;
c401cc
-            }
c401cc
+        } else if (actualType == VIR_DOMAIN_DISK_TYPE_NETWORK) {
c401cc
+            if (qemuBuildDriveURIString(conn, disk, &opt) < 0)
c401cc
+                goto error;
c401cc
         } else {
c401cc
             if ((actualType == VIR_DOMAIN_DISK_TYPE_BLOCK) &&
c401cc
                 (disk->tray_status == VIR_DOMAIN_DISK_TRAY_OPEN)) {
c401cc
-- 
c401cc
1.9.0
c401cc