Blob Blame History Raw
From fc5386bf423e746c5263060032f0385424e6db25 Mon Sep 17 00:00:00 2001
Message-Id: <fc5386bf423e746c5263060032f0385424e6db25@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 26 Feb 2014 14:54:59 +0100
Subject: [PATCH] qemu: Split out NBD command generation

https://bugzilla.redhat.com/show_bug.cgi?id=1032370

(cherry picked from commit d94fd0c9c245ca30b1c70a9bb854e81ee5ab37da)

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_command.c | 117 +++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 56 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index f45b681..c1dab06 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3586,7 +3586,7 @@ qemuNetworkDriveGetPort(int protocol,
     return -1;
 }
 
-
+#define QEMU_DEFAULT_NBD_PORT "10809"
 
 char *
 qemuBuildNetworkDriveURI(int protocol,
@@ -3597,12 +3597,69 @@ qemuBuildNetworkDriveURI(int protocol,
                          const char *secret)
 {
     char *ret = NULL;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
     virURIPtr uri = NULL;
 
     switch ((enum virDomainDiskProtocol) protocol) {
+        case VIR_DOMAIN_DISK_PROTOCOL_NBD:
+            if (nhosts != 1) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("protocol '%s' accepts only one host"),
+                               virDomainDiskProtocolTypeToString(protocol));
+                goto cleanup;
+            }
+
+            if (!((hosts->name && strchr(hosts->name, ':')) ||
+                  (hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_TCP &&
+                   !hosts->name) ||
+                  (hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_UNIX &&
+                   hosts->socket &&
+                   hosts->socket[0] != '/'))) {
+
+                virBufferAddLit(&buf, "nbd:");
+
+                switch (hosts->transport) {
+                case VIR_DOMAIN_DISK_PROTO_TRANS_TCP:
+                    virBufferStrcat(&buf, hosts->name, NULL);
+                    virBufferAsprintf(&buf, ":%s",
+                                      hosts->port ? hosts->port :
+                                      QEMU_DEFAULT_NBD_PORT);
+                    break;
+
+                case VIR_DOMAIN_DISK_PROTO_TRANS_UNIX:
+                    if (!hosts->socket) {
+                        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                                       _("socket attribute required for "
+                                         "unix transport"));
+                        goto cleanup;
+                    }
+
+                    virBufferAsprintf(&buf, "unix:%s", hosts->socket);
+                    break;
+
+                default:
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("nbd does not support transport '%s'"),
+                                   virDomainDiskProtocolTransportTypeToString(hosts->transport));
+                    goto cleanup;
+                }
+
+                if (src)
+                    virBufferAsprintf(&buf, ":exportname=%s", src);
+
+                if (virBufferError(&buf) < 0) {
+                    virReportOOMError();
+                    goto cleanup;
+                }
+
+                ret = virBufferContentAndReset(&buf);
+                goto cleanup;
+            }
+            /* fallthrough */
+            /* NBD code uses same formatting scheme as others in some cases */
+
         case VIR_DOMAIN_DISK_PROTOCOL_ISCSI:
         case VIR_DOMAIN_DISK_PROTOCOL_GLUSTER:
-        case VIR_DOMAIN_DISK_PROTOCOL_NBD:
             if (nhosts != 1) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("protocol '%s' accepts only one host"),
@@ -3687,6 +3744,7 @@ qemuBuildNetworkDriveURI(int protocol,
     }
 
 cleanup:
+    virBufferFreeAndReset(&buf);
     virURIFree(uri);
 
     return ret;
@@ -3740,56 +3798,6 @@ cleanup:
 }
 
 
-#define QEMU_DEFAULT_NBD_PORT "10809"
-
-static int
-qemuBuildNBDString(virConnectPtr conn, virDomainDiskDefPtr disk, virBufferPtr opt)
-{
-    const char *transp;
-
-    if (disk->nhosts != 1) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("nbd accepts only one host"));
-        return -1;
-    }
-
-    if ((disk->hosts->name && strchr(disk->hosts->name, ':')) ||
-        (disk->hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_TCP && !disk->hosts->name) ||
-        (disk->hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_UNIX && disk->hosts->socket && disk->hosts->socket[0] != '/'))
-        return qemuBuildDriveURIString(conn, disk, opt);
-
-    virBufferAddLit(opt, "file=nbd:");
-
-    switch (disk->hosts->transport) {
-    case VIR_DOMAIN_DISK_PROTO_TRANS_TCP:
-        if (disk->hosts->name)
-            virBufferEscape(opt, ',', ",", "%s", disk->hosts->name);
-        virBufferEscape(opt, ',', ",", ":%s",
-                        disk->hosts->port ? disk->hosts->port :
-                        QEMU_DEFAULT_NBD_PORT);
-        break;
-    case VIR_DOMAIN_DISK_PROTO_TRANS_UNIX:
-        if (!disk->hosts->socket) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("socket attribute required for unix transport"));
-            return -1;
-        }
-        virBufferEscape(opt, ',', ",", "unix:%s", disk->hosts->socket);
-        break;
-    default:
-        transp = virDomainDiskProtocolTransportTypeToString(disk->hosts->transport);
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("nbd does not support transport '%s'"), transp);
-        break;
-    }
-
-    if (disk->src)
-        virBufferEscape(opt, ',', ",", ":exportname=%s", disk->src);
-
-    virBufferAddChar(opt, ',');
-
-    return 0;
-}
 
 
 char *
@@ -3912,10 +3920,6 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
                 virBufferEscape(&opt, ',', ",", "file=fat:%s,", disk->src);
         } else if (actualType == VIR_DOMAIN_DISK_TYPE_NETWORK) {
             switch (disk->protocol) {
-            case VIR_DOMAIN_DISK_PROTOCOL_NBD:
-                if (qemuBuildNBDString(conn, disk, &opt) < 0)
-                    goto error;
-                break;
             case VIR_DOMAIN_DISK_PROTOCOL_RBD:
                 virBufferAddLit(&opt, "file=");
                 if (qemuBuildRBDString(conn, disk, &opt) < 0)
@@ -3923,6 +3927,7 @@ qemuBuildDriveStr(virConnectPtr conn ATTRIBUTE_UNUSED,
                 virBufferAddChar(&opt, ',');
                 break;
 
+            case VIR_DOMAIN_DISK_PROTOCOL_NBD:
             case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
             case VIR_DOMAIN_DISK_PROTOCOL_GLUSTER:
             case VIR_DOMAIN_DISK_PROTOCOL_ISCSI:
-- 
1.9.0