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