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