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