From 1177373b6a0aa3698c2ec300bed0ad338f8f4721 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 9 Aug 2023 02:30:33 +0900 Subject: [PATCH] fstab-util: introduce fstab_enabled() helper function 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 | 27 +++++++++++++++++++++++++++ src/shared/fstab-util.h | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 28121b8e2c..fed0e1113f 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -13,15 +13,39 @@ #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; diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h index a1c079fd41..9cf34f0f8b 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -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); -- 2.25.1