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