]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
ndisc: Also set link hoplimit
[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
3519d230 419static int write_requires_mounts_for(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
424 assert(f);
425 assert(opts);
426
d6cef552 427 r = fstab_filter_options(opts, "x-systemd.requires-mounts-for\0", NULL, NULL, &paths, NULL);
3519d230
KZ
428 if (r < 0)
429 return log_warning_errno(r, "Failed to parse options: %m");
430 if (r == 0)
431 return 0;
432
98bad05e
LP
433 r = specifier_escape_strv(paths, &paths_escaped);
434 if (r < 0)
435 return log_error_errno(r, "Failed to escape paths: %m");
436
437 res = strv_join(paths_escaped, " ");
3519d230
KZ
438 if (!res)
439 return log_oom();
440
441 fprintf(f, "RequiresMountsFor=%s\n", res);
442
443 return 0;
444}
445
6371e69b
FB
446static int write_extra_dependencies(FILE *f, const char *opts) {
447 int r;
448
449 assert(f);
450
451 if (opts) {
452 r = write_after(f, opts);
453 if (r < 0)
454 return r;
455 r = write_requires_after(f, opts);
456 if (r < 0)
457 return r;
458 r = write_before(f, opts);
459 if (r < 0)
460 return r;
461 r = write_requires_mounts_for(f, opts);
462 if (r < 0)
463 return r;
464 }
465
466 return 0;
467}
468
e8d2f6cd 469static int add_mount(
7772c177 470 const char *source,
91214a37 471 const char *dest,
e8d2f6cd
LP
472 const char *what,
473 const char *where,
634735b5 474 const char *original_where,
6db615c1 475 const char *fstype,
e8d2f6cd
LP
476 const char *opts,
477 int passno,
d2194e15 478 MountPointFlags flags,
da69e8e4 479 const char *target_unit) {
e48fdd84 480
7fd1b19b 481 _cleanup_free_ char
da495a03 482 *name = NULL,
fb883e75 483 *automount_name = NULL,
98bad05e
LP
484 *filtered = NULL,
485 *where_escaped = NULL;
be02c1cf 486 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 487 _cleanup_fclose_ FILE *f = NULL;
94192cda 488 int r;
6b1dc2bd
LP
489
490 assert(what);
491 assert(where);
5e398e54 492 assert(opts);
da69e8e4 493 assert(target_unit);
5e398e54 494 assert(source);
6b1dc2bd 495
6db615c1 496 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
497 return 0;
498
499 if (!is_path(where)) {
500 log_warning("Mount point %s is not a valid path, ignoring.", where);
501 return 0;
502 }
503
504 if (mount_point_is_api(where) ||
505 mount_point_ignore(where))
506 return 0;
507
028a981c
ZJS
508 if (arg_sysroot_check) {
509 log_info("%s should be mounted in the initrd, will request daemon-reload.", where);
510 return true;
511 }
512
d6cef552 513 r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL);
be02c1cf
AR
514 if (r < 0)
515 return r;
516
d6cef552 517 r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL);
be02c1cf
AR
518 if (r < 0)
519 return r;
520
e48fdd84 521 if (path_equal(where, "/")) {
d2194e15
LP
522 if (flags & MOUNT_NOAUTO)
523 log_warning("Ignoring \"noauto\" option for root device");
524 if (flags & MOUNT_NOFAIL)
525 log_warning("Ignoring \"nofail\" option for root device");
526 if (flags & MOUNT_AUTOMOUNT)
527 log_warning("Ignoring \"automount\" option for root device");
be02c1cf 528 if (!strv_isempty(wanted_by))
d2194e15 529 log_warning("Ignoring \"x-systemd.wanted-by=\" option for root device");
be02c1cf 530 if (!strv_isempty(required_by))
d2194e15 531 log_warning("Ignoring \"x-systemd.required-by=\" option for root device");
2e852276 532
be02c1cf
AR
533 required_by = strv_free(required_by);
534 wanted_by = strv_free(wanted_by);
d2194e15 535 SET_FLAG(flags, MOUNT_NOAUTO | MOUNT_NOFAIL | MOUNT_AUTOMOUNT, false);
e48fdd84
LP
536 }
537
7410616c
LP
538 r = unit_name_from_path(where, ".mount", &name);
539 if (r < 0)
540 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 541
7772c177 542 r = generator_open_unit_file(dest, source, name, &f);
fb883e75
ZJS
543 if (r < 0)
544 return r;
0d536673 545
5e398e54 546 fprintf(f,
c3834f9b 547 "[Unit]\n"
a7e88558
LP
548 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
549 "SourcePath=%s\n",
c3834f9b 550 source);
6b1dc2bd 551
d2194e15 552 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) &&
65e1dee7
N
553 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
554 /* The default retry timeout that mount.nfs uses for 'bg' mounts
555 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
556 * As we are making 'bg' mounts look like an 'fg' mount to
557 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
558 * we need to explicitly preserve that default, and also ensure
559 * the systemd mount-timeout doesn't interfere.
5a12d1ca 560 * By placing these options first, they can be overridden by
65e1dee7 561 * settings in /etc/fstab. */
e66d2eee 562 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
d2194e15 563 SET_FLAG(flags, MOUNT_NOFAIL, true);
65e1dee7
N
564 }
565
6371e69b
FB
566 r = write_extra_dependencies(f, opts);
567 if (r < 0)
568 return r;
3519d230 569
da69e8e4 570 /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
fa138f5e 571 * target unit won't affect us. */
da69e8e4
ZJS
572 if (!FLAGS_SET(flags, MOUNT_NOFAIL))
573 fprintf(f, "Before=%s\n", target_unit);
fa138f5e 574
e48fdd84 575 if (passno != 0) {
91214a37 576 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
577 if (r < 0)
578 return r;
579 }
64e70e4b 580
a7e88558
LP
581 r = generator_write_blockdev_dependency(f, what);
582 if (r < 0)
583 return r;
584
585 fprintf(f,
586 "\n"
587 "[Mount]\n");
588
b2efed52
ZJS
589 r = write_what(f, what);
590 if (r < 0)
591 return r;
592
634735b5
CW
593 if (original_where)
594 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
595
596 where_escaped = specifier_escape(where);
597 if (!where_escaped)
598 return log_oom();
599 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 600
98bad05e 601 if (!isempty(fstype) && !streq(fstype, "auto")) {
c2b2df60 602 _cleanup_free_ char *t = NULL;
98bad05e
LP
603
604 t = specifier_escape(fstype);
605 if (!t)
606 return -ENOMEM;
607
608 fprintf(f, "Type=%s\n", t);
609 }
6b1dc2bd 610
91214a37 611 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
612 if (r < 0)
613 return r;
614
4195077a
MK
615 r = generator_write_device_deps(dest, what, where, opts);
616 if (r < 0)
617 return r;
618
9cf22035
LF
619 if (in_initrd() && path_equal(where, "/sysroot") && is_device_path(what)) {
620 r = generator_write_initrd_root_device_deps(dest, what);
621 if (r < 0)
622 return r;
623 }
624
110773f6
CH
625 r = write_mount_timeout(f, where, opts);
626 if (r < 0)
627 return r;
628
d5cc4be2
LP
629 r = write_options(f, filtered);
630 if (r < 0)
631 return r;
5e398e54 632
d2194e15 633 if (flags & MOUNT_RW_ONLY)
f42aa416
MH
634 fprintf(f, "ReadWriteOnly=yes\n");
635
4652c56c
ZJS
636 r = fflush_and_check(f);
637 if (r < 0)
fb883e75 638 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 639
d2194e15 640 if (flags & MOUNT_MAKEFS) {
da495a03
ZJS
641 r = generator_hook_up_mkfs(dest, what, where, fstype);
642 if (r < 0)
643 return r;
644 }
645
d2194e15 646 if (flags & MOUNT_GROWFS) {
da69e8e4 647 r = generator_hook_up_growfs(dest, where, target_unit);
7f2806d5
ZJS
648 if (r < 0)
649 return r;
650 }
651
04959faa 652 if (flags & MOUNT_PCRFS) {
2f6c52b9
ZJS
653 r = efi_stub_measured(LOG_WARNING);
654 if (r == 0)
6c51b49c 655 log_debug("Kernel stub did not measure kernel image into PCR, skipping userspace measurement, too.");
2f6c52b9 656 else if (r > 0) {
6c51b49c
LP
657 r = generator_hook_up_pcrfs(dest, where, target_unit);
658 if (r < 0)
659 return r;
660 }
04959faa
LP
661 }
662
d2194e15
LP
663 if (!FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
664 if (!FLAGS_SET(flags, MOUNT_NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
da69e8e4 665 r = generator_add_symlink(dest, target_unit,
d2194e15 666 (flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
be02c1cf
AR
667 if (r < 0)
668 return r;
669 } else {
be02c1cf
AR
670 STRV_FOREACH(s, wanted_by) {
671 r = generator_add_symlink(dest, *s, "wants", name);
672 if (r < 0)
673 return r;
674 }
675
676 STRV_FOREACH(s, required_by) {
677 r = generator_add_symlink(dest, *s, "requires", name);
678 if (r < 0)
679 return r;
680 }
681 }
682 } else {
7410616c
LP
683 r = unit_name_from_path(where, ".automount", &automount_name);
684 if (r < 0)
685 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 686
8a7033ac 687 f = safe_fclose(f);
6b1dc2bd 688
7772c177 689 r = generator_open_unit_file(dest, source, automount_name, &f);
fb883e75
ZJS
690 if (r < 0)
691 return r;
0d536673 692
6b1dc2bd 693 fprintf(f,
6b1dc2bd 694 "[Unit]\n"
c3834f9b
LP
695 "SourcePath=%s\n"
696 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
697 source);
698
700e07ff 699 fprintf(f,
bd10a84b 700 "\n"
6b1dc2bd
LP
701 "[Automount]\n"
702 "Where=%s\n",
98bad05e 703 where_escaped);
6b1dc2bd 704
336b5c61 705 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
706 if (r < 0)
707 return r;
708
4652c56c
ZJS
709 r = fflush_and_check(f);
710 if (r < 0)
fb883e75 711 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
6b1dc2bd 712
da69e8e4 713 r = generator_add_symlink(dest, target_unit,
d2194e15 714 (flags & MOUNT_NOFAIL) ? "wants" : "requires", automount_name);
630d30d3
ZJS
715 if (r < 0)
716 return r;
6b1dc2bd
LP
717 }
718
028a981c
ZJS
719 return true;
720}
721
722static int do_daemon_reload(void) {
723 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
028a981c
ZJS
724 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
725 int r, k;
726
727 log_debug("Calling org.freedesktop.systemd1.Manager.Reload()...");
728
729 r = bus_connect_system_systemd(&bus);
730 if (r < 0)
731 return log_error_errno(r, "Failed to get D-Bus connection: %m");
732
a9399358 733 r = bus_service_manager_reload(bus);
028a981c 734 if (r < 0)
a9399358 735 return r;
028a981c
ZJS
736
737 /* We need to requeue the two targets so that any new units which previously were not part of the
738 * targets, and which we now added, will be started. */
739
740 r = 0;
741 FOREACH_STRING(unit, SPECIAL_INITRD_FS_TARGET, SPECIAL_SWAP_TARGET) {
742 log_info("Requesting %s/start/replace...", unit);
743
59228d0d 744 k = bus_call_method(bus, bus_systemd_mgr, "StartUnit", &error, NULL, "ss", unit, "replace");
028a981c
ZJS
745 if (k < 0) {
746 log_error_errno(k, "Failed to (re)start %s: %s", unit, bus_error_message(&error, r));
9ef648cc 747 RET_GATHER(r, k);
028a981c
ZJS
748 }
749 }
750
751 return r;
6b1dc2bd
LP
752}
753
99e3d476
ZJS
754static const char* sysroot_fstab_path(void) {
755 return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
756}
757
cfeb4d37
YW
758static bool sysfs_check(void) {
759 static int cached = -1;
760 int r;
761
762 if (cached < 0) {
763 r = getenv_bool_secure("SYSTEMD_SYSFS_CHECK");
764 if (r < 0 && r != -ENXIO)
765 log_debug_errno(r, "Failed to parse $SYSTEMD_SYSFS_CHECK, ignoring: %m");
766 cached = r != 0;
767 }
768
769 return cached;
770}
771
dfce61dd
LF
772static int add_sysusr_sysroot_usr_bind_mount(const char *source) {
773 return add_mount(source,
774 arg_dest,
775 "/sysusr/usr",
776 "/sysroot/usr",
777 NULL,
778 NULL,
779 "bind",
780 0,
781 0,
782 SPECIAL_INITRD_FS_TARGET);
783}
784
cfeb4d37
YW
785static MountPointFlags fstab_options_to_flags(const char *options, bool is_swap) {
786 MountPointFlags flags = 0;
787
788 if (fstab_test_option(options, "x-systemd.makefs\0"))
789 flags |= MOUNT_MAKEFS;
790 if (fstab_test_option(options, "x-systemd.growfs\0"))
791 flags |= MOUNT_GROWFS;
792 if (fstab_test_option(options, "x-systemd.pcrfs\0"))
793 flags |= MOUNT_PCRFS;
794 if (fstab_test_yes_no_option(options, "noauto\0" "auto\0"))
795 flags |= MOUNT_NOAUTO;
796 if (fstab_test_yes_no_option(options, "nofail\0" "fail\0"))
797 flags |= MOUNT_NOFAIL;
798
799 if (!is_swap) {
800 if (fstab_test_option(options, "x-systemd.rw-only\0"))
801 flags |= MOUNT_RW_ONLY;
802 if (fstab_test_option(options,
803 "comment=systemd.automount\0"
804 "x-systemd.automount\0"))
805 flags |= MOUNT_AUTOMOUNT;
806 }
807
808 return flags;
809}
810
8f88e573 811static int canonicalize_mount_path(const char *path, const char *type, bool prefix_sysroot, char **ret) {
b5fd3956
MY
812 _cleanup_free_ char *p = NULL;
813 bool changed;
814 int r;
815
816 assert(path);
817 assert(type);
818 assert(STR_IN_SET(type, "where", "what"));
819 assert(ret);
820
821 // FIXME: when chase() learns to chase non-existent paths, use this here and drop the prefixing with
822 // /sysroot on error below.
8f88e573 823 r = chase(path, prefix_sysroot ? "/sysroot" : NULL, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &p, NULL);
b5fd3956
MY
824 if (r < 0) {
825 log_debug_errno(r, "Failed to chase '%s', using as-is: %m", path);
826
8f88e573 827 if (prefix_sysroot)
b5fd3956
MY
828 p = path_join("/sysroot", path);
829 else
830 p = strdup(path);
831 if (!p)
832 return log_oom();
833
834 path_simplify(p);
835 }
836
837 changed = !streq(path, p);
838 if (changed)
839 log_debug("Canonicalized %s=%s to %s", type, path, p);
840
841 *ret = TAKE_PTR(p);
842 return changed;
843}
844
cfeb4d37
YW
845static int parse_fstab_one(
846 const char *source,
847 const char *what_original,
848 const char *where_original,
849 const char *fstype,
850 const char *options,
851 int passno,
8f88e573 852 bool prefix_sysroot,
22f5a825 853 bool accept_root, /* This takes an effect only when prefix_sysroot is true. */
55365b0a 854 bool use_swap_enabled) {
cfeb4d37 855
6742eca1 856 _cleanup_free_ char *what = NULL, *where = NULL;
cfeb4d37 857 MountPointFlags flags;
b5fd3956 858 bool is_swap, where_changed;
cfeb4d37
YW
859 int r;
860
861 assert(what_original);
cfeb4d37
YW
862 assert(fstype);
863 assert(options);
864
22f5a825 865 if (prefix_sysroot && !mount_in_initrd(where_original, options, accept_root))
cfeb4d37
YW
866 return 0;
867
256604cc 868 is_swap = streq_ptr(fstype, "swap");
55365b0a 869 if (is_swap && use_swap_enabled && !arg_swap_enabled) {
3aed2593 870 log_info("Swap unit generation disabled on kernel command line, ignoring swap entry for %s.", what_original);
94456233
YW
871 return 0;
872 }
256604cc 873
cfeb4d37
YW
874 what = fstab_node_to_udev_node(what_original);
875 if (!what)
876 return log_oom();
877
878 if (path_is_read_only_fs("/sys") > 0 &&
879 (streq(what, "sysfs") ||
880 (sysfs_check() && is_device_path(what)))) {
881 log_info("/sys/ is read-only (running in a container?), ignoring mount for %s.", what);
882 return 0;
883 }
884
256604cc
YW
885 flags = fstab_options_to_flags(options, is_swap);
886
887 if (is_swap)
888 return add_swap(source, what, options, flags);
889
239cce38
YW
890 if (passno < 0)
891 passno = is_device_path(what);
892
256604cc
YW
893 assert(where_original); /* 'where' is not necessary for swap entry. */
894
6742eca1 895 if (!is_path(where_original)) {
b5fd3956 896 log_warning("Mount point %s is not a valid path, ignoring.", where_original);
6742eca1
YW
897 return 0;
898 }
cfeb4d37 899
6742eca1
YW
900 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
901 * mount units, but causes problems since it historically worked to have symlinks in e.g.
902 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
903 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
b5fd3956 904 * target is the final directory. */
8f88e573 905 r = canonicalize_mount_path(where_original, "where", prefix_sysroot, &where);
b5fd3956
MY
906 if (r < 0)
907 return r;
908 where_changed = r > 0;
cfeb4d37 909
8f88e573 910 if (prefix_sysroot && fstab_is_bind(options, fstype)) {
b5fd3956
MY
911 /* When in initrd, the source of bind mount needs to be prepended with /sysroot as well. */
912 _cleanup_free_ char *p = NULL;
6742eca1 913
8f88e573 914 r = canonicalize_mount_path(what, "what", prefix_sysroot, &p);
b5fd3956
MY
915 if (r < 0)
916 return r;
cfeb4d37 917
b5fd3956
MY
918 free_and_replace(what, p);
919 }
6742eca1 920
cfeb4d37
YW
921 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
922 what, where, strna(fstype),
923 yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS),
924 yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL));
925
b5fd3956 926 bool is_sysroot = in_initrd() && path_equal(where, "/sysroot");
cfeb4d37
YW
927 /* See comment from add_sysroot_usr_mount() about the need for extra indirection in case /usr needs
928 * to be mounted in order for the root fs to be synthesized based on configuration included in /usr/,
929 * e.g. systemd-repart. */
b5fd3956 930 bool is_sysroot_usr = in_initrd() && path_equal(where, "/sysroot/usr");
cfeb4d37
YW
931
932 const char *target_unit =
cfeb4d37
YW
933 is_sysroot ? SPECIAL_INITRD_ROOT_FS_TARGET :
934 is_sysroot_usr ? SPECIAL_INITRD_USR_FS_TARGET :
b93d9e06 935 prefix_sysroot ? SPECIAL_INITRD_FS_TARGET :
cfeb4d37
YW
936 mount_is_network(fstype, options) ? SPECIAL_REMOTE_FS_TARGET :
937 SPECIAL_LOCAL_FS_TARGET;
938
cfeb4d37
YW
939 r = add_mount(source,
940 arg_dest,
941 what,
b5fd3956
MY
942 is_sysroot_usr ? "/sysusr/usr" : where,
943 !is_sysroot_usr && where_changed ? where_original : NULL,
cfeb4d37
YW
944 fstype,
945 options,
946 passno,
947 flags,
948 target_unit);
949 if (r <= 0)
950 return r;
951
952 if (is_sysroot_usr) {
953 log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind");
954 r = add_sysusr_sysroot_usr_bind_mount(source);
955 if (r < 0)
956 return r;
957 }
958
959 return true;
960}
961
8f88e573 962static int parse_fstab(bool prefix_sysroot) {
6db615c1 963 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 964 const char *fstab;
6b1dc2bd 965 struct mntent *me;
cfeb4d37 966 int r, ret = 0;
6b1dc2bd 967
8f88e573 968 if (prefix_sysroot)
99e3d476 969 fstab = sysroot_fstab_path();
028a981c 970 else {
99e3d476 971 fstab = fstab_path();
028a981c
ZJS
972 assert(!arg_sysroot_check);
973 }
99e3d476 974
ed4ad488 975 log_debug("Parsing %s...", fstab);
01a0f7d0 976
ed4ad488 977 f = setmntent(fstab, "re");
6b1dc2bd
LP
978 if (!f) {
979 if (errno == ENOENT)
980 return 0;
981
ed4ad488 982 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
983 }
984
985 while ((me = getmntent(f))) {
55365b0a
YW
986 r = parse_fstab_one(fstab,
987 me->mnt_fsname, me->mnt_dir, me->mnt_type, me->mnt_opts, me->mnt_passno,
22f5a825
YW
988 prefix_sysroot,
989 /* accept_root = */ false,
990 /* use_swap_enabled = */ true);
cfeb4d37
YW
991 if (r < 0 && ret >= 0)
992 ret = r;
993 if (arg_sysroot_check && r > 0)
028a981c 994 return true; /* We found a mount or swap that would be started… */
6b1dc2bd
LP
995 }
996
cfeb4d37 997 return ret;
6b1dc2bd
LP
998}
999
77b8e92d
YW
1000static int sysroot_is_nfsroot(void) {
1001 union in_addr_union u;
1002 const char *sep, *a;
1003 int r;
1004
1005 assert(arg_root_what);
1006
1007 /* From dracut.cmdline(7).
1008 *
1009 * root=[<server-ip>:]<root-dir>[:<nfs-options>]
1010 * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>],
1011 * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>],
1012 * root={dhcp|dhcp6}
1013 *
1014 * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server.
1015 * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options
1016 * can be appended with the prefix ":" or "," and are separated by ",". */
1017
1018 if (path_equal(arg_root_what, "/dev/nfs") ||
1019 STR_IN_SET(arg_root_what, "dhcp", "dhcp6") ||
1020 STARTSWITH_SET(arg_root_what, "nfs:", "nfs4:"))
1021 return true;
1022
1023 /* IPv6 address */
1024 if (arg_root_what[0] == '[') {
1025 sep = strchr(arg_root_what + 1, ']');
1026 if (!sep)
1027 return -EINVAL;
1028
ea1c9d3f 1029 a = strndupa_safe(arg_root_what + 1, sep - arg_root_what - 1);
77b8e92d
YW
1030
1031 r = in_addr_from_string(AF_INET6, a, &u);
1032 if (r < 0)
1033 return r;
1034
1035 return true;
1036 }
1037
1038 /* IPv4 address */
1039 sep = strchr(arg_root_what, ':');
1040 if (sep) {
ea1c9d3f 1041 a = strndupa_safe(arg_root_what, sep - arg_root_what);
77b8e92d
YW
1042
1043 if (in_addr_from_string(AF_INET, a, &u) >= 0)
1044 return true;
1045 }
1046
1047 /* root directory without address */
1048 return path_is_absolute(arg_root_what) && !path_startswith(arg_root_what, "/dev");
1049}
1050
2e852276 1051static int add_sysroot_mount(void) {
75a59316 1052 _cleanup_free_ char *what = NULL;
725ad3b0 1053 const char *opts, *fstype;
01fdfbb8
WF
1054 bool default_rw, makefs;
1055 MountPointFlags flags;
7163e1ca 1056 int r;
5e398e54 1057
093c2cfe
TH
1058 if (isempty(arg_root_what)) {
1059 log_debug("Could not find a root= entry on the kernel command line.");
1060 return 0;
135b5212 1061 }
5e398e54 1062
138f4c69 1063 if (streq(arg_root_what, "gpt-auto")) {
4981427c 1064 /* This is handled by gpt-auto-generator */
138f4c69
LP
1065 log_debug("Skipping root directory handling, as gpt-auto was requested.");
1066 return 0;
dfce61dd
LF
1067 } else if (streq(arg_root_what, "fstab")) {
1068 /* This is handled by parse_fstab */
1069 log_debug("Using initrd's fstab for /sysroot/ configuration.");
1070 return 0;
138f4c69
LP
1071 }
1072
77b8e92d
YW
1073 r = sysroot_is_nfsroot();
1074 if (r < 0)
1075 log_debug_errno(r, "Failed to determine if the root directory is on NFS, assuming not: %m");
1076 else if (r > 0) {
3ade16c9 1077 /* This is handled by the kernel or the initrd */
77b8e92d 1078 log_debug("Skipping root directory handling, as root on NFS was requested.");
155e1bb4
YW
1079 return 0;
1080 }
1081
1082 if (startswith(arg_root_what, "cifs://")) {
1083 log_debug("Skipping root directory handling, as root on CIFS was requested.");
1084 return 0;
1085 }
1086
1087 if (startswith(arg_root_what, "iscsi:")) {
1088 log_debug("Skipping root directory handling, as root on iSCSI was requested.");
1089 return 0;
1090 }
1091
1092 if (startswith(arg_root_what, "live:")) {
1093 log_debug("Skipping root directory handling, as root on live image was requested.");
3ade16c9
HH
1094 return 0;
1095 }
1096
725ad3b0
LP
1097 if (streq(arg_root_what, "tmpfs")) {
1098 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
1099
1100 what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
1101 if (!what)
1102 return log_oom();
1103
5a12d1ca 1104 fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */
725ad3b0 1105
5a12d1ca 1106 default_rw = true; /* writable, unless overridden */;
725ad3b0
LP
1107 } else {
1108
1109 what = fstab_node_to_udev_node(arg_root_what);
1110 if (!what)
1111 return log_oom();
1112
1113 fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */
1114
5a12d1ca 1115 default_rw = false; /* read-only, unless overridden */
725ad3b0 1116 }
093c2cfe 1117
6db615c1 1118 if (!arg_root_options)
725ad3b0 1119 opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro";
75a59316 1120 else if (arg_root_rw >= 0 ||
d15d0333 1121 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 1122 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
1123 else
1124 opts = arg_root_options;
5e398e54 1125
e19ae92a 1126 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts));
7163e1ca 1127
01fdfbb8
WF
1128 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1129 flags = makefs * MOUNT_MAKEFS;
1130
7772c177
ZJS
1131 return add_mount("/proc/cmdline",
1132 arg_dest,
91214a37 1133 what,
6db615c1 1134 "/sysroot",
634735b5 1135 NULL,
725ad3b0 1136 fstype,
75a59316 1137 opts,
f113f8e3 1138 is_device_path(what) ? 1 : 0, /* passno */
04959faa 1139 flags, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
7772c177 1140 SPECIAL_INITRD_ROOT_FS_TARGET);
5e398e54
TG
1141}
1142
2e852276 1143static int add_sysroot_usr_mount(void) {
9f103625
TH
1144 _cleanup_free_ char *what = NULL;
1145 const char *opts;
01fdfbb8
WF
1146 bool makefs;
1147 MountPointFlags flags;
29a24ab2
LP
1148 int r;
1149
1150 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
1151 * know for sure something else did */
9f103625
TH
1152
1153 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
1154 return 0;
1155
1156 if (arg_root_what && !arg_usr_what) {
40472036 1157 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 1158 arg_usr_what = strdup(arg_root_what);
9f103625
TH
1159 if (!arg_usr_what)
1160 return log_oom();
1161 }
1162
1163 if (arg_root_fstype && !arg_usr_fstype) {
1164 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
1165 if (!arg_usr_fstype)
1166 return log_oom();
1167 }
1168
1169 if (arg_root_options && !arg_usr_options) {
1170 arg_usr_options = strdup(arg_root_options);
9f103625
TH
1171 if (!arg_usr_options)
1172 return log_oom();
1173 }
1174
29a24ab2 1175 if (isempty(arg_usr_what)) {
32fe629a 1176 log_debug("Could not find a mount.usr= entry on the kernel command line.");
9f103625 1177 return 0;
29a24ab2
LP
1178 }
1179
1180 if (streq(arg_usr_what, "gpt-auto")) {
1181 /* This is handled by the gpt-auto generator */
1182 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
1183 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
1184 * unit file is being created for the host /usr/ mount. */
dfce61dd
LF
1185 } else if (streq(arg_usr_what, "fstab")) {
1186 /* This is handled by parse_fstab */
1187 log_debug("Using initrd's fstab for /sysroot/usr/ configuration.");
1188 return 1; /* parse_fstab will generate a unit for this, hence report that a
1189 * unit file is being created for the host /usr/ mount. */
29a24ab2
LP
1190 }
1191
1192 if (path_equal(arg_usr_what, "/dev/nfs")) {
1193 /* This is handled by the initrd (if at all supported, that is) */
1194 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
1195 return 1; /* As above, report that NFS code will create the unit */
1196 }
9f103625
TH
1197
1198 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
1199 if (!what)
1200 return log_oom();
9f103625 1201
eb580002
MM
1202 if (!arg_usr_options)
1203 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 1204 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 1205 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
1206 else
1207 opts = arg_usr_options;
9f103625 1208
29a24ab2
LP
1209 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
1210 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
1211 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
1212 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
1213 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
1214 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
1215
1216 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts));
1217
01fdfbb8
WF
1218 makefs = fstab_test_option(opts, "x-systemd.makefs\0");
1219 flags = makefs * MOUNT_MAKEFS;
1220
7772c177
ZJS
1221 r = add_mount("/proc/cmdline",
1222 arg_dest,
29a24ab2
LP
1223 what,
1224 "/sysusr/usr",
1225 NULL,
1226 arg_usr_fstype,
1227 opts,
1228 is_device_path(what) ? 1 : 0, /* passno */
01fdfbb8 1229 flags,
7772c177 1230 SPECIAL_INITRD_USR_FS_TARGET);
29a24ab2
LP
1231 if (r < 0)
1232 return r;
1233
dfce61dd 1234 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
29a24ab2 1235
dfce61dd 1236 r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline");
29a24ab2
LP
1237 if (r < 0)
1238 return r;
1239
1240 return 1;
1241}
1242
1243static int add_sysroot_usr_mount_or_fallback(void) {
1244 int r;
1245
1246 r = add_sysroot_usr_mount();
1247 if (r != 0)
1248 return r;
1249
1250 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
1251 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
de47cd06 1252 * themselves after it get the guarantee that /usr/ is definitely mounted somewhere. */
29a24ab2
LP
1253
1254 return generator_add_symlink(
1255 arg_dest,
1256 SPECIAL_INITRD_USR_FS_TARGET,
1257 "requires",
1258 "sysroot.mount");
9f103625
TH
1259}
1260
91214a37 1261static int add_volatile_root(void) {
1de7f825 1262
91214a37 1263 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 1264 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 1265
1de7f825 1266 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 1267 return 0;
91214a37 1268
00bb366d 1269 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
835cf75a 1270 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
1271}
1272
1273static int add_volatile_var(void) {
1274
1275 if (arg_volatile_mode != VOLATILE_STATE)
1276 return 0;
1277
1278 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
1279
7772c177
ZJS
1280 return add_mount("/proc/cmdline",
1281 arg_dest_late,
91214a37
LP
1282 "tmpfs",
1283 "/var",
634735b5 1284 NULL,
91214a37 1285 "tmpfs",
7d85383e 1286 "mode=0755" TMPFS_LIMITS_VAR,
91214a37 1287 0,
4191418b 1288 0,
7772c177 1289 SPECIAL_LOCAL_FS_TARGET);
91214a37
LP
1290}
1291
55365b0a
YW
1292static int add_mounts_from_cmdline(void) {
1293 int r, ret = 0;
1294
1295 /* Handle each entries found in cmdline as a fstab entry. */
1296
1297 FOREACH_ARRAY(m, arg_mounts, arg_n_mounts) {
45c535dd
YW
1298 if (m->for_initrd && !in_initrd())
1299 continue;
1300
55365b0a
YW
1301 r = parse_fstab_one(
1302 "/proc/cmdline",
1303 m->what,
1304 m->where,
1305 m->fstype,
1306 m->options,
239cce38 1307 /* passno = */ -1,
45c535dd 1308 /* prefix_sysroot = */ !m->for_initrd && in_initrd(),
22f5a825 1309 /* accept_root = */ true,
55365b0a
YW
1310 /* use_swap_enabled = */ false);
1311 if (r < 0 && ret >= 0)
1312 ret = r;
1313 }
1314
1315 return ret;
1316}
1317
dfd10549 1318static int add_mounts_from_creds(bool prefix_sysroot) {
6ac62485
LP
1319 _cleanup_free_ void *b = NULL;
1320 struct mntent *me;
1321 int r, ret = 0;
1322 size_t bs;
1323
dfd10549
YW
1324 assert(in_initrd() || !prefix_sysroot);
1325
6ac62485 1326 r = read_credential_with_decryption(
dfd10549 1327 in_initrd() && !prefix_sysroot ? "fstab.extra.initrd" : "fstab.extra",
6ac62485
LP
1328 &b, &bs);
1329 if (r <= 0)
1330 return r;
1331
1332 _cleanup_fclose_ FILE *f = NULL;
1333 f = fmemopen_unlocked(b, bs, "r");
1334 if (!f)
1335 return log_oom();
1336
1337 while ((me = getmntent(f))) {
1338 r = parse_fstab_one(
1339 "/run/credentials",
1340 me->mnt_fsname,
1341 me->mnt_dir,
1342 me->mnt_type,
1343 me->mnt_opts,
1344 me->mnt_passno,
dfd10549 1345 /* prefix_sysroot = */ prefix_sysroot,
22f5a825 1346 /* accept_root = */ true,
6ac62485
LP
1347 /* use_swap_enabled = */ true);
1348 if (r < 0 && ret >= 0)
1349 ret = r;
1350 }
1351
1352 return ret;
1353}
1354
96287a49 1355static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 1356 int r;
94734142 1357
a8d3315b
YW
1358 assert(key);
1359
9f103625
TH
1360 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
1361 * instance should take precedence. In the case of multiple rootflags=
1362 * or usrflags= the arguments should be concatenated */
6db615c1 1363
1d84ad94 1364 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 1365
1d84ad94 1366 r = value ? parse_boolean(value) : 1;
141a79f4 1367 if (r < 0)
059cb385 1368 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 1369 else
b564224b 1370 arg_fstab_enabled = fstab_set_enabled(r);
94734142 1371
1d84ad94
LP
1372 } else if (streq(key, "root")) {
1373
1374 if (proc_cmdline_value_missing(key, value))
1375 return 0;
6db615c1 1376
338da501 1377 return free_and_strdup_warn(&arg_root_what, empty_to_null(value));
6db615c1 1378
1d84ad94
LP
1379 } else if (streq(key, "rootfstype")) {
1380
1381 if (proc_cmdline_value_missing(key, value))
1382 return 0;
6db615c1 1383
338da501 1384 return free_and_strdup_warn(&arg_root_fstype, empty_to_null(value));
6db615c1 1385
1d84ad94 1386 } else if (streq(key, "rootflags")) {
6db615c1 1387
1d84ad94
LP
1388 if (proc_cmdline_value_missing(key, value))
1389 return 0;
1390
c2bc710b 1391 if (!strextend_with_separator(&arg_root_options, ",", value))
6db615c1
LP
1392 return log_oom();
1393
2f3dfc6f
LP
1394 } else if (streq(key, "roothash")) {
1395
1396 if (proc_cmdline_value_missing(key, value))
1397 return 0;
1398
338da501 1399 return free_and_strdup_warn(&arg_root_hash, empty_to_null(value));
6db615c1 1400
1d84ad94
LP
1401 } else if (streq(key, "mount.usr")) {
1402
1403 if (proc_cmdline_value_missing(key, value))
1404 return 0;
9f103625 1405
338da501 1406 return free_and_strdup_warn(&arg_usr_what, empty_to_null(value));
9f103625 1407
1d84ad94
LP
1408 } else if (streq(key, "mount.usrfstype")) {
1409
1410 if (proc_cmdline_value_missing(key, value))
1411 return 0;
9f103625 1412
338da501 1413 return free_and_strdup_warn(&arg_usr_fstype, empty_to_null(value));
9f103625 1414
1d84ad94 1415 } else if (streq(key, "mount.usrflags")) {
9f103625 1416
1d84ad94
LP
1417 if (proc_cmdline_value_missing(key, value))
1418 return 0;
1419
c2bc710b 1420 if (!strextend_with_separator(&arg_usr_options, ",", value))
9f103625
TH
1421 return log_oom();
1422
c1b9e3df
MB
1423 } else if (streq(key, "usrhash")) {
1424
1425 if (proc_cmdline_value_missing(key, value))
1426 return 0;
1427
338da501 1428 return free_and_strdup_warn(&arg_usr_hash, empty_to_null(value));
c1b9e3df 1429
6db615c1
LP
1430 } else if (streq(key, "rw") && !value)
1431 arg_root_rw = true;
1432 else if (streq(key, "ro") && !value)
1433 arg_root_rw = false;
91214a37
LP
1434 else if (streq(key, "systemd.volatile")) {
1435 VolatileMode m;
1436
1437 if (value) {
1438 m = volatile_mode_from_string(value);
1439 if (m < 0)
7211c853 1440 log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value);
91214a37
LP
1441 else
1442 arg_volatile_mode = m;
1443 } else
1444 arg_volatile_mode = VOLATILE_YES;
567a5307 1445
1446 } else if (streq(key, "systemd.swap")) {
1447
1448 r = value ? parse_boolean(value) : 1;
1449 if (r < 0)
1450 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
1451 else
1452 arg_swap_enabled = r;
200268c6
DDM
1453
1454 } else if (streq(key, "systemd.verity")) {
1455
1456 r = value ? parse_boolean(value) : 1;
1457 if (r < 0)
1458 log_warning("Failed to parse systemd.verity= kernel command line switch %s. Ignoring.", value);
1459 else
1460 arg_verity = r;
55365b0a 1461
45c535dd 1462 } else if (STR_IN_SET(key, "systemd.mount-extra", "rd.systemd.mount-extra")) {
55365b0a
YW
1463
1464 if (proc_cmdline_value_missing(key, value))
1465 return 0;
1466
45c535dd 1467 r = mount_array_add(startswith(key, "rd."), value);
55365b0a
YW
1468 if (r < 0)
1469 log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value);
1470
45c535dd 1471 } else if (STR_IN_SET(key, "systemd.swap-extra", "rd.systemd.swap-extra")) {
55365b0a
YW
1472
1473 if (proc_cmdline_value_missing(key, value))
1474 return 0;
1475
45c535dd 1476 r = mount_array_add_swap(startswith(key, "rd."), value);
55365b0a
YW
1477 if (r < 0)
1478 log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value);
91214a37 1479 }
94734142 1480
d0aa9ce5 1481 return 0;
94734142
LP
1482}
1483
2130a2e5
LP
1484static int determine_device(
1485 char **what,
1486 int *rw,
1487 char **options,
1488 const char *hash,
1489 const char *name) {
2f3dfc6f 1490
c1b9e3df
MB
1491 assert(what);
1492 assert(name);
1493
1494 /* If we have a hash but no device then Verity is used, and we use the DM device. */
1495 if (*what)
2f3dfc6f
LP
1496 return 0;
1497
c1b9e3df 1498 if (!hash)
2f3dfc6f
LP
1499 return 0;
1500
200268c6
DDM
1501 if (!arg_verity)
1502 return 0;
1503
c1b9e3df
MB
1504 *what = path_join("/dev/mapper/", name);
1505 if (!*what)
2f3dfc6f
LP
1506 return log_oom();
1507
2130a2e5
LP
1508 /* Verity is always read-only */
1509 if (rw)
1510 *rw = false;
1511 if (options && !strextend_with_separator(options, ",", "ro"))
1512 return log_oom();
2f3dfc6f 1513
2130a2e5 1514 log_info("Using verity %s device %s.", name, *what);
2f3dfc6f
LP
1515 return 1;
1516}
1517
c1b9e3df 1518static int determine_root(void) {
2130a2e5 1519 return determine_device(&arg_root_what, &arg_root_rw, NULL, arg_root_hash, "root");
c1b9e3df
MB
1520}
1521
1522static int determine_usr(void) {
2130a2e5 1523 return determine_device(&arg_usr_what, NULL, &arg_usr_options, arg_usr_hash, "usr");
c1b9e3df
MB
1524}
1525
028a981c
ZJS
1526/* If arg_sysroot_check is false, run as generator in the usual fashion.
1527 * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount
1528 * in the initrd, and call daemon-reload. We will get reinvoked as a generator,
1529 * with /sysroot/etc/fstab available, and then we can write additional units based
1530 * on that file. */
1531static int run_generator(void) {
2646f184 1532 int r, ret = 0;
6b1dc2bd 1533
1d84ad94 1534 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 1535 if (r < 0)
da927ba9 1536 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 1537
2f3dfc6f 1538 (void) determine_root();
c1b9e3df 1539 (void) determine_usr();
2f3dfc6f 1540
028a981c 1541 if (arg_sysroot_check) {
8f88e573 1542 r = parse_fstab(/* prefix_sysroot = */ true);
028a981c
ZJS
1543 if (r == 0)
1544 log_debug("Nothing interesting found, not doing daemon-reload.");
1545 if (r > 0)
1546 r = do_daemon_reload();
1547 return r;
1548 }
1549
9f103625
TH
1550 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1551 if (in_initrd()) {
2e852276 1552 r = add_sysroot_mount();
2646f184
YW
1553 if (r < 0 && ret >= 0)
1554 ret = r;
003cba39 1555
2646f184
YW
1556 r = add_sysroot_usr_mount_or_fallback();
1557 if (r < 0 && ret >= 0)
1558 ret = r;
91214a37 1559
2646f184
YW
1560 r = add_volatile_root();
1561 if (r < 0 && ret >= 0)
1562 ret = r;
1563 } else {
91214a37 1564 r = add_volatile_var();
2646f184
YW
1565 if (r < 0 && ret >= 0)
1566 ret = r;
1567 }
5e398e54 1568
e48fdd84
LP
1569 /* Honour /etc/fstab only when that's enabled */
1570 if (arg_fstab_enabled) {
e48fdd84 1571 /* Parse the local /etc/fstab, possibly from the initrd */
8f88e573 1572 r = parse_fstab(/* prefix_sysroot = */ false);
2646f184
YW
1573 if (r < 0 && ret >= 0)
1574 ret = r;
ac4785b0 1575
e48fdd84 1576 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e 1577 if (in_initrd())
8f88e573 1578 r = parse_fstab(/* prefix_sysroot = */ true);
9b69569d 1579 else
2646f184
YW
1580 r = generator_enable_remount_fs_service(arg_dest);
1581 if (r < 0 && ret >= 0)
1582 ret = r;
e48fdd84 1583 }
6b1dc2bd 1584
55365b0a
YW
1585 r = add_mounts_from_cmdline();
1586 if (r < 0 && ret >= 0)
1587 ret = r;
1588
dfd10549 1589 r = add_mounts_from_creds(/* prefix_sysroot = */ false);
6ac62485
LP
1590 if (r < 0 && ret >= 0)
1591 ret = r;
1592
dfd10549
YW
1593 if (in_initrd()) {
1594 r = add_mounts_from_creds(/* prefix_sysroot = */ true);
1595 if (r < 0 && ret >= 0)
1596 ret = r;
1597 }
1598
2646f184 1599 return ret;
6b1dc2bd 1600}
a4ef3e4d 1601
028a981c
ZJS
1602static int run(int argc, char **argv) {
1603 arg_sysroot_check = invoked_as(argv, "systemd-sysroot-fstab-check");
1604
1605 if (arg_sysroot_check) {
1606 /* Run as in systemd-sysroot-fstab-check mode */
1607 log_setup();
1608
1609 if (strv_length(argv) > 1)
1610 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1611 "This program takes no arguments.");
1612 if (!in_initrd())
1613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1614 "This program is only useful in the initrd.");
1615 } else {
1616 /* Run in generator mode */
1617 log_setup_generator();
1618
1619 if (!IN_SET(strv_length(argv), 2, 4))
1620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1621 "This program takes one or three arguments.");
1622
1623 arg_dest = ASSERT_PTR(argv[1]);
1624 arg_dest_late = ASSERT_PTR(argv[argc > 3 ? 3 : 1]);
1625 }
1626
1627 return run_generator();
1628}
1629
1630DEFINE_MAIN_FUNCTION(run);