]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fstab-generator/fstab-generator.c
fstab-generator: Chase symlinks where possible (#6293)
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "fd-util.h"
28 #include "fs-util.h"
29 #include "fileio.h"
30 #include "fstab-util.h"
31 #include "generator.h"
32 #include "log.h"
33 #include "mkdir.h"
34 #include "mount-setup.h"
35 #include "mount-util.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "proc-cmdline.h"
39 #include "special.h"
40 #include "stat-util.h"
41 #include "string-util.h"
42 #include "strv.h"
43 #include "unit-name.h"
44 #include "util.h"
45 #include "virt.h"
46 #include "volatile-util.h"
47
48 static const char *arg_dest = "/tmp";
49 static const char *arg_dest_late = "/tmp";
50 static bool arg_fstab_enabled = true;
51 static char *arg_root_what = NULL;
52 static char *arg_root_fstype = NULL;
53 static char *arg_root_options = NULL;
54 static char *arg_root_hash = NULL;
55 static int arg_root_rw = -1;
56 static char *arg_usr_what = NULL;
57 static char *arg_usr_fstype = NULL;
58 static char *arg_usr_options = NULL;
59 static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
60
61 static int write_options(FILE *f, const char *options) {
62 _cleanup_free_ char *o = NULL;
63
64 if (isempty(options))
65 return 0;
66
67 if (streq(options, "defaults"))
68 return 0;
69
70 o = strreplace(options, "%", "%%");
71 if (!o)
72 return log_oom();
73
74 fprintf(f, "Options=%s\n", o);
75 return 1;
76 }
77
78 static int write_what(FILE *f, const char *what) {
79 _cleanup_free_ char *w = NULL;
80
81 w = strreplace(what, "%", "%%");
82 if (!w)
83 return log_oom();
84
85 fprintf(f, "What=%s\n", w);
86 return 1;
87 }
88
89 static int add_swap(
90 const char *what,
91 struct mntent *me,
92 bool noauto,
93 bool nofail) {
94
95 _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
96 _cleanup_fclose_ FILE *f = NULL;
97 int r;
98
99 assert(what);
100 assert(me);
101
102 if (access("/proc/swaps", F_OK) < 0) {
103 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
104 return 0;
105 }
106
107 if (detect_container() > 0) {
108 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
109 return 0;
110 }
111
112 r = unit_name_from_path(what, ".swap", &name);
113 if (r < 0)
114 return log_error_errno(r, "Failed to generate unit name: %m");
115
116 unit = strjoin(arg_dest, "/", name);
117 if (!unit)
118 return log_oom();
119
120 f = fopen(unit, "wxe");
121 if (!f)
122 return log_error_errno(errno,
123 errno == EEXIST ?
124 "Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
125 "Failed to create unit file %s: %m",
126 unit);
127
128 fputs("# Automatically generated by systemd-fstab-generator\n\n"
129 "[Unit]\n"
130 "SourcePath=/etc/fstab\n"
131 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
132 "[Swap]\n", f);
133
134 r = write_what(f, what);
135 if (r < 0)
136 return r;
137
138 r = write_options(f, me->mnt_opts);
139 if (r < 0)
140 return r;
141
142 r = fflush_and_check(f);
143 if (r < 0)
144 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
145
146 /* use what as where, to have a nicer error message */
147 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
148 if (r < 0)
149 return r;
150
151 if (!noauto) {
152 lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET,
153 nofail ? ".wants/" : ".requires/", name, NULL);
154 if (!lnk)
155 return log_oom();
156
157 mkdir_parents_label(lnk, 0755);
158 if (symlink(unit, lnk) < 0)
159 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
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, UNIT_NAME_NOGLOB, ".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;
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 res = strv_join(paths, " ");
282 if (!res)
283 return log_oom();
284
285 fprintf(f, "RequiresMountsFor=%s\n", res);
286
287 return 0;
288 }
289
290 static int add_mount(
291 const char *dest,
292 const char *what,
293 const char *where,
294 const char *original_where,
295 const char *fstype,
296 const char *opts,
297 int passno,
298 bool noauto,
299 bool nofail,
300 bool automount,
301 const char *post,
302 const char *source) {
303
304 _cleanup_free_ char
305 *name = NULL, *unit = NULL, *lnk = NULL,
306 *automount_name = NULL, *automount_unit = NULL,
307 *filtered = NULL;
308 _cleanup_fclose_ FILE *f = NULL;
309 int r;
310
311 assert(what);
312 assert(where);
313 assert(opts);
314 assert(post);
315 assert(source);
316
317 if (streq_ptr(fstype, "autofs"))
318 return 0;
319
320 if (!is_path(where)) {
321 log_warning("Mount point %s is not a valid path, ignoring.", where);
322 return 0;
323 }
324
325 if (mount_point_is_api(where) ||
326 mount_point_ignore(where))
327 return 0;
328
329 if (path_equal(where, "/")) {
330 if (noauto)
331 log_warning("Ignoring \"noauto\" for root device");
332 if (nofail)
333 log_warning("Ignoring \"nofail\" for root device");
334 if (automount)
335 log_warning("Ignoring automount option for root device");
336
337 noauto = nofail = automount = false;
338 }
339
340 r = unit_name_from_path(where, ".mount", &name);
341 if (r < 0)
342 return log_error_errno(r, "Failed to generate unit name: %m");
343
344 unit = strjoin(dest, "/", name);
345 if (!unit)
346 return log_oom();
347
348 f = fopen(unit, "wxe");
349 if (!f)
350 return log_error_errno(errno,
351 errno == EEXIST ?
352 "Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
353 "Failed to create unit file %s: %m",
354 unit);
355
356 fprintf(f,
357 "# Automatically generated by systemd-fstab-generator\n\n"
358 "[Unit]\n"
359 "SourcePath=%s\n"
360 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
361 source);
362
363 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !automount &&
364 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
365 /* The default retry timeout that mount.nfs uses for 'bg' mounts
366 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
367 * As we are making 'bg' mounts look like an 'fg' mount to
368 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
369 * we need to explicitly preserve that default, and also ensure
370 * the systemd mount-timeout doesn't interfere.
371 * By placing these options first, they can be over-ridden by
372 * settings in /etc/fstab. */
373 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts, ",fg");
374 nofail = true;
375 }
376
377 if (!nofail && !automount)
378 fprintf(f, "Before=%s\n", post);
379
380 if (!automount && opts) {
381 r = write_after(f, opts);
382 if (r < 0)
383 return r;
384 r = write_requires_after(f, opts);
385 if (r < 0)
386 return r;
387 r = write_before(f, opts);
388 if (r < 0)
389 return r;
390 r = write_requires_mounts_for(f, opts);
391 if (r < 0)
392 return r;
393 }
394
395 if (passno != 0) {
396 r = generator_write_fsck_deps(f, dest, what, where, fstype);
397 if (r < 0)
398 return r;
399 }
400
401 fprintf(f, "\n[Mount]\n");
402 if (original_where)
403 fprintf(f, "# Canonicalized from %s\n", original_where);
404 fprintf(f, "Where=%s\n", where);
405
406 r = write_what(f, what);
407 if (r < 0)
408 return r;
409
410 if (!isempty(fstype) && !streq(fstype, "auto"))
411 fprintf(f, "Type=%s\n", fstype);
412
413 r = generator_write_timeouts(dest, what, where, opts, &filtered);
414 if (r < 0)
415 return r;
416
417 r = generator_write_device_deps(dest, what, where, opts);
418 if (r < 0)
419 return r;
420
421 r = write_mount_timeout(f, where, opts);
422 if (r < 0)
423 return r;
424
425 r = write_options(f, filtered);
426 if (r < 0)
427 return r;
428
429 r = fflush_and_check(f);
430 if (r < 0)
431 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
432
433 if (!noauto && !automount) {
434 lnk = strjoin(dest, "/", post, nofail ? ".wants/" : ".requires/", name);
435 if (!lnk)
436 return log_oom();
437
438 mkdir_parents_label(lnk, 0755);
439 if (symlink(unit, lnk) < 0)
440 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
441 }
442
443 if (automount) {
444 r = unit_name_from_path(where, ".automount", &automount_name);
445 if (r < 0)
446 return log_error_errno(r, "Failed to generate unit name: %m");
447
448 automount_unit = strjoin(dest, "/", automount_name);
449 if (!automount_unit)
450 return log_oom();
451
452 fclose(f);
453 f = fopen(automount_unit, "wxe");
454 if (!f)
455 return log_error_errno(errno, "Failed to create unit file %s: %m", automount_unit);
456
457 fprintf(f,
458 "# Automatically generated by systemd-fstab-generator\n\n"
459 "[Unit]\n"
460 "SourcePath=%s\n"
461 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
462 source);
463
464 fprintf(f, "Before=%s\n", post);
465
466 if (opts) {
467 r = write_after(f, opts);
468 if (r < 0)
469 return r;
470 r = write_requires_after(f, opts);
471 if (r < 0)
472 return r;
473 r = write_before(f, opts);
474 if (r < 0)
475 return r;
476 r = write_requires_mounts_for(f, opts);
477 if (r < 0)
478 return r;
479 }
480
481 fprintf(f,
482 "\n"
483 "[Automount]\n"
484 "Where=%s\n",
485 where);
486
487 r = write_idle_timeout(f, where, opts);
488 if (r < 0)
489 return r;
490
491 r = fflush_and_check(f);
492 if (r < 0)
493 return log_error_errno(r, "Failed to write unit file %s: %m", automount_unit);
494
495 free(lnk);
496 lnk = strjoin(dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name);
497 if (!lnk)
498 return log_oom();
499
500 mkdir_parents_label(lnk, 0755);
501 if (symlink(automount_unit, lnk) < 0)
502 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
503 }
504
505 return 0;
506 }
507
508 static int parse_fstab(bool initrd) {
509 _cleanup_endmntent_ FILE *f = NULL;
510 const char *fstab_path;
511 struct mntent *me;
512 int r = 0;
513
514 fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
515 f = setmntent(fstab_path, "re");
516 if (!f) {
517 if (errno == ENOENT)
518 return 0;
519
520 return log_error_errno(errno, "Failed to open %s: %m", fstab_path);
521 }
522
523 while ((me = getmntent(f))) {
524 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
525 bool noauto, nofail;
526 int k;
527
528 if (initrd && !mount_in_initrd(me))
529 continue;
530
531 what = fstab_node_to_udev_node(me->mnt_fsname);
532 if (!what)
533 return log_oom();
534
535 if (is_device_path(what) && path_is_read_only_fs("sys") > 0) {
536 log_info("Running in a container, ignoring fstab device entry for %s.", what);
537 continue;
538 }
539
540 where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir);
541 if (!where)
542 return log_oom();
543
544 if (is_path(where)) {
545 path_kill_slashes(where);
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 */
552 r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
553 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
554 &canonical_where);
555 if (r < 0)
556 /* In this case for now we continue on as if it wasn't a symlink */
557 log_warning_errno(r, "Failed to read symlink target for %s: %m", where);
558 else {
559 if (streq(canonical_where, where))
560 canonical_where = mfree(canonical_where);
561 else
562 log_debug("Canonicalized what=%s where=%s to %s",
563 what, where, canonical_where);
564 }
565 }
566
567 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
568 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
569 log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
570 what, where, me->mnt_type,
571 yes_no(noauto), yes_no(nofail));
572
573 if (streq(me->mnt_type, "swap"))
574 k = add_swap(what, me, noauto, 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 noauto,
597 nofail,
598 automount,
599 post,
600 fstab_path);
601 }
602
603 if (k < 0)
604 r = k;
605 }
606
607 return r;
608 }
609
610 static int add_sysroot_mount(void) {
611 _cleanup_free_ char *what = NULL;
612 const char *opts;
613 int r;
614
615 if (isempty(arg_root_what)) {
616 log_debug("Could not find a root= entry on the kernel command line.");
617 return 0;
618 }
619
620 if (streq(arg_root_what, "gpt-auto")) {
621 /* This is handled by the gpt-auto generator */
622 log_debug("Skipping root directory handling, as gpt-auto was requested.");
623 return 0;
624 }
625
626 if (path_equal(arg_root_what, "/dev/nfs")) {
627 /* This is handled by the kernel or the initrd */
628 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
629 return 0;
630 }
631
632 what = fstab_node_to_udev_node(arg_root_what);
633 if (!what)
634 return log_oom();
635
636 if (!arg_root_options)
637 opts = arg_root_rw > 0 ? "rw" : "ro";
638 else if (arg_root_rw >= 0 ||
639 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
640 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
641 else
642 opts = arg_root_options;
643
644 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
645
646 if (is_device_path(what)) {
647 r = generator_write_initrd_root_device_deps(arg_dest, what);
648 if (r < 0)
649 return r;
650 }
651
652 return add_mount(arg_dest,
653 what,
654 "/sysroot",
655 NULL,
656 arg_root_fstype,
657 opts,
658 is_device_path(what) ? 1 : 0, /* passno */
659 false, /* noauto off */
660 false, /* nofail off */
661 false, /* automount off */
662 SPECIAL_INITRD_ROOT_FS_TARGET,
663 "/proc/cmdline");
664 }
665
666 static int add_sysroot_usr_mount(void) {
667 _cleanup_free_ char *what = NULL;
668 const char *opts;
669
670 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
671 return 0;
672
673 if (arg_root_what && !arg_usr_what) {
674 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
675 arg_usr_what = strdup(arg_root_what);
676 if (!arg_usr_what)
677 return log_oom();
678 }
679
680 if (arg_root_fstype && !arg_usr_fstype) {
681 arg_usr_fstype = strdup(arg_root_fstype);
682 if (!arg_usr_fstype)
683 return log_oom();
684 }
685
686 if (arg_root_options && !arg_usr_options) {
687 arg_usr_options = strdup(arg_root_options);
688 if (!arg_usr_options)
689 return log_oom();
690 }
691
692 if (!arg_usr_what)
693 return 0;
694
695 what = fstab_node_to_udev_node(arg_usr_what);
696 if (!what)
697 return log_oom();
698
699 if (!arg_usr_options)
700 opts = arg_root_rw > 0 ? "rw" : "ro";
701 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
702 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
703 else
704 opts = arg_usr_options;
705
706 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
707 return add_mount(arg_dest,
708 what,
709 "/sysroot/usr",
710 NULL,
711 arg_usr_fstype,
712 opts,
713 is_device_path(what) ? 1 : 0, /* passno */
714 false, /* noauto off */
715 false, /* nofail off */
716 false, /* automount off */
717 SPECIAL_INITRD_FS_TARGET,
718 "/proc/cmdline");
719 }
720
721 static int add_volatile_root(void) {
722 const char *from, *to;
723
724 if (arg_volatile_mode != VOLATILE_YES)
725 return 0;
726
727 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
728 * requested, leaving only /usr from the root mount inside. */
729
730 from = strjoina(SYSTEM_DATA_UNIT_PATH "/systemd-volatile-root.service");
731 to = strjoina(arg_dest, "/" SPECIAL_INITRD_ROOT_FS_TARGET, ".requires/systemd-volatile-root.service");
732
733 (void) mkdir_parents(to, 0755);
734
735 if (symlink(from, to) < 0)
736 return log_error_errno(errno, "Failed to hook in volatile remount service: %m");
737
738 return 0;
739 }
740
741 static int add_volatile_var(void) {
742
743 if (arg_volatile_mode != VOLATILE_STATE)
744 return 0;
745
746 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
747
748 return add_mount(arg_dest_late,
749 "tmpfs",
750 "/var",
751 NULL,
752 "tmpfs",
753 "mode=0755",
754 0,
755 false,
756 false,
757 false,
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 char *o;
795
796 if (proc_cmdline_value_missing(key, value))
797 return 0;
798
799 o = arg_root_options ?
800 strjoin(arg_root_options, ",", value) :
801 strdup(value);
802 if (!o)
803 return log_oom();
804
805 free(arg_root_options);
806 arg_root_options = o;
807 } else if (streq(key, "roothash")) {
808
809 if (proc_cmdline_value_missing(key, value))
810 return 0;
811
812 if (free_and_strdup(&arg_root_hash, value) < 0)
813 return log_oom();
814
815 } else if (streq(key, "mount.usr")) {
816
817 if (proc_cmdline_value_missing(key, value))
818 return 0;
819
820 if (free_and_strdup(&arg_usr_what, value) < 0)
821 return log_oom();
822
823 } else if (streq(key, "mount.usrfstype")) {
824
825 if (proc_cmdline_value_missing(key, value))
826 return 0;
827
828 if (free_and_strdup(&arg_usr_fstype, value) < 0)
829 return log_oom();
830
831 } else if (streq(key, "mount.usrflags")) {
832 char *o;
833
834 if (proc_cmdline_value_missing(key, value))
835 return 0;
836
837 o = arg_usr_options ?
838 strjoin(arg_usr_options, ",", value) :
839 strdup(value);
840 if (!o)
841 return log_oom();
842
843 free(arg_usr_options);
844 arg_usr_options = o;
845
846 } else if (streq(key, "rw") && !value)
847 arg_root_rw = true;
848 else if (streq(key, "ro") && !value)
849 arg_root_rw = false;
850 else if (streq(key, "systemd.volatile")) {
851 VolatileMode m;
852
853 if (value) {
854 m = volatile_mode_from_string(value);
855 if (m < 0)
856 log_warning("Failed to parse systemd.volatile= argument: %s", value);
857 else
858 arg_volatile_mode = m;
859 } else
860 arg_volatile_mode = VOLATILE_YES;
861 }
862
863 return 0;
864 }
865
866 static int determine_root(void) {
867 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
868
869 if (arg_root_what)
870 return 0;
871
872 if (!arg_root_hash)
873 return 0;
874
875 arg_root_what = strdup("/dev/mapper/root");
876 if (!arg_root_what)
877 return log_oom();
878
879 log_info("Using verity root device %s.", arg_root_what);
880
881 return 1;
882 }
883
884 int main(int argc, char *argv[]) {
885 int r = 0;
886
887 if (argc > 1 && argc != 4) {
888 log_error("This program takes three or no arguments.");
889 return EXIT_FAILURE;
890 }
891
892 if (argc > 1)
893 arg_dest = argv[1];
894 if (argc > 3)
895 arg_dest_late = argv[3];
896
897 log_set_target(LOG_TARGET_SAFE);
898 log_parse_environment();
899 log_open();
900
901 umask(0022);
902
903 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
904 if (r < 0)
905 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
906
907 (void) determine_root();
908
909 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
910 if (in_initrd()) {
911 int k;
912
913 r = add_sysroot_mount();
914
915 k = add_sysroot_usr_mount();
916 if (k < 0)
917 r = k;
918
919 k = add_volatile_root();
920 if (k < 0)
921 r = k;
922 } else
923 r = add_volatile_var();
924
925 /* Honour /etc/fstab only when that's enabled */
926 if (arg_fstab_enabled) {
927 int k;
928
929 log_debug("Parsing /etc/fstab");
930
931 /* Parse the local /etc/fstab, possibly from the initrd */
932 k = parse_fstab(false);
933 if (k < 0)
934 r = k;
935
936 /* If running in the initrd also parse the /etc/fstab from the host */
937 if (in_initrd()) {
938 log_debug("Parsing /sysroot/etc/fstab");
939
940 k = parse_fstab(true);
941 if (k < 0)
942 r = k;
943 }
944 }
945
946 free(arg_root_what);
947 free(arg_root_fstype);
948 free(arg_root_options);
949 free(arg_root_hash);
950
951 free(arg_usr_what);
952 free(arg_usr_fstype);
953 free(arg_usr_options);
954
955 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
956 }