render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
0a7476
From 5b0a3757656c53186fa27ebb8b562ba3e5b7bd69 Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <5b0a3757656c53186fa27ebb8b562ba3e5b7bd69@dist-git>
0a7476
From: John Ferlan <jferlan@redhat.com>
0a7476
Date: Wed, 3 Apr 2019 09:12:19 -0400
0a7476
Subject: [PATCH] storage: Add default mount options for fs/netfs storage pools
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
https://bugzilla.redhat.com/show_bug.cgi?id=1584663
0a7476
0a7476
Modify the command generation to add some default options to the
0a7476
fs/netfs storage pools based on the OS type. For Linux, it'll be
0a7476
the "nodev, nosuid, noexec". For FreeBSD, it'll be "nosuid, noexec".
0a7476
For others, just leave the options alone.
0a7476
0a7476
Modify the storagepoolxml2argvtest to handle the fact that the
0a7476
same input XML could generate different output XML based on whether
0a7476
Linux, FreeBSD, or other was being built.
0a7476
0a7476
Signed-off-by: John Ferlan <jferlan@redhat.com>
0a7476
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
0a7476
(cherry picked from commit f00cde7f1133fee96dc13a80d7f402c704346974)
0a7476
0a7476
Resolved conflict in tests/storagepoolxml2argvtest.c since commit
0a7476
a15fe1247dfea01b301a825f9b66d09999d059aa is not present downstream.
0a7476
0a7476
Resolved build issue in src/storage/storage_util.c since the VIR_AUTOFREE
0a7476
is not present downstream.
0a7476
0a7476
Signed-off-by: John Ferlan <jferlan@redhat.com>
0a7476
Message-Id: <20190403131219.16385-8-jferlan@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
---
0a7476
 src/storage/storage_util.c                    | 38 +++++++++++++--
0a7476
 .../pool-fs-freebsd.argv                      |  1 +
0a7476
 .../pool-fs-linux.argv                        |  1 +
0a7476
 .../pool-netfs-auto-freebsd.argv              |  1 +
0a7476
 .../pool-netfs-auto-linux.argv                |  1 +
0a7476
 .../pool-netfs-cifs-freebsd.argv              |  1 +
0a7476
 .../pool-netfs-cifs-linux.argv                |  1 +
0a7476
 .../pool-netfs-freebsd.argv                   |  1 +
0a7476
 .../pool-netfs-gluster-freebsd.argv           |  2 +
0a7476
 .../pool-netfs-gluster-linux.argv             |  2 +
0a7476
 .../pool-netfs-linux.argv                     |  1 +
0a7476
 tests/storagepoolxml2argvtest.c               | 48 +++++++++++++++----
0a7476
 12 files changed, 86 insertions(+), 12 deletions(-)
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-fs-freebsd.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-fs-linux.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-auto-freebsd.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-auto-linux.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-cifs-freebsd.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-cifs-linux.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-freebsd.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-gluster-freebsd.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-gluster-linux.argv
0a7476
 create mode 100644 tests/storagepoolxml2argvdata/pool-netfs-linux.argv
0a7476
0a7476
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
0a7476
index 70ce600581..48117bef62 100644
0a7476
--- a/src/storage/storage_util.c
0a7476
+++ b/src/storage/storage_util.c
0a7476
@@ -36,6 +36,11 @@
0a7476
 # ifndef FS_NOCOW_FL
0a7476
 #  define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
0a7476
 # endif
0a7476
+# define default_mount_opts "nodev,nosuid,noexec"
0a7476
+#elif defined(__FreeBSD__)
0a7476
+# define default_mount_opts "nosuid,noexec"
0a7476
+#else
0a7476
+# define default_mount_opts ""
0a7476
 #endif
0a7476
 
0a7476
 #if WITH_BLKID
0a7476
@@ -4245,12 +4250,36 @@ virStorageBackendFileSystemGetPoolSource(virStoragePoolObjPtr pool)
0a7476
 }
0a7476
 
0a7476
 
0a7476
+static void
0a7476
+virStorageBackendFileSystemMountAddOptions(virCommandPtr cmd,
0a7476
+                                           const char *providedOpts)
0a7476
+{
0a7476
+    char *mountOpts = NULL;
0a7476
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
0a7476
+
0a7476
+    if (*default_mount_opts != '\0')
0a7476
+        virBufferAsprintf(&buf, "%s,", default_mount_opts);
0a7476
+
0a7476
+    if (providedOpts)
0a7476
+        virBufferAsprintf(&buf, "%s,", providedOpts);
0a7476
+
0a7476
+    virBufferTrim(&buf, ",", -1);
0a7476
+    mountOpts = virBufferContentAndReset(&buf;;
0a7476
+
0a7476
+    if (mountOpts)
0a7476
+        virCommandAddArgList(cmd, "-o", mountOpts, NULL);
0a7476
+
0a7476
+    VIR_FREE(mountOpts);
0a7476
+}
0a7476
+
0a7476
+
0a7476
 static void
0a7476
 virStorageBackendFileSystemMountNFSArgs(virCommandPtr cmd,
0a7476
                                         const char *src,
0a7476
                                         virStoragePoolDefPtr def)
0a7476
 {
0a7476
     virCommandAddArgList(cmd, src, def->target.path, NULL);
0a7476
+    virStorageBackendFileSystemMountAddOptions(cmd, NULL);
0a7476
 }
0a7476
 
0a7476
 
0a7476
@@ -4262,8 +4291,8 @@ virStorageBackendFileSystemMountGlusterArgs(virCommandPtr cmd,
0a7476
     const char *fmt;
0a7476
 
0a7476
     fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
0a7476
-    virCommandAddArgList(cmd, "-t", fmt, src, "-o", "direct-io-mode=1",
0a7476
-                         def->target.path, NULL);
0a7476
+    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
0a7476
+    virStorageBackendFileSystemMountAddOptions(cmd, "direct-io-mode=1");
0a7476
 }
0a7476
 
0a7476
 
0a7476
@@ -4275,8 +4304,8 @@ virStorageBackendFileSystemMountCIFSArgs(virCommandPtr cmd,
0a7476
     const char *fmt;
0a7476
 
0a7476
     fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
0a7476
-    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path,
0a7476
-                         "-o", "guest", NULL);
0a7476
+    virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
0a7476
+    virStorageBackendFileSystemMountAddOptions(cmd, "guest");
0a7476
 }
0a7476
 
0a7476
 
0a7476
@@ -4292,6 +4321,7 @@ virStorageBackendFileSystemMountDefaultArgs(virCommandPtr cmd,
0a7476
     else
0a7476
         fmt = virStoragePoolFormatFileSystemNetTypeToString(def->source.format);
0a7476
     virCommandAddArgList(cmd, "-t", fmt, src, def->target.path, NULL);
0a7476
+    virStorageBackendFileSystemMountAddOptions(cmd, NULL);
0a7476
 }
0a7476
 
0a7476
 
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-fs-freebsd.argv b/tests/storagepoolxml2argvdata/pool-fs-freebsd.argv
0a7476
new file mode 100644
0a7476
index 0000000000..a35d73e254
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-fs-freebsd.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t ext3 /dev/sda6 /mnt -o nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-fs-linux.argv b/tests/storagepoolxml2argvdata/pool-fs-linux.argv
0a7476
new file mode 100644
0a7476
index 0000000000..19543f442d
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-fs-linux.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t ext3 /dev/sda6 /mnt -o nodev,nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-auto-freebsd.argv b/tests/storagepoolxml2argvdata/pool-netfs-auto-freebsd.argv
0a7476
new file mode 100644
0a7476
index 0000000000..39e5c97aed
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-auto-freebsd.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount localhost:/var/lib/libvirt/images /mnt -o nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-auto-linux.argv b/tests/storagepoolxml2argvdata/pool-netfs-auto-linux.argv
0a7476
new file mode 100644
0a7476
index 0000000000..1f82d3d29c
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-auto-linux.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount localhost:/var/lib/libvirt/images /mnt -o nodev,nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-cifs-freebsd.argv b/tests/storagepoolxml2argvdata/pool-netfs-cifs-freebsd.argv
0a7476
new file mode 100644
0a7476
index 0000000000..d72749a032
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-cifs-freebsd.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t cifs //example.com/samba_share /mnt/cifs -o nosuid,noexec,guest
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-cifs-linux.argv b/tests/storagepoolxml2argvdata/pool-netfs-cifs-linux.argv
0a7476
new file mode 100644
0a7476
index 0000000000..85aa9cf23f
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-cifs-linux.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t cifs //example.com/samba_share /mnt/cifs -o nodev,nosuid,noexec,guest
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-freebsd.argv b/tests/storagepoolxml2argvdata/pool-netfs-freebsd.argv
0a7476
new file mode 100644
0a7476
index 0000000000..05c1951f32
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-freebsd.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t nfs localhost:/var/lib/libvirt/images /mnt -o nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-gluster-freebsd.argv b/tests/storagepoolxml2argvdata/pool-netfs-gluster-freebsd.argv
0a7476
new file mode 100644
0a7476
index 0000000000..700107d78e
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-gluster-freebsd.argv
0a7476
@@ -0,0 +1,2 @@
0a7476
+mount -t glusterfs example.com:/volume /mnt/gluster -o nosuid,noexec,\
0a7476
+direct-io-mode=1
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-gluster-linux.argv b/tests/storagepoolxml2argvdata/pool-netfs-gluster-linux.argv
0a7476
new file mode 100644
0a7476
index 0000000000..9535c8a1b9
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-gluster-linux.argv
0a7476
@@ -0,0 +1,2 @@
0a7476
+mount -t glusterfs example.com:/volume /mnt/gluster -o nodev,nosuid,noexec,\
0a7476
+direct-io-mode=1
0a7476
diff --git a/tests/storagepoolxml2argvdata/pool-netfs-linux.argv b/tests/storagepoolxml2argvdata/pool-netfs-linux.argv
0a7476
new file mode 100644
0a7476
index 0000000000..22fafd7b32
0a7476
--- /dev/null
0a7476
+++ b/tests/storagepoolxml2argvdata/pool-netfs-linux.argv
0a7476
@@ -0,0 +1 @@
0a7476
+mount -t nfs localhost:/var/lib/libvirt/images /mnt -o nodev,nosuid,noexec
0a7476
diff --git a/tests/storagepoolxml2argvtest.c b/tests/storagepoolxml2argvtest.c
0a7476
index 3bf2c3b003..0ea8b3b94c 100644
0a7476
--- a/tests/storagepoolxml2argvtest.c
0a7476
+++ b/tests/storagepoolxml2argvtest.c
0a7476
@@ -86,6 +86,8 @@ testCompareXMLToArgvFiles(bool shouldFail,
0a7476
 struct testInfo {
0a7476
     bool shouldFail;
0a7476
     const char *pool;
0a7476
+    bool linuxOut;
0a7476
+    bool freebsdOut;
0a7476
 };
0a7476
 
0a7476
 static int
0a7476
@@ -100,9 +102,19 @@ testCompareXMLToArgvHelper(const void *data)
0a7476
                     abs_srcdir, info->pool) < 0)
0a7476
         goto cleanup;
0a7476
 
0a7476
-    if (virAsprintf(&cmdline, "%s/storagepoolxml2argvdata/%s.argv",
0a7476
-                    abs_srcdir, info->pool) < 0 && !info->shouldFail)
0a7476
-        goto cleanup;
0a7476
+    if (info->linuxOut) {
0a7476
+        if (virAsprintf(&cmdline, "%s/storagepoolxml2argvdata/%s-linux.argv",
0a7476
+                        abs_srcdir, info->pool) < 0 && !info->shouldFail)
0a7476
+            goto cleanup;
0a7476
+    } else if (info->freebsdOut) {
0a7476
+        if (virAsprintf(&cmdline, "%s/storagepoolxml2argvdata/%s-freebsd.argv",
0a7476
+                        abs_srcdir, info->pool) < 0 && !info->shouldFail)
0a7476
+            goto cleanup;
0a7476
+    } else {
0a7476
+        if (virAsprintf(&cmdline, "%s/storagepoolxml2argvdata/%s.argv",
0a7476
+                        abs_srcdir, info->pool) < 0 && !info->shouldFail)
0a7476
+            goto cleanup;
0a7476
+    }
0a7476
 
0a7476
     result = testCompareXMLToArgvFiles(info->shouldFail, poolxml, cmdline);
0a7476
 
0a7476
@@ -119,9 +131,9 @@ mymain(void)
0a7476
 {
0a7476
     int ret = 0;
0a7476
 
0a7476
-#define DO_TEST_FULL(shouldFail, pool) \
0a7476
+#define DO_TEST_FULL(shouldFail, pool, linuxOut, freebsdOut) \
0a7476
     do { \
0a7476
-        struct testInfo info = { shouldFail, pool }; \
0a7476
+        struct testInfo info = { shouldFail, pool, linuxOut, freebsdOut }; \
0a7476
         if (virTestRun("Storage Pool XML-2-argv " pool, \
0a7476
                        testCompareXMLToArgvHelper, &info) < 0) \
0a7476
             ret = -1; \
0a7476
@@ -129,14 +141,19 @@ mymain(void)
0a7476
     while (0);
0a7476
 
0a7476
 #define DO_TEST(pool, ...) \
0a7476
-    DO_TEST_FULL(false, pool)
0a7476
+    DO_TEST_FULL(false, pool, false, false)
0a7476
 
0a7476
 #define DO_TEST_FAIL(pool, ...) \
0a7476
-    DO_TEST_FULL(true, pool)
0a7476
+    DO_TEST_FULL(true, pool, false, false)
0a7476
+
0a7476
+#define DO_TEST_LINUX(pool, ...) \
0a7476
+    DO_TEST_FULL(false, pool, true, false)
0a7476
+
0a7476
+#define DO_TEST_FREEBSD(pool, ...) \
0a7476
+    DO_TEST_FULL(false, pool, false, true)
0a7476
 
0a7476
     DO_TEST_FAIL("pool-dir");
0a7476
     DO_TEST_FAIL("pool-dir-naming");
0a7476
-    DO_TEST("pool-fs");
0a7476
     DO_TEST_FAIL("pool-logical");
0a7476
     DO_TEST_FAIL("pool-logical-nopath");
0a7476
     DO_TEST_FAIL("pool-logical-create");
0a7476
@@ -145,10 +162,25 @@ mymain(void)
0a7476
     DO_TEST_FAIL("pool-disk-device-nopartsep");
0a7476
     DO_TEST_FAIL("pool-iscsi");
0a7476
     DO_TEST_FAIL("pool-iscsi-auth");
0a7476
+#ifdef __linux__
0a7476
+    DO_TEST_LINUX("pool-fs");
0a7476
+    DO_TEST_LINUX("pool-netfs");
0a7476
+    DO_TEST_LINUX("pool-netfs-auto");
0a7476
+    DO_TEST_LINUX("pool-netfs-gluster");
0a7476
+    DO_TEST_LINUX("pool-netfs-cifs");
0a7476
+#elif defined(__FreeBSD__)
0a7476
+    DO_TEST_FREEBSD("pool-fs");
0a7476
+    DO_TEST_FREEBSD("pool-netfs");
0a7476
+    DO_TEST_FREEBSD("pool-netfs-auto");
0a7476
+    DO_TEST_FREEBSD("pool-netfs-gluster");
0a7476
+    DO_TEST_FREEBSD("pool-netfs-cifs");
0a7476
+#else
0a7476
+    DO_TEST("pool-fs");
0a7476
     DO_TEST("pool-netfs");
0a7476
     DO_TEST("pool-netfs-auto");
0a7476
     DO_TEST("pool-netfs-gluster");
0a7476
     DO_TEST("pool-netfs-cifs");
0a7476
+#endif
0a7476
     DO_TEST_FAIL("pool-scsi");
0a7476
     DO_TEST_FAIL("pool-scsi-type-scsi-host");
0a7476
     DO_TEST_FAIL("pool-scsi-type-fc-host");
0a7476
-- 
0a7476
2.21.0
0a7476