]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: storage: Add JSON backing volume parser for 'nbd' protocol
authorPeter Krempa <pkrempa@redhat.com>
Thu, 14 Jul 2016 06:10:31 +0000 (08:10 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 27 Jul 2016 11:24:20 +0000 (13:24 +0200)
src/util/virstoragefile.c
tests/virstoragetest.c

index 66dbbefeb3a61949cae43c19337bfc81cb09c087..53ff710d3a3c7e1cbddfb2ad8b17504c1efb5a0d 100644 (file)
@@ -2702,6 +2702,50 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
 }
 
 
+static int
+virStorageSourceParseBackingJSONNbd(virStorageSourcePtr src,
+                                    virJSONValuePtr json,
+                                    int opaque ATTRIBUTE_UNUSED)
+{
+    const char *path = virJSONValueObjectGetString(json, "path");
+    const char *host = virJSONValueObjectGetString(json, "host");
+    const char *port = virJSONValueObjectGetString(json, "port");
+    const char *export = virJSONValueObjectGetString(json, "export");
+
+    if (!path && !host) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("missing path or host of NBD server in JSON backing "
+                         "volume definition"));
+        return -1;
+    }
+
+    src->type = VIR_STORAGE_TYPE_NETWORK;
+    src->protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
+
+    if (VIR_STRDUP(src->path, export) < 0)
+        return -1;
+
+    if (VIR_ALLOC_N(src->hosts, 1) < 0)
+        return -1;
+    src->nhosts = 1;
+
+    if (path) {
+        src->hosts[0].transport = VIR_STORAGE_NET_HOST_TRANS_UNIX;
+        if (VIR_STRDUP(src->hosts[0].socket, path) < 0)
+            return -1;
+    } else {
+        src->hosts[0].transport = VIR_STORAGE_NET_HOST_TRANS_TCP;
+        if (VIR_STRDUP(src->hosts[0].name, host) < 0)
+            return -1;
+
+        if (VIR_STRDUP(src->hosts[0].port, port) < 0)
+            return -1;
+    }
+
+    return 0;
+}
+
+
 struct virStorageSourceJSONDriverParser {
     const char *drvname;
     int (*func)(virStorageSourcePtr src, virJSONValuePtr json, int opaque);
@@ -2719,6 +2763,7 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
     {"tftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_TFTP},
     {"gluster", virStorageSourceParseBackingJSONGluster, 0},
     {"iscsi", virStorageSourceParseBackingJSONiSCSI, 0},
+    {"nbd", virStorageSourceParseBackingJSONNbd, 0},
 };
 
 
index 22aae47996496886678df4d390df24b9584c72d9..4ddcac06cd0f9362d8b7019bd95b083fc0d9015a 100644 (file)
@@ -1437,6 +1437,36 @@ mymain(void)
                         "  <host transport='unix' socket='/path/socket'/>\n"
                         "  <host name='example.com'/>\n"
                         "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
+                                       "\"path\":\"/path/to/socket\""
+                                      "}"
+                            "}",
+                       "<source protocol='nbd'>\n"
+                       "  <host transport='unix' socket='/path/to/socket'/>\n"
+                       "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
+                             "\"file.path\":\"/path/to/socket\""
+                            "}",
+                       "<source protocol='nbd'>\n"
+                       "  <host transport='unix' socket='/path/to/socket'/>\n"
+                       "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
+                                       "\"export\":\"blah\","
+                                       "\"host\":\"example.org\","
+                                       "\"port\":\"6000\""
+                                      "}"
+                            "}",
+                       "<source protocol='nbd' name='blah'>\n"
+                       "  <host name='example.org' port='6000'/>\n"
+                       "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
+                             "\"file.export\":\"blah\","
+                             "\"file.host\":\"example.org\","
+                             "\"file.port\":\"6000\""
+                            "}",
+                       "<source protocol='nbd' name='blah'>\n"
+                       "  <host name='example.org' port='6000'/>\n"
+                       "</source>\n");
 
  cleanup:
     /* Final cleanup */