]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fstab-util: introduce fstab_enabled() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Aug 2023 17:30:33 +0000 (02:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 9 Aug 2023 08:53:46 +0000 (17:53 +0900)
And refuse to parse fstab when 'fstab=no' is specified in the kernel
command line.

When 'fstab=no' is specified in the kernel command line, fstab-generator
does not parse fstab and will not create e.g. /boot or /efi mount entry
even if fstab contains entries for the mount points. However, gpt-auto
generator may parse fstab file, and adjust or ignore mounts for EFI or
XBOOTLDR partitions based on the fstab file.

This makes gpt-auto also ignore fstab entries if 'fstab=no' is set in
the kernel command line.

src/shared/fstab-util.c
src/shared/fstab-util.h

index 28121b8e2c61ba5a10e79825ffe33987d32f888f..fed0e1113fde4313d801d22faacb9779898f477e 100644 (file)
 #include "nulstr-util.h"
 #include "parse-util.h"
 #include "path-util.h"
+#include "proc-cmdline.h"
 #include "string-util.h"
 #include "strv.h"
 
+bool fstab_enabled_full(int enabled) {
+        static int cached = -1;
+        bool val;
+        int r;
+
+        /* If 'enabled' is non-negative, then update the cache with it. */
+        if (enabled >= 0)
+                cached = enabled;
+
+        if (cached >= 0)
+                return cached;
+
+        r = proc_cmdline_get_bool("fstab", PROC_CMDLINE_STRIP_RD_PREFIX, &val);
+        if (r < 0)
+                log_debug_errno(r, "Failed to parse kernel command line, ignoring: %m");
+
+        /* If nothing specified, then defaults to true. */
+        return (cached = r > 0 ? val : true);
+}
+
 int fstab_has_fstype(const char *fstype) {
         _cleanup_endmntent_ FILE *f = NULL;
         struct mntent *m;
 
         assert(fstype);
 
+        if (!fstab_enabled())
+                return false;
+
         f = setmntent(fstab_path(), "re");
         if (!f)
                 return errno == ENOENT ? false : -errno;
@@ -88,6 +112,9 @@ int fstab_is_mount_point_full(const char *where, const char *path) {
 
         assert(where || path);
 
+        if (!fstab_enabled())
+                return false;
+
         f = setmntent(fstab_path(), "re");
         if (!f)
                 return errno == ENOENT ? false : -errno;
index a1c079fd41719a2619284af168618f896461e8db..9cf34f0f8ba186a5b21857ffea66da7037a8ad63 100644 (file)
@@ -6,6 +6,14 @@
 
 #include "macro.h"
 
+bool fstab_enabled_full(int enabled);
+static inline bool fstab_enabled(void) {
+        return fstab_enabled_full(-1);
+}
+static inline bool fstab_set_enabled(bool enabled) {
+        return fstab_enabled_full(enabled);
+}
+
 bool fstab_is_extrinsic(const char *mount, const char *opts);
 int fstab_has_fstype(const char *fstype);