]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
generators: optionally, measure file systems at boot
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6b1dc2bd 2
6b1dc2bd 3#include <errno.h>
07630cea 4#include <stdio.h>
6b1dc2bd
LP
5#include <unistd.h>
6
b5efdb8a 7#include "alloc-util.h"
028a981c
ZJS
8#include "bus-error.h"
9#include "bus-locator.h"
f4351959 10#include "chase-symlinks.h"
3ffd4af2 11#include "fd-util.h"
0d39fa9c 12#include "fileio.h"
d15d0333 13#include "fstab-util.h"
07630cea 14#include "generator.h"
77b8e92d 15#include "in-addr-util.h"
baa6a42d 16#include "initrd-util.h"
07630cea 17#include "log.h"
a4ef3e4d 18#include "main-func.h"
07630cea 19#include "mkdir.h"
6b1dc2bd 20#include "mount-setup.h"
4349cd7c 21#include "mount-util.h"
049af8ad 22#include "mountpoint-util.h"
6bedfcbb 23#include "parse-util.h"
07630cea 24#include "path-util.h"
4e731273 25#include "proc-cmdline.h"
028a981c 26#include "process-util.h"
6b1dc2bd 27#include "special.h"
98bad05e 28#include "specifier.h"
8fcde012 29#include "stat-util.h"
07630cea 30#include "string-util.h"
059cb385 31#include "strv.h"
07630cea 32#include "unit-name.h"
689aede8 33#include "virt.h"
91214a37 34#include "volatile-util.h"
6b1dc2bd 35
d2194e15
LP
36typedef enum MountPointFlags {
37 MOUNT_NOAUTO = 1 << 0,
38 MOUNT_NOFAIL = 1 << 1,
39 MOUNT_AUTOMOUNT = 1 << 2,
40 MOUNT_MAKEFS = 1 << 3,
41 MOUNT_GROWFS = 1 << 4,
42 MOUNT_RW_ONLY = 1 << 5,
04959faa 43 MOUNT_PCRFS = 1 << 6,
d2194e15 44} MountPointFlags;
4191418b 45
028a981c 46static bool arg_sysroot_check = false;
7a44c7e3
ZJS
47static const char *arg_dest = NULL;
48static const char *arg_dest_late = NULL;
e48fdd84 49static bool arg_fstab_enabled = true;
567a5307 50static bool arg_swap_enabled = true;
6db615c1
LP
51static char *arg_root_what = NULL;
52static char *arg_root_fstype = NULL;
53static char *arg_root_options = NULL;
2f3dfc6f 54static char *arg_root_hash = NULL;
6db615c1 55static int arg_root_rw = -1;
9f103625
TH
56static char *arg_usr_what = NULL;
57static char *arg_usr_fstype = NULL;
58static char *arg_usr_options = NULL;
c1b9e3df 59static char *arg_usr_hash = NULL;
91214a37 60static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
6b1dc2bd 61
a4ef3e4d
YW
62STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
63STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
64STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
65STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
66STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
67STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
68STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
c1b9e3df 69STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep);
a4ef3e4d 70
d5cc4be2
LP
71static int write_options(FILE *f, const char *options) {
72 _cleanup_free_ char *o = NULL;
73
74 if (isempty(options))
75 return 0;
76
77 if (streq(options, "defaults"))
78 return 0;
79
98bad05e 80 o = specifier_escape(options);
d5cc4be2
LP
81 if (!o)
82 return log_oom();
83
84 fprintf(f, "Options=%s\n", o);
85 return 1;
86}
87
19d0833b
LP
88static int write_what(FILE *f, const char *what) {
89 _cleanup_free_ char *w = NULL;
90
98bad05e 91 w = specifier_escape(what);
19d0833b
LP
92 if (!w)
93 return log_oom();
94
95 fprintf(f, "What=%s\n", w);
96 return 1;
97}
98
5607d856 99static int add_swap(
7772c177 100 const char *source,
5607d856
ZJS
101 const char *what,
102 struct mntent *me,
d2194e15 103 MountPointFlags flags) {
5607d856 104
fb883e75 105 _cleanup_free_ char *name = NULL;
7fd1b19b 106 _cleanup_fclose_ FILE *f = NULL;
bf1d7ba7 107 int r;
6b1dc2bd
LP
108
109 assert(what);
110 assert(me);
111
567a5307 112 if (!arg_swap_enabled) {
113 log_info("Swap unit generation disabled on kernel command line, ignoring fstab swap entry for %s.", what);
114 return 0;
115 }
116
00b4ffde
LP
117 if (access("/proc/swaps", F_OK) < 0) {
118 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
119 return 0;
120 }
121
75f86906 122 if (detect_container() > 0) {
689aede8
LP
123 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
124 return 0;
125 }
126
028a981c
ZJS
127 if (arg_sysroot_check) {
128 log_info("%s should be enabled in the initrd, will request daemon-reload.", what);
129 return true;
130 }
131
7410616c
LP
132 r = unit_name_from_path(what, ".swap", &name);
133 if (r < 0)
134 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 135
7772c177 136 r = generator_open_unit_file(arg_dest, source, name, &f);
fb883e75
ZJS
137 if (r < 0)
138 return r;
0d536673 139
ed4ad488
ZJS
140 fprintf(f,
141 "[Unit]\n"
a7e88558
LP
142 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
143 "SourcePath=%s\n",
7772c177 144 source);
19d0833b 145
a7e88558
LP
146 r = generator_write_blockdev_dependency(f, what);
147 if (r < 0)
148 return r;
149
150 fprintf(f,
151 "\n"
152 "[Swap]\n");
153
19d0833b
LP
154 r = write_what(f, what);
155 if (r < 0)
156 return r;
6b1dc2bd 157
d5cc4be2
LP
158 r = write_options(f, me->mnt_opts);
159 if (r < 0)
160 return r;
6b1dc2bd 161
47cb901e 162 r = fflush_and_check(f);
23bbb0de 163 if (r < 0)
fb883e75 164 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 165
b3208b66 166 /* use what as where, to have a nicer error message */
bf1d7ba7 167 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
b3208b66
ZJS
168 if (r < 0)
169 return r;
170
d2194e15 171 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
172 r = generator_hook_up_mkswap(arg_dest, what);
173 if (r < 0)
174 return r;
175 }
176
d2194e15 177 if (flags & MOUNT_GROWFS)
7f2806d5
ZJS
178 /* TODO: swap devices must be wiped and recreated */
179 log_warning("%s: growing swap devices is currently unsupported.", what);
04959faa
LP
180 if (flags & MOUNT_PCRFS)
181 log_warning("%s: measuring swap devices is currently unsupported.", what);
7f2806d5 182
d2194e15 183 if (!(flags & MOUNT_NOAUTO)) {
630d30d3 184 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
d2194e15 185 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
630d30d3
ZJS
186 if (r < 0)
187 return r;
4e82fe52
TG
188 }
189
028a981c 190 return true;
6b1dc2bd
LP
191}
192
6b1dc2bd
LP
193static bool mount_is_network(struct mntent *me) {
194 assert(me);
195
b9f111b9
ZJS
196 return fstab_test_option(me->mnt_opts, "_netdev\0") ||
197 fstype_is_network(me->mnt_type);
6b1dc2bd
LP
198}
199
3d22d1ab
TG
200static bool mount_in_initrd(struct mntent *me) {
201 assert(me);
202
b9f111b9 203 return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
efc5fd3d 204 path_equal(me->mnt_dir, "/usr");
3d22d1ab
TG
205}
206
33a4c983
LP
207static int write_timeout(
208 FILE *f,
209 const char *where,
210 const char *opts,
211 const char *filter,
212 const char *variable) {
213
deb0a77c 214 _cleanup_free_ char *timeout = NULL;
deb0a77c
MO
215 usec_t u;
216 int r;
217
ff0c31bc 218 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, NULL);
deb0a77c
MO
219 if (r < 0)
220 return log_warning_errno(r, "Failed to parse options: %m");
221 if (r == 0)
222 return 0;
223
0004f698 224 r = parse_sec_fix_0(timeout, &u);
deb0a77c
MO
225 if (r < 0) {
226 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
227 return 0;
228 }
229
5291f26d 230 fprintf(f, "%s=%s\n", variable, FORMAT_TIMESPAN(u, 0));
deb0a77c
MO
231
232 return 0;
233}
2e852276 234
110773f6
CH
235static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
236 return write_timeout(f, where, opts,
237 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
238}
239
240static int write_mount_timeout(FILE *f, const char *where, const char *opts) {
241 return write_timeout(f, where, opts,
242 "x-systemd.mount-timeout\0", "TimeoutSec");
243}
244
33a4c983
LP
245static int write_dependency(
246 FILE *f,
247 const char *opts,
248 const char *filter,
249 const char *format) {
250
3519d230
KZ
251 _cleanup_strv_free_ char **names = NULL, **units = NULL;
252 _cleanup_free_ char *res = NULL;
3519d230
KZ
253 int r;
254
255 assert(f);
256 assert(opts);
257
d6cef552 258 r = fstab_filter_options(opts, filter, NULL, NULL, &names, NULL);
3519d230
KZ
259 if (r < 0)
260 return log_warning_errno(r, "Failed to parse options: %m");
261 if (r == 0)
262 return 0;
263
264 STRV_FOREACH(s, names) {
265 char *x;
266
df7c4eb6 267 r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x);
3519d230
KZ
268 if (r < 0)
269 return log_error_errno(r, "Failed to generate unit name: %m");
33a4c983 270
3519d230
KZ
271 r = strv_consume(&units, x);
272 if (r < 0)
273 return log_oom();
274 }
275
276 if (units) {
277 res = strv_join(units, " ");
278 if (!res)
279 return log_oom();
56e577c6
LP
280
281 DISABLE_WARNING_FORMAT_NONLITERAL;
ae325185 282 fprintf(f, format, res);
56e577c6 283 REENABLE_WARNING;
3519d230
KZ
284 }
285
286 return 0;
287}
288
ae325185 289static int write_after(FILE *f, const char *opts) {
33a4c983 290 return write_dependency(f, opts,
d6cef552 291 "x-systemd.after\0", "After=%1$s\n");
ae325185
RB
292}
293
294static int write_requires_after(FILE *f, const char *opts) {
295 return write_dependency(f, opts,
d6cef552 296 "x-systemd.requires\0", "After=%1$s\nRequires=%1$s\n");
ae325185
RB
297}
298
299static int write_before(FILE *f, const char *opts) {
300 return write_dependency(f, opts,
d6cef552 301 "x-systemd.before\0", "Before=%1$s\n");
ae325185
RB
302}
303
3519d230 304static int write_requires_mounts_for(FILE *f, const char *opts) {
98bad05e 305 _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL;
3519d230
KZ
306 _cleanup_free_ char *res = NULL;
307 int r;
308
309 assert(f);
310 assert(opts);
311
d6cef552 312 r = fstab_filter_options(opts, "x-systemd.requires-mounts-for\0", NULL, NULL, &paths, NULL);
3519d230
KZ
313 if (r < 0)
314 return log_warning_errno(r, "Failed to parse options: %m");
315 if (r == 0)
316 return 0;
317
98bad05e
LP
318 r = specifier_escape_strv(paths, &paths_escaped);
319 if (r < 0)
320 return log_error_errno(r, "Failed to escape paths: %m");
321
322 res = strv_join(paths_escaped, " ");
3519d230
KZ
323 if (!res)
324 return log_oom();
325
326 fprintf(f, "RequiresMountsFor=%s\n", res);
327
328 return 0;
329}
330
6371e69b
FB
331static int write_extra_dependencies(FILE *f, const char *opts) {
332 int r;
333
334 assert(f);
335
336 if (opts) {
337 r = write_after(f, opts);
338 if (r < 0)
339 return r;
340 r = write_requires_after(f, opts);
341 if (r < 0)
342 return r;
343 r = write_before(f, opts);
344 if (r < 0)
345 return r;
346 r = write_requires_mounts_for(f, opts);
347 if (r < 0)
348 return r;
349 }
350
351 return 0;
352}
353
e8d2f6cd 354static int add_mount(
7772c177 355 const char *source,
91214a37 356 const char *dest,
e8d2f6cd
LP
357 const char *what,
358 const char *where,
634735b5 359 const char *original_where,
6db615c1 360 const char *fstype,
e8d2f6cd
LP
361 const char *opts,
362 int passno,
d2194e15 363 MountPointFlags flags,
da69e8e4 364 const char *target_unit) {
e48fdd84 365
7fd1b19b 366 _cleanup_free_ char
da495a03 367 *name = NULL,
fb883e75 368 *automount_name = NULL,
98bad05e
LP
369 *filtered = NULL,
370 *where_escaped = NULL;
be02c1cf 371 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 372 _cleanup_fclose_ FILE *f = NULL;
94192cda 373 int r;
6b1dc2bd
LP
374
375 assert(what);
376 assert(where);
5e398e54 377 assert(opts);
da69e8e4 378 assert(target_unit);
5e398e54 379 assert(source);
6b1dc2bd 380
6db615c1 381 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
382 return 0;
383
384 if (!is_path(where)) {
385 log_warning("Mount point %s is not a valid path, ignoring.", where);
386 return 0;
387 }
388
389 if (mount_point_is_api(where) ||
390 mount_point_ignore(where))
391 return 0;
392
028a981c
ZJS
393 if (arg_sysroot_check) {
394 log_info("%s should be mounted in the initrd, will request daemon-reload.", where);
395 return true;
396 }
397
d6cef552 398 r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL);
be02c1cf
AR
399 if (r < 0)
400 return r;
401
d6cef552 402 r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL);
be02c1cf
AR
403 if (r < 0)
404 return r;
405
e48fdd84 406 if (path_equal(where, "/")) {
d2194e15
LP
407 if (flags & MOUNT_NOAUTO)
408 log_warning("Ignoring \"noauto\" option for root device");
409 if (flags & MOUNT_NOFAIL)
410 log_warning("Ignoring \"nofail\" option for root device");
411 if (flags & MOUNT_AUTOMOUNT)
412 log_warning("Ignoring \"automount\" option for root device");
be02c1cf 413 if (!strv_isempty(wanted_by))
d2194e15 414 log_warning("Ignoring \"x-systemd.wanted-by=\" option for root device");
be02c1cf 415 if (!strv_isempty(required_by))
d2194e15 416 log_warning("Ignoring \"x-systemd.required-by=\" option for root device");
2e852276 417
be02c1cf
AR
418 required_by = strv_free(required_by);
419 wanted_by = strv_free(wanted_by);
d2194e15 420 SET_FLAG(flags, MOUNT_NOAUTO | MOUNT_NOFAIL | MOUNT_AUTOMOUNT, false);
e48fdd84
LP
421 }
422
7410616c
LP
423 r = unit_name_from_path(where, ".mount", &name);
424 if (r < 0)
425 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 426
7772c177 427 r = generator_open_unit_file(dest, source, name, &f);
fb883e75
ZJS
428 if (r < 0)
429 return r;
0d536673 430
5e398e54 431 fprintf(f,
c3834f9b 432 "[Unit]\n"
a7e88558
LP
433 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
434 "SourcePath=%s\n",
c3834f9b 435 source);
6b1dc2bd 436
d2194e15 437 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) &&
65e1dee7
N
438 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
439 /* The default retry timeout that mount.nfs uses for 'bg' mounts
440 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
441 * As we are making 'bg' mounts look like an 'fg' mount to
442 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
443 * we need to explicitly preserve that default, and also ensure
444 * the systemd mount-timeout doesn't interfere.
5a12d1ca 445 * By placing these options first, they can be overridden by
65e1dee7 446 * settings in /etc/fstab. */
e66d2eee 447 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
d2194e15 448 SET_FLAG(flags, MOUNT_NOFAIL, true);
65e1dee7
N
449 }
450
6371e69b
FB
451 r = write_extra_dependencies(f, opts);
452 if (r < 0)
453 return r;
3519d230 454
da69e8e4 455 /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
fa138f5e 456 * target unit won't affect us. */
da69e8e4
ZJS
457 if (!FLAGS_SET(flags, MOUNT_NOFAIL))
458 fprintf(f, "Before=%s\n", target_unit);
fa138f5e 459
e48fdd84 460 if (passno != 0) {
91214a37 461 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
462 if (r < 0)
463 return r;
464 }
64e70e4b 465
a7e88558
LP
466 r = generator_write_blockdev_dependency(f, what);
467 if (r < 0)
468 return r;
469
470 fprintf(f,
471 "\n"
472 "[Mount]\n");
473
b2efed52
ZJS
474 r = write_what(f, what);
475 if (r < 0)
476 return r;
477
634735b5
CW
478 if (original_where)
479 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
480
481 where_escaped = specifier_escape(where);
482 if (!where_escaped)
483 return log_oom();
484 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 485
98bad05e 486 if (!isempty(fstype) && !streq(fstype, "auto")) {
c2b2df60 487 _cleanup_free_ char *t = NULL;
98bad05e
LP
488
489 t = specifier_escape(fstype);
490 if (!t)
491 return -ENOMEM;
492
493 fprintf(f, "Type=%s\n", t);
494 }
6b1dc2bd 495
91214a37 496 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
497 if (r < 0)
498 return r;
499
4195077a
MK
500 r = generator_write_device_deps(dest, what, where, opts);
501 if (r < 0)
502 return r;
503
110773f6
CH
504 r = write_mount_timeout(f, where, opts);
505 if (r < 0)
506 return r;
507
d5cc4be2
LP
508 r = write_options(f, filtered);
509 if (r < 0)
510 return r;
5e398e54 511
d2194e15 512 if (flags & MOUNT_RW_ONLY)
f42aa416
MH
513 fprintf(f, "ReadWriteOnly=yes\n");
514
4652c56c
ZJS
515 r = fflush_and_check(f);
516 if (r < 0)
fb883e75 517 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 518
d2194e15 519 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
520 r = generator_hook_up_mkfs(dest, what, where, fstype);
521 if (r < 0)
522 return r;
523 }
524
d2194e15 525 if (flags & MOUNT_GROWFS) {
da69e8e4 526 r = generator_hook_up_growfs(dest, where, target_unit);
7f2806d5
ZJS
527 if (r < 0)
528 return r;
529 }
530
04959faa
LP
531 if (flags & MOUNT_PCRFS) {
532 r = generator_hook_up_pcrfs(dest, where, target_unit);
533 if (r < 0)
534 return r;
535 }
536
d2194e15
LP
537 if (!FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
538 if (!FLAGS_SET(flags, MOUNT_NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
da69e8e4 539 r = generator_add_symlink(dest, target_unit,
d2194e15 540 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
be02c1cf
AR
541 if (r < 0)
542 return r;
543 } else {
be02c1cf
AR
544 STRV_FOREACH(s, wanted_by) {
545 r = generator_add_symlink(dest, *s, "wants", name);
546 if (r < 0)
547 return r;
548 }
549
550 STRV_FOREACH(s, required_by) {
551 r = generator_add_symlink(dest, *s, "requires", name);
552 if (r < 0)
553 return r;
554 }
555 }
556 } else {
7410616c
LP
557 r = unit_name_from_path(where, ".automount", &automount_name);
558 if (r < 0)
559 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 560
8a7033ac 561 f = safe_fclose(f);
6b1dc2bd 562
7772c177 563 r = generator_open_unit_file(dest, source, automount_name, &f);
fb883e75
ZJS
564 if (r < 0)
565 return r;
0d536673 566
6b1dc2bd 567 fprintf(f,
6b1dc2bd 568 "[Unit]\n"
c3834f9b
LP
569 "SourcePath=%s\n"
570 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
571 source);
572
700e07ff 573 fprintf(f,
bd10a84b 574 "\n"
6b1dc2bd
LP
575 "[Automount]\n"
576 "Where=%s\n",
98bad05e 577 where_escaped);
6b1dc2bd 578
336b5c61 579 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
580 if (r < 0)
581 return r;
582
4652c56c
ZJS
583 r = fflush_and_check(f);
584 if (r < 0)
fb883e75 585 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
6b1dc2bd 586
da69e8e4 587 r = generator_add_symlink(dest, target_unit,
d2194e15 588 (flags & MOUNT_NOFAIL) ? "wants" : "requires", automount_name);
630d30d3
ZJS
589 if (r < 0)
590 return r;
6b1dc2bd
LP
591 }
592
028a981c
ZJS
593 return true;
594}
595
596static int do_daemon_reload(void) {
597 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
598 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
599 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
600 int r, k;
601
602 log_debug("Calling org.freedesktop.systemd1.Manager.Reload()...");
603
604 r = bus_connect_system_systemd(&bus);
605 if (r < 0)
606 return log_error_errno(r, "Failed to get D-Bus connection: %m");
607
608 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "Reload");
609 if (r < 0)
610 return bus_log_create_error(r);
611
612 r = sd_bus_call(bus, m, DAEMON_RELOAD_TIMEOUT_SEC, &error, NULL);
613 if (r < 0)
614 return log_error_errno(r, "Failed to reload daemon: %s", bus_error_message(&error, r));
615
616 /* We need to requeue the two targets so that any new units which previously were not part of the
617 * targets, and which we now added, will be started. */
618
619 r = 0;
620 FOREACH_STRING(unit, SPECIAL_INITRD_FS_TARGET, SPECIAL_SWAP_TARGET) {
621 log_info("Requesting %s/start/replace...", unit);
622
623 k = sd_bus_call_method(bus,
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
627 "StartUnit",
628 &error,
629 NULL,
630 "ss", unit, "replace");
631 if (k < 0) {
632 log_error_errno(k, "Failed to (re)start %s: %s", unit, bus_error_message(&error, r));
633 if (r == 0)
634 r = k;
635 }
636 }
637
638 return r;
6b1dc2bd
LP
639}
640
99e3d476
ZJS
641static const char* sysroot_fstab_path(void) {
642 return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
643}
644
e48fdd84 645static int parse_fstab(bool initrd) {
6db615c1 646 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 647 const char *fstab;
6b1dc2bd 648 struct mntent *me;
e48fdd84 649 int r = 0;
6b1dc2bd 650
99e3d476
ZJS
651 if (initrd)
652 fstab = sysroot_fstab_path();
028a981c 653 else {
99e3d476 654 fstab = fstab_path();
028a981c
ZJS
655 assert(!arg_sysroot_check);
656 }
99e3d476 657
ed4ad488 658 log_debug("Parsing %s...", fstab);
01a0f7d0 659
ed4ad488 660 f = setmntent(fstab, "re");
6b1dc2bd
LP
661 if (!f) {
662 if (errno == ENOENT)
663 return 0;
664
ed4ad488 665 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
666 }
667
668 while ((me = getmntent(f))) {
634735b5 669 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
04959faa 670 bool makefs, growfs, pcrfs, noauto, nofail;
d2194e15 671 MountPointFlags flags;
6b1dc2bd
LP
672 int k;
673
3d22d1ab
TG
674 if (initrd && !mount_in_initrd(me))
675 continue;
676
6b1dc2bd 677 what = fstab_node_to_udev_node(me->mnt_fsname);
e48fdd84
LP
678 if (!what)
679 return log_oom();
680
18f0eaaf
YW
681 if (path_is_read_only_fs("/sys") > 0) {
682 if (streq(what, "sysfs")) {
c41fff1e 683 log_info("/sys/ is read-only (running in a container?), ignoring fstab entry for %s.", me->mnt_dir);
18f0eaaf
YW
684 continue;
685 }
686
687 if (is_device_path(what)) {
c41fff1e 688 log_info("/sys/ is read-only (running in a container?), ignoring fstab device entry for %s.", what);
18f0eaaf
YW
689 continue;
690 }
689aede8
LP
691 }
692
98eda38a 693 where = strdup(me->mnt_dir);
e48fdd84 694 if (!where)
5862d652 695 return log_oom();
6b1dc2bd 696
634735b5 697 if (is_path(where)) {
4ff361cc 698 path_simplify(where);
f1a2c758 699
634735b5
CW
700 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
701 * mount units, but causes problems since it historically worked to have symlinks in e.g.
702 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
703 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
ff52ff25
ZJS
704 * target is the final directory.
705 *
706 * FIXME: when chase_symlinks() learns to chase non-existent paths, use this here and
707 * drop the prefixing with /sysroot on error below.
708 */
1677b88d 709 k = chase_symlinks(where, initrd ? "/sysroot" : NULL,
634735b5 710 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
a5648b80 711 &canonical_where, NULL);
ff52ff25
ZJS
712 if (k < 0) {
713 /* If we can't canonicalize, continue as if it wasn't a symlink */
714 log_debug_errno(k, "Failed to read symlink target for %s, using as-is: %m", where);
715
716 if (initrd) {
717 canonical_where = path_join("/sysroot", where);
718 if (!canonical_where)
719 return log_oom();
720 }
721
722 } else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
f1a2c758
LP
723 canonical_where = mfree(canonical_where);
724 else
725 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
634735b5 726 }
6b1dc2bd 727
da495a03 728 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
7f2806d5 729 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
04959faa 730 pcrfs = fstab_test_option(me->mnt_opts, "x-systemd.pcrfs\0");
b9f111b9
ZJS
731 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
732 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
f1a2c758 733
04959faa 734 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
5607d856 735 what, where, me->mnt_type,
04959faa 736 yes_no(makefs), yes_no(growfs), yes_no(pcrfs),
5607d856 737 yes_no(noauto), yes_no(nofail));
6b1dc2bd 738
d2194e15
LP
739 flags = makefs * MOUNT_MAKEFS |
740 growfs * MOUNT_GROWFS |
04959faa 741 pcrfs * MOUNT_PCRFS |
d2194e15
LP
742 noauto * MOUNT_NOAUTO |
743 nofail * MOUNT_NOFAIL;
744
6b1dc2bd 745 if (streq(me->mnt_type, "swap"))
7772c177 746 k = add_swap(fstab, what, me, flags);
5e398e54 747 else {
d2194e15 748 bool rw_only, automount;
5e398e54 749
d2194e15 750 rw_only = fstab_test_option(me->mnt_opts, "x-systemd.rw-only\0");
b9f111b9
ZJS
751 automount = fstab_test_option(me->mnt_opts,
752 "comment=systemd.automount\0"
753 "x-systemd.automount\0");
d2194e15
LP
754
755 flags |= rw_only * MOUNT_RW_ONLY |
756 automount * MOUNT_AUTOMOUNT;
757
da69e8e4
ZJS
758 const char *target_unit =
759 initrd ? SPECIAL_INITRD_FS_TARGET :
760 mount_is_network(me) ? SPECIAL_REMOTE_FS_TARGET :
761 SPECIAL_LOCAL_FS_TARGET;
5e398e54 762
7772c177
ZJS
763 k = add_mount(fstab,
764 arg_dest,
91214a37 765 what,
634735b5
CW
766 canonical_where ?: where,
767 canonical_where ? where: NULL,
6db615c1
LP
768 me->mnt_type,
769 me->mnt_opts,
770 me->mnt_passno,
d2194e15 771 flags,
da69e8e4 772 target_unit);
5e398e54 773 }
6b1dc2bd 774
028a981c
ZJS
775 if (arg_sysroot_check && k > 0)
776 return true; /* We found a mount or swap that would be started… */
7f2806d5 777 if (r >= 0 && k < 0)
6b1dc2bd
LP
778 r = k;
779 }
780
6b1dc2bd
LP
781 return r;
782}
783
77b8e92d
YW
784static int sysroot_is_nfsroot(void) {
785 union in_addr_union u;
786 const char *sep, *a;
787 int r;
788
789 assert(arg_root_what);
790
791 /* From dracut.cmdline(7).
792 *
793 * root=[<server-ip>:]<root-dir>[:<nfs-options>]
794 * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>],
795 * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>],
796 * root={dhcp|dhcp6}
797 *
798 * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server.
799 * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options
800 * can be appended with the prefix ":" or "," and are separated by ",". */
801
802 if (path_equal(arg_root_what, "/dev/nfs") ||
803 STR_IN_SET(arg_root_what, "dhcp", "dhcp6") ||
804 STARTSWITH_SET(arg_root_what, "nfs:", "nfs4:"))
805 return true;
806
807 /* IPv6 address */
808 if (arg_root_what[0] == '[') {
809 sep = strchr(arg_root_what + 1, ']');
810 if (!sep)
811 return -EINVAL;
812
ea1c9d3f 813 a = strndupa_safe(arg_root_what + 1, sep - arg_root_what - 1);
77b8e92d
YW
814
815 r = in_addr_from_string(AF_INET6, a, &u);
816 if (r < 0)
817 return r;
818
819 return true;
820 }
821
822 /* IPv4 address */
823 sep = strchr(arg_root_what, ':');
824 if (sep) {
ea1c9d3f 825 a = strndupa_safe(arg_root_what, sep - arg_root_what);
77b8e92d
YW
826
827 if (in_addr_from_string(AF_INET, a, &u) >= 0)
828 return true;
829 }
830
831 /* root directory without address */
832 return path_is_absolute(arg_root_what) && !path_startswith(arg_root_what, "/dev");
833}
834
2e852276 835static int add_sysroot_mount(void) {
75a59316 836 _cleanup_free_ char *what = NULL;
725ad3b0 837 const char *opts, *fstype;
01fdfbb8
WF
838 bool default_rw, makefs;
839 MountPointFlags flags;
7163e1ca 840 int r;
5e398e54 841
093c2cfe
TH
842 if (isempty(arg_root_what)) {
843 log_debug("Could not find a root= entry on the kernel command line.");
844 return 0;
135b5212 845 }
5e398e54 846
138f4c69 847 if (streq(arg_root_what, "gpt-auto")) {
4981427c 848 /* This is handled by gpt-auto-generator */
138f4c69
LP
849 log_debug("Skipping root directory handling, as gpt-auto was requested.");
850 return 0;
851 }
852
77b8e92d
YW
853 r = sysroot_is_nfsroot();
854 if (r < 0)
855 log_debug_errno(r, "Failed to determine if the root directory is on NFS, assuming not: %m");
856 else if (r > 0) {
3ade16c9 857 /* This is handled by the kernel or the initrd */
77b8e92d 858 log_debug("Skipping root directory handling, as root on NFS was requested.");
155e1bb4
YW
859 return 0;
860 }
861
862 if (startswith(arg_root_what, "cifs://")) {
863 log_debug("Skipping root directory handling, as root on CIFS was requested.");
864 return 0;
865 }
866
867 if (startswith(arg_root_what, "iscsi:")) {
868 log_debug("Skipping root directory handling, as root on iSCSI was requested.");
869 return 0;
870 }
871
872 if (startswith(arg_root_what, "live:")) {
873 log_debug("Skipping root directory handling, as root on live image was requested.");
3ade16c9
HH
874 return 0;
875 }
876
725ad3b0
LP
877 if (streq(arg_root_what, "tmpfs")) {
878 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
879
880 what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
881 if (!what)
882 return log_oom();
883
5a12d1ca 884 fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
725ad3b0 885
5a12d1ca 886 default_rw = true; /* writable, unless overridden */;
725ad3b0
LP
887 } else {
888
889 what = fstab_node_to_udev_node(arg_root_what);
890 if (!what)
891 return log_oom();
892
893 fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */
894
5a12d1ca 895 default_rw = false; /* read-only, unless overridden */
725ad3b0 896 }
093c2cfe 897
6db615c1 898 if (!arg_root_options)
725ad3b0 899 opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
75a59316 900 else if (arg_root_rw >= 0 ||
d15d0333 901 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 902 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
903 else
904 opts = arg_root_options;
5e398e54 905
e19ae92a 906 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
7163e1ca
DD
907
908 if (is_device_path(what)) {
909 r = generator_write_initrd_root_device_deps(arg_dest, what);
910 if (r < 0)
911 return r;
912 }
913
01fdfbb8
WF
914 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
915 flags = makefs * MOUNT_MAKEFS;
916
7772c177
ZJS
917 return add_mount("/proc/cmdline",
918 arg_dest,
91214a37 919 what,
6db615c1 920 "/sysroot",
634735b5 921 NULL,
725ad3b0 922 fstype,
75a59316 923 opts,
f113f8e3 924 is_device_path(what) ? 1 : 0, /* passno */
04959faa 925 flags, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
7772c177 926 SPECIAL_INITRD_ROOT_FS_TARGET);
5e398e54
TG
927}
928
2e852276 929static int add_sysroot_usr_mount(void) {
9f103625
TH
930 _cleanup_free_ char *what = NULL;
931 const char *opts;
01fdfbb8
WF
932 bool makefs;
933 MountPointFlags flags;
29a24ab2
LP
934 int r;
935
936 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
937 * know for sure something else did */
9f103625
TH
938
939 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
940 return 0;
941
942 if (arg_root_what && !arg_usr_what) {
40472036 943 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 944 arg_usr_what = strdup(arg_root_what);
9f103625
TH
945 if (!arg_usr_what)
946 return log_oom();
947 }
948
949 if (arg_root_fstype && !arg_usr_fstype) {
950 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
951 if (!arg_usr_fstype)
952 return log_oom();
953 }
954
955 if (arg_root_options && !arg_usr_options) {
956 arg_usr_options = strdup(arg_root_options);
9f103625
TH
957 if (!arg_usr_options)
958 return log_oom();
959 }
960
29a24ab2
LP
961 if (isempty(arg_usr_what)) {
962 log_debug("Could not find a usr= entry on the kernel command line.");
9f103625 963 return 0;
29a24ab2
LP
964 }
965
966 if (streq(arg_usr_what, "gpt-auto")) {
967 /* This is handled by the gpt-auto generator */
968 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
969 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
970 * unit file is being created for the host /usr/ mount. */
971 }
972
973 if (path_equal(arg_usr_what, "/dev/nfs")) {
974 /* This is handled by the initrd (if at all supported, that is) */
975 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
976 return 1; /* As above, report that NFS code will create the unit */
977 }
9f103625
TH
978
979 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
980 if (!what)
981 return log_oom();
9f103625 982
eb580002
MM
983 if (!arg_usr_options)
984 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 985 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 986 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
987 else
988 opts = arg_usr_options;
9f103625 989
29a24ab2
LP
990 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
991 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
992 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
993 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
994 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
995 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
996
997 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts));
998
01fdfbb8
WF
999 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1000 flags = makefs * MOUNT_MAKEFS;
1001
7772c177
ZJS
1002 r = add_mount("/proc/cmdline",
1003 arg_dest,
29a24ab2
LP
1004 what,
1005 "/sysusr/usr",
1006 NULL,
1007 arg_usr_fstype,
1008 opts,
1009 is_device_path(what) ? 1 : 0, /* passno */
01fdfbb8 1010 flags,
7772c177 1011 SPECIAL_INITRD_USR_FS_TARGET);
29a24ab2
LP
1012 if (r < 0)
1013 return r;
1014
1015 log_debug("Synthesizing entry what=/sysusr/usr where=/sysrootr/usr opts=bind");
1016
7772c177
ZJS
1017 r = add_mount("/proc/cmdline",
1018 arg_dest,
29a24ab2
LP
1019 "/sysusr/usr",
1020 "/sysroot/usr",
1021 NULL,
1022 NULL,
1023 "bind",
1024 0,
1025 0,
7772c177 1026 SPECIAL_INITRD_FS_TARGET);
29a24ab2
LP
1027 if (r < 0)
1028 return r;
1029
1030 return 1;
1031}
1032
1033static int add_sysroot_usr_mount_or_fallback(void) {
1034 int r;
1035
1036 r = add_sysroot_usr_mount();
1037 if (r != 0)
1038 return r;
1039
1040 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
1041 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
1042 * themselves get the guarantee that /usr/ is definitely mounted somewhere. */
1043
1044 return generator_add_symlink(
1045 arg_dest,
1046 SPECIAL_INITRD_USR_FS_TARGET,
1047 "requires",
1048 "sysroot.mount");
9f103625
TH
1049}
1050
91214a37 1051static int add_volatile_root(void) {
1de7f825 1052
91214a37 1053 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 1054 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 1055
1de7f825 1056 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 1057 return 0;
91214a37 1058
00bb366d 1059 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
835cf75a 1060 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
1061}
1062
1063static int add_volatile_var(void) {
1064
1065 if (arg_volatile_mode != VOLATILE_STATE)
1066 return 0;
1067
1068 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
1069
7772c177
ZJS
1070 return add_mount("/proc/cmdline",
1071 arg_dest_late,
91214a37
LP
1072 "tmpfs",
1073 "/var",
634735b5 1074 NULL,
91214a37 1075 "tmpfs",
7d85383e 1076 "mode=0755" TMPFS_LIMITS_VAR,
91214a37 1077 0,
4191418b 1078 0,
7772c177 1079 SPECIAL_LOCAL_FS_TARGET);
91214a37
LP
1080}
1081
96287a49 1082static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 1083 int r;
94734142 1084
9f103625
TH
1085 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
1086 * instance should take precedence. In the case of multiple rootflags=
1087 * or usrflags= the arguments should be concatenated */
6db615c1 1088
1d84ad94 1089 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 1090
1d84ad94 1091 r = value ? parse_boolean(value) : 1;
141a79f4 1092 if (r < 0)
059cb385 1093 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 1094 else
e48fdd84 1095 arg_fstab_enabled = r;
94734142 1096
1d84ad94
LP
1097 } else if (streq(key, "root")) {
1098
1099 if (proc_cmdline_value_missing(key, value))
1100 return 0;
6db615c1 1101
b3f9c17a 1102 return free_and_strdup_warn(&arg_root_what, value);
6db615c1 1103
1d84ad94
LP
1104 } else if (streq(key, "rootfstype")) {
1105
1106 if (proc_cmdline_value_missing(key, value))
1107 return 0;
6db615c1 1108
b3f9c17a 1109 return free_and_strdup_warn(&arg_root_fstype, value);
6db615c1 1110
1d84ad94 1111 } else if (streq(key, "rootflags")) {
6db615c1 1112
1d84ad94
LP
1113 if (proc_cmdline_value_missing(key, value))
1114 return 0;
1115
c2bc710b 1116 if (!strextend_with_separator(&arg_root_options, ",", value))
6db615c1
LP
1117 return log_oom();
1118
2f3dfc6f
LP
1119 } else if (streq(key, "roothash")) {
1120
1121 if (proc_cmdline_value_missing(key, value))
1122 return 0;
1123
b3f9c17a 1124 return free_and_strdup_warn(&arg_root_hash, value);
6db615c1 1125
1d84ad94
LP
1126 } else if (streq(key, "mount.usr")) {
1127
1128 if (proc_cmdline_value_missing(key, value))
1129 return 0;
9f103625 1130
b3f9c17a 1131 return free_and_strdup_warn(&arg_usr_what, value);
9f103625 1132
1d84ad94
LP
1133 } else if (streq(key, "mount.usrfstype")) {
1134
1135 if (proc_cmdline_value_missing(key, value))
1136 return 0;
9f103625 1137
b3f9c17a 1138 return free_and_strdup_warn(&arg_usr_fstype, value);
9f103625 1139
1d84ad94 1140 } else if (streq(key, "mount.usrflags")) {
9f103625 1141
1d84ad94
LP
1142 if (proc_cmdline_value_missing(key, value))
1143 return 0;
1144
c2bc710b 1145 if (!strextend_with_separator(&arg_usr_options, ",", value))
9f103625
TH
1146 return log_oom();
1147
c1b9e3df
MB
1148 } else if (streq(key, "usrhash")) {
1149
1150 if (proc_cmdline_value_missing(key, value))
1151 return 0;
1152
1153 return free_and_strdup_warn(&arg_usr_hash, value);
1154
6db615c1
LP
1155 } else if (streq(key, "rw") && !value)
1156 arg_root_rw = true;
1157 else if (streq(key, "ro") && !value)
1158 arg_root_rw = false;
91214a37
LP
1159 else if (streq(key, "systemd.volatile")) {
1160 VolatileMode m;
1161
1162 if (value) {
1163 m = volatile_mode_from_string(value);
1164 if (m < 0)
7211c853 1165 log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value);
91214a37
LP
1166 else
1167 arg_volatile_mode = m;
1168 } else
1169 arg_volatile_mode = VOLATILE_YES;
567a5307 1170
1171 } else if (streq(key, "systemd.swap")) {
1172
1173 r = value ? parse_boolean(value) : 1;
1174 if (r < 0)
1175 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
1176 else
1177 arg_swap_enabled = r;
91214a37 1178 }
94734142 1179
d0aa9ce5 1180 return 0;
94734142
LP
1181}
1182
c1b9e3df 1183static int determine_device(char **what, const char *hash, const char *name) {
2f3dfc6f 1184
c1b9e3df
MB
1185 assert(what);
1186 assert(name);
1187
1188 /* If we have a hash but no device then Verity is used, and we use the DM device. */
1189 if (*what)
2f3dfc6f
LP
1190 return 0;
1191
c1b9e3df 1192 if (!hash)
2f3dfc6f
LP
1193 return 0;
1194
c1b9e3df
MB
1195 *what = path_join("/dev/mapper/", name);
1196 if (!*what)
2f3dfc6f
LP
1197 return log_oom();
1198
c1b9e3df 1199 log_info("Using verity %s device %s.", name, *what);
2f3dfc6f
LP
1200
1201 return 1;
1202}
1203
c1b9e3df
MB
1204static int determine_root(void) {
1205 return determine_device(&arg_root_what, arg_root_hash, "root");
1206}
1207
1208static int determine_usr(void) {
1209 return determine_device(&arg_usr_what, arg_usr_hash, "usr");
1210}
1211
028a981c
ZJS
1212/* If arg_sysroot_check is false, run as generator in the usual fashion.
1213 * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount
1214 * in the initrd, and call daemon-reload. We will get reinvoked as a generator,
1215 * with /sysroot/etc/fstab available, and then we can write additional units based
1216 * on that file. */
1217static int run_generator(void) {
2572957e 1218 int r, r2 = 0, r3 = 0;
6b1dc2bd 1219
1d84ad94 1220 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 1221 if (r < 0)
da927ba9 1222 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 1223
2f3dfc6f 1224 (void) determine_root();
c1b9e3df 1225 (void) determine_usr();
2f3dfc6f 1226
028a981c
ZJS
1227 if (arg_sysroot_check) {
1228 r = parse_fstab(true);
1229 if (r == 0)
1230 log_debug("Nothing interesting found, not doing daemon-reload.");
1231 if (r > 0)
1232 r = do_daemon_reload();
1233 return r;
1234 }
1235
9f103625
TH
1236 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1237 if (in_initrd()) {
2e852276 1238 r = add_sysroot_mount();
003cba39 1239
29a24ab2 1240 r2 = add_sysroot_usr_mount_or_fallback();
91214a37 1241
2572957e 1242 r3 = add_volatile_root();
003cba39 1243 } else
91214a37 1244 r = add_volatile_var();
5e398e54 1245
e48fdd84
LP
1246 /* Honour /etc/fstab only when that's enabled */
1247 if (arg_fstab_enabled) {
e48fdd84 1248 /* Parse the local /etc/fstab, possibly from the initrd */
2572957e 1249 r2 = parse_fstab(false);
ac4785b0 1250
e48fdd84 1251 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e
ZJS
1252 if (in_initrd())
1253 r3 = parse_fstab(true);
9b69569d
ZJS
1254 else
1255 r3 = generator_enable_remount_fs_service(arg_dest);
e48fdd84 1256 }
6b1dc2bd 1257
2572957e 1258 return r < 0 ? r : r2 < 0 ? r2 : r3;
6b1dc2bd 1259}
a4ef3e4d 1260
028a981c
ZJS
1261static int run(int argc, char **argv) {
1262 arg_sysroot_check = invoked_as(argv, "systemd-sysroot-fstab-check");
1263
1264 if (arg_sysroot_check) {
1265 /* Run as in systemd-sysroot-fstab-check mode */
1266 log_setup();
1267
1268 if (strv_length(argv) > 1)
1269 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1270 "This program takes no arguments.");
1271 if (!in_initrd())
1272 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1273 "This program is only useful in the initrd.");
1274 } else {
1275 /* Run in generator mode */
1276 log_setup_generator();
1277
1278 if (!IN_SET(strv_length(argv), 2, 4))
1279 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1280 "This program takes one or three arguments.");
1281
1282 arg_dest = ASSERT_PTR(argv[1]);
1283 arg_dest_late = ASSERT_PTR(argv[argc > 3 ? 3 : 1]);
1284 }
1285
1286 return run_generator();
1287}
1288
1289DEFINE_MAIN_FUNCTION(run);