]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fstab-generator/fstab-generator.c
Merge pull request #14618 from poettering/growfs-rootfs
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "fstab-util.h"
12 #include "generator.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "mkdir.h"
16 #include "mount-setup.h"
17 #include "mount-util.h"
18 #include "mountpoint-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "proc-cmdline.h"
22 #include "special.h"
23 #include "specifier.h"
24 #include "stat-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "unit-name.h"
28 #include "util.h"
29 #include "virt.h"
30 #include "volatile-util.h"
31
32 typedef enum MountpointFlags {
33 NOAUTO = 1 << 0,
34 NOFAIL = 1 << 1,
35 AUTOMOUNT = 1 << 2,
36 MAKEFS = 1 << 3,
37 GROWFS = 1 << 4,
38 } MountpointFlags;
39
40 static const char *arg_dest = NULL;
41 static const char *arg_dest_late = NULL;
42 static bool arg_fstab_enabled = true;
43 static char *arg_root_what = NULL;
44 static char *arg_root_fstype = NULL;
45 static char *arg_root_options = NULL;
46 static char *arg_root_hash = NULL;
47 static int arg_root_rw = -1;
48 static char *arg_usr_what = NULL;
49 static char *arg_usr_fstype = NULL;
50 static char *arg_usr_options = NULL;
51 static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
52
53 STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
54 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
55 STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
56 STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
57 STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
58 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
59 STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
60
61 static int write_options(FILE *f, const char *options) {
62 _cleanup_free_ char *o = NULL;
63
64 if (isempty(options))
65 return 0;
66
67 if (streq(options, "defaults"))
68 return 0;
69
70 o = specifier_escape(options);
71 if (!o)
72 return log_oom();
73
74 fprintf(f, "Options=%s\n", o);
75 return 1;
76 }
77
78 static int write_what(FILE *f, const char *what) {
79 _cleanup_free_ char *w = NULL;
80
81 w = specifier_escape(what);
82 if (!w)
83 return log_oom();
84
85 fprintf(f, "What=%s\n", w);
86 return 1;
87 }
88
89 static int add_swap(
90 const char *what,
91 struct mntent *me,
92 MountpointFlags flags) {
93
94 _cleanup_free_ char *name = NULL;
95 _cleanup_fclose_ FILE *f = NULL;
96 int r;
97
98 assert(what);
99 assert(me);
100
101 if (access("/proc/swaps", F_OK) < 0) {
102 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
103 return 0;
104 }
105
106 if (detect_container() > 0) {
107 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
108 return 0;
109 }
110
111 r = unit_name_from_path(what, ".swap", &name);
112 if (r < 0)
113 return log_error_errno(r, "Failed to generate unit name: %m");
114
115 r = generator_open_unit_file(arg_dest, fstab_path(), name, &f);
116 if (r < 0)
117 return r;
118
119 fprintf(f,
120 "[Unit]\n"
121 "SourcePath=%s\n"
122 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
123 "[Swap]\n",
124 fstab_path());
125
126 r = write_what(f, what);
127 if (r < 0)
128 return r;
129
130 r = write_options(f, me->mnt_opts);
131 if (r < 0)
132 return r;
133
134 r = fflush_and_check(f);
135 if (r < 0)
136 return log_error_errno(r, "Failed to write unit file %s: %m", name);
137
138 /* use what as where, to have a nicer error message */
139 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
140 if (r < 0)
141 return r;
142
143 if (flags & MAKEFS) {
144 r = generator_hook_up_mkswap(arg_dest, what);
145 if (r < 0)
146 return r;
147 }
148
149 if (flags & GROWFS)
150 /* TODO: swap devices must be wiped and recreated */
151 log_warning("%s: growing swap devices is currently unsupported.", what);
152
153 if (!(flags & NOAUTO)) {
154 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
155 (flags & NOFAIL) ? "wants" : "requires", name);
156 if (r < 0)
157 return r;
158 }
159
160 return 0;
161 }
162
163 static bool mount_is_network(struct mntent *me) {
164 assert(me);
165
166 return fstab_test_option(me->mnt_opts, "_netdev\0") ||
167 fstype_is_network(me->mnt_type);
168 }
169
170 static bool mount_in_initrd(struct mntent *me) {
171 assert(me);
172
173 return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
174 streq(me->mnt_dir, "/usr");
175 }
176
177 static int write_timeout(FILE *f, const char *where, const char *opts,
178 const char *filter, const char *variable) {
179 _cleanup_free_ char *timeout = NULL;
180 char timespan[FORMAT_TIMESPAN_MAX];
181 usec_t u;
182 int r;
183
184 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL);
185 if (r < 0)
186 return log_warning_errno(r, "Failed to parse options: %m");
187 if (r == 0)
188 return 0;
189
190 r = parse_sec_fix_0(timeout, &u);
191 if (r < 0) {
192 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
193 return 0;
194 }
195
196 fprintf(f, "%s=%s\n", variable, format_timespan(timespan, sizeof(timespan), u, 0));
197
198 return 0;
199 }
200
201 static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
202 return write_timeout(f, where, opts,
203 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
204 }
205
206 static int write_mount_timeout(FILE *f, const char *where, const char *opts) {
207 return write_timeout(f, where, opts,
208 "x-systemd.mount-timeout\0", "TimeoutSec");
209 }
210
211 static int write_dependency(FILE *f, const char *opts,
212 const char *filter, const char *format) {
213 _cleanup_strv_free_ char **names = NULL, **units = NULL;
214 _cleanup_free_ char *res = NULL;
215 char **s;
216 int r;
217
218 assert(f);
219 assert(opts);
220
221 r = fstab_extract_values(opts, filter, &names);
222 if (r < 0)
223 return log_warning_errno(r, "Failed to parse options: %m");
224 if (r == 0)
225 return 0;
226
227 STRV_FOREACH(s, names) {
228 char *x;
229
230 r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x);
231 if (r < 0)
232 return log_error_errno(r, "Failed to generate unit name: %m");
233 r = strv_consume(&units, x);
234 if (r < 0)
235 return log_oom();
236 }
237
238 if (units) {
239 res = strv_join(units, " ");
240 if (!res)
241 return log_oom();
242 #pragma GCC diagnostic push
243 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
244 fprintf(f, format, res);
245 #pragma GCC diagnostic pop
246 }
247
248 return 0;
249 }
250
251 static int write_after(FILE *f, const char *opts) {
252 return write_dependency(f, opts, "x-systemd.after", "After=%1$s\n");
253 }
254
255 static int write_requires_after(FILE *f, const char *opts) {
256 return write_dependency(f, opts,
257 "x-systemd.requires", "After=%1$s\nRequires=%1$s\n");
258 }
259
260 static int write_before(FILE *f, const char *opts) {
261 return write_dependency(f, opts,
262 "x-systemd.before", "Before=%1$s\n");
263 }
264
265 static int write_requires_mounts_for(FILE *f, const char *opts) {
266 _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL;
267 _cleanup_free_ char *res = NULL;
268 int r;
269
270 assert(f);
271 assert(opts);
272
273 r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths);
274 if (r < 0)
275 return log_warning_errno(r, "Failed to parse options: %m");
276 if (r == 0)
277 return 0;
278
279 r = specifier_escape_strv(paths, &paths_escaped);
280 if (r < 0)
281 return log_error_errno(r, "Failed to escape paths: %m");
282
283 res = strv_join(paths_escaped, " ");
284 if (!res)
285 return log_oom();
286
287 fprintf(f, "RequiresMountsFor=%s\n", res);
288
289 return 0;
290 }
291
292 static int add_mount(
293 const char *dest,
294 const char *what,
295 const char *where,
296 const char *original_where,
297 const char *fstype,
298 const char *opts,
299 int passno,
300 MountpointFlags flags,
301 const char *post,
302 const char *source) {
303
304 _cleanup_free_ char
305 *name = NULL,
306 *automount_name = NULL,
307 *filtered = NULL,
308 *where_escaped = NULL;
309 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
310 _cleanup_fclose_ FILE *f = NULL;
311 int r;
312
313 assert(what);
314 assert(where);
315 assert(opts);
316 assert(post);
317 assert(source);
318
319 if (streq_ptr(fstype, "autofs"))
320 return 0;
321
322 if (!is_path(where)) {
323 log_warning("Mount point %s is not a valid path, ignoring.", where);
324 return 0;
325 }
326
327 if (mount_point_is_api(where) ||
328 mount_point_ignore(where))
329 return 0;
330
331 r = fstab_extract_values(opts, "x-systemd.wanted-by", &wanted_by);
332 if (r < 0)
333 return r;
334
335 r = fstab_extract_values(opts, "x-systemd.required-by", &required_by);
336 if (r < 0)
337 return r;
338
339 if (path_equal(where, "/")) {
340 if (flags & NOAUTO)
341 log_warning("Ignoring \"noauto\" for root device");
342 if (flags & NOFAIL)
343 log_warning("Ignoring \"nofail\" for root device");
344 if (flags & AUTOMOUNT)
345 log_warning("Ignoring automount option for root device");
346 if (!strv_isempty(wanted_by))
347 log_warning("Ignoring \"x-systemd.wanted-by=\" for root device");
348 if (!strv_isempty(required_by))
349 log_warning("Ignoring \"x-systemd.required-by=\" for root device");
350
351 required_by = strv_free(required_by);
352 wanted_by = strv_free(wanted_by);
353 SET_FLAG(flags, NOAUTO | NOFAIL | AUTOMOUNT, false);
354 }
355
356 r = unit_name_from_path(where, ".mount", &name);
357 if (r < 0)
358 return log_error_errno(r, "Failed to generate unit name: %m");
359
360 r = generator_open_unit_file(dest, fstab_path(), name, &f);
361 if (r < 0)
362 return r;
363
364 fprintf(f,
365 "[Unit]\n"
366 "SourcePath=%s\n"
367 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
368 source);
369
370 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW, it's not
371 * technically part of the basic initrd filesystem itself, and so shouldn't inherit the default
372 * Before=local-fs.target dependency. */
373 if (in_initrd() && path_startswith(where, "/sysroot"))
374 fprintf(f, "DefaultDependencies=no\n");
375
376 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & AUTOMOUNT) &&
377 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
378 /* The default retry timeout that mount.nfs uses for 'bg' mounts
379 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
380 * As we are making 'bg' mounts look like an 'fg' mount to
381 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
382 * we need to explicitly preserve that default, and also ensure
383 * the systemd mount-timeout doesn't interfere.
384 * By placing these options first, they can be over-ridden by
385 * settings in /etc/fstab. */
386 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts, ",fg");
387 SET_FLAG(flags, NOFAIL, true);
388 }
389
390 if (!(flags & NOFAIL) && !(flags & AUTOMOUNT))
391 fprintf(f, "Before=%s\n", post);
392
393 if (!(flags & AUTOMOUNT) && opts) {
394 r = write_after(f, opts);
395 if (r < 0)
396 return r;
397 r = write_requires_after(f, opts);
398 if (r < 0)
399 return r;
400 r = write_before(f, opts);
401 if (r < 0)
402 return r;
403 r = write_requires_mounts_for(f, opts);
404 if (r < 0)
405 return r;
406 }
407
408 if (passno != 0) {
409 r = generator_write_fsck_deps(f, dest, what, where, fstype);
410 if (r < 0)
411 return r;
412 }
413
414 fprintf(f, "\n[Mount]\n");
415 if (original_where)
416 fprintf(f, "# Canonicalized from %s\n", original_where);
417
418 where_escaped = specifier_escape(where);
419 if (!where_escaped)
420 return log_oom();
421 fprintf(f, "Where=%s\n", where_escaped);
422
423 r = write_what(f, what);
424 if (r < 0)
425 return r;
426
427 if (!isempty(fstype) && !streq(fstype, "auto")) {
428 _cleanup_free_ char *t;
429
430 t = specifier_escape(fstype);
431 if (!t)
432 return -ENOMEM;
433
434 fprintf(f, "Type=%s\n", t);
435 }
436
437 r = generator_write_timeouts(dest, what, where, opts, &filtered);
438 if (r < 0)
439 return r;
440
441 r = generator_write_device_deps(dest, what, where, opts);
442 if (r < 0)
443 return r;
444
445 r = write_mount_timeout(f, where, opts);
446 if (r < 0)
447 return r;
448
449 r = write_options(f, filtered);
450 if (r < 0)
451 return r;
452
453 r = fflush_and_check(f);
454 if (r < 0)
455 return log_error_errno(r, "Failed to write unit file %s: %m", name);
456
457 if (flags & MAKEFS) {
458 r = generator_hook_up_mkfs(dest, what, where, fstype);
459 if (r < 0)
460 return r;
461 }
462
463 if (flags & GROWFS) {
464 r = generator_hook_up_growfs(dest, where, post);
465 if (r < 0)
466 return r;
467 }
468
469 if (!FLAGS_SET(flags, AUTOMOUNT)) {
470 if (!FLAGS_SET(flags, NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
471 r = generator_add_symlink(dest, post,
472 (flags & NOFAIL) ? "wants" : "requires", name);
473 if (r < 0)
474 return r;
475 } else {
476 char **s;
477
478 STRV_FOREACH(s, wanted_by) {
479 r = generator_add_symlink(dest, *s, "wants", name);
480 if (r < 0)
481 return r;
482 }
483
484 STRV_FOREACH(s, required_by) {
485 r = generator_add_symlink(dest, *s, "requires", name);
486 if (r < 0)
487 return r;
488 }
489 }
490 } else {
491 r = unit_name_from_path(where, ".automount", &automount_name);
492 if (r < 0)
493 return log_error_errno(r, "Failed to generate unit name: %m");
494
495 f = safe_fclose(f);
496
497 r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
498 if (r < 0)
499 return r;
500
501 fprintf(f,
502 "[Unit]\n"
503 "SourcePath=%s\n"
504 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
505 source);
506
507 fprintf(f, "Before=%s\n", post);
508
509 if (opts) {
510 r = write_after(f, opts);
511 if (r < 0)
512 return r;
513 r = write_requires_after(f, opts);
514 if (r < 0)
515 return r;
516 r = write_before(f, opts);
517 if (r < 0)
518 return r;
519 r = write_requires_mounts_for(f, opts);
520 if (r < 0)
521 return r;
522 }
523
524 fprintf(f,
525 "\n"
526 "[Automount]\n"
527 "Where=%s\n",
528 where_escaped);
529
530 r = write_idle_timeout(f, where, opts);
531 if (r < 0)
532 return r;
533
534 r = fflush_and_check(f);
535 if (r < 0)
536 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
537
538 r = generator_add_symlink(dest, post,
539 (flags & NOFAIL) ? "wants" : "requires", automount_name);
540 if (r < 0)
541 return r;
542 }
543
544 return 0;
545 }
546
547 static int parse_fstab(bool initrd) {
548 _cleanup_endmntent_ FILE *f = NULL;
549 const char *fstab;
550 struct mntent *me;
551 int r = 0;
552
553 fstab = initrd ? "/sysroot/etc/fstab" : fstab_path();
554 log_debug("Parsing %s...", fstab);
555
556 f = setmntent(fstab, "re");
557 if (!f) {
558 if (errno == ENOENT)
559 return 0;
560
561 return log_error_errno(errno, "Failed to open %s: %m", fstab);
562 }
563
564 while ((me = getmntent(f))) {
565 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
566 bool makefs, growfs, noauto, nofail;
567 int k;
568
569 if (initrd && !mount_in_initrd(me))
570 continue;
571
572 what = fstab_node_to_udev_node(me->mnt_fsname);
573 if (!what)
574 return log_oom();
575
576 if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
577 log_info("Running in a container, ignoring fstab device entry for %s.", what);
578 continue;
579 }
580
581 where = strdup(me->mnt_dir);
582 if (!where)
583 return log_oom();
584
585 if (is_path(where)) {
586 path_simplify(where, false);
587
588 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
589 * mount units, but causes problems since it historically worked to have symlinks in e.g.
590 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
591 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
592 * target is the final directory. */
593 r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
594 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
595 &canonical_where, NULL);
596 if (r < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
597 log_debug_errno(r, "Failed to read symlink target for %s, ignoring: %m", where);
598 else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
599 canonical_where = mfree(canonical_where);
600 else
601 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
602 }
603
604 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
605 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
606 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
607 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
608
609 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
610 what, where, me->mnt_type,
611 yes_no(makefs), yes_no(growfs),
612 yes_no(noauto), yes_no(nofail));
613
614 if (streq(me->mnt_type, "swap"))
615 k = add_swap(what, me,
616 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL);
617 else {
618 bool automount;
619 const char *post;
620
621 automount = fstab_test_option(me->mnt_opts,
622 "comment=systemd.automount\0"
623 "x-systemd.automount\0");
624 if (initrd)
625 post = SPECIAL_INITRD_FS_TARGET;
626 else if (mount_is_network(me))
627 post = SPECIAL_REMOTE_FS_TARGET;
628 else
629 post = SPECIAL_LOCAL_FS_TARGET;
630
631 k = add_mount(arg_dest,
632 what,
633 canonical_where ?: where,
634 canonical_where ? where: NULL,
635 me->mnt_type,
636 me->mnt_opts,
637 me->mnt_passno,
638 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT,
639 post,
640 fstab);
641 }
642
643 if (r >= 0 && k < 0)
644 r = k;
645 }
646
647 return r;
648 }
649
650 static int add_sysroot_mount(void) {
651 _cleanup_free_ char *what = NULL;
652 const char *opts;
653 int r;
654
655 if (isempty(arg_root_what)) {
656 log_debug("Could not find a root= entry on the kernel command line.");
657 return 0;
658 }
659
660 if (streq(arg_root_what, "gpt-auto")) {
661 /* This is handled by the gpt-auto generator */
662 log_debug("Skipping root directory handling, as gpt-auto was requested.");
663 return 0;
664 }
665
666 if (path_equal(arg_root_what, "/dev/nfs")) {
667 /* This is handled by the kernel or the initrd */
668 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
669 return 0;
670 }
671
672 what = fstab_node_to_udev_node(arg_root_what);
673 if (!what)
674 return log_oom();
675
676 if (!arg_root_options)
677 opts = arg_root_rw > 0 ? "rw" : "ro";
678 else if (arg_root_rw >= 0 ||
679 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
680 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
681 else
682 opts = arg_root_options;
683
684 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
685
686 if (is_device_path(what)) {
687 r = generator_write_initrd_root_device_deps(arg_dest, what);
688 if (r < 0)
689 return r;
690 }
691
692 return add_mount(arg_dest,
693 what,
694 "/sysroot",
695 NULL,
696 arg_root_fstype,
697 opts,
698 is_device_path(what) ? 1 : 0, /* passno */
699 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
700 SPECIAL_INITRD_ROOT_FS_TARGET,
701 "/proc/cmdline");
702 }
703
704 static int add_sysroot_usr_mount(void) {
705 _cleanup_free_ char *what = NULL;
706 const char *opts;
707
708 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
709 return 0;
710
711 if (arg_root_what && !arg_usr_what) {
712 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
713 arg_usr_what = strdup(arg_root_what);
714 if (!arg_usr_what)
715 return log_oom();
716 }
717
718 if (arg_root_fstype && !arg_usr_fstype) {
719 arg_usr_fstype = strdup(arg_root_fstype);
720 if (!arg_usr_fstype)
721 return log_oom();
722 }
723
724 if (arg_root_options && !arg_usr_options) {
725 arg_usr_options = strdup(arg_root_options);
726 if (!arg_usr_options)
727 return log_oom();
728 }
729
730 if (!arg_usr_what)
731 return 0;
732
733 what = fstab_node_to_udev_node(arg_usr_what);
734 if (!what)
735 return log_oom();
736
737 if (!arg_usr_options)
738 opts = arg_root_rw > 0 ? "rw" : "ro";
739 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
740 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
741 else
742 opts = arg_usr_options;
743
744 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
745 return add_mount(arg_dest,
746 what,
747 "/sysroot/usr",
748 NULL,
749 arg_usr_fstype,
750 opts,
751 is_device_path(what) ? 1 : 0, /* passno */
752 0,
753 SPECIAL_INITRD_FS_TARGET,
754 "/proc/cmdline");
755 }
756
757 static int add_volatile_root(void) {
758
759 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
760 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
761
762 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
763 return 0;
764
765 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
766 SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE);
767 }
768
769 static int add_volatile_var(void) {
770
771 if (arg_volatile_mode != VOLATILE_STATE)
772 return 0;
773
774 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
775
776 return add_mount(arg_dest_late,
777 "tmpfs",
778 "/var",
779 NULL,
780 "tmpfs",
781 "mode=0755",
782 0,
783 0,
784 SPECIAL_LOCAL_FS_TARGET,
785 "/proc/cmdline");
786 }
787
788 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
789 int r;
790
791 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
792 * instance should take precedence. In the case of multiple rootflags=
793 * or usrflags= the arguments should be concatenated */
794
795 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
796
797 r = value ? parse_boolean(value) : 1;
798 if (r < 0)
799 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
800 else
801 arg_fstab_enabled = r;
802
803 } else if (streq(key, "root")) {
804
805 if (proc_cmdline_value_missing(key, value))
806 return 0;
807
808 if (free_and_strdup(&arg_root_what, value) < 0)
809 return log_oom();
810
811 } else if (streq(key, "rootfstype")) {
812
813 if (proc_cmdline_value_missing(key, value))
814 return 0;
815
816 if (free_and_strdup(&arg_root_fstype, value) < 0)
817 return log_oom();
818
819 } else if (streq(key, "rootflags")) {
820
821 if (proc_cmdline_value_missing(key, value))
822 return 0;
823
824 if (!strextend_with_separator(&arg_root_options, ",", value, NULL))
825 return log_oom();
826
827 } else if (streq(key, "roothash")) {
828
829 if (proc_cmdline_value_missing(key, value))
830 return 0;
831
832 if (free_and_strdup(&arg_root_hash, value) < 0)
833 return log_oom();
834
835 } else if (streq(key, "mount.usr")) {
836
837 if (proc_cmdline_value_missing(key, value))
838 return 0;
839
840 if (free_and_strdup(&arg_usr_what, value) < 0)
841 return log_oom();
842
843 } else if (streq(key, "mount.usrfstype")) {
844
845 if (proc_cmdline_value_missing(key, value))
846 return 0;
847
848 if (free_and_strdup(&arg_usr_fstype, value) < 0)
849 return log_oom();
850
851 } else if (streq(key, "mount.usrflags")) {
852
853 if (proc_cmdline_value_missing(key, value))
854 return 0;
855
856 if (!strextend_with_separator(&arg_usr_options, ",", value, NULL))
857 return log_oom();
858
859 } else if (streq(key, "rw") && !value)
860 arg_root_rw = true;
861 else if (streq(key, "ro") && !value)
862 arg_root_rw = false;
863 else if (streq(key, "systemd.volatile")) {
864 VolatileMode m;
865
866 if (value) {
867 m = volatile_mode_from_string(value);
868 if (m < 0)
869 log_warning("Failed to parse systemd.volatile= argument: %s", value);
870 else
871 arg_volatile_mode = m;
872 } else
873 arg_volatile_mode = VOLATILE_YES;
874 }
875
876 return 0;
877 }
878
879 static int determine_root(void) {
880 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
881
882 if (arg_root_what)
883 return 0;
884
885 if (!arg_root_hash)
886 return 0;
887
888 arg_root_what = strdup("/dev/mapper/root");
889 if (!arg_root_what)
890 return log_oom();
891
892 log_info("Using verity root device %s.", arg_root_what);
893
894 return 1;
895 }
896
897 static int run(const char *dest, const char *dest_early, const char *dest_late) {
898 int r, r2 = 0, r3 = 0;
899
900 assert_se(arg_dest = dest);
901 assert_se(arg_dest_late = dest_late);
902
903 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
904 if (r < 0)
905 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
906
907 (void) determine_root();
908
909 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
910 if (in_initrd()) {
911 r = add_sysroot_mount();
912
913 r2 = add_sysroot_usr_mount();
914
915 r3 = add_volatile_root();
916 } else
917 r = add_volatile_var();
918
919 /* Honour /etc/fstab only when that's enabled */
920 if (arg_fstab_enabled) {
921 /* Parse the local /etc/fstab, possibly from the initrd */
922 r2 = parse_fstab(false);
923
924 /* If running in the initrd also parse the /etc/fstab from the host */
925 if (in_initrd())
926 r3 = parse_fstab(true);
927 else
928 r3 = generator_enable_remount_fs_service(arg_dest);
929 }
930
931 return r < 0 ? r : r2 < 0 ? r2 : r3;
932 }
933
934 DEFINE_MAIN_GENERATOR_FUNCTION(run);