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