Blame SOURCES/libvirt-qemu-Refactor-qemuBuildNetworkDriveURI-to-take-a-virStorageSourcePtr.patch

9119d9
From ad0a3115376560248d7e1a5bfac20e8c5912c740 Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <ad0a3115376560248d7e1a5bfac20e8c5912c740@dist-git>
9119d9
From: Peter Krempa <pkrempa@redhat.com>
9119d9
Date: Fri, 21 Nov 2014 15:04:06 +0100
9119d9
Subject: [PATCH] qemu: Refactor qemuBuildNetworkDriveURI to take a
9119d9
 virStorageSourcePtr
9119d9
9119d9
https://bugzilla.redhat.com/show_bug.cgi?id=1164528
9119d9
9119d9
Instead of splitting out various fields, pass the complete structure and
9119d9
let the function pick various things of it.
9119d9
9119d9
As one of the callers isn't using virStorageSourcePtr to store the data,
9119d9
this patch adds glue code that fills the data into a dummy
9119d9
virStorageSourcePtr before calling the func.
9119d9
9119d9
This change will help when adding new fields that need output processing
9119d9
in the future.
9119d9
9119d9
(cherry picked from commit dc0175f5359d995db955974457f3a00c92ffde35)
9119d9
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/qemu/qemu_command.c | 129 +++++++++++++++++++++++-------------------------
9119d9
 1 file changed, 62 insertions(+), 67 deletions(-)
