]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fstab-generator/fstab-generator.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 "mountpoint-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
25 #include "special.h"
26 #include "specifier.h"
27 #include "stat-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "unit-name.h"
31 #include "util.h"
32 #include "virt.h"
33 #include "volatile-util.h"
34
35 typedef enum MountpointFlags {
36 NOAUTO = 1 << 0,
37 NOFAIL = 1 << 1,
38 AUTOMOUNT = 1 << 2,
39 MAKEFS = 1 << 3,
40 GROWFS = 1 << 4,
41 } MountpointFlags;
42
43 static const char *arg_dest = NULL;
44 static const char *arg_dest_late = NULL;
45 static bool arg_fstab_enabled = true;
46 static char *arg_root_what = NULL;
47 static char *arg_root_fstype = NULL;
48 static char *arg_root_options = NULL;
49 static char *arg_root_hash = NULL;
50 static int arg_root_rw = -1;
51 static char *arg_usr_what = NULL;
52 static char *arg_usr_fstype = NULL;
53 static char *arg_usr_options = NULL;
54 static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
55
56 STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
57 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
58 STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
59 STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
60 STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
61 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
62 STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
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 log_debug("Parsing %s...", fstab_path);
522
523 f = setmntent(fstab_path, "re");
524 if (!f) {
525 if (errno == ENOENT)
526 return 0;
527
528 return log_error_errno(errno, "Failed to open %s: %m", fstab_path);
529 }
530
531 while ((me = getmntent(f))) {
532 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
533 bool makefs, growfs, noauto, nofail;
534 int k;
535
536 if (initrd && !mount_in_initrd(me))
537 continue;
538
539 what = fstab_node_to_udev_node(me->mnt_fsname);
540 if (!what)
541 return log_oom();
542
543 if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
544 log_info("Running in a container, ignoring fstab device entry for %s.", what);
545 continue;
546 }
547
548 where = strdup(me->mnt_dir);
549 if (!where)
550 return log_oom();
551
552 if (is_path(where)) {
553 path_simplify(where, false);
554
555 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
556 * mount units, but causes problems since it historically worked to have symlinks in e.g.
557 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
558 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
559 * target is the final directory. */
560 r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
561 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
562 &canonical_where);
563 if (r < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
564 log_debug_errno(r, "Failed to read symlink target for %s, ignoring: %m", where);
565 else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
566 canonical_where = mfree(canonical_where);
567 else
568 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
569 }
570
571 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
572 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
573 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
574 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
575
576 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
577 what, where, me->mnt_type,
578 yes_no(makefs), yes_no(growfs),
579 yes_no(noauto), yes_no(nofail));
580
581 if (streq(me->mnt_type, "swap"))
582 k = add_swap(what, me,
583 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL);
584 else {
585 bool automount;
586 const char *post;
587
588 automount = fstab_test_option(me->mnt_opts,
589 "comment=systemd.automount\0"
590 "x-systemd.automount\0");
591 if (initrd)
592 post = SPECIAL_INITRD_FS_TARGET;
593 else if (mount_is_network(me))
594 post = SPECIAL_REMOTE_FS_TARGET;
595 else
596 post = SPECIAL_LOCAL_FS_TARGET;
597
598 k = add_mount(arg_dest,
599 what,
600 canonical_where ?: where,
601 canonical_where ? where: NULL,
602 me->mnt_type,
603 me->mnt_opts,
604 me->mnt_passno,
605 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT,
606 post,
607 fstab_path);
608 }
609
610 if (r >= 0 && k < 0)
611 r = k;
612 }
613
614 return r;
615 }
616
617 static int add_sysroot_mount(void) {
618 _cleanup_free_ char *what = NULL;
619 const char *opts;
620 int r;
621
622 if (isempty(arg_root_what)) {
623 log_debug("Could not find a root= entry on the kernel command line.");
624 return 0;
625 }
626
627 if (streq(arg_root_what, "gpt-auto")) {
628 /* This is handled by the gpt-auto generator */
629 log_debug("Skipping root directory handling, as gpt-auto was requested.");
630 return 0;
631 }
632
633 if (path_equal(arg_root_what, "/dev/nfs")) {
634 /* This is handled by the kernel or the initrd */
635 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
636 return 0;
637 }
638
639 what = fstab_node_to_udev_node(arg_root_what);
640 if (!what)
641 return log_oom();
642
643 if (!arg_root_options)
644 opts = arg_root_rw > 0 ? "rw" : "ro";
645 else if (arg_root_rw >= 0 ||
646 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
647 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
648 else
649 opts = arg_root_options;
650
651 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
652
653 if (is_device_path(what)) {
654 r = generator_write_initrd_root_device_deps(arg_dest, what);
655 if (r < 0)
656 return r;
657 }
658
659 return add_mount(arg_dest,
660 what,
661 "/sysroot",
662 NULL,
663 arg_root_fstype,
664 opts,
665 is_device_path(what) ? 1 : 0, /* passno */
666 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
667 SPECIAL_INITRD_ROOT_FS_TARGET,
668 "/proc/cmdline");
669 }
670
671 static int add_sysroot_usr_mount(void) {
672 _cleanup_free_ char *what = NULL;
673 const char *opts;
674
675 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
676 return 0;
677
678 if (arg_root_what && !arg_usr_what) {
679 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
680 arg_usr_what = strdup(arg_root_what);
681 if (!arg_usr_what)
682 return log_oom();
683 }
684
685 if (arg_root_fstype && !arg_usr_fstype) {
686 arg_usr_fstype = strdup(arg_root_fstype);
687 if (!arg_usr_fstype)
688 return log_oom();
689 }
690
691 if (arg_root_options && !arg_usr_options) {
692 arg_usr_options = strdup(arg_root_options);
693 if (!arg_usr_options)
694 return log_oom();
695 }
696
697 if (!arg_usr_what)
698 return 0;
699
700 what = fstab_node_to_udev_node(arg_usr_what);
701 if (!what)
702 return log_oom();
703
704 if (!arg_usr_options)
705 opts = arg_root_rw > 0 ? "rw" : "ro";
706 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
707 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
708 else
709 opts = arg_usr_options;
710
711 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
712 return add_mount(arg_dest,
713 what,
714 "/sysroot/usr",
715 NULL,
716 arg_usr_fstype,
717 opts,
718 is_device_path(what) ? 1 : 0, /* passno */
719 0,
720 SPECIAL_INITRD_FS_TARGET,
721 "/proc/cmdline");
722 }
723
724 static int add_volatile_root(void) {
725 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
726 * requested, leaving only /usr from the root mount inside. */
727
728 if (arg_volatile_mode != VOLATILE_YES)
729 return 0;
730
731 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
732 SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE);
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 static int run(const char *dest, const char *dest_early, const char *dest_late) {
864 int r, r2 = 0, r3 = 0;
865
866 assert_se(arg_dest = dest);
867 assert_se(arg_dest_late = dest_late);
868
869 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
870 if (r < 0)
871 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
872
873 (void) determine_root();
874
875 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
876 if (in_initrd()) {
877 r = add_sysroot_mount();
878
879 r2 = add_sysroot_usr_mount();
880
881 r3 = add_volatile_root();
882 } else
883 r = add_volatile_var();
884
885 /* Honour /etc/fstab only when that's enabled */
886 if (arg_fstab_enabled) {
887 /* Parse the local /etc/fstab, possibly from the initrd */
888 r2 = parse_fstab(false);
889
890 /* If running in the initrd also parse the /etc/fstab from the host */
891 if (in_initrd())
892 r3 = parse_fstab(true);
893 else
894 r3 = generator_enable_remount_fs_service(arg_dest);
895 }
896
897 return r < 0 ? r : r2 < 0 ? r2 : r3;
898 }
899
900 DEFINE_MAIN_GENERATOR_FUNCTION(run);