]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fstab-generator/fstab-generator.c
Merge pull request #20303 from andir/sysconfig-example
[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 "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 MOUNT_NOAUTO = 1 << 0,
34 MOUNT_NOFAIL = 1 << 1,
35 MOUNT_AUTOMOUNT = 1 << 2,
36 MOUNT_MAKEFS = 1 << 3,
37 MOUNT_GROWFS = 1 << 4,
38 MOUNT_RW_ONLY = 1 << 5,
39 } MountPointFlags;
40
41 static const char *arg_dest = NULL;
42 static const char *arg_dest_late = NULL;
43 static bool arg_fstab_enabled = true;
44 static bool arg_swap_enabled = true;
45 static char *arg_root_what = NULL;
46 static char *arg_root_fstype = NULL;
47 static char *arg_root_options = NULL;
48 static char *arg_root_hash = NULL;
49 static int arg_root_rw = -1;
50 static char *arg_usr_what = NULL;
51 static char *arg_usr_fstype = NULL;
52 static char *arg_usr_options = NULL;
53 static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
54
55 STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
56 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
57 STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
58 STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
59 STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
60 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
61 STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
62
63 static int write_options(FILE *f, const char *options) {
64 _cleanup_free_ char *o = NULL;
65
66 if (isempty(options))
67 return 0;
68
69 if (streq(options, "defaults"))
70 return 0;
71
72 o = specifier_escape(options);
73 if (!o)
74 return log_oom();
75
76 fprintf(f, "Options=%s\n", o);
77 return 1;
78 }
79
80 static int write_what(FILE *f, const char *what) {
81 _cleanup_free_ char *w = NULL;
82
83 w = specifier_escape(what);
84 if (!w)
85 return log_oom();
86
87 fprintf(f, "What=%s\n", w);
88 return 1;
89 }
90
91 static int add_swap(
92 const char *what,
93 struct mntent *me,
94 MountPointFlags flags) {
95
96 _cleanup_free_ char *name = NULL;
97 _cleanup_fclose_ FILE *f = NULL;
98 int r;
99
100 assert(what);
101 assert(me);
102
103 if (!arg_swap_enabled) {
104 log_info("Swap unit generation disabled on kernel command line, ignoring fstab swap entry for %s.", what);
105 return 0;
106 }
107
108 if (access("/proc/swaps", F_OK) < 0) {
109 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
110 return 0;
111 }
112
113 if (detect_container() > 0) {
114 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
115 return 0;
116 }
117
118 r = unit_name_from_path(what, ".swap", &name);
119 if (r < 0)
120 return log_error_errno(r, "Failed to generate unit name: %m");
121
122 r = generator_open_unit_file(arg_dest, fstab_path(), name, &f);
123 if (r < 0)
124 return r;
125
126 fprintf(f,
127 "[Unit]\n"
128 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
129 "SourcePath=%s\n",
130 fstab_path());
131
132 r = generator_write_blockdev_dependency(f, what);
133 if (r < 0)
134 return r;
135
136 fprintf(f,
137 "\n"
138 "[Swap]\n");
139
140 r = write_what(f, what);
141 if (r < 0)
142 return r;
143
144 r = write_options(f, me->mnt_opts);
145 if (r < 0)
146 return r;
147
148 r = fflush_and_check(f);
149 if (r < 0)
150 return log_error_errno(r, "Failed to write unit file %s: %m", name);
151
152 /* use what as where, to have a nicer error message */
153 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
154 if (r < 0)
155 return r;
156
157 if (flags & MOUNT_MAKEFS) {
158 r = generator_hook_up_mkswap(arg_dest, what);
159 if (r < 0)
160 return r;
161 }
162
163 if (flags & MOUNT_GROWFS)
164 /* TODO: swap devices must be wiped and recreated */
165 log_warning("%s: growing swap devices is currently unsupported.", what);
166
167 if (!(flags & MOUNT_NOAUTO)) {
168 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
169 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
170 if (r < 0)
171 return r;
172 }
173
174 return 0;
175 }
176
177 static bool mount_is_network(struct mntent *me) {
178 assert(me);
179
180 return fstab_test_option(me->mnt_opts, "_netdev\0") ||
181 fstype_is_network(me->mnt_type);
182 }
183
184 static bool mount_in_initrd(struct mntent *me) {
185 assert(me);
186
187 return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
188 streq(me->mnt_dir, "/usr");
189 }
190
191 static int write_timeout(
192 FILE *f,
193 const char *where,
194 const char *opts,
195 const char *filter,
196 const char *variable) {
197
198 _cleanup_free_ char *timeout = NULL;
199 usec_t u;
200 int r;
201
202 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, 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(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_filter_options(opts, filter, NULL, NULL, &names, NULL);
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
266 DISABLE_WARNING_FORMAT_NONLITERAL;
267 fprintf(f, format, res);
268 REENABLE_WARNING;
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\0", "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\0", "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\0", "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_filter_options(opts, "x-systemd.requires-mounts-for\0", NULL, NULL, &paths, NULL);
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 write_extra_dependencies(FILE *f, const char *opts) {
317 int r;
318
319 assert(f);
320
321 if (opts) {
322 r = write_after(f, opts);
323 if (r < 0)
324 return r;
325 r = write_requires_after(f, opts);
326 if (r < 0)
327 return r;
328 r = write_before(f, opts);
329 if (r < 0)
330 return r;
331 r = write_requires_mounts_for(f, opts);
332 if (r < 0)
333 return r;
334 }
335
336 return 0;
337 }
338
339 static int add_mount(
340 const char *dest,
341 const char *what,
342 const char *where,
343 const char *original_where,
344 const char *fstype,
345 const char *opts,
346 int passno,
347 MountPointFlags flags,
348 const char *post,
349 const char *source) {
350
351 _cleanup_free_ char
352 *name = NULL,
353 *automount_name = NULL,
354 *filtered = NULL,
355 *where_escaped = NULL;
356 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
357 _cleanup_fclose_ FILE *f = NULL;
358 int r;
359
360 assert(what);
361 assert(where);
362 assert(opts);
363 assert(post);
364 assert(source);
365
366 if (streq_ptr(fstype, "autofs"))
367 return 0;
368
369 if (!is_path(where)) {
370 log_warning("Mount point %s is not a valid path, ignoring.", where);
371 return 0;
372 }
373
374 if (mount_point_is_api(where) ||
375 mount_point_ignore(where))
376 return 0;
377
378 r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL);
379 if (r < 0)
380 return r;
381
382 r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL);
383 if (r < 0)
384 return r;
385
386 if (path_equal(where, "/")) {
387 if (flags & MOUNT_NOAUTO)
388 log_warning("Ignoring \"noauto\" option for root device");
389 if (flags & MOUNT_NOFAIL)
390 log_warning("Ignoring \"nofail\" option for root device");
391 if (flags & MOUNT_AUTOMOUNT)
392 log_warning("Ignoring \"automount\" option for root device");
393 if (!strv_isempty(wanted_by))
394 log_warning("Ignoring \"x-systemd.wanted-by=\" option for root device");
395 if (!strv_isempty(required_by))
396 log_warning("Ignoring \"x-systemd.required-by=\" option for root device");
397
398 required_by = strv_free(required_by);
399 wanted_by = strv_free(wanted_by);
400 SET_FLAG(flags, MOUNT_NOAUTO | MOUNT_NOFAIL | MOUNT_AUTOMOUNT, false);
401 }
402
403 r = unit_name_from_path(where, ".mount", &name);
404 if (r < 0)
405 return log_error_errno(r, "Failed to generate unit name: %m");
406
407 r = generator_open_unit_file(dest, fstab_path(), name, &f);
408 if (r < 0)
409 return r;
410
411 fprintf(f,
412 "[Unit]\n"
413 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
414 "SourcePath=%s\n",
415 source);
416
417 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) &&
418 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
419 /* The default retry timeout that mount.nfs uses for 'bg' mounts
420 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
421 * As we are making 'bg' mounts look like an 'fg' mount to
422 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
423 * we need to explicitly preserve that default, and also ensure
424 * the systemd mount-timeout doesn't interfere.
425 * By placing these options first, they can be overridden by
426 * settings in /etc/fstab. */
427 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
428 SET_FLAG(flags, MOUNT_NOFAIL, true);
429 }
430
431 r = write_extra_dependencies(f, opts);
432 if (r < 0)
433 return r;
434
435 /* Order the mount unit we generate relative to the post unit, so that DefaultDependencies= on the
436 * target unit won't affect us. */
437 if (post && !FLAGS_SET(flags, MOUNT_AUTOMOUNT) && !FLAGS_SET(flags, MOUNT_NOAUTO) &&
438 !FLAGS_SET(flags, MOUNT_NOFAIL))
439 fprintf(f, "Before=%s\n", post);
440
441 if (passno != 0) {
442 r = generator_write_fsck_deps(f, dest, what, where, fstype);
443 if (r < 0)
444 return r;
445 }
446
447 r = generator_write_blockdev_dependency(f, what);
448 if (r < 0)
449 return r;
450
451 fprintf(f,
452 "\n"
453 "[Mount]\n");
454
455 r = write_what(f, what);
456 if (r < 0)
457 return r;
458
459 if (original_where)
460 fprintf(f, "# Canonicalized from %s\n", original_where);
461
462 where_escaped = specifier_escape(where);
463 if (!where_escaped)
464 return log_oom();
465 fprintf(f, "Where=%s\n", where_escaped);
466
467 if (!isempty(fstype) && !streq(fstype, "auto")) {
468 _cleanup_free_ char *t = NULL;
469
470 t = specifier_escape(fstype);
471 if (!t)
472 return -ENOMEM;
473
474 fprintf(f, "Type=%s\n", t);
475 }
476
477 r = generator_write_timeouts(dest, what, where, opts, &filtered);
478 if (r < 0)
479 return r;
480
481 r = generator_write_device_deps(dest, what, where, opts);
482 if (r < 0)
483 return r;
484
485 r = write_mount_timeout(f, where, opts);
486 if (r < 0)
487 return r;
488
489 r = write_options(f, filtered);
490 if (r < 0)
491 return r;
492
493 if (flags & MOUNT_RW_ONLY)
494 fprintf(f, "ReadWriteOnly=yes\n");
495
496 r = fflush_and_check(f);
497 if (r < 0)
498 return log_error_errno(r, "Failed to write unit file %s: %m", name);
499
500 if (flags & MOUNT_MAKEFS) {
501 r = generator_hook_up_mkfs(dest, what, where, fstype);
502 if (r < 0)
503 return r;
504 }
505
506 if (flags & MOUNT_GROWFS) {
507 r = generator_hook_up_growfs(dest, where, post);
508 if (r < 0)
509 return r;
510 }
511
512 if (!FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
513 if (!FLAGS_SET(flags, MOUNT_NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
514 r = generator_add_symlink(dest, post,
515 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
516 if (r < 0)
517 return r;
518 } else {
519 char **s;
520
521 STRV_FOREACH(s, wanted_by) {
522 r = generator_add_symlink(dest, *s, "wants", name);
523 if (r < 0)
524 return r;
525 }
526
527 STRV_FOREACH(s, required_by) {
528 r = generator_add_symlink(dest, *s, "requires", name);
529 if (r < 0)
530 return r;
531 }
532 }
533 } else {
534 r = unit_name_from_path(where, ".automount", &automount_name);
535 if (r < 0)
536 return log_error_errno(r, "Failed to generate unit name: %m");
537
538 f = safe_fclose(f);
539
540 r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
541 if (r < 0)
542 return r;
543
544 fprintf(f,
545 "[Unit]\n"
546 "SourcePath=%s\n"
547 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
548 source);
549
550 fprintf(f,
551 "\n"
552 "[Automount]\n"
553 "Where=%s\n",
554 where_escaped);
555
556 r = write_idle_timeout(f, where, opts);
557 if (r < 0)
558 return r;
559
560 r = fflush_and_check(f);
561 if (r < 0)
562 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
563
564 r = generator_add_symlink(dest, post,
565 (flags & MOUNT_NOFAIL) ? "wants" : "requires", automount_name);
566 if (r < 0)
567 return r;
568 }
569
570 return 0;
571 }
572
573 static int parse_fstab(bool initrd) {
574 _cleanup_endmntent_ FILE *f = NULL;
575 const char *fstab;
576 struct mntent *me;
577 int r = 0;
578
579 fstab = initrd ? "/sysroot/etc/fstab" : fstab_path();
580 log_debug("Parsing %s...", fstab);
581
582 f = setmntent(fstab, "re");
583 if (!f) {
584 if (errno == ENOENT)
585 return 0;
586
587 return log_error_errno(errno, "Failed to open %s: %m", fstab);
588 }
589
590 while ((me = getmntent(f))) {
591 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
592 bool makefs, growfs, noauto, nofail;
593 MountPointFlags flags;
594 int k;
595
596 if (initrd && !mount_in_initrd(me))
597 continue;
598
599 what = fstab_node_to_udev_node(me->mnt_fsname);
600 if (!what)
601 return log_oom();
602
603 if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
604 log_info("Running in a container, ignoring fstab device entry for %s.", what);
605 continue;
606 }
607
608 where = strdup(me->mnt_dir);
609 if (!where)
610 return log_oom();
611
612 if (is_path(where)) {
613 path_simplify(where);
614
615 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
616 * mount units, but causes problems since it historically worked to have symlinks in e.g.
617 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
618 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
619 * target is the final directory. */
620 k = chase_symlinks(where, initrd ? "/sysroot" : NULL,
621 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
622 &canonical_where, NULL);
623 if (k < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
624 log_debug_errno(k, "Failed to read symlink target for %s, ignoring: %m", where);
625 else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
626 canonical_where = mfree(canonical_where);
627 else
628 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
629 }
630
631 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
632 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
633 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
634 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
635
636 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
637 what, where, me->mnt_type,
638 yes_no(makefs), yes_no(growfs),
639 yes_no(noauto), yes_no(nofail));
640
641 flags = makefs * MOUNT_MAKEFS |
642 growfs * MOUNT_GROWFS |
643 noauto * MOUNT_NOAUTO |
644 nofail * MOUNT_NOFAIL;
645
646 if (streq(me->mnt_type, "swap"))
647 k = add_swap(what, me, flags);
648 else {
649 bool rw_only, automount;
650 const char *post;
651
652 rw_only = fstab_test_option(me->mnt_opts, "x-systemd.rw-only\0");
653 automount = fstab_test_option(me->mnt_opts,
654 "comment=systemd.automount\0"
655 "x-systemd.automount\0");
656
657 flags |= rw_only * MOUNT_RW_ONLY |
658 automount * MOUNT_AUTOMOUNT;
659
660 if (initrd)
661 post = SPECIAL_INITRD_FS_TARGET;
662 else if (mount_is_network(me))
663 post = SPECIAL_REMOTE_FS_TARGET;
664 else
665 post = SPECIAL_LOCAL_FS_TARGET;
666
667 k = add_mount(arg_dest,
668 what,
669 canonical_where ?: where,
670 canonical_where ? where: NULL,
671 me->mnt_type,
672 me->mnt_opts,
673 me->mnt_passno,
674 flags,
675 post,
676 fstab);
677 }
678
679 if (r >= 0 && k < 0)
680 r = k;
681 }
682
683 return r;
684 }
685
686 static int add_sysroot_mount(void) {
687 _cleanup_free_ char *what = NULL;
688 const char *opts, *fstype;
689 bool default_rw;
690 int r;
691
692 if (isempty(arg_root_what)) {
693 log_debug("Could not find a root= entry on the kernel command line.");
694 return 0;
695 }
696
697 if (streq(arg_root_what, "gpt-auto")) {
698 /* This is handled by the gpt-auto generator */
699 log_debug("Skipping root directory handling, as gpt-auto was requested.");
700 return 0;
701 }
702
703 if (path_equal(arg_root_what, "/dev/nfs")) {
704 /* This is handled by the kernel or the initrd */
705 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
706 return 0;
707 }
708
709 if (streq(arg_root_what, "tmpfs")) {
710 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
711
712 what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
713 if (!what)
714 return log_oom();
715
716 fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
717
718 default_rw = true; /* writable, unless overridden */;
719 } else {
720
721 what = fstab_node_to_udev_node(arg_root_what);
722 if (!what)
723 return log_oom();
724
725 fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */
726
727 default_rw = false; /* read-only, unless overridden */
728 }
729
730 if (!arg_root_options)
731 opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
732 else if (arg_root_rw >= 0 ||
733 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
734 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
735 else
736 opts = arg_root_options;
737
738 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
739
740 if (is_device_path(what)) {
741 r = generator_write_initrd_root_device_deps(arg_dest, what);
742 if (r < 0)
743 return r;
744 }
745
746 return add_mount(arg_dest,
747 what,
748 "/sysroot",
749 NULL,
750 fstype,
751 opts,
752 is_device_path(what) ? 1 : 0, /* passno */
753 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
754 SPECIAL_INITRD_ROOT_FS_TARGET,
755 "/proc/cmdline");
756 }
757
758 static int add_sysroot_usr_mount(void) {
759 _cleanup_free_ char *what = NULL;
760 const char *opts;
761 int r;
762
763 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
764 * know for sure something else did */
765
766 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
767 return 0;
768
769 if (arg_root_what && !arg_usr_what) {
770 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
771 arg_usr_what = strdup(arg_root_what);
772 if (!arg_usr_what)
773 return log_oom();
774 }
775
776 if (arg_root_fstype && !arg_usr_fstype) {
777 arg_usr_fstype = strdup(arg_root_fstype);
778 if (!arg_usr_fstype)
779 return log_oom();
780 }
781
782 if (arg_root_options && !arg_usr_options) {
783 arg_usr_options = strdup(arg_root_options);
784 if (!arg_usr_options)
785 return log_oom();
786 }
787
788 if (isempty(arg_usr_what)) {
789 log_debug("Could not find a usr= entry on the kernel command line.");
790 return 0;
791 }
792
793 if (streq(arg_usr_what, "gpt-auto")) {
794 /* This is handled by the gpt-auto generator */
795 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
796 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
797 * unit file is being created for the host /usr/ mount. */
798 }
799
800 if (path_equal(arg_usr_what, "/dev/nfs")) {
801 /* This is handled by the initrd (if at all supported, that is) */
802 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
803 return 1; /* As above, report that NFS code will create the unit */
804 }
805
806 what = fstab_node_to_udev_node(arg_usr_what);
807 if (!what)
808 return log_oom();
809
810 if (!arg_usr_options)
811 opts = arg_root_rw > 0 ? "rw" : "ro";
812 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
813 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
814 else
815 opts = arg_usr_options;
816
817 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
818 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
819 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
820 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
821 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
822 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
823
824 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts));
825
826 r = add_mount(arg_dest,
827 what,
828 "/sysusr/usr",
829 NULL,
830 arg_usr_fstype,
831 opts,
832 is_device_path(what) ? 1 : 0, /* passno */
833 0,
834 SPECIAL_INITRD_USR_FS_TARGET,
835 "/proc/cmdline");
836 if (r < 0)
837 return r;
838
839 log_debug("Synthesizing entry what=/sysusr/usr where=/sysrootr/usr opts=bind");
840
841 r = add_mount(arg_dest,
842 "/sysusr/usr",
843 "/sysroot/usr",
844 NULL,
845 NULL,
846 "bind",
847 0,
848 0,
849 SPECIAL_INITRD_FS_TARGET,
850 "/proc/cmdline");
851 if (r < 0)
852 return r;
853
854 return 1;
855 }
856
857 static int add_sysroot_usr_mount_or_fallback(void) {
858 int r;
859
860 r = add_sysroot_usr_mount();
861 if (r != 0)
862 return r;
863
864 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
865 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
866 * themselves get the guarantee that /usr/ is definitely mounted somewhere. */
867
868 return generator_add_symlink(
869 arg_dest,
870 SPECIAL_INITRD_USR_FS_TARGET,
871 "requires",
872 "sysroot.mount");
873 }
874
875 static int add_volatile_root(void) {
876
877 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
878 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
879
880 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
881 return 0;
882
883 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
884 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE);
885 }
886
887 static int add_volatile_var(void) {
888
889 if (arg_volatile_mode != VOLATILE_STATE)
890 return 0;
891
892 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
893
894 return add_mount(arg_dest_late,
895 "tmpfs",
896 "/var",
897 NULL,
898 "tmpfs",
899 "mode=0755" TMPFS_LIMITS_VAR,
900 0,
901 0,
902 SPECIAL_LOCAL_FS_TARGET,
903 "/proc/cmdline");
904 }
905
906 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
907 int r;
908
909 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
910 * instance should take precedence. In the case of multiple rootflags=
911 * or usrflags= the arguments should be concatenated */
912
913 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
914
915 r = value ? parse_boolean(value) : 1;
916 if (r < 0)
917 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
918 else
919 arg_fstab_enabled = r;
920
921 } else if (streq(key, "root")) {
922
923 if (proc_cmdline_value_missing(key, value))
924 return 0;
925
926 return free_and_strdup_warn(&arg_root_what, value);
927
928 } else if (streq(key, "rootfstype")) {
929
930 if (proc_cmdline_value_missing(key, value))
931 return 0;
932
933 return free_and_strdup_warn(&arg_root_fstype, value);
934
935 } else if (streq(key, "rootflags")) {
936
937 if (proc_cmdline_value_missing(key, value))
938 return 0;
939
940 if (!strextend_with_separator(&arg_root_options, ",", value))
941 return log_oom();
942
943 } else if (streq(key, "roothash")) {
944
945 if (proc_cmdline_value_missing(key, value))
946 return 0;
947
948 return free_and_strdup_warn(&arg_root_hash, value);
949
950 } else if (streq(key, "mount.usr")) {
951
952 if (proc_cmdline_value_missing(key, value))
953 return 0;
954
955 return free_and_strdup_warn(&arg_usr_what, value);
956
957 } else if (streq(key, "mount.usrfstype")) {
958
959 if (proc_cmdline_value_missing(key, value))
960 return 0;
961
962 return free_and_strdup_warn(&arg_usr_fstype, value);
963
964 } else if (streq(key, "mount.usrflags")) {
965
966 if (proc_cmdline_value_missing(key, value))
967 return 0;
968
969 if (!strextend_with_separator(&arg_usr_options, ",", value))
970 return log_oom();
971
972 } else if (streq(key, "rw") && !value)
973 arg_root_rw = true;
974 else if (streq(key, "ro") && !value)
975 arg_root_rw = false;
976 else if (streq(key, "systemd.volatile")) {
977 VolatileMode m;
978
979 if (value) {
980 m = volatile_mode_from_string(value);
981 if (m < 0)
982 log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value);
983 else
984 arg_volatile_mode = m;
985 } else
986 arg_volatile_mode = VOLATILE_YES;
987
988 } else if (streq(key, "systemd.swap")) {
989
990 r = value ? parse_boolean(value) : 1;
991 if (r < 0)
992 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
993 else
994 arg_swap_enabled = r;
995 }
996
997 return 0;
998 }
999
1000 static int determine_root(void) {
1001 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
1002
1003 if (arg_root_what)
1004 return 0;
1005
1006 if (!arg_root_hash)
1007 return 0;
1008
1009 arg_root_what = strdup("/dev/mapper/root");
1010 if (!arg_root_what)
1011 return log_oom();
1012
1013 log_info("Using verity root device %s.", arg_root_what);
1014
1015 return 1;
1016 }
1017
1018 static int run(const char *dest, const char *dest_early, const char *dest_late) {
1019 int r, r2 = 0, r3 = 0;
1020
1021 assert_se(arg_dest = dest);
1022 assert_se(arg_dest_late = dest_late);
1023
1024 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
1025 if (r < 0)
1026 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
1027
1028 (void) determine_root();
1029
1030 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1031 if (in_initrd()) {
1032 r = add_sysroot_mount();
1033
1034 r2 = add_sysroot_usr_mount_or_fallback();
1035
1036 r3 = add_volatile_root();
1037 } else
1038 r = add_volatile_var();
1039
1040 /* Honour /etc/fstab only when that's enabled */
1041 if (arg_fstab_enabled) {
1042 /* Parse the local /etc/fstab, possibly from the initrd */
1043 r2 = parse_fstab(false);
1044
1045 /* If running in the initrd also parse the /etc/fstab from the host */
1046 if (in_initrd())
1047 r3 = parse_fstab(true);
1048 else
1049 r3 = generator_enable_remount_fs_service(arg_dest);
1050 }
1051
1052 return r < 0 ? r : r2 < 0 ? r2 : r3;
1053 }
1054
1055 DEFINE_MAIN_GENERATOR_FUNCTION(run);