9119d9
9119d9
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
9119d9
index c161824..557c985 100644
9119d9
--- a/src/qemu/qemu_command.c
9119d9
+++ b/src/qemu/qemu_command.c
9119d9
@@ -2939,11 +2939,7 @@ qemuNetworkDriveGetPort(int protocol,
9119d9
 #define QEMU_DEFAULT_NBD_PORT "10809"
9119d9
 
9119d9
 static char *
9119d9
-qemuBuildNetworkDriveURI(int protocol,
9119d9
-                         const char *src,
9119d9
-                         const char *volume,
9119d9
-                         size_t nhosts,
9119d9
-                         virStorageNetHostDefPtr hosts,
9119d9
+qemuBuildNetworkDriveURI(virStorageSourcePtr src,
9119d9
                          const char *username,
9119d9
                          const char *secret)
9119d9
 {
9119d9
@@ -2952,52 +2948,52 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
     virURIPtr uri = NULL;
9119d9
     size_t i;
9119d9
 
9119d9
-    switch ((virStorageNetProtocol) protocol) {
9119d9
+    switch ((virStorageNetProtocol) src->protocol) {
9119d9
         case VIR_STORAGE_NET_PROTOCOL_NBD:
9119d9
-            if (nhosts != 1) {
9119d9
+            if (src->nhosts != 1) {
9119d9
                 virReportError(VIR_ERR_INTERNAL_ERROR,
9119d9
                                _("protocol '%s' accepts only one host"),
9119d9
-                               virStorageNetProtocolTypeToString(protocol));
9119d9
+                               virStorageNetProtocolTypeToString(src->protocol));
9119d9
                 goto cleanup;
9119d9
             }
9119d9
 
9119d9
-            if (!((hosts->name && strchr(hosts->name, ':')) ||
9119d9
-                  (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP &&
9119d9
-                   !hosts->name) ||
9119d9
-                  (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX &&
9119d9
-                   hosts->socket &&
9119d9
-                   hosts->socket[0] != '/'))) {
9119d9
+            if (!((src->hosts->name && strchr(src->hosts->name, ':')) ||
9119d9
+                  (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP &&
9119d9
+                   !src->hosts->name) ||
9119d9
+                  (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX &&
9119d9
+                   src->hosts->socket &&
9119d9
+                   src->hosts->socket[0] != '/'))) {
9119d9
 
9119d9
                 virBufferAddLit(&buf, "nbd:");
9119d9
 
9119d9
-                switch (hosts->transport) {
9119d9
+                switch (src->hosts->transport) {
9119d9
                 case VIR_STORAGE_NET_HOST_TRANS_TCP:
9119d9
-                    virBufferStrcat(&buf, hosts->name, NULL);
9119d9
+                    virBufferStrcat(&buf, src->hosts->name, NULL);
9119d9
                     virBufferAsprintf(&buf, ":%s",
9119d9
-                                      hosts->port ? hosts->port :
9119d9
+                                      src->hosts->port ? src->hosts->port :
9119d9
                                       QEMU_DEFAULT_NBD_PORT);
9119d9
                     break;
9119d9
 
9119d9
                 case VIR_STORAGE_NET_HOST_TRANS_UNIX:
9119d9
-                    if (!hosts->socket) {
9119d9
+                    if (!src->hosts->socket) {
9119d9
                         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
                                        _("socket attribute required for "
9119d9
                                          "unix transport"));
9119d9
                         goto cleanup;
9119d9
                     }
9119d9
 
9119d9
-                    virBufferAsprintf(&buf, "unix:%s", hosts->socket);
9119d9
+                    virBufferAsprintf(&buf, "unix:%s", src->hosts->socket);
9119d9
                     break;
9119d9
 
9119d9
                 default:
9119d9
                     virReportError(VIR_ERR_INTERNAL_ERROR,
9119d9
                                    _("nbd does not support transport '%s'"),
9119d9
-                                   virStorageNetHostTransportTypeToString(hosts->transport));
9119d9
+                                   virStorageNetHostTransportTypeToString(src->hosts->transport));
9119d9
                     goto cleanup;
9119d9
                 }
9119d9
 
9119d9
-                if (src)
9119d9
-                    virBufferAsprintf(&buf, ":exportname=%s", src);
9119d9
+                if (src->path)
9119d9
+                    virBufferAsprintf(&buf, ":exportname=%s", src->path);
9119d9
 
9119d9
                 if (virBufferCheckError(&buf) < 0)
9119d9
                     goto cleanup;
9119d9
@@ -3015,45 +3011,45 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
         case VIR_STORAGE_NET_PROTOCOL_TFTP:
9119d9
         case VIR_STORAGE_NET_PROTOCOL_ISCSI:
9119d9
         case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
9119d9
-            if (nhosts != 1) {
9119d9
+            if (src->nhosts != 1) {
9119d9
                 virReportError(VIR_ERR_INTERNAL_ERROR,
9119d9
                                _("protocol '%s' accepts only one host"),
9119d9
-                               virStorageNetProtocolTypeToString(protocol));
9119d9
+                               virStorageNetProtocolTypeToString(src->protocol));
9119d9
                 goto cleanup;
9119d9
             }
9119d9
 
9119d9
             if (VIR_ALLOC(uri) < 0)
9119d9
                 goto cleanup;
9119d9
 
9119d9
-            if (hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
9119d9
+            if (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
9119d9
                 if (VIR_STRDUP(uri->scheme,
9119d9
-                               virStorageNetProtocolTypeToString(protocol)) < 0)
9119d9
+                               virStorageNetProtocolTypeToString(src->protocol)) < 0)
9119d9
                     goto cleanup;
9119d9
             } else {
9119d9
                 if (virAsprintf(&uri->scheme, "%s+%s",
9119d9
-                                virStorageNetProtocolTypeToString(protocol),
9119d9
-                                virStorageNetHostTransportTypeToString(hosts->transport)) < 0)
9119d9
+                                virStorageNetProtocolTypeToString(src->protocol),
9119d9
+                                virStorageNetHostTransportTypeToString(src->hosts->transport)) < 0)
9119d9
                     goto cleanup;
9119d9
             }
9119d9
 
9119d9
-            if ((uri->port = qemuNetworkDriveGetPort(protocol, hosts->port)) < 0)
9119d9
+            if ((uri->port = qemuNetworkDriveGetPort(src->protocol, src->hosts->port)) < 0)
9119d9
                 goto cleanup;
9119d9
 
9119d9
-            if (src) {
9119d9
-                if (volume) {
9119d9
+            if (src->path) {
9119d9
+                if (src->volume) {
9119d9
                     if (virAsprintf(&uri->path, "/%s%s",
9119d9
-                                    volume, src) < 0)
9119d9
+                                    src->volume, src->path) < 0)
9119d9
                         goto cleanup;
9119d9
                 } else {
9119d9
                     if (virAsprintf(&uri->path, "%s%s",
9119d9
-                                    src[0] == '/' ? "" : "/",
9119d9
-                                    src) < 0)
9119d9
+                                    src->path[0] == '/' ? "" : "/",
9119d9
+                                    src->path) < 0)
9119d9
                         goto cleanup;
9119d9
                 }
9119d9
             }
9119d9
 
9119d9
-            if (hosts->socket &&
9119d9
-                virAsprintf(&uri->query, "socket=%s", hosts->socket) < 0)
9119d9
+            if (src->hosts->socket &&
9119d9
+                virAsprintf(&uri->query, "socket=%s", src->hosts->socket) < 0)
9119d9
                 goto cleanup;
9119d9
 
9119d9
             if (username) {
9119d9
@@ -3066,7 +3062,7 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
                 }
9119d9
             }
9119d9
 
9119d9
-            if (VIR_STRDUP(uri->server, hosts->name) < 0)
9119d9
+            if (VIR_STRDUP(uri->server, src->hosts->name) < 0)
9119d9
                 goto cleanup;
9119d9
 
9119d9
             ret = virURIFormat(uri);
9119d9
@@ -3074,20 +3070,20 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
             break;
9119d9
 
9119d9
         case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
9119d9
-            if (!src) {
9119d9
+            if (!src->path) {
9119d9
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
                                _("missing disk source for 'sheepdog' protocol"));
9119d9
                 goto cleanup;
9119d9
             }
9119d9
 
9119d9
-            if (nhosts == 0) {
9119d9
-                if (virAsprintf(&ret, "sheepdog:%s", src) < 0)
9119d9
+            if (src->nhosts == 0) {
9119d9
+                if (virAsprintf(&ret, "sheepdog:%s", src->path) < 0)
9119d9
                     goto cleanup;
9119d9
-            } else if (nhosts == 1) {
9119d9
+            } else if (src->nhosts == 1) {
9119d9
                 if (virAsprintf(&ret, "sheepdog:%s:%s:%s",
9119d9
-                                hosts->name,
9119d9
-                                hosts->port ? hosts->port : "7000",
9119d9
-                                src) < 0)
9119d9
+                                src->hosts->name,
9119d9
+                                src->hosts->port ? src->hosts->port : "7000",
9119d9
+                                src->path) < 0)
9119d9
                     goto cleanup;
9119d9
             } else {
9119d9
                 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
@@ -3098,14 +3094,14 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
             break;
9119d9
 
9119d9
         case VIR_STORAGE_NET_PROTOCOL_RBD:
9119d9
-            if (strchr(src, ':')) {
9119d9
+            if (strchr(src->path, ':')) {
9119d9
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
9119d9
                                _("':' not allowed in RBD source volume name '%s'"),
9119d9
-                               src);
9119d9
+                               src->path);
9119d9
                 goto cleanup;
9119d9
             }
