]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: virstoragefile: Add 'json:' pseudo-protocol parser for 'nfs' protocol
authorRyan Gahagan <rgahagan@cs.utexas.edu>
Wed, 6 Jan 2021 21:32:31 +0000 (15:32 -0600)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 8 Jan 2021 14:09:26 +0000 (15:09 +0100)
Enable parsing of backing store strings containing the native 'nfs'
protocol specification.

Signed-off-by: Ryan Gahagan <rgahagan@cs.utexas.edu>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/util/virstoragefile.c
tests/virstoragetest.c

index 3097e11984722c4fd479b19485d3a9a6e8c3e16d..d67f0f2c3fec67d94c66427294f5371b136c34cc 100644 (file)
@@ -3652,6 +3652,54 @@ virStorageSourceParseBackingJSONVxHS(virStorageSourcePtr src,
 }
 
 
+static int
+virStorageSourceParseBackingJSONNFS(virStorageSourcePtr src,
+                                    virJSONValuePtr json,
+                                    const char *jsonstr G_GNUC_UNUSED,
+                                    int opaque G_GNUC_UNUSED)
+{
+    virJSONValuePtr server = virJSONValueObjectGetObject(json, "server");
+    int uidStore = -1;
+    int gidStore = -1;
+    int gotUID = virJSONValueObjectGetNumberInt(json, "user", &uidStore);
+    int gotGID = virJSONValueObjectGetNumberInt(json, "group", &gidStore);
+
+    if (!server) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("missing 'server' attribute in JSON backing definition for NFS volume"));
+        return -1;
+    }
+
+    if (gotUID < 0 || gotGID < 0) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("missing 'user' or 'group' attribute in JSON backing definition for NFS volume"));
+        return -1;
+    }
+
+    src->path = g_strdup(virJSONValueObjectGetString(json, "path"));
+    if (!src->path) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("missing 'path' attribute in JSON backing definition for NFS volume"));
+        return -1;
+    }
+
+    src->nfs_user = g_strdup_printf("+%d", uidStore);
+    src->nfs_group = g_strdup_printf("+%d", gidStore);
+
+    src->type = VIR_STORAGE_TYPE_NETWORK;
+    src->protocol = VIR_STORAGE_NET_PROTOCOL_NFS;
+
+    src->hosts = g_new0(virStorageNetHostDef, 1);
+    src->nhosts = 1;
+
+    if (virStorageSourceParseBackingJSONInetSocketAddress(src->hosts,
+                                                          server) < 0)
+        return -1;
+
+    return 0;
+}
+
+
 static int
 virStorageSourceParseBackingJSONNVMe(virStorageSourcePtr src,
                                      virJSONValuePtr json,
@@ -3711,6 +3759,7 @@ static const struct virStorageSourceJSONDriverParser jsonParsers[] = {
     {"ssh", false, virStorageSourceParseBackingJSONSSH, 0},
     {"rbd", false, virStorageSourceParseBackingJSONRBD, 0},
     {"raw", true, virStorageSourceParseBackingJSONRaw, 0},
+    {"nfs", false, virStorageSourceParseBackingJSONNFS, 0},
     {"vxhs", false, virStorageSourceParseBackingJSONVxHS, 0},
     {"nvme", false, virStorageSourceParseBackingJSONNVMe, 0},
 };
index a376154defdcf7661bcfc733c76a589776240215..86c7cd910c778af0af345b5c64b896da47a1776b 100644 (file)
@@ -1624,6 +1624,19 @@ mymain(void)
                        "<source protocol='vxhs' name='c6718f6b-0401-441d-a8c3-1f0064d75ee0'>\n"
                        "  <host name='example.com' port='9999'/>\n"
                        "</source>\n");
+    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nfs\","
+                                   "\"user\":2,"
+                                   "\"group\":9,"
+                                   "\"path\":\"/foo/bar/baz\","
+                                   "\"server\": {  \"host\":\"example.com\","
+                                                  "\"type\":\"inet\""
+                                               "}"
+                                      "}"
+                            "}",
+                       "<source protocol='nfs' name='/foo/bar/baz'>\n"
+                       "  <host name='example.com'/>\n"
+                       "  <identity user='+2' group='+9'/>\n"
+                       "</source>\n");
     TEST_BACKING_PARSE_FULL("json:{ \"driver\": \"raw\","
                                     "\"offset\": 10752,"
                                     "\"size\": 4063232,"