Skip to content

Commit 22f5a82

Browse files
committed
fstab-generator: add a flag to accept entry for "/" in initrd
When both prefix_sysroot and accept_root is true, the entry for "/" will be accepted and converted to "/sysroot/". Why? If the entry is read from the main system's fstab, then we already mounted /sysroot/, hence it is not and should not re-add the .mount unit for /sysroot/. However, if we want to specify the root mount through the kernel command line or credential, without this change, we need to specify the same entry in the two options. E.g. === systemd.mount-extra=/dev/sda1:/:auto:defaults rd.systemd.mount-extra=/dev/sda1:/sysroot:auto:defaults === That's inconvenient. Of course, we can dedup that by using traditional options, but cannot when defined in credential.
1 parent dfd1054 commit 22f5a82

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/fstab-generator/fstab-generator.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ static bool mount_is_network(const char *fstype, const char *options) {
314314
(fstype && fstype_is_network(fstype));
315315
}
316316

317-
static bool mount_in_initrd(const char *where, const char *options) {
317+
static bool mount_in_initrd(const char *where, const char *options, bool accept_root) {
318318
return fstab_test_option(options, "x-initrd.mount\0") ||
319-
(where && path_equal(where, "/usr"));
319+
(where && PATH_IN_SET(where, "/usr", accept_root ? "/" : NULL));
320320
}
321321

322322
static int write_timeout(
@@ -851,6 +851,7 @@ static int parse_fstab_one(
851851
const char *options,
852852
int passno,
853853
bool prefix_sysroot,
854+
bool accept_root, /* This takes an effect only when prefix_sysroot is true. */
854855
bool use_swap_enabled) {
855856

856857
_cleanup_free_ char *what = NULL, *where = NULL;
@@ -862,7 +863,7 @@ static int parse_fstab_one(
862863
assert(fstype);
863864
assert(options);
864865

865-
if (prefix_sysroot && !mount_in_initrd(where_original, options))
866+
if (prefix_sysroot && !mount_in_initrd(where_original, options, accept_root))
866867
return 0;
867868

868869
is_swap = streq_ptr(fstype, "swap");
@@ -982,7 +983,9 @@ static int parse_fstab(bool prefix_sysroot) {
982983
while ((me = getmntent(f))) {
983984
r = parse_fstab_one(fstab,
984985
me->mnt_fsname, me->mnt_dir, me->mnt_type, me->mnt_opts, me->mnt_passno,
985-
prefix_sysroot, /* use_swap_enabled = */ true);
986+
prefix_sysroot,
987+
/* accept_root = */ false,
988+
/* use_swap_enabled = */ true);
986989
if (r < 0 && ret >= 0)
987990
ret = r;
988991
if (arg_sysroot_check && r > 0)
@@ -1301,6 +1304,7 @@ static int add_mounts_from_cmdline(void) {
13011304
m->options,
13021305
/* passno = */ 0,
13031306
/* prefix_sysroot = */ !m->for_initrd && in_initrd(),
1307+
/* accept_root = */ true,
13041308
/* use_swap_enabled = */ false);
13051309
if (r < 0 && ret >= 0)
13061310
ret = r;
@@ -1337,6 +1341,7 @@ static int add_mounts_from_creds(bool prefix_sysroot) {
13371341
me->mnt_opts,
13381342
me->mnt_passno,
13391343
/* prefix_sysroot = */ prefix_sysroot,
1344+
/* accept_root = */ true,
13401345
/* use_swap_enabled = */ true);
13411346
if (r < 0 && ret >= 0)
13421347
ret = r;

0 commit comments

Comments
 (0)