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