]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: storage: Add support for URI based backing volumes in qemu's JSON pseudo-protocol
authorPeter Krempa <pkrempa@redhat.com>
Tue, 12 Jul 2016 15:59:58 +0000 (17:59 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 27 Jul 2016 11:24:20 +0000 (13:24 +0200)
http(s), ftp(s) and tftp use URIs for volume definitions in the JSON
pseudo protocol so it's pretty straightforward to add support for them.

src/util/virstoragefile.c
tests/virstoragetest.c

index 766ae28562f6e2acd17406250c2b3ebd01704063..af79f79e4c6bdb2eebb8e503abf74dd583fe9dda 100644 (file)
@@ -2537,6 +2537,44 @@ virStorageSourceParseBackingJSONPath(virStorageSourcePtr src,
 }
 
 
+static int
+virStorageSourceParseBackingJSONUriStr(virStorageSourcePtr src,
+                                       const char *uri,
+                                       int protocol)
+{
+    if (virStorageSourceParseBackingURI(src, uri) < 0)
+        return -1;
+
+    if (src->protocol != protocol) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("expected protocol '%s' but got '%s' in URI JSON volume "
+                         "definition"),
+                       virStorageNetProtocolTypeToString(protocol),
+                       virStorageNetProtocolTypeToString(src->protocol));
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static int
+virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
+                                    virJSONValuePtr json,
+                                    int protocol)
+{
+    const char *uri;
+
+    if (!(uri = virJSONValueObjectGetString(json, "uri"))) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("missing URI in JSON backing volume definition"));
+        return -1;
+    }
+
+    return virStorageSourceParseBackingJSONUriStr(src, uri, protocol);
+}
+
+
 struct virStorageSourceJSONDriverParser {
     const char *drvname;
     int (*func)(virStorageSourcePtr src, virJSONValuePtr json, int opaque);
@@ -2547,6 +2585,11 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
     {"file", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_FILE},
     {"host_device", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_BLOCK},
     {"host_cdrom", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_BLOCK},
+    {"http", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_HTTP},
+    {"https", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_HTTPS},
+    {"ftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_FTP},
+    {"ftps", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_FTPS},
+    {"tftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_TFTP},
 };
 
 
index 6873180db2f1854c9bfe2bf36ee930ef459d5b66..f8e6e9aa37538066970a798fe23d023212697531 100644 (file)
@@ -1376,6 +1376,21 @@ mymain(void)
     TEST_BACKING_PARSE("json:{\"file.driver\":\"host_cdrom\", "
                              "\"file.filename\":\"/path/to/cdrom\"}",
                        "<source dev='/path/to/cdrom'/>\n");
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"http\", "
+                             "\"file.uri\":\"http://example.com/file\"}",
+                       "<source protocol='http' name='file'>\n"
+                       "  <host name='example.com'/>\n"
+                       "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file\":{ \"driver\":\"http\","
+                                        "\"uri\":\"http://example.com/file\""
+                                      "}"
+                            "}",
+                       "<source protocol='http' name='file'>\n"
+                       "  <host name='example.com'/>\n"
+                       "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"ftp\", "
+                             "\"file.uri\":\"http://example.com/file\"}",
+                       NULL);
 
  cleanup:
     /* Final cleanup */