6ae9ed
From 35b27f217c3614ffe368262297ca4e77e9bec4bb Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <35b27f217c3614ffe368262297ca4e77e9bec4bb@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Tue, 2 Aug 2016 13:41:53 +0200
6ae9ed
Subject: [PATCH] util: storage: Add JSON backing volume parser for 'nbd'
6ae9ed
 protocol
6ae9ed
6ae9ed
(cherry picked from commit a1674fd9d98d5e0c39c02a0c05841089c8ed63f2)
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1134878 [JSON backing]
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host]
6ae9ed
---
6ae9ed
 src/util/virstoragefile.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
6ae9ed
 tests/virstoragetest.c    | 30 ++++++++++++++++++++++++++++++
6ae9ed
 2 files changed, 75 insertions(+)
6ae9ed
6ae9ed
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
6ae9ed
index 7fd1593..7f598fa 100644
6ae9ed
--- a/src/util/virstoragefile.c
6ae9ed
+++ b/src/util/virstoragefile.c
6ae9ed
@@ -2711,6 +2711,50 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
+static int
6ae9ed
+virStorageSourceParseBackingJSONNbd(virStorageSourcePtr src,
6ae9ed
+                                    virJSONValuePtr json,
6ae9ed
+                                    int opaque ATTRIBUTE_UNUSED)
6ae9ed
+{
6ae9ed
+    const char *path = virJSONValueObjectGetString(json, "path");
6ae9ed
+    const char *host = virJSONValueObjectGetString(json, "host");
6ae9ed
+    const char *port = virJSONValueObjectGetString(json, "port");
6ae9ed
+    const char *export = virJSONValueObjectGetString(json, "export");
6ae9ed
+
6ae9ed
+    if (!path && !host) {
6ae9ed
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
6ae9ed
+                       _("missing path or host of NBD server in JSON backing "
6ae9ed
+                         "volume definition"));
6ae9ed
+        return -1;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    src->type = VIR_STORAGE_TYPE_NETWORK;
6ae9ed
+    src->protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
6ae9ed
+
6ae9ed
+    if (VIR_STRDUP(src->path, export) < 0)
6ae9ed
+        return -1;
6ae9ed
+
6ae9ed
+    if (VIR_ALLOC_N(src->hosts, 1) < 0)
6ae9ed
+        return -1;
6ae9ed
+    src->nhosts = 1;
6ae9ed
+
6ae9ed
+    if (path) {
6ae9ed
+        src->hosts[0].transport = VIR_STORAGE_NET_HOST_TRANS_UNIX;
6ae9ed
+        if (VIR_STRDUP(src->hosts[0].socket, path) < 0)
6ae9ed
+            return -1;
6ae9ed
+    } else {
6ae9ed
+        src->hosts[0].transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
6ae9ed
+        if (VIR_STRDUP(src->hosts[0].name, host) < 0)
6ae9ed
+            return -1;
6ae9ed
+
6ae9ed
+        if (VIR_STRDUP(src->hosts[0].port, port) < 0)
6ae9ed
+            return -1;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    return 0;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 struct virStorageSourceJSONDriverParser {
6ae9ed
     const char *drvname;
6ae9ed
     int (*func)(virStorageSourcePtr src, virJSONValuePtr json, int opaque);
6ae9ed
@@ -2728,6 +2772,7 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
6ae9ed
     {"tftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_TFTP},
6ae9ed
     {"gluster", virStorageSourceParseBackingJSONGluster, 0},
6ae9ed
     {"iscsi", virStorageSourceParseBackingJSONiSCSI, 0},
6ae9ed
+    {"nbd", virStorageSourceParseBackingJSONNbd, 0},
6ae9ed
 };
6ae9ed
 
6ae9ed
 
6ae9ed
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
6ae9ed
index 22aae47..4ddcac0 100644
6ae9ed
--- a/tests/virstoragetest.c
6ae9ed
+++ b/tests/virstoragetest.c
6ae9ed
@@ -1437,6 +1437,36 @@ mymain(void)
6ae9ed
                         "  <host transport='unix' socket='/path/socket'/>\n"
6ae9ed
                         "  <host name='example.com'/>\n"
6ae9ed
                         "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
6ae9ed
+                                       "\"path\":\"/path/to/socket\""
6ae9ed
+                                      "}"
6ae9ed
+                            "}",
6ae9ed
+                       "<source protocol='nbd'>\n"
6ae9ed
+                       "  <host transport='unix' socket='/path/to/socket'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
6ae9ed
+                             "\"file.path\":\"/path/to/socket\""
6ae9ed
+                            "}",
6ae9ed
+                       "<source protocol='nbd'>\n"
6ae9ed
+                       "  <host transport='unix' socket='/path/to/socket'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
6ae9ed
+                                       "\"export\":\"blah\","
6ae9ed
+                                       "\"host\":\"example.org\","
6ae9ed
+                                       "\"port\":\"6000\""
6ae9ed
+                                      "}"
6ae9ed
+                            "}",
6ae9ed
+                       "<source protocol='nbd' name='blah'>\n"
6ae9ed
+                       "  <host name='example.org' port='6000'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
6ae9ed
+                             "\"file.export\":\"blah\","
6ae9ed
+                             "\"file.host\":\"example.org\","
6ae9ed
+                             "\"file.port\":\"6000\""
6ae9ed
+                            "}",
6ae9ed
+                       "<source protocol='nbd' name='blah'>\n"
6ae9ed
+                       "  <host name='example.org' port='6000'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
 
6ae9ed
  cleanup:
6ae9ed
     /* Final cleanup */
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed