chiark / gitweb /
Support negated fstab options
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 11 Jan 2015 05:04:00 +0000 (00:04 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 12 Jan 2015 04:41:41 +0000 (23:41 -0500)
We would ignore options like "fail" and "auto", and for any option
which takes a value the first assignment would win. Repeated and
options equivalent to the default are rarely used, but they have been
documented forever, and people might use them. Especially on the
kernel command line it is easier to append a repeated or negated
option at the end.

src/cryptsetup/cryptsetup-generator.c
src/cryptsetup/cryptsetup.c
src/fstab-generator/fstab-generator.c
src/shared/fstab-util.h
src/test/test-fstab-util.c

index 3657890..27b2360 100644 (file)
@@ -67,8 +67,8 @@ static int create_disk(
         assert(name);
         assert(device);
 
-        noauto = fstab_test_option(options, "noauto\0");
-        nofail = fstab_test_option(options, "nofail\0");
+        noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
+        nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
         tmp = fstab_test_option(options, "tmp\0");
         swap = fstab_test_option(options, "swap\0");
 
index 15dea7b..e6b37ac 100644 (file)
@@ -69,7 +69,7 @@ static int parse_one_option(const char *option) {
         assert(option);
 
         /* Handled outside of this tool */
-        if (streq(option, "noauto") || streq(option, "nofail"))
+        if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail"))
                 return 0;
 
         if (startswith(option, "cipher=")) {
index 64eed49..bc4c155 100644 (file)
@@ -135,17 +135,15 @@ static int add_swap(
 static bool mount_is_network(struct mntent *me) {
         assert(me);
 
-        return
-                hasmntopt(me, "_netdev") ||
-                fstype_is_network(me->mnt_type);
+        return fstab_test_option(me->mnt_opts, "_netdev\0") ||
+               fstype_is_network(me->mnt_type);
 }
 
 static bool mount_in_initrd(struct mntent *me) {
         assert(me);
 
-        return
-                hasmntopt(me, "x-initrd.mount") ||
-                streq(me->mnt_dir, "/usr");
+        return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
+               streq(me->mnt_dir, "/usr");
 }
 
 static int add_mount(
@@ -344,8 +342,8 @@ static int parse_fstab(bool initrd) {
                 if (is_path(where))
                         path_kill_slashes(where);
 
-                noauto = !!hasmntopt(me, "noauto");
-                nofail = !!hasmntopt(me, "nofail");
+                noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
+                nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
                 log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
                           what, where, me->mnt_type,
                           yes_no(noauto), yes_no(nofail));
@@ -356,10 +354,9 @@ static int parse_fstab(bool initrd) {
                         bool automount;
                         const char *post;
 
-                        automount =
-                                  hasmntopt(me, "comment=systemd.automount") ||
-                                  hasmntopt(me, "x-systemd.automount");
-
+                        automount = fstab_test_option(me->mnt_opts,
+                                                      "comment=systemd.automount\0"
+                                                      "x-systemd.automount\0");
                         if (initrd)
                                 post = SPECIAL_INITRD_FS_TARGET;
                         else if (mount_in_initrd(me))
index 39ddb71..9f6b32e 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <stdbool.h>
 #include <stddef.h>
+#include "macro.h"
 
 int fstab_filter_options(const char *opts, const char *names,
                          const char **namefound, char **value, char **filtered);
@@ -32,3 +33,16 @@ static inline bool fstab_test_option(const char *opts, const char *names) {
 }
 
 int fstab_find_pri(const char *options, int *ret);
+
+static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no) {
+        int r;
+        const char *opt;
+
+        /* If first name given is last, return 1.
+         * If second name given is last or neither is found, return 0. */
+
+        r = fstab_filter_options(opts, yes_no, &opt, NULL, NULL);
+        assert(r >= 0);
+
+        return opt == yes_no;
+}
index ddf965d..284484d 100644 (file)
@@ -107,6 +107,15 @@ static void test_fstab_filter_options(void) {
         do_fstab_filter_options("", "opt\0", 0, NULL, NULL, "");
 }
 
+static void test_fstab_yes_no_option(void) {
+        assert_se(fstab_test_yes_no_option("nofail,fail,nofail", "nofail\0fail\0") == true);
+        assert_se(fstab_test_yes_no_option("nofail,nofail,fail", "nofail\0fail\0") == false);
+        assert_se(fstab_test_yes_no_option("abc,cde,afail", "nofail\0fail\0") == false);
+        assert_se(fstab_test_yes_no_option("nofail,fail=0,nofail=0", "nofail\0fail\0") == true);
+        assert_se(fstab_test_yes_no_option("nofail,nofail=0,fail=0", "nofail\0fail\0") == false);
+}
+
 int main(void) {
         test_fstab_filter_options();
+        test_fstab_yes_no_option();
 }