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