Skip to content

Commit 45c535d

Browse files
committed
fstab-generator: add rd.systemd.mount-extra= and friends
Previously, mounts specified in systemd.mount-extra= are equally handled both in initrd and the main system. So, the mounts for the main system are also mounted in initrd. This introduces rd.systemd.mount-extra=, which specifies mounts in initrd. Then, mounts specified in systemd.mount-extra= are still mounted both in initrd and the main system, but prefixed with /sysroot/ when running in initrd. Fixes #28516.
1 parent b93d9e0 commit 45c535d

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

man/systemd-fstab-generator.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,15 @@
242242

243243
<varlistentry>
244244
<term><varname>systemd.mount-extra=<replaceable>WHAT</replaceable>:<replaceable>WHERE</replaceable>[:<replaceable>FSTYPE</replaceable>[:<replaceable>OPTIONS</replaceable>]]</varname></term>
245+
<term><varname>rd.systemd.mount-extra=<replaceable>WHAT</replaceable>:<replaceable>WHERE</replaceable>[:<replaceable>FSTYPE</replaceable>[:<replaceable>OPTIONS</replaceable>]]</varname></term>
245246

246247
<listitem>
247248
<para>Specifies the mount unit. Takes at least two and at most four fields separated with a colon
248249
(<literal>:</literal>). Each field is handled as the corresponding fstab field. This option can be
249-
specified multiple times.</para>
250+
specified multiple times. <varname>rd.systemd.mount-extra=</varname> is honored only in the initrd,
251+
while <varname>systemd.mount-extra=</varname> is honored by both the main system and the initrd.
252+
In the initrd, the mount point (and also source path if the mount is bind mount) specified in
253+
<varname>systemd.mount-extra=</varname> is prefixed with <filename>/sysroot/</filename>.</para>
250254
<para>Example:
251255
<programlisting>
252256
systemd.mount-extra=/dev/sda1:/mount-point:ext4:rw,noatime</programlisting>
@@ -256,10 +260,13 @@ systemd.mount-extra=/dev/sda1:/mount-point:ext4:rw,noatime</programlisting>
256260

257261
<varlistentry>
258262
<term><varname>systemd.swap-extra=<replaceable>WHAT</replaceable>[:<replaceable>OPTIONS</replaceable>]</varname></term>
263+
<term><varname>rd.systemd.swap-extra=<replaceable>WHAT</replaceable>[:<replaceable>OPTIONS</replaceable>]</varname></term>
259264

260265
<listitem>
261266
<para>Specifies the swap unit. Takes the block device to be used as a swap device, and optionally
262-
takes mount options followed by a colon (<literal>:</literal>).</para>
267+
takes mount options followed by a colon (<literal>:</literal>). This option can be specified
268+
multiple times. <varname>rd.systemd.swap-extra=</varname> is honored only in the initrd, while
269+
<varname>systemd.swap-extra=</varname> is honored by both the main system and the initrd.</para>
263270
<para>Example:
264271
<programlisting>
265272
systemd.swap=/dev/sda2:x-systemd.makefs</programlisting>

src/fstab-generator/fstab-generator.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef enum MountPointFlags {
4949
} MountPointFlags;
5050

