]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
man/systemd.mount: update implicit deps on device unit
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6b1dc2bd 2
6b1dc2bd 3#include <errno.h>
07630cea 4#include <stdio.h>
6b1dc2bd
LP
5#include <unistd.h>
6
b5efdb8a 7#include "alloc-util.h"
028a981c
ZJS
8#include "bus-error.h"
9#include "bus-locator.h"
a9399358 10#include "bus-unit-util.h"
f461a28d 11#include "chase.h"
6ac62485 12#include "creds-util.h"
6c51b49c 13#include "efi-loader.h"
905dd992 14#include "env-util.h"
3ffd4af2 15#include "fd-util.h"
0d39fa9c 16#include "fileio.h"
d15d0333 17#include "fstab-util.h"
07630cea 18#include "generator.h"
77b8e92d 19#include "in-addr-util.h"
baa6a42d 20#include "initrd-util.h"
07630cea 21#include "log.h"
a4ef3e4d 22#include "main-func.h"
07630cea 23#include "mkdir.h"
6b1dc2bd 24#include "mount-setup.h"
4349cd7c 25#include "mount-util.h"
049af8ad 26#include "mountpoint-util.h"
55365b0a 27#include "nulstr-util.h"
6bedfcbb 28#include "parse-util.h"
07630cea 29#include "path-util.h"
4e731273 30#include "proc-cmdline.h"
028a981c 31#include "process-util.h"
6b1dc2bd 32#include "special.h"
98bad05e 33#include "specifier.h"
8fcde012 34#include "stat-util.h"
07630cea 35#include "string-util.h"
059cb385 36#include "strv.h"
07630cea 37#include "unit-name.h"
689aede8 38#include "virt.h"
91214a37 39#include "volatile-util.h"
6b1dc2bd 40
d2194e15
LP
41typedef enum MountPointFlags {
42 MOUNT_NOAUTO = 1 << 0,
43 MOUNT_NOFAIL = 1 << 1,
44 MOUNT_AUTOMOUNT = 1 << 2,
45 MOUNT_MAKEFS = 1 << 3,
46 MOUNT_GROWFS = 1 << 4,
47 MOUNT_RW_ONLY = 1 << 5,
04959faa 48 MOUNT_PCRFS = 1 << 6,
d2194e15 49} MountPointFlags;
4191418b 50
55365b0a 51typedef struct Mount {
45c535dd 52 bool for_initrd;
55365b0a
YW
53 char *what;
54 char *where;
55 char *fstype;
56 char *options;
57} Mount;
58
59static void mount_array_free(Mount *mounts, size_t n);
60
028a981c 61static bool arg_sysroot_check = false;
7a44c7e3
ZJS
62static const char *arg_dest = NULL;
63static const char *arg_dest_late = NULL;
e48fdd84 64static bool arg_fstab_enabled = true;
567a5307 65static bool arg_swap_enabled = true;
6db615c1
LP
66static char *arg_root_what = NULL;
67static char *arg_root_fstype = NULL;
68static char *arg_root_options = NULL;
2f3dfc6f 69static char *arg_root_hash = NULL;
6db615c1 70static int arg_root_rw = -1;
9f103625
TH
71static char *arg_usr_what = NULL;
72static char *arg_usr_fstype = NULL;
73static char *arg_usr_options = NULL;
c1b9e3df 74static char *arg_usr_hash = NULL;
91214a37 75static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
200268c6 76static bool arg_verity = true;
55365b0a
YW
77static Mount *arg_mounts = NULL;
78static size_t arg_n_mounts = 0;
6b1dc2bd 79
a4ef3e4d
YW
80STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
81STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
82STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
83STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
84STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
85STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
86STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
c1b9e3df 87STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep);
55365b0a
YW
88STATIC_ARRAY_DESTRUCTOR_REGISTER(arg_mounts, arg_n_mounts, mount_array_free);
89
90static void mount_done(Mount *m) {
91 assert(m);
92
93 free(m->what);
94 free(m->where);
95 free(m->fstype);
96 free(m->options);
97}
98
99static void mount_array_free(Mount *mounts, size_t n) {
100 FOREACH_ARRAY(m, mounts, n)
101 mount_done(m);
102
103 free(mounts);
104}
105
45c535dd
YW
106static 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
55365b0a
YW
113 _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
114 int r;
115
116 /* This takes what and where. */
117
118 what = ASSERT_PTR(in_what);
119 where = in_where;
120
121 fstype = strdup(isempty(in_fstype) ? "auto" : in_fstype);
122 if (!fstype)
123 return -ENOMEM;
124
125 if (streq(fstype, "swap"))
126 where = mfree(where);
127
128 if (!isempty(in_options)) {
129 _cleanup_strv_free_ char **options_strv = NULL;
130
131 r = strv_split_full(&options_strv, in_options, ",", 0);
132 if (r < 0)
133 return r;
134
135 r = strv_make_nulstr(options_strv, &options, NULL);
136 } else
137 r = strv_make_nulstr(STRV_MAKE("defaults"), &options, NULL);
138 if (r < 0)
139 return r;
140
141 if (!GREEDY_REALLOC(arg_mounts, arg_n_mounts + 1))
142 return -ENOMEM;
143
144 arg_mounts[arg_n_mounts++] = (Mount) {
45c535dd 145 .for_initrd = for_initrd,
55365b0a
YW
146 .what = TAKE_PTR(what),
147 .where = TAKE_PTR(where),
148 .fstype = TAKE_PTR(fstype),
149 .options = TAKE_PTR(options),
150 };
151
152 return 0;
153}
154
45c535dd 155static int mount_array_add(bool for_initrd, const char *str) {
55365b0a
YW
156 _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
157 int r;
158
159 assert(str);
160
161 r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS,
162 &what, &where, &fstype, &options, NULL);
163 if (r < 0)
164 return r;
165 if (r < 2)
166 return -EINVAL;
167 if (!isempty(str))
168 return -EINVAL;
169
45c535dd 170 return mount_array_add_internal(for_initrd, TAKE_PTR(what), TAKE_PTR(where), fstype, options);
55365b0a
YW
171}
172
45c535dd 173static int mount_array_add_swap(bool for_initrd, const char *str) {
55365b0a
YW
174 _cleanup_free_ char *what = NULL, *options = NULL;
175 int r;
176
177 assert(str);
178
179 r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS,
180 &what, &options, NULL);
181 if (r < 0)
182 return r;
183 if (r < 1)
184 return -EINVAL;
185 if (!isempty(str))
186 return -EINVAL;
187
45c535dd 188 return mount_array_add_internal(for_initrd, TAKE_PTR(what), NULL, "swap", options);
55365b0a 189}
a4ef3e4d 190
d5cc4be2
LP
191static int write_options(FILE *f, const char *options) {
192 _cleanup_free_ char *o = NULL;
193
194 if (isempty(options))
195 return 0;
196
197 if (streq(options, "defaults"))
198 return 0;
199
98bad05e 200 o = specifier_escape(options);
d5cc4be2
LP
201 if (!o)
202 return log_oom();
203
204 fprintf(f, "Options=%s\n", o);
205 return 1;
206}
207
19d0833b
LP
208static int write_what(FILE *f, const char *what) {
209 _cleanup_free_ char *w = NULL;
210
98bad05e 211 w = specifier_escape(what);
19d0833b
LP
212 if (!w)
213 return log_oom();
214
215 fprintf(f, "What=%s\n", w);
216 return 1;
217}
218
5607d856 219static int add_swap(
7772c177 220 const char *source,
5607d856 221 const char *what,
cfeb4d37 222 const char *options,
d2194e15 223 MountPointFlags flags) {
5607d856 224
fb883e75 225 _cleanup_free_ char *name = NULL;
7fd1b19b 226 _cleanup_fclose_ FILE *f = NULL;
bf1d7ba7 227 int r;
6b1dc2bd
LP
228
229 assert(what);
6b1dc2bd 230
00b4ffde 231 if (access("/proc/swaps", F_OK) < 0) {
55365b0a 232 log_info("Swap not supported, ignoring swap entry for %s.", what);
00b4ffde
LP
233 return 0;
234 }
235
75f86906 236 if (detect_container() > 0) {
55365b0a 237 log_info("Running in a container, ignoring swap entry for %s.", what);
689aede8
LP
238 return 0;
239 }
240
028a981c
ZJS
241 if (arg_sysroot_check) {
242 log_info("%s should be enabled in the initrd, will request daemon-reload.", what);
243 return true;
244 }
245
256604cc
YW
246 log_debug("Found swap entry what=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
247 what,
248 yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS),
249 yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL));
250
7410616c
LP
251 r = unit_name_from_path(what, ".swap", &name);
252 if (r < 0)
253 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 254
7772c177 255 r = generator_open_unit_file(arg_dest, source, name, &f);
fb883e75
ZJS
256 if (r < 0)
257 return r;
0d536673 258
ed4ad488
ZJS
259 fprintf(f,
260 "[Unit]\n"
a7e88558
LP
261 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
262 "SourcePath=%s\n",
7772c177 263 source);
19d0833b 264
a7e88558
LP
265 r = generator_write_blockdev_dependency(f, what);
266 if (r < 0)
267 return r;
268
269 fprintf(f,
270 "\n"
271 "[Swap]\n");
272
19d0833b
LP
273 r = write_what(f, what);
274 if (r < 0)
275 return r;
6b1dc2bd 276
cfeb4d37 277 r = write_options(f, options);
d5cc4be2
LP
278 if (r < 0)
279 return r;
6b1dc2bd 280
47cb901e 281 r = fflush_and_check(f);
23bbb0de 282 if (r < 0)
fb883e75 283 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 284
b3208b66 285 /* use what as where, to have a nicer error message */
cfeb4d37 286 r = generator_write_timeouts(arg_dest, what, what, options, NULL);
b3208b66
ZJS
287 if (r < 0)
288 return r;
289
d2194e15 290 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
291 r = generator_hook_up_mkswap(arg_dest, what);
292 if (r < 0)
293 return r;
294 }
295
d2194e15 296 if (flags & MOUNT_GROWFS)
7f2806d5
ZJS
297 /* TODO: swap devices must be wiped and recreated */
298 log_warning("%s: growing swap devices is currently unsupported.", what);
04959faa
LP
299 if (flags & MOUNT_PCRFS)
300 log_warning("%s: measuring swap devices is currently unsupported.", what);
7f2806d5 301
d2194e15 302 if (!(flags & MOUNT_NOAUTO)) {
630d30d3 303 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
d2194e15 304 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
630d30d3
ZJS
305 if (r < 0)
306 return r;
4e82fe52
TG
307 }
308
028a981c 309 return true;
6b1dc2bd
LP
310}
311
cfeb4d37
YW
312static bool mount_is_network(const char *fstype, const char *options) {
313 return fstab_test_option(options, "_netdev\0") ||
314 (fstype && fstype_is_network(fstype));
6b1dc2bd
LP
315}
316
22f5a825 317static bool mount_in_initrd(const char *where, const char *options, bool accept_root) {
cfeb4d37 318 return fstab_test_option(options, "x-initrd.mount\0") ||
22f5a825 319 (where && PATH_IN_SET(where, "/usr", accept_root ? "/" : NULL));
3d22d1ab
TG
320}
321
33a4c983
LP
322static int write_timeout(
323 FILE *f,
324 const char *where,
325 const char *opts,
326 const char *filter,
327 const char *variable) {
328
deb0a77c 329 _cleanup_free_ char *timeout = NULL;
deb0a77c
MO
330 usec_t u;
331 int r;
332
ff0c31bc 333 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, NULL);
deb0a77c
MO
334 if (r < 0)
335 return log_warning_errno(r, "Failed to parse options: %m");
336 if (r == 0)
337 return 0;
338
0004f698 339 r = parse_sec_fix_0(timeout, &u);
deb0a77c
MO
340 if (r < 0) {
341 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
342 return 0;
343 }
344
5291f26d 345 fprintf(f, "%s=%s\n", variable, FORMAT_TIMESPAN(u, 0));
deb0a77c
MO
346
347 return 0;
348}
2e852276 349
110773f6
CH
350static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
351 return write_timeout(f, where, opts,
352 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
353}
354
355static int write_mount_timeout(FILE *f, const char *where, const char *opts) {
356 return write_timeout(f, where, opts,
357 "x-systemd.mount-timeout\0", "TimeoutSec");
358}
359
33a4c983
LP
360static int write_dependency(
361 FILE *f,
362 const char *opts,
363 const char *filter,
364 const char *format) {
365
3519d230
KZ
366 _cleanup_strv_free_ char **names = NULL, **units = NULL;
367 _cleanup_free_ char *res = NULL;
3519d230
KZ
368 int r;
369
370 assert(f);
371 assert(opts);
372
d6cef552 373 r = fstab_filter_options(opts, filter, NULL, NULL, &names, NULL);
3519d230
KZ
374 if (r < 0)
375 return log_warning_errno(r, "Failed to parse options: %m");
376 if (r == 0)
377 return 0;
378
379 STRV_FOREACH(s, names) {
380 char *x;
381
df7c4eb6 382 r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x);
3519d230
KZ
383 if (r < 0)
384 return log_error_errno(r, "Failed to generate unit name: %m");
33a4c983 385
3519d230
KZ
386 r = strv_consume(&units, x);
387 if (r < 0)
388 return log_oom();
389 }
390
391 if (units) {
392 res = strv_join(units, " ");
393 if (!res)
394 return log_oom();
56e577c6
LP
395
396 DISABLE_WARNING_FORMAT_NONLITERAL;
ae325185 397 fprintf(f, format, res);
56e577c6 398 REENABLE_WARNING;
3519d230
KZ
399 }
400
401 return 0;
402}
403
ae325185 404static int write_after(FILE *f, const char *opts) {
33a4c983 405 return write_dependency(f, opts,
d6cef552 406 "x-systemd.after\0", "After=%1$s\n");
ae325185
RB
407}
408
409static int write_requires_after(FILE *f, const char *opts) {
410 return write_dependency(f, opts,
d6cef552 411 "x-systemd.requires\0", "After=%1$s\nRequires=%1$s\n");
ae325185
RB
412}
413
414static int write_before(FILE *f, const char *opts) {
415 return write_dependency(f, opts,
d6cef552 416 "x-systemd.before\0", "Before=%1$s\n");
ae325185
RB
417}
418
9e615fa3 419static int write_mounts_for(const char *x_opt, const char *unit_setting, FILE *f, const char *opts) {
98bad05e 420 _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL;
3519d230
KZ
421 _cleanup_free_ char *res = NULL;
422 int r;
423
9e615fa3
LB
424 assert(x_opt);
425 assert(unit_setting);
3519d230
KZ
426 assert(f);
427 assert(opts);
428
9e615fa3 429 r = fstab_filter_options(opts, x_opt, NULL, NULL, &paths, NULL);
3519d230
KZ
430 if (r < 0)
431 return log_warning_errno(r, "Failed to parse options: %m");
432 if (r == 0)
433 return 0;
434
98bad05e
LP
435 r = specifier_escape_strv(paths, &paths_escaped);
436 if (r < 0)
437 return log_error_errno(r, "Failed to escape paths: %m");
438
439 res = strv_join(paths_escaped, " ");
3519d230
KZ
440 if (!res)
441 return log_oom();
442
9e615fa3 443 fprintf(f, "%s=%s\n", unit_setting, res);
3519d230
KZ
444
445 return 0;
446}
447
6371e69b
FB
448static int write_extra_dependencies(FILE *f, const char *opts) {
449 int r;
450
451 assert(f);
452
453 if (opts) {
454 r = write_after(f, opts);
455 if (r < 0)
456 return r;
457 r = write_requires_after(f, opts);
458 if (r < 0)
459 return r;
460 r = write_before(f, opts);
461 if (r < 0)
462 return r;
9e615fa3
LB
463 r = write_mounts_for("x-systemd.requires-mounts-for\0", "RequiresMountsFor", f, opts);
464 if (r < 0)
465 return r;
466 r = write_mounts_for("x-systemd.wants-mounts-for\0", "WantsMountsFor", f, opts);
6371e69b
FB
467 if (r < 0)
468 return r;
469 }
470
471 return 0;
472}
473
b3ee0148
MY
474static int mandatory_mount_drop_unapplicable_options(
475 MountPointFlags *flags,
476 const char *where,
477 const char *options,
478 char **ret_options) {
479
480 int r;
481
482 assert(flags);
483 assert(where);
484 assert(options);
485 assert(ret_options);
486
487 if (!(*flags & (MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT))) {
488 _cleanup_free_ char *opts = NULL;
489
490 opts = strdup(options);
491 if (!opts)
492 return -ENOMEM;
493
494 *ret_options = TAKE_PTR(opts);
495 return 0;
496 }
497
498 log_debug("Mount '%s' is mandatory, ignoring 'noauto', 'nofail', and 'x-systemd.automount' options.",
499 where);
500
501 *flags &= ~(MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT);
502
503 r = fstab_filter_options(options, "noauto\0nofail\0x-systemd.automount\0", NULL, NULL, NULL, ret_options);
504 if (r < 0)
505 return r;
506
507 return 1;
508}
509
e8d2f6cd 510static int add_mount(
7772c177 511 const char *source,
91214a37 512 const char *dest,
e8d2f6cd
LP
513 const char *what,
514 const char *where,
634735b5 515 const char *original_where,
6db615c1 516 const char *fstype,
e8d2f6cd
LP
517 const char *opts,
518 int passno,
d2194e15 519 MountPointFlags flags,
da69e8e4 520 const char *target_unit) {
e48fdd84 521
b3ee0148
MY
522 _cleanup_free_ char *name = NULL, *automount_name = NULL, *filtered = NULL, *where_escaped = NULL,
523 *opts_root_filtered = NULL;
be02c1cf 524 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 525 _cleanup_fclose_ FILE *f = NULL;
94192cda 526 int r;
6b1dc2bd
LP
527
528 assert(what);
529 assert(where);
5e398e54 530 assert(opts);
da69e8e4 531 assert(target_unit);
5e398e54 532 assert(source);
6b1dc2bd 533
6db615c1 534 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
535 return 0;
536
537 if (!is_path(where)) {
538 log_warning("Mount point %s is not a valid path, ignoring.", where);
539 return 0;
540 }
541
542 if (mount_point_is_api(where) ||
543 mount_point_ignore(where))
544 return 0;
545
028a981c
ZJS
546 if (arg_sysroot_check) {
547 log_info("%s should be mounted in the initrd, will request daemon-reload.", where);
548 return true;
549 }
550
d6cef552 551 r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL);
be02c1cf
AR
552 if (r < 0)
553 return r;
554
d6cef552 555 r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL);
be02c1cf
AR
556 if (r < 0)
557 return r;
558
e48fdd84 559 if (path_equal(where, "/")) {
b3ee0148
MY
560 r = mandatory_mount_drop_unapplicable_options(&flags, where, opts, &opts_root_filtered);
561 if (r < 0)
562 return r;
563 opts = opts_root_filtered;
564
be02c1cf 565 if (!strv_isempty(wanted_by))
b3ee0148 566 log_debug("Ignoring 'x-systemd.wanted-by=' option for root device.");
be02c1cf 567 if (!strv_isempty(required_by))
b3ee0148 568 log_debug("Ignoring 'x-systemd.required-by=' option for root device.");
2e852276 569
be02c1cf
AR
570 required_by = strv_free(required_by);
571 wanted_by = strv_free(wanted_by);
e48fdd84
LP
572 }
573
7410616c
LP
574 r = unit_name_from_path(where, ".mount", &name);
575 if (r < 0)
576 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 577
7772c177 578 r = generator_open_unit_file(dest, source, name, &f);
fb883e75
ZJS
579 if (r < 0)
580 return r;
0d536673 581
5e398e54 582 fprintf(f,
c3834f9b 583 "[Unit]\n"
a7e88558
LP
584 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
585 "SourcePath=%s\n",
c3834f9b 586 source);
6b1dc2bd 587
d2194e15 588 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) &&
65e1dee7
N
589 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
590 /* The default retry timeout that mount.nfs uses for 'bg' mounts
591 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
592 * As we are making 'bg' mounts look like an 'fg' mount to
593 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
594 * we need to explicitly preserve that default, and also ensure
595 * the systemd mount-timeout doesn't interfere.
5a12d1ca 596 * By placing these options first, they can be overridden by
65e1dee7 597 * settings in /etc/fstab. */
e66d2eee 598 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
d2194e15 599 SET_FLAG(flags, MOUNT_NOFAIL, true);
65e1dee7
N
600 }
601
6371e69b
FB
602 r = write_extra_dependencies(f, opts);
603 if (r < 0)
604 return r;
3519d230 605
da69e8e4 606 /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
fa138f5e 607 * target unit won't affect us. */
da69e8e4
ZJS
608 if (!FLAGS_SET(flags, MOUNT_NOFAIL))
609 fprintf(f, "Before=%s\n", target_unit);
fa138f5e 610
e48fdd84 611 if (passno != 0) {
91214a37 612 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
613 if (r < 0)
614 return r;
615 }
64e70e4b 616
a7e88558
LP
617 r = generator_write_blockdev_dependency(f, what);
618 if (r < 0)
619 return r;
620
621 fprintf(f,
622 "\n"
623 "[Mount]\n");
624
b2efed52
ZJS
625 r = write_what(f, what);
626 if (r < 0)
627 return r;
628
634735b5
CW
629 if (original_where)
630 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
631
632 where_escaped = specifier_escape(where);
633 if (!where_escaped)
634 return log_oom();
635 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 636
98bad05e 637 if (!isempty(fstype) && !streq(fstype, "auto")) {
c2b2df60 638 _cleanup_free_ char *t = NULL;
98bad05e
LP
639
640 t = specifier_escape(fstype);
641 if (!t)
642 return -ENOMEM;
643
644 fprintf(f, "Type=%s\n", t);
645 }
6b1dc2bd 646
91214a37 647 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
648 if (r < 0)
649 return r;
650
4195077a
MK
651 r = generator_write_device_deps(dest, what, where, opts);
652 if (r < 0)
653 return r;
654
9cf22035
LF
655 if (in_initrd() && path_equal(where, "/sysroot") && is_device_path(what)) {
656 r = generator_write_initrd_root_device_deps(dest, what);
657 if (r < 0)
658 return r;
659 }
660
110773f6
CH
661 r = write_mount_timeout(f, where, opts);
662 if (r < 0)
663 return r;
664
d5cc4be2
LP
665 r = write_options(f, filtered);
666 if (r < 0)
667 return r;
5e398e54 668
d2194e15 669 if (flags & MOUNT_RW_ONLY)
f42aa416
MH
670 fprintf(f, "ReadWriteOnly=yes\n");
671
4652c56c
ZJS
672 r = fflush_and_check(f);
673 if (r < 0)
fb883e75 674 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 675
d2194e15 676 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
677 r = generator_hook_up_mkfs(dest, what, where, fstype);
678 if (r < 0)
679 return r;
680 }
681
d2194e15 682 if (flags & MOUNT_GROWFS) {
da69e8e4 683 r = generator_hook_up_growfs(dest, where, target_unit);
7f2806d5
ZJS
684 if (r < 0)
685 return r;
686 }
687
04959faa 688 if (flags & MOUNT_PCRFS) {
be8f478c 689 r = efi_measured_uki(LOG_WARNING);
2f6c52b9 690 if (r == 0)
6c51b49c 691 log_debug("Kernel stub did not measure kernel image into PCR, skipping userspace measurement, too.");
2f6c52b9 692 else if (r > 0) {
6c51b49c
LP
693 r = generator_hook_up_pcrfs(dest, where, target_unit);
694 if (r < 0)
695 return r;
696 }
04959faa
LP
697 }
698
d2194e15
LP
699 if (!FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
700 if (!FLAGS_SET(flags, MOUNT_NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
da69e8e4 701 r = generator_add_symlink(dest, target_unit,
d2194e15 702 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
be02c1cf
AR
703 if (r < 0)
704 return r;
705 } else {
be02c1cf
AR
706 STRV_FOREACH(s, wanted_by) {
707 r = generator_add_symlink(dest, *s, "wants", name);
708 if (r < 0)
709 return r;
710 }
711
712 STRV_FOREACH(s, required_by) {
713 r = generator_add_symlink(dest, *s, "requires", name);
714 if (r < 0)
715 return r;
716 }
717 }
718 } else {
7410616c
LP
719 r = unit_name_from_path(where, ".automount", &automount_name);
720 if (r < 0)
721 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 722
8a7033ac 723 f = safe_fclose(f);
6b1dc2bd 724
7772c177 725 r = generator_open_unit_file(dest, source, automount_name, &f);
fb883e75
ZJS
726 if (r < 0)
727 return r;
0d536673 728
6b1dc2bd 729 fprintf(f,
6b1dc2bd 730 "[Unit]\n"
c3834f9b
LP
731 "SourcePath=%s\n"
732 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
733 source);
734
700e07ff 735 fprintf(f,
bd10a84b 736 "\n"
6b1dc2bd
LP
737 "[Automount]\n"
738 "Where=%s\n",
98bad05e 739 where_escaped);
6b1dc2bd 740
336b5c61 741 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
742 if (r < 0)
743 return r;
744
4652c56c
ZJS
745 r = fflush_and_check(f);
746 if (r < 0)
fb883e75 747 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
6b1dc2bd 748
da69e8e4 749 r = generator_add_symlink(dest, target_unit,
d2194e15 750 (flags & MOUNT_NOFAIL) ? "wants" : "requires", automount_name);
630d30d3
ZJS
751 if (r < 0)
752 return r;
6b1dc2bd
LP
753 }
754
028a981c
ZJS
755 return true;
756}
757
758static int do_daemon_reload(void) {
759 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
028a981c
ZJS
760 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
761 int r, k;
762
763 log_debug("Calling org.freedesktop.systemd1.Manager.Reload()...");
764
765 r = bus_connect_system_systemd(&bus);
766 if (r < 0)
767 return log_error_errno(r, "Failed to get D-Bus connection: %m");
768
a9399358 769 r = bus_service_manager_reload(bus);
028a981c 770 if (r < 0)
a9399358 771 return r;
028a981c
ZJS
772
773 /* We need to requeue the two targets so that any new units which previously were not part of the
774 * targets, and which we now added, will be started. */
775
776 r = 0;
777 FOREACH_STRING(unit, SPECIAL_INITRD_FS_TARGET, SPECIAL_SWAP_TARGET) {
778 log_info("Requesting %s/start/replace...", unit);
779
59228d0d 780 k = bus_call_method(bus, bus_systemd_mgr, "StartUnit", &error, NULL, "ss", unit, "replace");
028a981c
ZJS
781 if (k < 0) {
782 log_error_errno(k, "Failed to (re)start %s: %s", unit, bus_error_message(&error, r));
9ef648cc 783 RET_GATHER(r, k);
028a981c
ZJS
784 }
785 }
786
787 return r;
6b1dc2bd
LP
788}
789
99e3d476
ZJS
790static const char* sysroot_fstab_path(void) {
791 return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
792}
793
cfeb4d37
YW
794static bool sysfs_check(void) {
795 static int cached = -1;
796 int r;
797
798 if (cached < 0) {
799 r = getenv_bool_secure("SYSTEMD_SYSFS_CHECK");
800 if (r < 0 && r != -ENXIO)
801 log_debug_errno(r, "Failed to parse $SYSTEMD_SYSFS_CHECK, ignoring: %m");
802 cached = r != 0;
803 }
804
805 return cached;
806}
807
dfce61dd
LF
808static int add_sysusr_sysroot_usr_bind_mount(const char *source) {
809 return add_mount(source,
810 arg_dest,
811 "/sysusr/usr",
812 "/sysroot/usr",
813 NULL,
814 NULL,
815 "bind",
816 0,
817 0,
818 SPECIAL_INITRD_FS_TARGET);
819}
820
cfeb4d37
YW
821static MountPointFlags fstab_options_to_flags(const char *options, bool is_swap) {
822 MountPointFlags flags = 0;
823
824 if (fstab_test_option(options, "x-systemd.makefs\0"))
825 flags |= MOUNT_MAKEFS;
826 if (fstab_test_option(options, "x-systemd.growfs\0"))
827 flags |= MOUNT_GROWFS;
828 if (fstab_test_option(options, "x-systemd.pcrfs\0"))
829 flags |= MOUNT_PCRFS;
830 if (fstab_test_yes_no_option(options, "noauto\0" "auto\0"))
831 flags |= MOUNT_NOAUTO;
832 if (fstab_test_yes_no_option(options, "nofail\0" "fail\0"))
833 flags |= MOUNT_NOFAIL;
834
835 if (!is_swap) {
836 if (fstab_test_option(options, "x-systemd.rw-only\0"))
837 flags |= MOUNT_RW_ONLY;
838 if (fstab_test_option(options,
839 "comment=systemd.automount\0"
840 "x-systemd.automount\0"))
841 flags |= MOUNT_AUTOMOUNT;
842 }
843
844 return flags;
845}
846
8f88e573 847static int canonicalize_mount_path(const char *path, const char *type, bool prefix_sysroot, char **ret) {
b5fd3956
MY
848 _cleanup_free_ char *p = NULL;
849 bool changed;
850 int r;
851
852 assert(path);
853 assert(type);
854 assert(STR_IN_SET(type, "where", "what"));
855 assert(ret);
856
857 // FIXME: when chase() learns to chase non-existent paths, use this here and drop the prefixing with
858 // /sysroot on error below.
8f88e573 859 r = chase(path, prefix_sysroot ? "/sysroot" : NULL, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &p, NULL);
b5fd3956
MY
860 if (r < 0) {
861 log_debug_errno(r, "Failed to chase '%s', using as-is: %m", path);
862
8f88e573 863 if (prefix_sysroot)
b5fd3956
MY
864 p = path_join("/sysroot", path);
865 else
866 p = strdup(path);
867 if (!p)
868 return log_oom();
869
870 path_simplify(p);
871 }
872
873 changed = !streq(path, p);
874 if (changed)
875 log_debug("Canonicalized %s=%s to %s", type, path, p);
876
877 *ret = TAKE_PTR(p);
878 return changed;
879}
880
cfeb4d37
YW
881static int parse_fstab_one(
882 const char *source,
883 const char *what_original,
884 const char *where_original,
885 const char *fstype,
886 const char *options,
887 int passno,
8f88e573 888 bool prefix_sysroot,
22f5a825 889 bool accept_root, /* This takes an effect only when prefix_sysroot is true. */
55365b0a 890 bool use_swap_enabled) {
cfeb4d37 891
1e9b2e4f 892 _cleanup_free_ char *what = NULL, *where = NULL, *opts = NULL;
cfeb4d37 893 MountPointFlags flags;
b5fd3956 894 bool is_swap, where_changed;
cfeb4d37
YW
895 int r;
896
897 assert(what_original);
cfeb4d37
YW
898 assert(fstype);
899 assert(options);
900
22f5a825 901 if (prefix_sysroot && !mount_in_initrd(where_original, options, accept_root))
cfeb4d37
YW
902 return 0;
903
256604cc 904 is_swap = streq_ptr(fstype, "swap");
55365b0a 905 if (is_swap && use_swap_enabled && !arg_swap_enabled) {
3aed2593 906 log_info("Swap unit generation disabled on kernel command line, ignoring swap entry for %s.", what_original);
94456233
YW
907 return 0;
908 }
256604cc 909
cfeb4d37
YW
910 what = fstab_node_to_udev_node(what_original);
911 if (!what)
912 return log_oom();
913
914 if (path_is_read_only_fs("/sys") > 0 &&
915 (streq(what, "sysfs") ||
916 (sysfs_check() && is_device_path(what)))) {
917 log_info("/sys/ is read-only (running in a container?), ignoring mount for %s.", what);
918 return 0;
919 }
920
256604cc
YW
921 flags = fstab_options_to_flags(options, is_swap);
922
923 if (is_swap)
924 return add_swap(source, what, options, flags);
925
239cce38
YW
926 if (passno < 0)
927 passno = is_device_path(what);
928
256604cc
YW
929 assert(where_original); /* 'where' is not necessary for swap entry. */
930
6742eca1 931 if (!is_path(where_original)) {
b5fd3956 932 log_warning("Mount point %s is not a valid path, ignoring.", where_original);
6742eca1
YW
933 return 0;
934 }
cfeb4d37 935
6742eca1
YW
936 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
937 * mount units, but causes problems since it historically worked to have symlinks in e.g.
938 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
939 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
b5fd3956 940 * target is the final directory. */
8f88e573 941 r = canonicalize_mount_path(where_original, "where", prefix_sysroot, &where);
b5fd3956
MY
942 if (r < 0)
943 return r;
944 where_changed = r > 0;
cfeb4d37 945
8f88e573 946 if (prefix_sysroot && fstab_is_bind(options, fstype)) {
b5fd3956
MY
947 /* When in initrd, the source of bind mount needs to be prepended with /sysroot as well. */
948 _cleanup_free_ char *p = NULL;
6742eca1 949
8f88e573 950 r = canonicalize_mount_path(what, "what", prefix_sysroot, &p);
b5fd3956
MY
951 if (r < 0)
952 return r;
cfeb4d37 953
b5fd3956
MY
954 free_and_replace(what, p);
955 }
6742eca1 956
cfeb4d37
YW
957 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
958 what, where, strna(fstype),
959 yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS),
960 yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL));
961
b5fd3956 962 bool is_sysroot = in_initrd() && path_equal(where, "/sysroot");
cfeb4d37
YW
963 /* See comment from add_sysroot_usr_mount() about the need for extra indirection in case /usr needs
964 * to be mounted in order for the root fs to be synthesized based on configuration included in /usr/,
965 * e.g. systemd-repart. */
b5fd3956 966 bool is_sysroot_usr = in_initrd() && path_equal(where, "/sysroot/usr");
cfeb4d37
YW
967
968 const char *target_unit =
cfeb4d37
YW
969 is_sysroot ? SPECIAL_INITRD_ROOT_FS_TARGET :
970 is_sysroot_usr ? SPECIAL_INITRD_USR_FS_TARGET :
b93d9e06 971 prefix_sysroot ? SPECIAL_INITRD_FS_TARGET :
cfeb4d37
YW
972 mount_is_network(fstype, options) ? SPECIAL_REMOTE_FS_TARGET :
973 SPECIAL_LOCAL_FS_TARGET;
974
b3ee0148
MY
975 /* nofail, noauto and x-systemd.automount don't make sense for critical filesystems we must mount in initrd. */
976 if (is_sysroot || is_sysroot_usr) {
977 r = mandatory_mount_drop_unapplicable_options(&flags, where, options, &opts);
1e9b2e4f
MS
978 if (r < 0)
979 return r;
1e9b2e4f
MS
980 options = opts;
981 }
982
cfeb4d37
YW
983 r = add_mount(source,
984 arg_dest,
985 what,
b5fd3956
MY
986 is_sysroot_usr ? "/sysusr/usr" : where,
987 !is_sysroot_usr && where_changed ? where_original : NULL,
cfeb4d37
YW
988 fstype,
989 options,
990 passno,
991 flags,
992 target_unit);
993 if (r <= 0)
994 return r;
995
996 if (is_sysroot_usr) {
997 log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind");
998 r = add_sysusr_sysroot_usr_bind_mount(source);
999 if (r < 0)
1000 return r;
1001 }
1002
1003 return true;
1004}
1005
8f88e573 1006static int parse_fstab(bool prefix_sysroot) {
6db615c1 1007 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 1008 const char *fstab;
6b1dc2bd 1009 struct mntent *me;
cfeb4d37 1010 int r, ret = 0;
6b1dc2bd 1011
8f88e573 1012 if (prefix_sysroot)
99e3d476 1013 fstab = sysroot_fstab_path();
028a981c 1014 else {
99e3d476 1015 fstab = fstab_path();
028a981c
ZJS
1016 assert(!arg_sysroot_check);
1017 }
99e3d476 1018
ed4ad488 1019 log_debug("Parsing %s...", fstab);
01a0f7d0 1020
ed4ad488 1021 f = setmntent(fstab, "re");
6b1dc2bd
LP
1022 if (!f) {
1023 if (errno == ENOENT)
1024 return 0;
1025
ed4ad488 1026 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
1027 }
1028
1029 while ((me = getmntent(f))) {
55365b0a
YW
1030 r = parse_fstab_one(fstab,
1031 me->mnt_fsname, me->mnt_dir, me->mnt_type, me->mnt_opts, me->mnt_passno,
22f5a825
YW
1032 prefix_sysroot,
1033 /* accept_root = */ false,
1034 /* use_swap_enabled = */ true);
cfeb4d37
YW
1035 if (r < 0 && ret >= 0)
1036 ret = r;
1037 if (arg_sysroot_check && r > 0)
028a981c 1038 return true; /* We found a mount or swap that would be started… */
6b1dc2bd
LP
1039 }
1040
cfeb4d37 1041 return ret;
6b1dc2bd
LP
1042}
1043
77b8e92d
YW
1044static int sysroot_is_nfsroot(void) {
1045 union in_addr_union u;
1046 const char *sep, *a;
1047 int r;
1048
1049 assert(arg_root_what);
1050
1051 /* From dracut.cmdline(7).
1052 *
1053 * root=[<server-ip>:]<root-dir>[:<nfs-options>]
1054 * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>],
1055 * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>],
1056 * root={dhcp|dhcp6}
1057 *
1058 * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server.
1059 * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options
1060 * can be appended with the prefix ":" or "," and are separated by ",". */
1061
1062 if (path_equal(arg_root_what, "/dev/nfs") ||
1063 STR_IN_SET(arg_root_what, "dhcp", "dhcp6") ||
1064 STARTSWITH_SET(arg_root_what, "nfs:", "nfs4:"))
1065 return true;
1066
1067 /* IPv6 address */
1068 if (arg_root_what[0] == '[') {
1069 sep = strchr(arg_root_what + 1, ']');
1070 if (!sep)
1071 return -EINVAL;
1072
ea1c9d3f 1073 a = strndupa_safe(arg_root_what + 1, sep - arg_root_what - 1);
77b8e92d
YW
1074
1075 r = in_addr_from_string(AF_INET6, a, &u);
1076 if (r < 0)
1077 return r;
1078
1079 return true;
1080 }
1081
1082 /* IPv4 address */
1083 sep = strchr(arg_root_what, ':');
1084 if (sep) {
ea1c9d3f 1085 a = strndupa_safe(arg_root_what, sep - arg_root_what);
77b8e92d
YW
1086
1087 if (in_addr_from_string(AF_INET, a, &u) >= 0)
1088 return true;
1089 }
1090
1091 /* root directory without address */
1092 return path_is_absolute(arg_root_what) && !path_startswith(arg_root_what, "/dev");
1093}
1094
2e852276 1095static int add_sysroot_mount(void) {
75a59316 1096 _cleanup_free_ char *what = NULL;
725ad3b0 1097 const char *opts, *fstype;
01fdfbb8
WF
1098 bool default_rw, makefs;
1099 MountPointFlags flags;
7163e1ca 1100 int r;
5e398e54 1101
093c2cfe
TH
1102 if (isempty(arg_root_what)) {
1103 log_debug("Could not find a root= entry on the kernel command line.");
1104 return 0;
135b5212 1105 }
5e398e54 1106
138f4c69 1107 if (streq(arg_root_what, "gpt-auto")) {
4981427c 1108 /* This is handled by gpt-auto-generator */
138f4c69
LP
1109 log_debug("Skipping root directory handling, as gpt-auto was requested.");
1110 return 0;
dfce61dd
LF
1111 } else if (streq(arg_root_what, "fstab")) {
1112 /* This is handled by parse_fstab */
1113 log_debug("Using initrd's fstab for /sysroot/ configuration.");
1114 return 0;
138f4c69
LP
1115 }
1116
77b8e92d
YW
1117 r = sysroot_is_nfsroot();
1118 if (r < 0)
1119 log_debug_errno(r, "Failed to determine if the root directory is on NFS, assuming not: %m");
1120 else if (r > 0) {
3ade16c9 1121 /* This is handled by the kernel or the initrd */
77b8e92d 1122 log_debug("Skipping root directory handling, as root on NFS was requested.");
155e1bb4
YW
1123 return 0;
1124 }
1125
1126 if (startswith(arg_root_what, "cifs://")) {
1127 log_debug("Skipping root directory handling, as root on CIFS was requested.");
1128 return 0;
1129 }
1130
1131 if (startswith(arg_root_what, "iscsi:")) {
1132 log_debug("Skipping root directory handling, as root on iSCSI was requested.");
1133 return 0;
1134 }
1135
1136 if (startswith(arg_root_what, "live:")) {
1137 log_debug("Skipping root directory handling, as root on live image was requested.");
3ade16c9
HH
1138 return 0;
1139 }
1140
725ad3b0
LP
1141 if (streq(arg_root_what, "tmpfs")) {
1142 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
1143
1144 what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
1145 if (!what)
1146 return log_oom();
1147
5a12d1ca 1148 fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
725ad3b0 1149
5a12d1ca 1150 default_rw = true; /* writable, unless overridden */;
725ad3b0
LP
1151 } else {
1152
1153 what = fstab_node_to_udev_node(arg_root_what);
1154 if (!what)
1155 return log_oom();
1156
1157 fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */
1158
5a12d1ca 1159 default_rw = false; /* read-only, unless overridden */
725ad3b0 1160 }
093c2cfe 1161
6db615c1 1162 if (!arg_root_options)
725ad3b0 1163 opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
75a59316 1164 else if (arg_root_rw >= 0 ||
d15d0333 1165 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 1166 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
1167 else
1168 opts = arg_root_options;
5e398e54 1169
e19ae92a 1170 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
7163e1ca 1171
01fdfbb8
WF
1172 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1173 flags = makefs * MOUNT_MAKEFS;
1174
7772c177
ZJS
1175 return add_mount("/proc/cmdline",
1176 arg_dest,
91214a37 1177 what,
6db615c1 1178 "/sysroot",
634735b5 1179 NULL,
725ad3b0 1180 fstype,
75a59316 1181 opts,
f113f8e3 1182 is_device_path(what) ? 1 : 0, /* passno */
04959faa 1183 flags, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
7772c177 1184 SPECIAL_INITRD_ROOT_FS_TARGET);
5e398e54
TG
1185}
1186
2e852276 1187static int add_sysroot_usr_mount(void) {
9f103625
TH
1188 _cleanup_free_ char *what = NULL;
1189 const char *opts;
01fdfbb8
WF
1190 bool makefs;
1191 MountPointFlags flags;
29a24ab2
LP
1192 int r;
1193
1194 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
1195 * know for sure something else did */
9f103625
TH
1196
1197 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
1198 return 0;
1199
1200 if (arg_root_what && !arg_usr_what) {
40472036 1201 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 1202 arg_usr_what = strdup(arg_root_what);
9f103625
TH
1203 if (!arg_usr_what)
1204 return log_oom();
1205 }
1206
1207 if (arg_root_fstype && !arg_usr_fstype) {
1208 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
1209 if (!arg_usr_fstype)
1210 return log_oom();
1211 }
1212
1213 if (arg_root_options && !arg_usr_options) {
1214 arg_usr_options = strdup(arg_root_options);
9f103625
TH
1215 if (!arg_usr_options)
1216 return log_oom();
1217 }
1218
29a24ab2 1219 if (isempty(arg_usr_what)) {
32fe629a 1220 log_debug("Could not find a mount.usr= entry on the kernel command line.");
9f103625 1221 return 0;
29a24ab2
LP
1222 }
1223
1224 if (streq(arg_usr_what, "gpt-auto")) {
1225 /* This is handled by the gpt-auto generator */
1226 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
1227 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
1228 * unit file is being created for the host /usr/ mount. */
dfce61dd
LF
1229 } else if (streq(arg_usr_what, "fstab")) {
1230 /* This is handled by parse_fstab */
1231 log_debug("Using initrd's fstab for /sysroot/usr/ configuration.");
1232 return 1; /* parse_fstab will generate a unit for this, hence report that a
1233 * unit file is being created for the host /usr/ mount. */
29a24ab2
LP
1234 }
1235
1236 if (path_equal(arg_usr_what, "/dev/nfs")) {
1237 /* This is handled by the initrd (if at all supported, that is) */
1238 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
1239 return 1; /* As above, report that NFS code will create the unit */
1240 }
9f103625
TH
1241
1242 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
1243 if (!what)
1244 return log_oom();
9f103625 1245
eb580002
MM
1246 if (!arg_usr_options)
1247 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 1248 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 1249 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
1250 else
1251 opts = arg_usr_options;
9f103625 1252
29a24ab2
LP
1253 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
1254 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
1255 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
1256 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
1257 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
1258 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
1259
1260 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts));
1261
01fdfbb8
WF
1262 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1263 flags = makefs * MOUNT_MAKEFS;
1264
7772c177
ZJS
1265 r = add_mount("/proc/cmdline",
1266 arg_dest,
29a24ab2
LP
1267 what,
1268 "/sysusr/usr",
1269 NULL,
1270 arg_usr_fstype,
1271 opts,
1272 is_device_path(what) ? 1 : 0, /* passno */
01fdfbb8 1273 flags,
7772c177 1274 SPECIAL_INITRD_USR_FS_TARGET);
29a24ab2
LP
1275 if (r < 0)
1276 return r;
1277
dfce61dd 1278 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
29a24ab2 1279
dfce61dd 1280 r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline");
29a24ab2
LP
1281 if (r < 0)
1282 return r;
1283
1284 return 1;
1285}
1286
1287static int add_sysroot_usr_mount_or_fallback(void) {
1288 int r;
1289
1290 r = add_sysroot_usr_mount();
1291 if (r != 0)
1292 return r;
1293
1294 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
1295 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
de47cd06 1296 * themselves after it get the guarantee that /usr/ is definitely mounted somewhere. */
29a24ab2
LP
1297
1298 return generator_add_symlink(
1299 arg_dest,
1300 SPECIAL_INITRD_USR_FS_TARGET,
1301 "requires",
1302 "sysroot.mount");
9f103625
TH
1303}
1304
91214a37 1305static int add_volatile_root(void) {
1de7f825 1306
91214a37 1307 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 1308 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 1309
1de7f825 1310 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 1311 return 0;
91214a37 1312
00bb366d 1313 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
835cf75a 1314 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
1315}
1316
1317static int add_volatile_var(void) {
1318
1319 if (arg_volatile_mode != VOLATILE_STATE)
1320 return 0;
1321
1322 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
1323
7772c177
ZJS
1324 return add_mount("/proc/cmdline",
1325 arg_dest_late,
91214a37
LP
1326 "tmpfs",
1327 "/var",
634735b5 1328 NULL,
91214a37 1329 "tmpfs",
7d85383e 1330 "mode=0755" TMPFS_LIMITS_VAR,
91214a37 1331 0,
4191418b 1332 0,
7772c177 1333 SPECIAL_LOCAL_FS_TARGET);
91214a37
LP
1334}
1335
55365b0a 1336static int add_mounts_from_cmdline(void) {
ba2f3ec8 1337 int r = 0;
55365b0a
YW
1338
1339 /* Handle each entries found in cmdline as a fstab entry. */
1340
1341 FOREACH_ARRAY(m, arg_mounts, arg_n_mounts) {
45c535dd
YW
1342 if (m->for_initrd && !in_initrd())
1343 continue;
1344
ba2f3ec8
MY
1345 RET_GATHER(r, parse_fstab_one("/proc/cmdline",
1346 m->what,
1347 m->where,
1348 m->fstype,
1349 m->options,
1350 /* passno = */ -1,
1351 /* prefix_sysroot = */ !m->for_initrd && in_initrd(),
1352 /* accept_root = */ true,
1353 /* use_swap_enabled = */ false));
55365b0a
YW
1354 }
1355
ba2f3ec8 1356 return r;
55365b0a
YW
1357}
1358
dfd10549 1359static int add_mounts_from_creds(bool prefix_sysroot) {
6ac62485
LP
1360 _cleanup_free_ void *b = NULL;
1361 struct mntent *me;
6ac62485 1362 size_t bs;
ba2f3ec8 1363 int r;
6ac62485 1364
dfd10549
YW
1365 assert(in_initrd() || !prefix_sysroot);
1366
6ac62485 1367 r = read_credential_with_decryption(
dfd10549 1368 in_initrd() && !prefix_sysroot ? "fstab.extra.initrd" : "fstab.extra",
6ac62485
LP
1369 &b, &bs);
1370 if (r <= 0)
1371 return r;
1372
1373 _cleanup_fclose_ FILE *f = NULL;
1374 f = fmemopen_unlocked(b, bs, "r");
1375 if (!f)
1376 return log_oom();
1377
ba2f3ec8 1378 r = 0;
6ac62485 1379
ba2f3ec8
MY
1380 while ((me = getmntent(f)))
1381 RET_GATHER(r, parse_fstab_one("/run/credentials",
1382 me->mnt_fsname,
1383 me->mnt_dir,
1384 me->mnt_type,
1385 me->mnt_opts,
1386 me->mnt_passno,
1387 /* prefix_sysroot = */ prefix_sysroot,
1388 /* accept_root = */ true,
1389 /* use_swap_enabled = */ true));
1390
1391 return r;
6ac62485
LP
1392}
1393
96287a49 1394static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 1395 int r;
94734142 1396
a8d3315b
YW
1397 assert(key);
1398
9f103625
TH
1399 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
1400 * instance should take precedence. In the case of multiple rootflags=
1401 * or usrflags= the arguments should be concatenated */
6db615c1 1402
1d84ad94 1403 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 1404
1d84ad94 1405 r = value ? parse_boolean(value) : 1;
141a79f4 1406 if (r < 0)
059cb385 1407 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 1408 else
b564224b 1409 arg_fstab_enabled = fstab_set_enabled(r);
94734142 1410
1d84ad94
LP
1411 } else if (streq(key, "root")) {
1412
1413 if (proc_cmdline_value_missing(key, value))
1414 return 0;
6db615c1 1415
338da501 1416 return free_and_strdup_warn(&arg_root_what, empty_to_null(value));
6db615c1 1417
1d84ad94
LP
1418 } else if (streq(key, "rootfstype")) {
1419
1420 if (proc_cmdline_value_missing(key, value))
1421 return 0;
6db615c1 1422
338da501 1423 return free_and_strdup_warn(&arg_root_fstype, empty_to_null(value));
6db615c1 1424
1d84ad94 1425 } else if (streq(key, "rootflags")) {
6db615c1 1426
1d84ad94
LP
1427 if (proc_cmdline_value_missing(key, value))
1428 return 0;
1429
c2bc710b 1430 if (!strextend_with_separator(&arg_root_options, ",", value))
6db615c1
LP
1431 return log_oom();
1432
2f3dfc6f
LP
1433 } else if (streq(key, "roothash")) {
1434
1435 if (proc_cmdline_value_missing(key, value))
1436 return 0;
1437
338da501 1438 return free_and_strdup_warn(&arg_root_hash, empty_to_null(value));
6db615c1 1439
1d84ad94
LP
1440 } else if (streq(key, "mount.usr")) {
1441
1442 if (proc_cmdline_value_missing(key, value))
1443 return 0;
9f103625 1444
338da501 1445 return free_and_strdup_warn(&arg_usr_what, empty_to_null(value));
9f103625 1446
1d84ad94
LP
1447 } else if (streq(key, "mount.usrfstype")) {
1448
1449 if (proc_cmdline_value_missing(key, value))
1450 return 0;
9f103625 1451
338da501 1452 return free_and_strdup_warn(&arg_usr_fstype, empty_to_null(value));
9f103625 1453
1d84ad94 1454 } else if (streq(key, "mount.usrflags")) {
9f103625 1455
1d84ad94
LP
1456 if (proc_cmdline_value_missing(key, value))
1457 return 0;
1458
c2bc710b 1459 if (!strextend_with_separator(&arg_usr_options, ",", value))
9f103625
TH
1460 return log_oom();
1461
c1b9e3df
MB
1462 } else if (streq(key, "usrhash")) {
1463
1464 if (proc_cmdline_value_missing(key, value))
1465 return 0;
1466
338da501 1467 return free_and_strdup_warn(&arg_usr_hash, empty_to_null(value));
c1b9e3df 1468
6db615c1
LP
1469 } else if (streq(key, "rw") && !value)
1470 arg_root_rw = true;
1471 else if (streq(key, "ro") && !value)
1472 arg_root_rw = false;
91214a37
LP
1473 else if (streq(key, "systemd.volatile")) {
1474 VolatileMode m;
1475
1476 if (value) {
1477 m = volatile_mode_from_string(value);
1478 if (m < 0)
7211c853 1479 log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value);
91214a37
LP
1480 else
1481 arg_volatile_mode = m;
1482 } else
1483 arg_volatile_mode = VOLATILE_YES;
567a5307 1484
1485 } else if (streq(key, "systemd.swap")) {
1486
1487 r = value ? parse_boolean(value) : 1;
1488 if (r < 0)
1489 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
1490 else
1491 arg_swap_enabled = r;
200268c6
DDM
1492
1493 } else if (streq(key, "systemd.verity")) {
1494
1495 r = value ? parse_boolean(value) : 1;
1496 if (r < 0)
1497 log_warning("Failed to parse systemd.verity= kernel command line switch %s. Ignoring.", value);
1498 else
1499 arg_verity = r;
55365b0a 1500
45c535dd 1501 } else if (STR_IN_SET(key, "systemd.mount-extra", "rd.systemd.mount-extra")) {
55365b0a
YW
1502
1503 if (proc_cmdline_value_missing(key, value))
1504 return 0;
1505
45c535dd 1506 r = mount_array_add(startswith(key, "rd."), value);
55365b0a
YW
1507 if (r < 0)
1508 log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value);
1509
45c535dd 1510 } else if (STR_IN_SET(key, "systemd.swap-extra", "rd.systemd.swap-extra")) {
55365b0a
YW
1511
1512 if (proc_cmdline_value_missing(key, value))
1513 return 0;
1514
45c535dd 1515 r = mount_array_add_swap(startswith(key, "rd."), value);
55365b0a
YW
1516 if (r < 0)
1517 log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value);
91214a37 1518 }
94734142 1519
d0aa9ce5 1520 return 0;
94734142
LP
1521}
1522
2130a2e5
LP
1523static int determine_device(
1524 char **what,
1525 int *rw,
1526 char **options,
1527 const char *hash,
1528 const char *name) {
2f3dfc6f 1529
c1b9e3df
MB
1530 assert(what);
1531 assert(name);
1532
1533 /* If we have a hash but no device then Verity is used, and we use the DM device. */
1534 if (*what)
2f3dfc6f
LP
1535 return 0;
1536
c1b9e3df 1537 if (!hash)
2f3dfc6f
LP
1538 return 0;
1539
200268c6
DDM
1540 if (!arg_verity)
1541 return 0;
1542
c1b9e3df
MB
1543 *what = path_join("/dev/mapper/", name);
1544 if (!*what)
2f3dfc6f
LP
1545 return log_oom();
1546
2130a2e5
LP
1547 /* Verity is always read-only */
1548 if (rw)
1549 *rw = false;
1550 if (options && !strextend_with_separator(options, ",", "ro"))
1551 return log_oom();
2f3dfc6f 1552
2130a2e5 1553 log_info("Using verity %s device %s.", name, *what);
2f3dfc6f
LP
1554 return 1;
1555}
1556
c1b9e3df 1557static int determine_root(void) {
2130a2e5 1558 return determine_device(&arg_root_what, &arg_root_rw, NULL, arg_root_hash, "root");
c1b9e3df
MB
1559}
1560
1561static int determine_usr(void) {
2130a2e5 1562 return determine_device(&arg_usr_what, NULL, &arg_usr_options, arg_usr_hash, "usr");
c1b9e3df
MB
1563}
1564
028a981c
ZJS
1565/* If arg_sysroot_check is false, run as generator in the usual fashion.
1566 * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount
1567 * in the initrd, and call daemon-reload. We will get reinvoked as a generator,
1568 * with /sysroot/etc/fstab available, and then we can write additional units based
1569 * on that file. */
1570static int run_generator(void) {
ba2f3ec8 1571 int r = 0;
6b1dc2bd 1572
1d84ad94 1573 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 1574 if (r < 0)
da927ba9 1575 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 1576
2f3dfc6f 1577 (void) determine_root();
c1b9e3df 1578 (void) determine_usr();
2f3dfc6f 1579
028a981c 1580 if (arg_sysroot_check) {
8f88e573 1581 r = parse_fstab(/* prefix_sysroot = */ true);
028a981c
ZJS
1582 if (r == 0)
1583 log_debug("Nothing interesting found, not doing daemon-reload.");
1584 if (r > 0)
1585 r = do_daemon_reload();
1586 return r;
1587 }
1588
ba2f3ec8
MY
1589 r = 0;
1590
9f103625
TH
1591 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1592 if (in_initrd()) {
ba2f3ec8 1593 RET_GATHER(r, add_sysroot_mount());
003cba39 1594
ba2f3ec8 1595 RET_GATHER(r, add_sysroot_usr_mount_or_fallback());
91214a37 1596
ba2f3ec8
MY
1597 RET_GATHER(r, add_volatile_root());
1598 } else
1599 RET_GATHER(r, add_volatile_var());
5e398e54 1600
e48fdd84
LP
1601 /* Honour /etc/fstab only when that's enabled */
1602 if (arg_fstab_enabled) {
e48fdd84 1603 /* Parse the local /etc/fstab, possibly from the initrd */
ba2f3ec8 1604 RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ false));
ac4785b0 1605
e48fdd84 1606 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e 1607 if (in_initrd())
ba2f3ec8 1608 RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ true));
9b69569d 1609 else
ba2f3ec8 1610 RET_GATHER(r, generator_enable_remount_fs_service(arg_dest));
e48fdd84 1611 }
6b1dc2bd 1612
ba2f3ec8 1613 RET_GATHER(r, add_mounts_from_cmdline());
55365b0a 1614
ba2f3ec8 1615 RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ false));
6ac62485 1616
ba2f3ec8
MY
1617 if (in_initrd())
1618 RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ true));
dfd10549 1619
ba2f3ec8 1620 return r;
6b1dc2bd 1621}
a4ef3e4d 1622
028a981c
ZJS
1623static int run(int argc, char **argv) {
1624 arg_sysroot_check = invoked_as(argv, "systemd-sysroot-fstab-check");
1625
1626 if (arg_sysroot_check) {
1627 /* Run as in systemd-sysroot-fstab-check mode */
1628 log_setup();
1629
1630 if (strv_length(argv) > 1)
1631 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1632 "This program takes no arguments.");
1633 if (!in_initrd())
1634 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1635 "This program is only useful in the initrd.");
1636 } else {
1637 /* Run in generator mode */
1638 log_setup_generator();
1639
1640 if (!IN_SET(strv_length(argv), 2, 4))
1641 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1642 "This program takes one or three arguments.");
1643
1644 arg_dest = ASSERT_PTR(argv[1]);
1645 arg_dest_late = ASSERT_PTR(argv[argc > 3 ? 3 : 1]);
1646 }
1647
1648 return run_generator();
1649}
1650
1651DEFINE_MAIN_FUNCTION(run);