From 39642b0130ceab794b1f5697b5756dfe24f6017b Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 12 Apr 2025 14:50:46 +0200 Subject: [PATCH 1/3] gpt-auto: do not add /sysusr/usr/ -> /sysroot/usr/ after switch-root Follow-up for 7852e301e0eb839adf4bf45aa41e39c0dfc03403 --- src/gpt-auto-generator/gpt-auto-generator.c | 28 +++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index 5274f81e30..73c4183f16 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -918,8 +918,7 @@ static int add_usr_mount(void) { int r; /* /usr/ discovery must be enabled explicitly. */ - if (arg_auto_usr == GPT_AUTO_ROOT_OFF || - arg_auto_usr < 0) + if (arg_auto_usr <= 0) return 0; /* We do not support the other gpt-auto modes for /usr/, but the parser should already have checked that. */ @@ -987,19 +986,22 @@ static int add_usr_mount(void) { if (r < 0) return r; - log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind"); + if (in_initrd()) { + log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind"); - return add_mount("usr-bind", - "/sysusr/usr", - "/sysroot/usr", - /* fstype= */ NULL, - /* flags= */ 0, - "bind", - "/usr/ Partition (Final)", - in_initrd() ? SPECIAL_INITRD_FS_TARGET : SPECIAL_LOCAL_FS_TARGET); -#else - return 0; + r = add_mount("usr-bind", + "/sysusr/usr", + "/sysroot/usr", + /* fstype= */ NULL, + /* flags= */ 0, + "bind", + "/usr/ Partition (Final)", + SPECIAL_INITRD_FS_TARGET); + if (r < 0) + return r; + } #endif + return 0; } static int process_loader_partitions(DissectedPartition *esp, DissectedPartition *xbootldr) { From 7369c037e2f328f0481f13ebc2b968836c4e6250 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 12 Apr 2025 15:06:36 +0200 Subject: [PATCH 2/3] fstab/gpt-auto: hook up validatefs to /sysroot/usr/ rather than /sysusr/usr/ We carry a special logic for /usr/ in initrd that it initially gets mounted to /sysusr/usr/ and later bind mounted to /sysroot/usr/. When hooking up validatefs it is hence necessary to wait for the bind mount and validate that, rather than the /sysusr/usr/ established as intermediate transport. Alternative to #37102 --- src/fstab-generator/fstab-generator.c | 15 +++++++-------- src/gpt-auto-generator/gpt-auto-generator.c | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 88aafd95db..3b3bc9db67 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -813,7 +813,9 @@ static bool sysfs_check(void) { return cached; } -static int add_sysusr_sysroot_usr_bind_mount(const char *source) { +static int add_sysusr_sysroot_usr_bind_mount(const char *source, bool validatefs) { + log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind validatefs=%s", yes_no(validatefs)); + return add_mount(source, arg_dest, "/sysusr/usr", @@ -822,7 +824,7 @@ static int add_sysusr_sysroot_usr_bind_mount(const char *source) { /* fstype= */ NULL, "bind", /* passno= */ 0, - /* flags= */ 0, + validatefs ? MOUNT_VALIDATEFS : 0, SPECIAL_INITRD_FS_TARGET, /* extra_after= */ NULL); } @@ -1004,15 +1006,14 @@ static int parse_fstab_one( fstype, options, passno, - flags, + flags & ~(is_sysroot_usr ? MOUNT_VALIDATEFS : 0), target_unit, /* extra_after= */ NULL); if (r <= 0) return r; if (is_sysroot_usr) { - log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind"); - r = add_sysusr_sysroot_usr_bind_mount(source); + r = add_sysusr_sysroot_usr_bind_mount(source, flags & MOUNT_VALIDATEFS); if (r < 0) return r; } @@ -1324,9 +1325,7 @@ static int add_sysroot_usr_mount(void) { if (r < 0) return r; - log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind"); - - r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline"); + r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline", /* validatefs = */ false); if (r < 0) return r; diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index 73c4183f16..103acffe77 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -979,7 +979,7 @@ static int add_usr_mount(void) { "/dev/disk/by-designator/usr", in_initrd() ? "/sysusr/usr" : "/usr", arg_usr_fstype, - (in_initrd() ? MOUNT_VALIDATEFS : 0), + /* flags = */ 0, options, "/usr/ Partition", in_initrd() ? SPECIAL_INITRD_USR_FS_TARGET : SPECIAL_LOCAL_FS_TARGET); @@ -993,7 +993,7 @@ static int add_usr_mount(void) { "/sysusr/usr", "/sysroot/usr", /* fstype= */ NULL, - /* flags= */ 0, + MOUNT_VALIDATEFS, "bind", "/usr/ Partition (Final)", SPECIAL_INITRD_FS_TARGET); From e844ee075259162c6b2585cd4b026865077a4015 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 12 Apr 2025 15:22:05 +0200 Subject: [PATCH 3/3] fstab-generator: honor x-systemd.validatefs from kernel cmdline too --- src/fstab-generator/fstab-generator.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 3b3bc9db67..6f80289881 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -1167,7 +1167,7 @@ static bool validate_root_or_usr_mount_source(const char *what, const char *swit static int add_sysroot_mount(void) { _cleanup_free_ char *what = NULL; const char *extra_opts = NULL, *fstype = NULL; - bool default_rw = true, makefs = false; + bool default_rw = true; MountPointFlags flags; if (!validate_root_or_usr_mount_source(arg_root_what, "root=")) @@ -1218,8 +1218,9 @@ static int add_sysroot_mount(void) { log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(combined_options)); - makefs = fstab_test_option(combined_options, "x-systemd.makefs\0"); - flags = makefs * MOUNT_MAKEFS; + /* Only honor x-systemd.makefs and .validatefs here, others are not relevant in initrd/not used + * at all (also see mandatory_mount_drop_unapplicable_options()) */ + flags = fstab_options_to_flags(combined_options, /* is_swap = */ false) & (MOUNT_MAKEFS|MOUNT_VALIDATEFS); return add_mount("/proc/cmdline", arg_dest, @@ -1237,8 +1238,7 @@ static int add_sysroot_mount(void) { static int add_sysroot_usr_mount(void) { _cleanup_free_ char *what = NULL; const char *extra_opts = NULL; - MountPointFlags flags; - bool makefs; + bool makefs, validatefs; int r; /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we @@ -1308,8 +1308,10 @@ static int add_sysroot_usr_mount(void) { log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(combined_options)); + /* Only honor x-systemd.makefs and .validatefs here, others are not relevant in initrd/not used + * at all (also see mandatory_mount_drop_unapplicable_options()) */ makefs = fstab_test_option(combined_options, "x-systemd.makefs\0"); - flags = makefs * MOUNT_MAKEFS; + validatefs = fstab_test_option(combined_options, "x-systemd.validatefs\0"); r = add_mount("/proc/cmdline", arg_dest, @@ -1319,13 +1321,13 @@ static int add_sysroot_usr_mount(void) { arg_usr_fstype, combined_options, /* passno= */ is_device_path(what) ? 1 : 0, - flags, + makefs ? MOUNT_MAKEFS : 0, SPECIAL_INITRD_USR_FS_TARGET, "imports.target"); if (r < 0) return r; - r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline", /* validatefs = */ false); + r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline", validatefs); if (r < 0) return r;