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