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