5151
typedef struct Mount {
52+
bool for_initrd;
5253
char *what;
5354
char *where;
5455
char *fstype;
@@ -102,7 +103,13 @@ static void mount_array_free(Mount *mounts, size_t n) {
102103
free(mounts);
103104
}
104105

105-
static int mount_array_add_internal(char *in_what, char *in_where, const char *in_fstype, const char *in_options) {
106+
static int mount_array_add_internal(
107+
bool for_initrd,
108+
char *in_what,
109+
char *in_where,
110+
const char *in_fstype,
111+
const char *in_options) {
112+
106113
_cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
107114
int r;
108115

@@ -135,6 +142,7 @@ static int mount_array_add_internal(char *in_what, char *in_where, const char *i
135142
return -ENOMEM;
136143

137144
arg_mounts[arg_n_mounts++] = (Mount) {
145+
.for_initrd = for_initrd,
138146
.what = TAKE_PTR(what),
139147
.where = TAKE_PTR(where),
140148
.fstype = TAKE_PTR(fstype),
@@ -144,7 +152,7 @@ static int mount_array_add_internal(char *in_what, char *in_where, const char *i
144152
return 0;
145153
}
146154

147-
static int mount_array_add(const char *str) {
155+
static int mount_array_add(bool for_initrd, const char *str) {
148156
_cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
149157
int r;
150158

@@ -159,10 +167,10 @@ static int mount_array_add(const char *str) {
159167
if (!isempty(str))
160168
return -EINVAL;
161169

162-
return mount_array_add_internal(TAKE_PTR(what), TAKE_PTR(where), fstype, options);
170+
return mount_array_add_internal(for_initrd, TAKE_PTR(what), TAKE_PTR(where), fstype, options);
163171
}
164172

165-
static int mount_array_add_swap(const char *str) {
173+
static int mount_array_add_swap(bool for_initrd, const char *str) {
166174
_cleanup_free_ char *what = NULL, *options = NULL;
167175
int r;
168176

@@ -177,7 +185,7 @@ static int mount_array_add_swap(const char *str) {
177185
if (!isempty(str))
178186
return -EINVAL;
179187

180-
return mount_array_add_internal(TAKE_PTR(what), NULL, "swap", options);
188+
return mount_array_add_internal(for_initrd, TAKE_PTR(what), NULL, "swap", options);
181189
}
182190

183191
static int write_options(FILE *f, const char *options) {
@@ -1282,14 +1290,17 @@ static int add_mounts_from_cmdline(void) {
12821290
/* Handle each entries found in cmdline as a fstab entry. */
12831291

12841292
FOREACH_ARRAY(m, arg_mounts, arg_n_mounts) {
1293+
if (m->for_initrd && !in_initrd())
1294+
continue;
1295+
12851296
r = parse_fstab_one(
12861297
"/proc/cmdline",
12871298
m->what,
12881299
m->where,
12891300
m->fstype,
12901301
m->options,
12911302
/* passno = */ 0,
1292-
/* prefix_sysroot = */ false,
1303+
/* prefix_sysroot = */ !m->for_initrd && in_initrd(),
12931304
/* use_swap_enabled = */ false);
12941305
if (r < 0 && ret >= 0)
12951306
ret = r;
@@ -1437,21 +1448,21 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
14371448
else
14381449
arg_verity = r;
14391450

1440-
} else if (streq(key, "systemd.mount-extra")) {
1451+
} else if (STR_IN_SET(key, "systemd.mount-extra", "rd.systemd.mount-extra")) {
14411452

14421453
if (proc_cmdline_value_missing(key, value))
14431454
return 0;
14441455

1445-
r = mount_array_add(value);
1456+
r = mount_array_add(startswith(key, "rd."), value);
14461457
if (r < 0)
14471458
log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value);
14481459

1449-
} else if (streq(key, "systemd.swap-extra")) {
1460+
} else if (STR_IN_SET(key, "systemd.swap-extra", "rd.systemd.swap-extra")) {
14501461

14511462
if (proc_cmdline_value_missing(key, value))
14521463
return 0;
14531464

1454-
r = mount_array_add_swap(value);
1465+
r = mount_array_add_swap(startswith(key, "rd."), value);
14551466
if (r < 0)
14561467
log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value);
14571468
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
systemd.mount-extra=/dev/sdx1:/sysroot:auto:defaults
2-
systemd.mount-extra=/dev/sdx2:/hoge/without_options:auto
3-
systemd.mount-extra=/dev/sdx3:/hoge/without_fstype
4-
systemd.mount-extra=/dev/sdx4
5-
systemd.mount-extra=//foo\ufffebar:/hoge/with\x20space:cifs:rw,seclabel
1+
rd.systemd.mount-extra=/dev/sdx1:/sysroot:auto:defaults
2+
rd.systemd.mount-extra=/dev/sdx2:/hoge/without_options:auto
3+
rd.systemd.mount-extra=/dev/sdx3:/hoge/without_fstype
4+
rd.systemd.mount-extra=/dev/sdx4
5+
rd.systemd.mount-extra=//foo\ufffebar:/hoge/with\x20space:cifs:rw,seclabel
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
systemd.mount-extra=/dev/sdy1:none:swap
2-
systemd.mount-extra=/dev/sdy2:none:swap:x-systemd.makefs
3-
systemd.swap-extra=/dev/sdy3:x-systemd.makefs,nofail
4-
systemd.swap-extra=/dev/sdy4
1+
rd.systemd.mount-extra=/dev/sdy1:none:swap
2+
rd.systemd.mount-extra=/dev/sdy2:none:swap:x-systemd.makefs
3+
rd.systemd.swap-extra=/dev/sdy3:x-systemd.makefs,nofail
4+
rd.systemd.swap-extra=/dev/sdy4

0 commit comments

Comments
 (0)