render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
6ae9ed
From 98ee2fc5d1f1650aa8fafaf5448f48bf6bdcd48b Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <98ee2fc5d1f1650aa8fafaf5448f48bf6bdcd48b@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Tue, 2 Aug 2016 13:41:50 +0200
6ae9ed
Subject: [PATCH] util: storage: Add support for URI based backing volumes in
6ae9ed
 qemu's JSON pseudo-protocol
6ae9ed
6ae9ed
http(s), ftp(s) and tftp use URIs for volume definitions in the JSON
6ae9ed
pseudo protocol so it's pretty straightforward to add support for them.
6ae9ed
6ae9ed
(cherry picked from commit ba05b5b7e78476497fb2222f449b8cad2738acb1)
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 | 43 +++++++++++++++++++++++++++++++++++++++++++
6ae9ed
 tests/virstoragetest.c    | 15 +++++++++++++++
6ae9ed
 2 files changed, 58 insertions(+)
6ae9ed
6ae9ed
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
6ae9ed
index 45266c5..06f7f56 100644
6ae9ed
--- a/src/util/virstoragefile.c
6ae9ed
+++ b/src/util/virstoragefile.c
6ae9ed
@@ -2546,6 +2546,44 @@ virStorageSourceParseBackingJSONPath(virStorageSourcePtr src,
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
+static int
6ae9ed
+virStorageSourceParseBackingJSONUriStr(virStorageSourcePtr src,
6ae9ed
+                                       const char *uri,
6ae9ed
+                                       int protocol)
6ae9ed
+{
6ae9ed
+    if (virStorageSourceParseBackingURI(src, uri) < 0)
6ae9ed
+        return -1;
6ae9ed
+
6ae9ed
+    if (src->protocol != protocol) {
6ae9ed
+        virReportError(VIR_ERR_INVALID_ARG,
6ae9ed
+                       _("expected protocol '%s' but got '%s' in URI JSON volume "
6ae9ed
+                         "definition"),
6ae9ed
+                       virStorageNetProtocolTypeToString(protocol),
6ae9ed
+                       virStorageNetProtocolTypeToString(src->protocol));
6ae9ed
+        return -1;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    return 0;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
+static int
6ae9ed
+virStorageSourceParseBackingJSONUri(virStorageSourcePtr src,
6ae9ed
+                                    virJSONValuePtr json,
6ae9ed
+                                    int protocol)
6ae9ed
+{
6ae9ed
+    const char *uri;
6ae9ed
+
6ae9ed
+    if (!(uri = virJSONValueObjectGetString(json, "uri"))) {
6ae9ed
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
6ae9ed
+                       _("missing URI in JSON backing volume definition"));
6ae9ed
+        return -1;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    return virStorageSourceParseBackingJSONUriStr(src, uri, protocol);
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 struct virStorageSourceJSONDriverParser {
6ae9ed
     const char *drvname;
6ae9ed
     int (*func)(virStorageSourcePtr src, virJSONValuePtr json, int opaque);
6ae9ed
@@ -2556,6 +2594,11 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
6ae9ed
     {"file", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_FILE},
6ae9ed
     {"host_device", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_BLOCK},
6ae9ed
     {"host_cdrom", virStorageSourceParseBackingJSONPath, VIR_STORAGE_TYPE_BLOCK},
6ae9ed
+    {"http", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_HTTP},
6ae9ed
+    {"https", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_HTTPS},
6ae9ed
+    {"ftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_FTP},
6ae9ed
+    {"ftps", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_FTPS},
6ae9ed
+    {"tftp", virStorageSourceParseBackingJSONUri, VIR_STORAGE_NET_PROTOCOL_TFTP},
6ae9ed
 };
6ae9ed
 
6ae9ed
 
6ae9ed
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
6ae9ed
index 6873180..f8e6e9a 100644
6ae9ed
--- a/tests/virstoragetest.c
6ae9ed
+++ b/tests/virstoragetest.c
6ae9ed
@@ -1376,6 +1376,21 @@ mymain(void)
6ae9ed
     TEST_BACKING_PARSE("json:{\"file.driver\":\"host_cdrom\", "
6ae9ed
                              "\"file.filename\":\"/path/to/cdrom\"}",
6ae9ed
                        "<source dev='/path/to/cdrom'/>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"http\", "
6ae9ed
+                             "\"file.uri\":\"http://example.com/file\"}",
6ae9ed
+                       "<source protocol='http' name='file'>\n"
6ae9ed
+                       "  <host name='example.com'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file\":{ \"driver\":\"http\","
6ae9ed
+                                        "\"uri\":\"http://example.com/file\""
6ae9ed
+                                      "}"
6ae9ed
+                            "}",
6ae9ed
+                       "<source protocol='http' name='file'>\n"
6ae9ed
+                       "  <host name='example.com'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("json:{\"file.driver\":\"ftp\", "
6ae9ed
+                             "\"file.uri\":\"http://example.com/file\"}",
6ae9ed
+                       NULL);
6ae9ed
 
6ae9ed
  cleanup:
6ae9ed
     /* Final cleanup */
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed