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