]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
fstab-generator: drop unneeded initialization
[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"
a9399358 10#include "bus-unit-util.h"
f461a28d 11#include "chase.h"
6ac62485 12#include "creds-util.h"
6c51b49c 13#include "efi-loader.h"
905dd992 14#include "env-util.h"
3ffd4af2 15#include "fd-util.h"
0d39fa9c 16#include "fileio.h"
d15d0333 17#include "fstab-util.h"
07630cea 18#include "generator.h"
77b8e92d 19#include "in-addr-util.h"
baa6a42d 20#include "initrd-util.h"
07630cea 21#include "log.h"
a4ef3e4d 22#include "main-func.h"
07630cea 23#include "mkdir.h"
6b1dc2bd 24#include "mount-setup.h"
4349cd7c 25#include "mount-util.h"
049af8ad 26#include "mountpoint-util.h"
55365b0a 27#include "nulstr-util.h"
6bedfcbb 28#include "parse-util.h"
07630cea 29#include "path-util.h"
4e731273 30#include "proc-cmdline.h"
028a981c 31#include "process-util.h"
6b1dc2bd 32#include "special.h"
98bad05e 33#include "specifier.h"
8fcde012 34#include "stat-util.h"
07630cea 35#include "string-util.h"
059cb385 36#include "strv.h"
07630cea 37#include "unit-name.h"
689aede8 38#include "virt.h"
91214a37 39#include "volatile-util.h"
6b1dc2bd 40
d2194e15
LP
41typedef enum MountPointFlags {
42 MOUNT_NOAUTO = 1 << 0,
43 MOUNT_NOFAIL = 1 << 1,
44 MOUNT_AUTOMOUNT = 1 << 2,
45 MOUNT_MAKEFS = 1 << 3,
46 MOUNT_GROWFS = 1 << 4,
47 MOUNT_RW_ONLY = 1 << 5,
04959faa 48 MOUNT_PCRFS = 1 << 6,
d2194e15 49} MountPointFlags;
4191418b 50
55365b0a 51typedef struct Mount {
45c535dd 52 bool for_initrd;
55365b0a
YW
53 char *what;
54 char *where;
55 char *fstype;
56 char *options;
57} Mount;
58
59static void mount_array_free(Mount *mounts, size_t n);
60
028a981c 61static bool arg_sysroot_check = false;
7a44c7e3
ZJS
62static const char *arg_dest = NULL;
63static const char *arg_dest_late = NULL;
e48fdd84 64static bool arg_fstab_enabled = true;
567a5307 65static bool arg_swap_enabled = true;
6db615c1
LP
66static char *arg_root_what = NULL;
67static char *arg_root_fstype = NULL;
68static char *arg_root_options = NULL;
2f3dfc6f 69static char *arg_root_hash = NULL;
6db615c1 70static int arg_root_rw = -1;
9f103625
TH
71static char *arg_usr_what = NULL;
72static char *arg_usr_fstype = NULL;
73static char *arg_usr_options = NULL;
c1b9e3df 74static char *arg_usr_hash = NULL;
91214a37 75static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
200268c6 76static bool arg_verity = true;
55365b0a
YW
77static Mount *arg_mounts = NULL;
78static size_t arg_n_mounts = 0;
6b1dc2bd 79
a4ef3e4d
YW
80STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
81STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
82STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
83STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
84STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
85STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
86STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
c1b9e3df 87STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep);
55365b0a
YW
88STATIC_ARRAY_DESTRUCTOR_REGISTER(arg_mounts, arg_n_mounts, mount_array_free);
89
90static void mount_done(Mount *m) {
91 assert(m);
92
93 free(m->what);
94 free(m->where);
95 free(m->fstype);
96 free(m->options);
97}
98
99static void mount_array_free(Mount *mounts, size_t n) {
100 FOREACH_ARRAY(m, mounts, n)
101 mount_done(m);
102
103 free(mounts);
104}
105
45c535dd
YW
106static int mount_array_add_internal(
107 bool for_initrd,
108 char *in_what,
109 char *in_where,
110 const char *in_fstype,
111 const char *in_options) {
112
55365b0a
YW
113 _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
114 int r;
115
116 /* This takes what and where. */
117
118 what = ASSERT_PTR(in_what);
119 where = in_where;
120
121 fstype = strdup(isempty(in_fstype) ? "auto" : in_fstype);
122 if (!fstype)
123 return -ENOMEM;
124
125 if (streq(fstype, "swap"))
126 where = mfree(where);
127
128 if (!isempty(in_options)) {
129 _cleanup_strv_free_ char **options_strv = NULL;
130
131 r = strv_split_full(&options_strv, in_options, ",", 0);
132 if (r < 0)
133 return r;
134
135 r = strv_make_nulstr(options_strv, &options, NULL);
136 } else
137 r = strv_make_nulstr(STRV_MAKE("defaults"), &options, NULL);
138 if (r < 0)
139 return r;
140
141 if (!GREEDY_REALLOC(arg_mounts, arg_n_mounts + 1))
142 return -ENOMEM;
143
144 arg_mounts[arg_n_mounts++] = (Mount) {
45c535dd 145 .for_initrd = for_initrd,
55365b0a
YW
146 .what = TAKE_PTR(what),
147 .where = TAKE_PTR(where),
148 .fstype = TAKE_PTR(fstype),
149 .options = TAKE_PTR(options),
150 };
151
152 return 0;
153}
154
45c535dd 155static int mount_array_add(bool for_initrd, const char *str) {
55365b0a
YW
156 _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL;
157 int r;
158
159 assert(str);
160
161 r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS,
162 &what, &where, &fstype, &options, NULL);
163 if (r < 0)
164 return r;
165 if (r < 2)
166 return -EINVAL;
167 if (!isempty(str))
168 return -EINVAL;
169
45c535dd 170 return mount_array_add_internal(for_initrd, TAKE_PTR(what), TAKE_PTR(where), fstype, options);
55365b0a
YW
171}
172
45c535dd 173static int mount_array_add_swap(bool for_initrd, const char *str) {
55365b0a
YW
174 _cleanup_free_ char *what = NULL, *options = NULL;
175 int r;
176
177 assert(str);
178
179 r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS,
180 &what, &options, NULL);
181 if (r < 0)
182 return r;
183 if (r < 1)
184 return -EINVAL;
185 if (!isempty(str))
186 return -EINVAL;
187
45c535dd 188 return mount_array_add_internal(for_initrd, TAKE_PTR(what), NULL, "swap", options);
55365b0a 189}
a4ef3e4d 190
d5cc4be2
LP
191static int write_options(FILE *f, const char *options) {
192 _cleanup_free_ char *o = NULL;
193
194 if (isempty(options))
195 return 0;
196
197 if (streq(options, "defaults"))
198 return 0;
199
98bad05e 200 o = specifier_escape(options);
d5cc4be2
LP
201 if (!o)
202 return log_oom();
203
204 fprintf(f, "Options=%s\n", o);
205 return 1;
206}
207
19d0833b
LP
208static int write_what(FILE *f, const char *what) {
209 _cleanup_free_ char *w = NULL;
210
98bad05e 211 w = specifier_escape(what);
19d0833b
LP
212 if (!w)
213 return log_oom();
214
215 fprintf(f, "What=%s\n", w);
216 return 1;
217}
218
5607d856 219static int add_swap(
7772c177 220 const char *source,
5607d856 221 const char *what,
cfeb4d37 222 const char *options,
d2194e15 223 MountPointFlags flags) {
5607d856 224
fb883e75 225 _cleanup_free_ char *name = NULL;
7fd1b19b 226 _cleanup_fclose_ FILE *f = NULL;
bf1d7ba7 227 int r;
6b1dc2bd
LP
228
229 assert(what);
6b1dc2bd 230
00b4ffde 231 if (access("/proc/swaps", F_OK) < 0) {
55365b0a 232 log_info("Swap not supported, ignoring swap entry for %s.", what);
00b4ffde
LP
233 return 0;
234 }
235
75f86906 236 if (detect_container() > 0) {
55365b0a 237 log_info("Running in a container, ignoring swap entry for %s.", what);
689aede8
LP
238 return 0;
239 }
240
028a981c
ZJS
241 if (arg_sysroot_check) {
242 log_info("%s should be enabled in the initrd, will request daemon-reload.", what);
243 return true;
244 }
245
256604cc
YW
246 log_debug("Found swap entry what=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
247 what,
248 yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS),
249 yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL));
250
7410616c
LP
251 r = unit_name_from_path(what, ".swap", &name);
252 if (r < 0)
253 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 254
7772c177 255 r = generator_open_unit_file(arg_dest, source, name, &f);
fb883e75
ZJS
256 if (r < 0)
257 return r;
0d536673 258
ed4ad488
ZJS
259 fprintf(f,
260 "[Unit]\n"
a7e88558
LP
261 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
262 "SourcePath=%s\n",
7772c177 263 source);
19d0833b 264
a7e88558
LP
265 r = generator_write_blockdev_dependency(f, what);
266 if (r < 0)
267 return r;
268
269 fprintf(f,
270 "\n"
271 "[Swap]\n");
272
19d0833b
LP
273 r = write_what(f, what);
274 if (r < 0)
275 return r;
6b1dc2bd 276
cfeb4d37 277 r = write_options(f, options);
d5cc4be2
LP
278 if (r < 0)
279 return r;
6b1dc2bd 280
47cb901e 281 r = fflush_and_check(f);
23bbb0de 282 if (r < 0)
fb883e75 283 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 284
b3208b66 285 /* use what as where, to have a nicer error message */
cfeb4d37 286 r = generator_write_timeouts(arg_dest, what, what, options, NULL);
b3208b66
ZJS
287 if (r < 0)
288 return r;
289
d2194e15 290 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
291 r = generator_hook_up_mkswap(arg_dest, what);
292 if (r < 0)
293 return r;
294 }
295
d2194e15 296 if (flags & MOUNT_GROWFS)
7f2806d5
ZJS
297 /* TODO: swap devices must be wiped and recreated */
298 log_warning("%s: growing swap devices is currently unsupported.", what);
04959faa
LP
299 if (flags & MOUNT_PCRFS)
300 log_warning("%s: measuring swap devices is currently unsupported.", what);
7f2806d5 301
d2194e15 302 if (!(flags & MOUNT_NOAUTO)) {
630d30d3 303 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
d2194e15 304 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
630d30d3
ZJS
305 if (r < 0)
306 return r;
4e82fe52
TG
307 }
308
028a981c 309 return true;
6b1dc2bd
LP
310}
311
cfeb4d37
YW
312static bool mount_is_network(const char *fstype, const char *options) {
313 return fstab_test_option(options, "_netdev\0") ||
314 (fstype && fstype_is_network(fstype));
6b1dc2bd
LP
315}
316
22f5a825 317static bool mount_in_initrd(const char *where, const char *options, bool accept_root) {
cfeb4d37 318 return fstab_test_option(options, "x-initrd.mount\0") ||
22f5a825 319 (where && PATH_IN_SET(where, "/usr", accept_root ? "/" : NULL));
3d22d1ab
TG
320}
321
33a4c983
LP
322static int write_timeout(
323 FILE *f,
324 const char *where,
325 const char *opts,
326 const char *filter,
327 const char *variable) {
328
deb0a77c 329 _cleanup_free_ char *timeout = NULL;
deb0a77c
MO
330 usec_t u;
331 int r;
332
ff0c31bc 333 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, NULL);
deb0a77c
MO
334 if (r < 0)
335 return log_warning_errno(r, "Failed to parse options: %m");
336 if (r == 0)
337 return 0;
338
0004f698 339 r = parse_sec_fix_0(timeout, &u);
deb0a77c
MO
340 if (r < 0) {
341 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
342 return 0;
343 }
344
5291f26d 345 fprintf(f, "%s=%s\n", variable, FORMAT_TIMESPAN(u, 0));
deb0a77c
MO
346
347 return 0;
348}
2e852276 349
110773f6
CH
350static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
351 return write_timeout(f, where, opts,
352 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
353}
354
355static int write_mount_timeout(FILE *f, const char *where, const char *opts) {
356 return write_timeout(f, where, opts,
357 "x-systemd.mount-timeout\0", "TimeoutSec");
358}
359
33a4c983
LP
360static int write_dependency(
361 FILE *f,
362 const char *opts,
363 const char *filter,
364 const char *format) {
365
3519d230
KZ
366 _cleanup_strv_free_ char **names = NULL, **units = NULL;
367 _cleanup_free_ char *res = NULL;
3519d230
KZ
368 int r;
369
370 assert(f);
371 assert(opts);
372
d6cef552 373 r = fstab_filter_options(opts, filter, NULL, NULL, &names, NULL);
3519d230
KZ
374 if (r < 0)
375 return log_warning_errno(r, "Failed to parse options: %m");
376 if (r == 0)
377 return 0;
378
379 STRV_FOREACH(s, names) {
380 char *x;
381
df7c4eb6 382 r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x);
3519d230
KZ
383 if (r < 0)
384 return log_error_errno(r, "Failed to generate unit name: %m");
33a4c983 385
3519d230
KZ
386 r = strv_consume(&units, x);
387 if (r < 0)
388 return log_oom();
389 }
390
391 if (units) {
392 res = strv_join(units, " ");
393 if (!res)
394 return log_oom();
56e577c6
LP
395
396 DISABLE_WARNING_FORMAT_NONLITERAL;
ae325185 397 fprintf(f, format, res);
56e577c6 398 REENABLE_WARNING;
3519d230
KZ
399 }
400
401 return 0;
402}
403
ae325185 404static int write_after(FILE *f, const char *opts) {
33a4c983 405 return write_dependency(f, opts,
d6cef552 406 "x-systemd.after\0", "After=%1$s\n");
ae325185
RB
407}
408
409static int write_requires_after(FILE *f, const char *opts) {
410 return write_dependency(f, opts,
d6cef552 411 "x-systemd.requires\0", "After=%1$s\nRequires=%1$s\n");
ae325185
RB
412}
413
414static int write_before(FILE *f, const char *opts) {
415 return write_dependency(f, opts,
d6cef552 416 "x-systemd.before\0", "Before=%1$s\n");
ae325185
RB
417}
418
9e615fa3 419static int write_mounts_for(const char *x_opt, const char *unit_setting, FILE *f, const char *opts) {
98bad05e 420 _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL;
3519d230
KZ
421 _cleanup_free_ char *res = NULL;
422 int r;
423
9e615fa3
LB
424 assert(x_opt);
425 assert(unit_setting);
3519d230
KZ
426 assert(f);
427 assert(opts);
428
9e615fa3 429 r = fstab_filter_options(opts, x_opt, NULL, NULL, &paths, NULL);
3519d230
KZ
430 if (r < 0)
431 return log_warning_errno(r, "Failed to parse options: %m");
432 if (r == 0)
433 return 0;
434
98bad05e
LP
435 r = specifier_escape_strv(paths, &paths_escaped);
436 if (r < 0)
437 return log_error_errno(r, "Failed to escape paths: %m");
438
439 res = strv_join(paths_escaped, " ");
3519d230
KZ
440 if (!res)
441 return log_oom();
442
9e615fa3 443 fprintf(f, "%s=%s\n", unit_setting, res);
3519d230
KZ
444
445 return 0;
446}
447
6371e69b
FB
448static int write_extra_dependencies(FILE *f, const char *opts) {
449 int r;
450
451 assert(f);
452
453 if (opts) {
454 r = write_after(f, opts);
455 if (r < 0)
456 return r;
457 r = write_requires_after(f, opts);
458 if (r < 0)
459 return r;
460 r = write_before(f, opts);
461 if (r < 0)
462 return r;
9e615fa3
LB
463 r = write_mounts_for("x-systemd.requires-mounts-for\0", "RequiresMountsFor", f, opts);
464 if (r < 0)
465 return r;
466 r = write_mounts_for("x-systemd.wants-mounts-for\0", "WantsMountsFor", f, opts);
6371e69b
FB
467 if (r < 0)
468 return r;
469 }
470
471 return 0;
472}
473
b3ee0148
MY
474static int mandatory_mount_drop_unapplicable_options(
475 MountPointFlags *flags,
476 const char *where,
477 const char *options,
478 char **ret_options) {
479
480 int r;
481
482 assert(flags);
483 assert(where);
b3ee0148
MY
484 assert(ret_options);
485
486 if (!(*flags & (MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT))) {
c521ce42
MY
487 r = strdup_or_null(options, ret_options);
488 if (r < 0)
489 return r;
b3ee0148 490
b3ee0148
MY
491 return 0;
492 }
493
494 log_debug("Mount '%s' is mandatory, ignoring 'noauto', 'nofail', and 'x-systemd.automount' options.",
495 where);
496
497 *flags &= ~(MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT);
498
499 r = fstab_filter_options(options, "noauto\0nofail\0x-systemd.automount\0", NULL, NULL, NULL, ret_options);
500 if (r < 0)
501 return r;
502
503 return 1;
504}
505
e8d2f6cd 506static int add_mount(
7772c177 507 const char *source,
91214a37 508 const char *dest,
e8d2f6cd
LP
509 const char *what,
510 const char *where,
634735b5 511 const char *original_where,
6db615c1 512 const char *fstype,
e8d2f6cd
LP
513 const char *opts,
514 int passno,
d2194e15 515 MountPointFlags flags,
da69e8e4 516 const char *target_unit) {
e48fdd84 517
b3ee0148
MY
518 _cleanup_free_ char *name = NULL, *automount_name = NULL, *filtered = NULL, *where_escaped = NULL,
519 *opts_root_filtered = NULL;
be02c1cf 520 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 521 _cleanup_fclose_ FILE *f = NULL;
94192cda 522 int r;
6b1dc2bd
LP
523
524 assert(what);
525 assert(where);
da69e8e4 526 assert(target_unit);
5e398e54 527 assert(source);
6b1dc2bd 528
6db615c1 529 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
530 return 0;
531
532 if (!is_path(where)) {
533 log_warning("Mount point %s is not a valid path, ignoring.", where);
534 return 0;
535 }
536
537 if (mount_point_is_api(where) ||
538 mount_point_ignore(where))
539 return 0;
540
028a981c
ZJS
541 if (arg_sysroot_check) {
542 log_info("%s should be mounted in the initrd, will request daemon-reload.", where);
543 return true;
544 }
545
d6cef552 546 r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL);
be02c1cf
AR
547 if (r < 0)
548 return r;
549
d6cef552 550 r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL);
be02c1cf
AR
551 if (r < 0)
552 return r;
553
e48fdd84 554 if (path_equal(where, "/")) {
b3ee0148
MY
555 r = mandatory_mount_drop_unapplicable_options(&flags, where, opts, &opts_root_filtered);
556 if (r < 0)
557 return r;
558 opts = opts_root_filtered;
559
be02c1cf 560 if (!strv_isempty(wanted_by))
b3ee0148 561 log_debug("Ignoring 'x-systemd.wanted-by=' option for root device.");
be02c1cf 562 if (!strv_isempty(required_by))
b3ee0148 563 log_debug("Ignoring 'x-systemd.required-by=' option for root device.");
2e852276 564
be02c1cf
AR
565 required_by = strv_free(required_by);
566 wanted_by = strv_free(wanted_by);
e48fdd84
LP
567 }
568
7410616c
LP
569 r = unit_name_from_path(where, ".mount", &name);
570 if (r < 0)
571 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 572
7772c177 573 r = generator_open_unit_file(dest, source, name, &f);
fb883e75
ZJS
574 if (r < 0)
575 return r;
0d536673 576
5e398e54 577 fprintf(f,
c3834f9b 578 "[Unit]\n"
a7e88558
LP
579 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
580 "SourcePath=%s\n",
c3834f9b 581 source);
6b1dc2bd 582
d2194e15 583 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) &&
65e1dee7
N
584 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
585 /* The default retry timeout that mount.nfs uses for 'bg' mounts
586 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
587 * As we are making 'bg' mounts look like an 'fg' mount to
588 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
589 * we need to explicitly preserve that default, and also ensure
590 * the systemd mount-timeout doesn't interfere.
5a12d1ca 591 * By placing these options first, they can be overridden by
65e1dee7 592 * settings in /etc/fstab. */
e66d2eee 593 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
d2194e15 594 SET_FLAG(flags, MOUNT_NOFAIL, true);
65e1dee7
N
595 }
596
39a4c452
MY
597 if (!strv_isempty(wanted_by) || !strv_isempty(required_by)) {
598 /* If x-systemd.{wanted,required}-by= is specified, target_unit is not used */
599 target_unit = NULL;
600
601 /* Don't set default ordering dependencies on local-fs.target or remote-fs.target, but we
602 * still need to conflict with umount.target. */
603 fputs("DefaultDependencies=no\n"
604 "Conflicts=umount.target\n"
605 "Before=umount.target\n",
606 f);
607 }
608
6371e69b
FB
609 r = write_extra_dependencies(f, opts);
610 if (r < 0)
611 return r;
3519d230 612
da69e8e4 613 /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
fa138f5e 614 * target unit won't affect us. */
39a4c452 615 if (target_unit && !FLAGS_SET(flags, MOUNT_NOFAIL))
da69e8e4 616 fprintf(f, "Before=%s\n", target_unit);
fa138f5e 617
e48fdd84 618 if (passno != 0) {
91214a37 619 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
620 if (r < 0)
621 return r;
622 }
64e70e4b 623
a7e88558
LP
624 r = generator_write_blockdev_dependency(f, what);
625 if (r < 0)
626 return r;
627
628 fprintf(f,
629 "\n"
630 "[Mount]\n");
631
b2efed52
ZJS
632 r = write_what(f, what);
633 if (r < 0)
634 return r;
635
634735b5
CW
636 if (original_where)
637 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
638
639 where_escaped = specifier_escape(where);
640 if (!where_escaped)
641 return log_oom();
642 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 643
98bad05e 644 if (!isempty(fstype) && !streq(fstype, "auto")) {
c2b2df60 645 _cleanup_free_ char *t = NULL;
98bad05e
LP
646
647 t = specifier_escape(fstype);
648 if (!t)
649 return -ENOMEM;
650
651 fprintf(f, "Type=%s\n", t);
652 }
6b1dc2bd 653
91214a37 654 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
655 if (r < 0)
656 return r;
657
4195077a
MK
658 r = generator_write_device_deps(dest, what, where, opts);
659 if (r < 0)
660 return r;
661
9cf22035
LF
662 if (in_initrd() && path_equal(where, "/sysroot") && is_device_path(what)) {
663 r = generator_write_initrd_root_device_deps(dest, what);
664 if (r < 0)
665 return r;
666 }
667
110773f6
CH
668 r = write_mount_timeout(f, where, opts);
669 if (r < 0)
670 return r;
671
d5cc4be2
LP
672 r = write_options(f, filtered);
673 if (r < 0)
674 return r;
5e398e54 675
d2194e15 676 if (flags & MOUNT_RW_ONLY)
f42aa416
MH
677 fprintf(f, "ReadWriteOnly=yes\n");
678
4652c56c
ZJS
679 r = fflush_and_check(f);
680 if (r < 0)
fb883e75 681 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 682
d2194e15 683 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
684 r = generator_hook_up_mkfs(dest, what, where, fstype);
685 if (r < 0)
686 return r;
687 }
688
d2194e15 689 if (flags & MOUNT_GROWFS) {
da69e8e4 690 r = generator_hook_up_growfs(dest, where, target_unit);
7f2806d5
ZJS
691 if (r < 0)
692 return r;
693 }
694
04959faa 695 if (flags & MOUNT_PCRFS) {
be8f478c 696 r = efi_measured_uki(LOG_WARNING);
2f6c52b9 697 if (r == 0)
6c51b49c 698 log_debug("Kernel stub did not measure kernel image into PCR, skipping userspace measurement, too.");
2f6c52b9 699 else if (r > 0) {
6c51b49c
LP
700 r = generator_hook_up_pcrfs(dest, where, target_unit);
701 if (r < 0)
702 return r;
703 }
04959faa
LP
704 }
705
39a4c452 706 if (FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
7410616c
LP
707 r = unit_name_from_path(where, ".automount", &automount_name);
708 if (r < 0)
709 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 710
8a7033ac 711 f = safe_fclose(f);
6b1dc2bd 712
7772c177 713 r = generator_open_unit_file(dest, source, automount_name, &f);
fb883e75
ZJS
714 if (r < 0)
715 return r;
0d536673 716
6b1dc2bd 717 fprintf(f,
6b1dc2bd 718 "[Unit]\n"
c3834f9b
LP
719 "SourcePath=%s\n"
720 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
721 source);
722
700e07ff 723 fprintf(f,
bd10a84b 724 "\n"
6b1dc2bd
LP
725 "[Automount]\n"
726 "Where=%s\n",
98bad05e 727 where_escaped);
6b1dc2bd 728
336b5c61 729 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
730 if (r < 0)
731 return r;
732
4652c56c
ZJS
733 r = fflush_and_check(f);
734 if (r < 0)
fb883e75 735 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
39a4c452 736 }
6b1dc2bd 737
39a4c452
MY
738 if (target_unit) {
739 assert(strv_isempty(wanted_by));
740 assert(strv_isempty(required_by));
741
742 /* noauto has no effect if x-systemd.automount is used */
743 if (!FLAGS_SET(flags, MOUNT_NOAUTO) || automount_name) {
744 r = generator_add_symlink(dest, target_unit,
745 FLAGS_SET(flags, MOUNT_NOFAIL) ? "wants" : "requires",
746 automount_name ?: name);
747 if (r < 0)
748 return r;
749 }
750 } else {
751 const char *unit_name = automount_name ?: name;
752
753 STRV_FOREACH(s, wanted_by) {
754 r = generator_add_symlink(dest, *s, "wants", unit_name);
755 if (r < 0)
756 return r;
757 }
758
759 STRV_FOREACH(s, required_by) {
760 r = generator_add_symlink(dest, *s, "requires", unit_name);
761 if (r < 0)
762 return r;
763 }
764
765 if ((flags & (MOUNT_NOAUTO|MOUNT_NOFAIL)) != 0)
766 log_warning("x-systemd.wanted-by= and/or x-systemd.required-by= specified, 'noauto' and 'nofail' have no effect.");
6b1dc2bd
LP
767 }
768
028a981c
ZJS
769 return true;
770}
771
772static int do_daemon_reload(void) {
773 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
028a981c
ZJS
774 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
775 int r, k;
776
777 log_debug("Calling org.freedesktop.systemd1.Manager.Reload()...");
778
779 r = bus_connect_system_systemd(&bus);
780 if (r < 0)
781 return log_error_errno(r, "Failed to get D-Bus connection: %m");
782
a9399358 783 r = bus_service_manager_reload(bus);
028a981c 784 if (r < 0)
a9399358 785 return r;
028a981c
ZJS
786
787 /* We need to requeue the two targets so that any new units which previously were not part of the
788 * targets, and which we now added, will be started. */
789
790 r = 0;
791 FOREACH_STRING(unit, SPECIAL_INITRD_FS_TARGET, SPECIAL_SWAP_TARGET) {
792 log_info("Requesting %s/start/replace...", unit);
793
59228d0d 794 k = bus_call_method(bus, bus_systemd_mgr, "StartUnit", &error, NULL, "ss", unit, "replace");
028a981c
ZJS
795 if (k < 0) {
796 log_error_errno(k, "Failed to (re)start %s: %s", unit, bus_error_message(&error, r));
9ef648cc 797 RET_GATHER(r, k);
028a981c
ZJS
798 }
799 }
800
801 return r;
6b1dc2bd
LP
802}
803
99e3d476
ZJS
804static const char* sysroot_fstab_path(void) {
805 return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
806}
807
cfeb4d37
YW
808static bool sysfs_check(void) {
809 static int cached = -1;
810 int r;
811
812 if (cached < 0) {
813 r = getenv_bool_secure("SYSTEMD_SYSFS_CHECK");
814 if (r < 0 && r != -ENXIO)
815 log_debug_errno(r, "Failed to parse $SYSTEMD_SYSFS_CHECK, ignoring: %m");
816 cached = r != 0;
817 }
818
819 return cached;
820}
821
dfce61dd
LF
822static int add_sysusr_sysroot_usr_bind_mount(const char *source) {
823 return add_mount(source,
824 arg_dest,
825 "/sysusr/usr",
826 "/sysroot/usr",
827 NULL,
828 NULL,
829 "bind",
830 0,
831 0,
832 SPECIAL_INITRD_FS_TARGET);
833}
834
cfeb4d37
YW
835static MountPointFlags fstab_options_to_flags(const char *options, bool is_swap) {
836 MountPointFlags flags = 0;
837
c521ce42
MY
838 if (isempty(options))
839 return 0;
840
cfeb4d37
YW
841 if (fstab_test_option(options, "x-systemd.makefs\0"))
842 flags |= MOUNT_MAKEFS;
843 if (fstab_test_option(options, "x-systemd.growfs\0"))
844 flags |= MOUNT_GROWFS;
845 if (fstab_test_option(options, "x-systemd.pcrfs\0"))
846 flags |= MOUNT_PCRFS;
847 if (fstab_test_yes_no_option(options, "noauto\0" "auto\0"))
848 flags |= MOUNT_NOAUTO;
849 if (fstab_test_yes_no_option(options, "nofail\0" "fail\0"))
850 flags |= MOUNT_NOFAIL;
851
852 if (!is_swap) {
853 if (fstab_test_option(options, "x-systemd.rw-only\0"))
854 flags |= MOUNT_RW_ONLY;
855 if (fstab_test_option(options,
856 "comment=systemd.automount\0"
857 "x-systemd.automount\0"))
858 flags |= MOUNT_AUTOMOUNT;
859 }
860
861 return flags;
862}
863
8f88e573 864static int canonicalize_mount_path(const char *path, const char *type, bool prefix_sysroot, char **ret) {
b5fd3956
MY
865 _cleanup_free_ char *p = NULL;
866 bool changed;
867 int r;
868
869 assert(path);
870 assert(type);
871 assert(STR_IN_SET(type, "where", "what"));
872 assert(ret);
873
874 // FIXME: when chase() learns to chase non-existent paths, use this here and drop the prefixing with
875 // /sysroot on error below.
8f88e573 876 r = chase(path, prefix_sysroot ? "/sysroot" : NULL, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &p, NULL);
b5fd3956
MY
877 if (r < 0) {
878 log_debug_errno(r, "Failed to chase '%s', using as-is: %m", path);
879
8f88e573 880 if (prefix_sysroot)
b5fd3956
MY
881 p = path_join("/sysroot", path);
882 else
883 p = strdup(path);
884 if (!p)
885 return log_oom();
886
887 path_simplify(p);
888 }
889
890 changed = !streq(path, p);
891 if (changed)
892 log_debug("Canonicalized %s=%s to %s", type, path, p);
893
894 *ret = TAKE_PTR(p);
895 return changed;
896}
897
cfeb4d37
YW
898static int parse_fstab_one(
899 const char *source,
900 const char *what_original,
901 const char *where_original,
902 const char *fstype,
903 const char *options,
904 int passno,
8f88e573 905 bool prefix_sysroot,
22f5a825 906 bool accept_root, /* This takes an effect only when prefix_sysroot is true. */
55365b0a 907 bool use_swap_enabled) {
cfeb4d37 908
1e9b2e4f 909 _cleanup_free_ char *what = NULL, *where = NULL, *opts = NULL;
cfeb4d37 910 MountPointFlags flags;
b5fd3956 911 bool is_swap, where_changed;
cfeb4d37
YW
912 int r;
913
914 assert(what_original);
cfeb4d37 915 assert(fstype);
cfeb4d37 916
22f5a825 917 if (prefix_sysroot && !mount_in_initrd(where_original, options, accept_root))
cfeb4d37
YW
918 return 0;
919
256604cc 920 is_swap = streq_ptr(fstype, "swap");
55365b0a 921 if (is_swap && use_swap_enabled && !arg_swap_enabled) {
3aed2593 922 log_info("Swap unit generation disabled on kernel command line, ignoring swap entry for %s.", what_original);
94456233
YW
923 return 0;
924 }
256604cc 925
cfeb4d37
YW
926 what = fstab_node_to_udev_node(what_original);
927 if (!what)
928 return log_oom();
929
930 if (path_is_read_only_fs("/sys") > 0 &&
931 (streq(what, "sysfs") ||
932 (sysfs_check() && is_device_path(what)))) {
933 log_info("/sys/ is read-only (running in a container?), ignoring mount for %s.", what);
934 return 0;
935 }
936
256604cc
YW
937 flags = fstab_options_to_flags(options, is_swap);
938
939 if (is_swap)
940 return add_swap(source, what, options, flags);
941
239cce38
YW
942 if (passno < 0)
943 passno = is_device_path(what);
944
256604cc
YW
945 assert(where_original); /* 'where' is not necessary for swap entry. */
946
6742eca1 947 if (!is_path(where_original)) {
b5fd3956 948 log_warning("Mount point %s is not a valid path, ignoring.", where_original);
6742eca1
YW
949 return 0;
950 }
cfeb4d37 951
6742eca1
YW
952 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
953 * mount units, but causes problems since it historically worked to have symlinks in e.g.
954 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
955 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
b5fd3956 956 * target is the final directory. */
8f88e573 957 r = canonicalize_mount_path(where_original, "where", prefix_sysroot, &where);
b5fd3956
MY
958 if (r < 0)
959 return r;
960 where_changed = r > 0;
cfeb4d37 961
8f88e573 962 if (prefix_sysroot && fstab_is_bind(options, fstype)) {
b5fd3956
MY
963 /* When in initrd, the source of bind mount needs to be prepended with /sysroot as well. */
964 _cleanup_free_ char *p = NULL;
6742eca1 965
8f88e573 966 r = canonicalize_mount_path(what, "what", prefix_sysroot, &p);
b5fd3956
MY
967 if (r < 0)
968 return r;
cfeb4d37 969
b5fd3956
MY
970 free_and_replace(what, p);
971 }
6742eca1 972
cfeb4d37
YW
973 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
974 what, where, strna(fstype),
975 yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS),
976 yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL));
977
b5fd3956 978 bool is_sysroot = in_initrd() && path_equal(where, "/sysroot");
cfeb4d37
YW
979 /* See comment from add_sysroot_usr_mount() about the need for extra indirection in case /usr needs
980 * to be mounted in order for the root fs to be synthesized based on configuration included in /usr/,
981 * e.g. systemd-repart. */
b5fd3956 982 bool is_sysroot_usr = in_initrd() && path_equal(where, "/sysroot/usr");
cfeb4d37
YW
983
984 const char *target_unit =
cfeb4d37
YW
985 is_sysroot ? SPECIAL_INITRD_ROOT_FS_TARGET :
986 is_sysroot_usr ? SPECIAL_INITRD_USR_FS_TARGET :
b93d9e06 987 prefix_sysroot ? SPECIAL_INITRD_FS_TARGET :
cfeb4d37
YW
988 mount_is_network(fstype, options) ? SPECIAL_REMOTE_FS_TARGET :
989 SPECIAL_LOCAL_FS_TARGET;
990
b3ee0148
MY
991 /* nofail, noauto and x-systemd.automount don't make sense for critical filesystems we must mount in initrd. */
992 if (is_sysroot || is_sysroot_usr) {
993 r = mandatory_mount_drop_unapplicable_options(&flags, where, options, &opts);
1e9b2e4f
MS
994 if (r < 0)
995 return r;
1e9b2e4f
MS
996 options = opts;
997 }
998
cfeb4d37
YW
999 r = add_mount(source,
1000 arg_dest,
1001 what,
b5fd3956
MY
1002 is_sysroot_usr ? "/sysusr/usr" : where,
1003 !is_sysroot_usr && where_changed ? where_original : NULL,
cfeb4d37
YW
1004 fstype,
1005 options,
1006 passno,
1007 flags,
1008 target_unit);
1009 if (r <= 0)
1010 return r;
1011
1012 if (is_sysroot_usr) {
1013 log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind");
1014 r = add_sysusr_sysroot_usr_bind_mount(source);
1015 if (r < 0)
1016 return r;
1017 }
1018
1019 return true;
1020}
1021
8f88e573 1022static int parse_fstab(bool prefix_sysroot) {
6db615c1 1023 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 1024 const char *fstab;
6b1dc2bd 1025 struct mntent *me;
cfeb4d37 1026 int r, ret = 0;
6b1dc2bd 1027
8f88e573 1028 if (prefix_sysroot)
99e3d476 1029 fstab = sysroot_fstab_path();
028a981c 1030 else {
99e3d476 1031 fstab = fstab_path();
028a981c
ZJS
1032 assert(!arg_sysroot_check);
1033 }
99e3d476 1034
ed4ad488 1035 log_debug("Parsing %s...", fstab);
01a0f7d0 1036
ed4ad488 1037 f = setmntent(fstab, "re");
6b1dc2bd
LP
1038 if (!f) {
1039 if (errno == ENOENT)
1040 return 0;
1041
ed4ad488 1042 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
1043 }
1044
1045 while ((me = getmntent(f))) {
55365b0a
YW
1046 r = parse_fstab_one(fstab,
1047 me->mnt_fsname, me->mnt_dir, me->mnt_type, me->mnt_opts, me->mnt_passno,
22f5a825
YW
1048 prefix_sysroot,
1049 /* accept_root = */ false,
1050 /* use_swap_enabled = */ true);
cfeb4d37
YW
1051 if (r < 0 && ret >= 0)
1052 ret = r;
1053 if (arg_sysroot_check && r > 0)
028a981c 1054 return true; /* We found a mount or swap that would be started… */
6b1dc2bd
LP
1055 }
1056
cfeb4d37 1057 return ret;
6b1dc2bd
LP
1058}
1059
77b8e92d
YW
1060static int sysroot_is_nfsroot(void) {
1061 union in_addr_union u;
1062 const char *sep, *a;
1063 int r;
1064
1065 assert(arg_root_what);
1066
1067 /* From dracut.cmdline(7).
1068 *
1069 * root=[<server-ip>:]<root-dir>[:<nfs-options>]
1070 * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>],
1071 * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>],
1072 * root={dhcp|dhcp6}
1073 *
1074 * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server.
1075 * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options
1076 * can be appended with the prefix ":" or "," and are separated by ",". */
1077
1078 if (path_equal(arg_root_what, "/dev/nfs") ||
1079 STR_IN_SET(arg_root_what, "dhcp", "dhcp6") ||
1080 STARTSWITH_SET(arg_root_what, "nfs:", "nfs4:"))
1081 return true;
1082
1083 /* IPv6 address */
1084 if (arg_root_what[0] == '[') {
1085 sep = strchr(arg_root_what + 1, ']');
1086 if (!sep)
1087 return -EINVAL;
1088
ea1c9d3f 1089 a = strndupa_safe(arg_root_what + 1, sep - arg_root_what - 1);
77b8e92d
YW
1090
1091 r = in_addr_from_string(AF_INET6, a, &u);
1092 if (r < 0)
1093 return r;
1094
1095 return true;
1096 }
1097
1098 /* IPv4 address */
1099 sep = strchr(arg_root_what, ':');
1100 if (sep) {
ea1c9d3f 1101 a = strndupa_safe(arg_root_what, sep - arg_root_what);
77b8e92d
YW
1102
1103 if (in_addr_from_string(AF_INET, a, &u) >= 0)
1104 return true;
1105 }
1106
1107 /* root directory without address */
1108 return path_is_absolute(arg_root_what) && !path_startswith(arg_root_what, "/dev");
1109}
1110
2e852276 1111static int add_sysroot_mount(void) {
75a59316 1112 _cleanup_free_ char *what = NULL;
725ad3b0 1113 const char *opts, *fstype;
01fdfbb8
WF
1114 bool default_rw, makefs;
1115 MountPointFlags flags;
7163e1ca 1116 int r;
5e398e54 1117
093c2cfe
TH
1118 if (isempty(arg_root_what)) {
1119 log_debug("Could not find a root= entry on the kernel command line.");
1120 return 0;
135b5212 1121 }
5e398e54 1122
138f4c69 1123 if (streq(arg_root_what, "gpt-auto")) {
4981427c 1124 /* This is handled by gpt-auto-generator */
138f4c69
LP
1125 log_debug("Skipping root directory handling, as gpt-auto was requested.");
1126 return 0;
dfce61dd
LF
1127 } else if (streq(arg_root_what, "fstab")) {
1128 /* This is handled by parse_fstab */
1129 log_debug("Using initrd's fstab for /sysroot/ configuration.");
1130 return 0;
138f4c69
LP
1131 }
1132
77b8e92d
YW
1133 r = sysroot_is_nfsroot();
1134 if (r < 0)
1135 log_debug_errno(r, "Failed to determine if the root directory is on NFS, assuming not: %m");
1136 else if (r > 0) {
3ade16c9 1137 /* This is handled by the kernel or the initrd */
77b8e92d 1138 log_debug("Skipping root directory handling, as root on NFS was requested.");
155e1bb4
YW
1139 return 0;
1140 }
1141
1142 if (startswith(arg_root_what, "cifs://")) {
1143 log_debug("Skipping root directory handling, as root on CIFS was requested.");
1144 return 0;
1145 }
1146
1147 if (startswith(arg_root_what, "iscsi:")) {
1148 log_debug("Skipping root directory handling, as root on iSCSI was requested.");
1149 return 0;
1150 }
1151
1152 if (startswith(arg_root_what, "live:")) {
1153 log_debug("Skipping root directory handling, as root on live image was requested.");
3ade16c9
HH
1154 return 0;
1155 }
1156
725ad3b0
LP
1157 if (streq(arg_root_what, "tmpfs")) {
1158 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
1159
1160 what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
1161 if (!what)
1162 return log_oom();
1163
5a12d1ca 1164 fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
725ad3b0 1165
5a12d1ca 1166 default_rw = true; /* writable, unless overridden */;
725ad3b0
LP
1167 } else {
1168
1169 what = fstab_node_to_udev_node(arg_root_what);
1170 if (!what)
1171 return log_oom();
1172
1173 fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */
1174
5a12d1ca 1175 default_rw = false; /* read-only, unless overridden */
725ad3b0 1176 }
093c2cfe 1177
6db615c1 1178 if (!arg_root_options)
725ad3b0 1179 opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
75a59316 1180 else if (arg_root_rw >= 0 ||
d15d0333 1181 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 1182 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
1183 else
1184 opts = arg_root_options;
5e398e54 1185
e19ae92a 1186 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
7163e1ca 1187
01fdfbb8
WF
1188 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1189 flags = makefs * MOUNT_MAKEFS;
1190
7772c177
ZJS
1191 return add_mount("/proc/cmdline",
1192 arg_dest,
91214a37 1193 what,
6db615c1 1194 "/sysroot",
634735b5 1195 NULL,
725ad3b0 1196 fstype,
75a59316 1197 opts,
f113f8e3 1198 is_device_path(what) ? 1 : 0, /* passno */
04959faa 1199 flags, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
7772c177 1200 SPECIAL_INITRD_ROOT_FS_TARGET);
5e398e54
TG
1201}
1202
2e852276 1203static int add_sysroot_usr_mount(void) {
9f103625
TH
1204 _cleanup_free_ char *what = NULL;
1205 const char *opts;
01fdfbb8
WF
1206 bool makefs;
1207 MountPointFlags flags;
29a24ab2
LP
1208 int r;
1209
1210 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
1211 * know for sure something else did */
9f103625
TH
1212
1213 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
1214 return 0;
1215
1216 if (arg_root_what && !arg_usr_what) {
40472036 1217 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 1218 arg_usr_what = strdup(arg_root_what);
9f103625
TH
1219 if (!arg_usr_what)
1220 return log_oom();
1221 }
1222
1223 if (arg_root_fstype && !arg_usr_fstype) {
1224 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
1225 if (!arg_usr_fstype)
1226 return log_oom();
1227 }
1228
1229 if (arg_root_options && !arg_usr_options) {
1230 arg_usr_options = strdup(arg_root_options);
9f103625
TH
1231 if (!arg_usr_options)
1232 return log_oom();
1233 }
1234
29a24ab2 1235 if (isempty(arg_usr_what)) {
32fe629a 1236 log_debug("Could not find a mount.usr= entry on the kernel command line.");
9f103625 1237 return 0;
29a24ab2
LP
1238 }
1239
1240 if (streq(arg_usr_what, "gpt-auto")) {
1241 /* This is handled by the gpt-auto generator */
1242 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
1243 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
1244 * unit file is being created for the host /usr/ mount. */
dfce61dd
LF
1245 } else if (streq(arg_usr_what, "fstab")) {
1246 /* This is handled by parse_fstab */
1247 log_debug("Using initrd's fstab for /sysroot/usr/ configuration.");
1248 return 1; /* parse_fstab will generate a unit for this, hence report that a
1249 * unit file is being created for the host /usr/ mount. */
29a24ab2
LP
1250 }
1251
1252 if (path_equal(arg_usr_what, "/dev/nfs")) {
1253 /* This is handled by the initrd (if at all supported, that is) */
1254 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
1255 return 1; /* As above, report that NFS code will create the unit */
1256 }
9f103625
TH
1257
1258 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
1259 if (!what)
1260 return log_oom();
9f103625 1261
eb580002
MM
1262 if (!arg_usr_options)
1263 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 1264 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 1265 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
1266 else
1267 opts = arg_usr_options;
9f103625 1268
29a24ab2
LP
1269 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
1270 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
1271 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
1272 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
1273 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
1274 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
1275
1276 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts));
1277
01fdfbb8
WF
1278 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1279 flags = makefs * MOUNT_MAKEFS;
1280
7772c177
ZJS
1281 r = add_mount("/proc/cmdline",
1282 arg_dest,
29a24ab2
LP
1283 what,
1284 "/sysusr/usr",
1285 NULL,
1286 arg_usr_fstype,
1287 opts,
1288 is_device_path(what) ? 1 : 0, /* passno */
01fdfbb8 1289 flags,
7772c177 1290 SPECIAL_INITRD_USR_FS_TARGET);
29a24ab2
LP
1291 if (r < 0)
1292 return r;
1293
dfce61dd 1294 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
29a24ab2 1295
dfce61dd 1296 r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline");
29a24ab2
LP
1297 if (r < 0)
1298 return r;
1299
1300 return 1;
1301}
1302
1303static int add_sysroot_usr_mount_or_fallback(void) {
1304 int r;
1305
1306 r = add_sysroot_usr_mount();
1307 if (r != 0)
1308 return r;
1309
1310 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
1311 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
de47cd06 1312 * themselves after it get the guarantee that /usr/ is definitely mounted somewhere. */
29a24ab2
LP
1313
1314 return generator_add_symlink(
1315 arg_dest,
1316 SPECIAL_INITRD_USR_FS_TARGET,
1317 "requires",
1318 "sysroot.mount");
9f103625
TH
1319}
1320
91214a37 1321static int add_volatile_root(void) {
1de7f825 1322
91214a37 1323 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 1324 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 1325
1de7f825 1326 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 1327 return 0;
91214a37 1328
00bb366d 1329 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
835cf75a 1330 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
1331}
1332
1333static int add_volatile_var(void) {
1334
1335 if (arg_volatile_mode != VOLATILE_STATE)
1336 return 0;
1337
1338 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
1339
7772c177
ZJS
1340 return add_mount("/proc/cmdline",
1341 arg_dest_late,
91214a37
LP
1342 "tmpfs",
1343 "/var",
634735b5 1344 NULL,
91214a37 1345 "tmpfs",
7d85383e 1346 "mode=0755" TMPFS_LIMITS_VAR,
91214a37 1347 0,
4191418b 1348 0,
7772c177 1349 SPECIAL_LOCAL_FS_TARGET);
91214a37
LP
1350}
1351
55365b0a 1352static int add_mounts_from_cmdline(void) {
ba2f3ec8 1353 int r = 0;
55365b0a
YW
1354
1355 /* Handle each entries found in cmdline as a fstab entry. */
1356
1357 FOREACH_ARRAY(m, arg_mounts, arg_n_mounts) {
45c535dd
YW
1358 if (m->for_initrd && !in_initrd())
1359 continue;
1360
ba2f3ec8
MY
1361 RET_GATHER(r, parse_fstab_one("/proc/cmdline",
1362 m->what,
1363 m->where,
1364 m->fstype,
1365 m->options,
1366 /* passno = */ -1,
1367 /* prefix_sysroot = */ !m->for_initrd && in_initrd(),
1368 /* accept_root = */ true,
1369 /* use_swap_enabled = */ false));
55365b0a
YW
1370 }
1371
ba2f3ec8 1372 return r;
55365b0a
YW
1373}
1374
dfd10549 1375static int add_mounts_from_creds(bool prefix_sysroot) {
6ac62485
LP
1376 _cleanup_free_ void *b = NULL;
1377 struct mntent *me;
6ac62485 1378 size_t bs;
ba2f3ec8 1379 int r;
6ac62485 1380
dfd10549
YW
1381 assert(in_initrd() || !prefix_sysroot);
1382
6ac62485 1383 r = read_credential_with_decryption(
dfd10549 1384 in_initrd() && !prefix_sysroot ? "fstab.extra.initrd" : "fstab.extra",
6ac62485
LP
1385 &b, &bs);
1386 if (r <= 0)
1387 return r;
1388
1389 _cleanup_fclose_ FILE *f = NULL;
1390 f = fmemopen_unlocked(b, bs, "r");
1391 if (!f)
1392 return log_oom();
1393
ba2f3ec8 1394 r = 0;
6ac62485 1395
ba2f3ec8
MY
1396 while ((me = getmntent(f)))
1397 RET_GATHER(r, parse_fstab_one("/run/credentials",
1398 me->mnt_fsname,
1399 me->mnt_dir,
1400 me->mnt_type,
1401 me->mnt_opts,
1402 me->mnt_passno,
1403 /* prefix_sysroot = */ prefix_sysroot,
1404 /* accept_root = */ true,
1405 /* use_swap_enabled = */ true));
1406
1407 return r;
6ac62485
LP
1408}
1409
96287a49 1410static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 1411 int r;
94734142 1412
a8d3315b
YW
1413 assert(key);
1414
9f103625
TH
1415 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
1416 * instance should take precedence. In the case of multiple rootflags=
1417 * or usrflags= the arguments should be concatenated */
6db615c1 1418
1d84ad94 1419 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 1420
1d84ad94 1421 r = value ? parse_boolean(value) : 1;
141a79f4 1422 if (r < 0)
059cb385 1423 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 1424 else
b564224b 1425 arg_fstab_enabled = fstab_set_enabled(r);
94734142 1426
1d84ad94
LP
1427 } else if (streq(key, "root")) {
1428
1429 if (proc_cmdline_value_missing(key, value))
1430 return 0;
6db615c1 1431
338da501 1432 return free_and_strdup_warn(&arg_root_what, empty_to_null(value));
6db615c1 1433
1d84ad94
LP
1434 } else if (streq(key, "rootfstype")) {
1435
1436 if (proc_cmdline_value_missing(key, value))
1437 return 0;
6db615c1 1438
338da501 1439 return free_and_strdup_warn(&arg_root_fstype, empty_to_null(value));
6db615c1 1440
1d84ad94 1441 } else if (streq(key, "rootflags")) {
6db615c1 1442
1d84ad94
LP
1443 if (proc_cmdline_value_missing(key, value))
1444 return 0;
1445
c2bc710b 1446 if (!strextend_with_separator(&arg_root_options, ",", value))
6db615c1
LP
1447 return log_oom();
1448
2f3dfc6f
LP
1449 } else if (streq(key, "roothash")) {
1450
1451 if (proc_cmdline_value_missing(key, value))
1452 return 0;
1453
338da501 1454 return free_and_strdup_warn(&arg_root_hash, empty_to_null(value));
6db615c1 1455
1d84ad94
LP
1456 } else if (streq(key, "mount.usr")) {
1457
1458 if (proc_cmdline_value_missing(key, value))
1459 return 0;
9f103625 1460
338da501 1461 return free_and_strdup_warn(&arg_usr_what, empty_to_null(value));
9f103625 1462
1d84ad94
LP
1463 } else if (streq(key, "mount.usrfstype")) {
1464
1465 if (proc_cmdline_value_missing(key, value))
1466 return 0;
9f103625 1467
338da501 1468 return free_and_strdup_warn(&arg_usr_fstype, empty_to_null(value));
9f103625 1469
1d84ad94 1470 } else if (streq(key, "mount.usrflags")) {
9f103625 1471
1d84ad94
LP
1472 if (proc_cmdline_value_missing(key, value))
1473 return 0;
1474
c2bc710b 1475 if (!strextend_with_separator(&arg_usr_options, ",", value))
9f103625
TH
1476 return log_oom();
1477
c1b9e3df
MB
1478 } else if (streq(key, "usrhash")) {
1479
1480 if (proc_cmdline_value_missing(key, value))
1481 return 0;
1482
338da501 1483 return free_and_strdup_warn(&arg_usr_hash, empty_to_null(value));
c1b9e3df 1484
6db615c1
LP
1485 } else if (streq(key, "rw") && !value)
1486 arg_root_rw = true;
1487 else if (streq(key, "ro") && !value)
1488 arg_root_rw = false;
91214a37
LP
1489 else if (streq(key, "systemd.volatile")) {
1490 VolatileMode m;
1491
1492 if (value) {
1493 m = volatile_mode_from_string(value);
1494 if (m < 0)
7211c853 1495 log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value);
91214a37
LP
1496 else
1497 arg_volatile_mode = m;
1498 } else
1499 arg_volatile_mode = VOLATILE_YES;
567a5307 1500
1501 } else if (streq(key, "systemd.swap")) {
1502
1503 r = value ? parse_boolean(value) : 1;
1504 if (r < 0)
1505 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
1506 else
1507 arg_swap_enabled = r;
200268c6
DDM
1508
1509 } else if (streq(key, "systemd.verity")) {
1510
1511 r = value ? parse_boolean(value) : 1;
1512 if (r < 0)
1513 log_warning("Failed to parse systemd.verity= kernel command line switch %s. Ignoring.", value);
1514 else
1515 arg_verity = r;
55365b0a 1516
45c535dd 1517 } else if (STR_IN_SET(key, "systemd.mount-extra", "rd.systemd.mount-extra")) {
55365b0a
YW
1518
1519 if (proc_cmdline_value_missing(key, value))
1520 return 0;
1521
45c535dd 1522 r = mount_array_add(startswith(key, "rd."), value);
55365b0a
YW
1523 if (r < 0)
1524 log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value);
1525
45c535dd 1526 } else if (STR_IN_SET(key, "systemd.swap-extra", "rd.systemd.swap-extra")) {
55365b0a
YW
1527
1528 if (proc_cmdline_value_missing(key, value))
1529 return 0;
1530
45c535dd 1531 r = mount_array_add_swap(startswith(key, "rd."), value);
55365b0a
YW
1532 if (r < 0)
1533 log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value);
91214a37 1534 }
94734142 1535
d0aa9ce5 1536 return 0;
94734142
LP
1537}
1538
2130a2e5
LP
1539static int determine_device(
1540 char **what,
1541 int *rw,
1542 char **options,
1543 const char *hash,
1544 const char *name) {
2f3dfc6f 1545
c1b9e3df
MB
1546 assert(what);
1547 assert(name);
1548
1549 /* If we have a hash but no device then Verity is used, and we use the DM device. */
1550 if (*what)
2f3dfc6f
LP
1551 return 0;
1552
c1b9e3df 1553 if (!hash)
2f3dfc6f
LP
1554 return 0;
1555
200268c6
DDM
1556 if (!arg_verity)
1557 return 0;
1558
c1b9e3df
MB
1559 *what = path_join("/dev/mapper/", name);
1560 if (!*what)
2f3dfc6f
LP
1561 return log_oom();
1562
2130a2e5
LP
1563 /* Verity is always read-only */
1564 if (rw)
1565 *rw = false;
1566 if (options && !strextend_with_separator(options, ",", "ro"))
1567 return log_oom();
2f3dfc6f 1568
2130a2e5 1569 log_info("Using verity %s device %s.", name, *what);
2f3dfc6f
LP
1570 return 1;
1571}
1572
c1b9e3df 1573static int determine_root(void) {
2130a2e5 1574 return determine_device(&arg_root_what, &arg_root_rw, NULL, arg_root_hash, "root");
c1b9e3df
MB
1575}
1576
1577static int determine_usr(void) {
2130a2e5 1578 return determine_device(&arg_usr_what, NULL, &arg_usr_options, arg_usr_hash, "usr");
c1b9e3df
MB
1579}
1580
028a981c
ZJS
1581/* If arg_sysroot_check is false, run as generator in the usual fashion.
1582 * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount
1583 * in the initrd, and call daemon-reload. We will get reinvoked as a generator,
1584 * with /sysroot/etc/fstab available, and then we can write additional units based
1585 * on that file. */
1586static int run_generator(void) {
4c7cc696 1587 int r;
6b1dc2bd 1588
1d84ad94 1589 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 1590 if (r < 0)
da927ba9 1591 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 1592
2f3dfc6f 1593 (void) determine_root();
c1b9e3df 1594 (void) determine_usr();
2f3dfc6f 1595
028a981c 1596 if (arg_sysroot_check) {
8f88e573 1597 r = parse_fstab(/* prefix_sysroot = */ true);
028a981c
ZJS
1598 if (r == 0)
1599 log_debug("Nothing interesting found, not doing daemon-reload.");
1600 if (r > 0)
1601 r = do_daemon_reload();
1602 return r;
1603 }
1604
ba2f3ec8
MY
1605 r = 0;
1606
9f103625
TH
1607 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1608 if (in_initrd()) {
ba2f3ec8 1609 RET_GATHER(r, add_sysroot_mount());
003cba39 1610
ba2f3ec8 1611 RET_GATHER(r, add_sysroot_usr_mount_or_fallback());
91214a37 1612
ba2f3ec8
MY
1613 RET_GATHER(r, add_volatile_root());
1614 } else
1615 RET_GATHER(r, add_volatile_var());
5e398e54 1616
e48fdd84
LP
1617 /* Honour /etc/fstab only when that's enabled */
1618 if (arg_fstab_enabled) {
e48fdd84 1619 /* Parse the local /etc/fstab, possibly from the initrd */
ba2f3ec8 1620 RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ false));
ac4785b0 1621
e48fdd84 1622 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e 1623 if (in_initrd())
ba2f3ec8 1624 RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ true));
9b69569d 1625 else
ba2f3ec8 1626 RET_GATHER(r, generator_enable_remount_fs_service(arg_dest));
e48fdd84 1627 }
6b1dc2bd 1628
ba2f3ec8 1629 RET_GATHER(r, add_mounts_from_cmdline());
55365b0a 1630
ba2f3ec8 1631 RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ false));
6ac62485 1632
ba2f3ec8
MY
1633 if (in_initrd())
1634 RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ true));
dfd10549 1635
ba2f3ec8 1636 return r;
6b1dc2bd 1637}
a4ef3e4d 1638
028a981c
ZJS
1639static int run(int argc, char **argv) {
1640 arg_sysroot_check = invoked_as(argv, "systemd-sysroot-fstab-check");
1641
1642 if (arg_sysroot_check) {
1643 /* Run as in systemd-sysroot-fstab-check mode */
1644 log_setup();
1645
1646 if (strv_length(argv) > 1)
1647 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1648 "This program takes no arguments.");
1649 if (!in_initrd())
1650 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1651 "This program is only useful in the initrd.");
1652 } else {
1653 /* Run in generator mode */
1654 log_setup_generator();
1655
1656 if (!IN_SET(strv_length(argv), 2, 4))
1657 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1658 "This program takes one or three arguments.");
1659
1660 arg_dest = ASSERT_PTR(argv[1]);
1661 arg_dest_late = ASSERT_PTR(argv[argc > 3 ? 3 : 1]);
1662 }
1663
1664 return run_generator();
1665}
1666
1667DEFINE_MAIN_FUNCTION(run);