Blob Blame History Raw
From 798cd86a4beff97eac491c4292e4a02b042a2a49 Mon Sep 17 00:00:00 2001
Message-Id: <798cd86a4beff97eac491c4292e4a02b042a2a49@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 26 Feb 2014 14:54:57 +0100
Subject: [PATCH] qemu: Use qemuBuildNetworkDriveURI to handle http/ftp and
 friends

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

Prepare the function to integrate other protocols and start folding
other network protocols into a common place.

Conflicts: Other protocols like TFTP, HTTP ... were not backported and
thus were removed from this patch.

(cherry picked from commit 078a102537380162e7198b63530a6bfb36726cd6)

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

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index b80ffe9..f305a5a 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3548,6 +3548,46 @@ error:
 }
 
 
+static int
+qemuNetworkDriveGetPort(int protocol,
+                        const char *port)
+{
+    int ret = 0;
+
+    if (port) {
+        if (virStrToLong_i(port, NULL, 10, &ret) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("failed to parse port number '%s'"),
+                           port);
+            return -1;
+        }
+
+        return ret;
+    }
+
+    switch ((enum virDomainDiskProtocol) protocol) {
+        case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
+            return 7000;
+
+        case VIR_DOMAIN_DISK_PROTOCOL_NBD:
+            return 10809;
+
+        case VIR_DOMAIN_DISK_PROTOCOL_ISCSI:
+        case VIR_DOMAIN_DISK_PROTOCOL_GLUSTER:
+            /* no default port specified */
+            return 0;
+
+        case VIR_DOMAIN_DISK_PROTOCOL_RBD:
+        case VIR_DOMAIN_DISK_PROTOCOL_LAST:
+            /* not aplicable */
+            return -1;
+    }
+
+    return -1;
+}
+
+
+
 char *
 qemuBuildNetworkDriveURI(int protocol,
                          const char *src,
@@ -3559,56 +3599,69 @@ qemuBuildNetworkDriveURI(int protocol,
     char *ret = NULL;
     virURIPtr uri = NULL;
 
-    if (nhosts != 1) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("protocol '%s' accepts only one host"),
-                       virDomainDiskProtocolTypeToString(protocol));
-        return NULL;
-    }
-
-    if (VIR_ALLOC(uri) < 0)
-        return NULL;
+    switch ((enum virDomainDiskProtocol) protocol) {
+        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"),
+                               virDomainDiskProtocolTypeToString(protocol));
+                goto cleanup;
+            }
 
-    if (hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_TCP) {
-        if (VIR_STRDUP(uri->scheme, virDomainDiskProtocolTypeToString(protocol)) < 0)
-            goto cleanup;
-    } else {
-        if (virAsprintf(&uri->scheme, "%s+%s",
-                        virDomainDiskProtocolTypeToString(protocol),
-                        virDomainDiskProtocolTransportTypeToString(hosts->transport)) < 0)
-            goto cleanup;
-    }
+            if (VIR_ALLOC(uri) < 0)
+                goto cleanup;
 
-    if (src &&
-        virAsprintf(&uri->path, "/%s", src) < 0)
-        goto cleanup;
+            if (hosts->transport == VIR_DOMAIN_DISK_PROTO_TRANS_TCP) {
+                if (VIR_STRDUP(uri->scheme,
+                               virDomainDiskProtocolTypeToString(protocol)) < 0)
+                    goto cleanup;
+            } else {
+                if (virAsprintf(&uri->scheme, "%s+%s",
+                                virDomainDiskProtocolTypeToString(protocol),
+                                virDomainDiskProtocolTransportTypeToString(hosts->transport)) < 0)
+                    goto cleanup;
+            }
 
-    if (hosts->port &&
-        virStrToLong_i(hosts->port, NULL, 10, &uri->port) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to parse port number '%s'"),
-                       hosts->port);
-        goto cleanup;
-    }
+            if ((uri->port = qemuNetworkDriveGetPort(protocol, hosts->port)) < 0)
+                goto cleanup;
 
-    if (hosts->socket &&
-        virAsprintf(&uri->query, "socket=%s", hosts->socket) < 0)
-        goto cleanup;
+            if (src &&
+                virAsprintf(&uri->path, "%s%s",
+                            src[0] == '/' ? "" : "/",
+                            src) < 0)
+                goto cleanup;
 
-    if (username) {
-        if (secret) {
-            if (virAsprintf(&uri->user, "%s:%s", username, secret) < 0)
+            if (hosts->socket &&
+                virAsprintf(&uri->query, "socket=%s", hosts->socket) < 0)
                 goto cleanup;
-        } else {
-            if (VIR_STRDUP(uri->user, username) < 0)
+
+            if (username) {
+                if (secret) {
+                    if (virAsprintf(&uri->user, "%s:%s", username, secret) < 0)
+                        goto cleanup;
+                } else {
+                    if (VIR_STRDUP(uri->user, username) < 0)
+                        goto cleanup;
+                }
+            }
+
+            if (VIR_STRDUP(uri->server, hosts->name) < 0)
                 goto cleanup;
-        }
-    }
 
-    if (VIR_STRDUP(uri->server, hosts->name) < 0)
-        goto cleanup;
+            ret = virURIFormat(uri);
+
+            break;
 
-    ret = virURIFormat(uri);
+        case VIR_DOMAIN_DISK_PROTOCOL_SHEEPDOG:
+        case VIR_DOMAIN_DISK_PROTOCOL_RBD:
+        case VIR_DOMAIN_DISK_PROTOCOL_LAST:
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("network disk protocol '%s' not supported"),
+                           virDomainDiskProtocolTypeToString(protocol));
+            goto cleanup;
+    }
 
 cleanup:
     virURIFree(uri);
@@ -3677,11 +3730,9 @@ qemuBuildNBDString(virConnectPtr conn, virDomainDiskDefPtr disk, virBufferPtr op
         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] != '/'))
+    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:");
-- 
1.9.0