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