9119d9
 
9119d9
-            virBufferStrcat(&buf, "rbd:", src, NULL);
9119d9
+            virBufferStrcat(&buf, "rbd:", src->path, NULL);
9119d9
 
9119d9
             if (username) {
9119d9
                 virBufferEscape(&buf, '\\', ":", ":id=%s", username);
9119d9
@@ -3116,20 +3112,21 @@ qemuBuildNetworkDriveURI(int protocol,
9119d9
                 virBufferAddLit(&buf, ":auth_supported=none");
9119d9
             }
9119d9
 
9119d9
-            if (nhosts > 0) {
9119d9
+            if (src->nhosts > 0) {
9119d9
                 virBufferAddLit(&buf, ":mon_host=");
9119d9
-                for (i = 0; i < nhosts; i++) {
9119d9
+                for (i = 0; i < src->nhosts; i++) {
9119d9
                     if (i)
9119d9
                         virBufferAddLit(&buf, "\\;");
9119d9
 
9119d9
                     /* assume host containing : is ipv6 */
9119d9
-                    if (strchr(hosts[i].name, ':'))
9119d9
-                        virBufferEscape(&buf, '\\', ":", "[%s]", hosts[i].name);
9119d9
+                    if (strchr(src->hosts[i].name, ':'))
9119d9
+                        virBufferEscape(&buf, '\\', ":", "[%s]",
9119d9
+                                        src->hosts[i].name);
9119d9
                     else
9119d9
-                        virBufferAsprintf(&buf, "%s", hosts[i].name);
9119d9
+                        virBufferAsprintf(&buf, "%s", src->hosts[i].name);
9119d9
 
9119d9
-                    if (hosts[i].port)
9119d9
-                        virBufferAsprintf(&buf, "\\:%s", hosts[i].port);
9119d9
+                    if (src->hosts[i].port)
9119d9
+                        virBufferAsprintf(&buf, "\\:%s", src->hosts[i].port);
9119d9
                 }
9119d9
             }
9119d9
 
9119d9
@@ -3205,13 +3202,7 @@ qemuGetDriveSourceString(virStorageSourcePtr src,
9119d9
         break;
9119d9
 
9119d9
     case VIR_STORAGE_TYPE_NETWORK:
9119d9
-        if (!(*source = qemuBuildNetworkDriveURI(src->protocol,
9119d9
-                                                 src->path,
9119d9
-                                                 src->volume,
9119d9
-                                                 src->nhosts,
9119d9
-                                                 src->hosts,
9119d9
-                                                 username,
9119d9
-                                                 secret)))
9119d9
+        if (!(*source = qemuBuildNetworkDriveURI(src, username, secret)))
9119d9
             goto cleanup;
9119d9
         break;
9119d9
 
9119d9
@@ -5304,6 +5295,10 @@ qemuBuildSCSIiSCSIHostdevDrvStr(virConnectPtr conn,
9119d9
     char *source = NULL;
9119d9
     char *secret = NULL;
9119d9
     char *username = NULL;
9119d9
+    virStorageSource src;
9119d9
+
9119d9
+    memset(&src, 0, sizeof(src));
9119d9
+
9119d9
     virDomainHostdevSubsysSCSIPtr scsisrc = &dev->source.subsys.u.scsi;
9119d9
     virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &scsisrc->u.iscsi;
9119d9
 
9119d9
@@ -5319,13 +5314,13 @@ qemuBuildSCSIiSCSIHostdevDrvStr(virConnectPtr conn,
9119d9
             goto cleanup;
9119d9
     }
9119d9
 
9119d9
+    src.protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI;
9119d9
+    src.path = iscsisrc->path;
9119d9
+    src.hosts = iscsisrc->hosts;
9119d9
+    src.nhosts = iscsisrc->nhosts;
9119d9
+
9119d9
     /* Rather than pull what we think we want - use the network disk code */
9119d9
-    source = qemuBuildNetworkDriveURI(VIR_STORAGE_NET_PROTOCOL_ISCSI,
9119d9
-                                      iscsisrc->path,
9119d9
-                                      NULL, /* volume */
9119d9
-                                      iscsisrc->nhosts,
9119d9
-                                      iscsisrc->hosts,
9119d9
-                                      username, secret);
9119d9
+    source = qemuBuildNetworkDriveURI(&src, username, secret);
9119d9
 
9119d9
  cleanup:
9119d9
     VIR_FREE(secret);
9119d9
-- 
9119d9
2.1.3
9119d9