]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
cryptsetup-generator: order after cryptsetup-pre.target unconditionally
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
3ffd4af2 8#include "fd-util.h"
0d39fa9c 9#include "fileio.h"
98bad05e 10#include "fs-util.h"
d15d0333 11#include "fstab-util.h"
07630cea
LP
12#include "generator.h"
13#include "log.h"
a4ef3e4d 14#include "main-func.h"
07630cea 15#include "mkdir.h"
6b1dc2bd 16#include "mount-setup.h"
4349cd7c 17#include "mount-util.h"
049af8ad 18#include "mountpoint-util.h"
6bedfcbb 19#include "parse-util.h"
07630cea 20#include "path-util.h"
4e731273 21#include "proc-cmdline.h"
6b1dc2bd 22#include "special.h"
98bad05e 23#include "specifier.h"
8fcde012 24#include "stat-util.h"
07630cea 25#include "string-util.h"
059cb385 26#include "strv.h"
07630cea
LP
27#include "unit-name.h"
28#include "util.h"
689aede8 29#include "virt.h"
91214a37 30#include "volatile-util.h"
6b1dc2bd 31
4191418b
ZJS
32typedef enum MountpointFlags {
33 NOAUTO = 1 << 0,
34 NOFAIL = 1 << 1,
35 AUTOMOUNT = 1 << 2,
da495a03 36 MAKEFS = 1 << 3,
7f2806d5 37 GROWFS = 1 << 4,
4191418b
ZJS
38} MountpointFlags;
39
7a44c7e3
ZJS
40static const char *arg_dest = NULL;
41static const char *arg_dest_late = NULL;
e48fdd84 42static bool arg_fstab_enabled = true;
6db615c1
LP
43static char *arg_root_what = NULL;
44static char *arg_root_fstype = NULL;
45static char *arg_root_options = NULL;
2f3dfc6f 46static char *arg_root_hash = NULL;
6db615c1 47static int arg_root_rw = -1;
9f103625
TH
48static char *arg_usr_what = NULL;
49static char *arg_usr_fstype = NULL;
50static char *arg_usr_options = NULL;
91214a37 51static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID;
6b1dc2bd 52
a4ef3e4d
YW
53STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep);
54STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep);
55STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep);
56STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
57STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep);
58STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep);
59STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep);
60
d5cc4be2
LP
61static int write_options(FILE *f, const char *options) {
62 _cleanup_free_ char *o = NULL;
63
64 if (isempty(options))
65 return 0;
66
67 if (streq(options, "defaults"))
68 return 0;
69
98bad05e 70 o = specifier_escape(options);
d5cc4be2
LP
71 if (!o)
72 return log_oom();
73
74 fprintf(f, "Options=%s\n", o);
75 return 1;
76}
77
19d0833b
LP
78static int write_what(FILE *f, const char *what) {
79 _cleanup_free_ char *w = NULL;
80
98bad05e 81 w = specifier_escape(what);
19d0833b
LP
82 if (!w)
83 return log_oom();
84
85 fprintf(f, "What=%s\n", w);
86 return 1;
87}
88
5607d856
ZJS
89static int add_swap(
90 const char *what,
91 struct mntent *me,
4191418b 92 MountpointFlags flags) {
5607d856 93
fb883e75 94 _cleanup_free_ char *name = NULL;
7fd1b19b 95 _cleanup_fclose_ FILE *f = NULL;
bf1d7ba7 96 int r;
6b1dc2bd
LP
97
98 assert(what);
99 assert(me);
100
00b4ffde
LP
101 if (access("/proc/swaps", F_OK) < 0) {
102 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
103 return 0;
104 }
105
75f86906 106 if (detect_container() > 0) {
689aede8
LP
107 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
108 return 0;
109 }
110
7410616c
LP
111 r = unit_name_from_path(what, ".swap", &name);
112 if (r < 0)
113 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 114
ed4ad488 115 r = generator_open_unit_file(arg_dest, fstab_path(), name, &f);
fb883e75
ZJS
116 if (r < 0)
117 return r;
0d536673 118
ed4ad488
ZJS
119 fprintf(f,
120 "[Unit]\n"
121 "SourcePath=%s\n"
122 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
123 "[Swap]\n",
124 fstab_path());
19d0833b
LP
125
126 r = write_what(f, what);
127 if (r < 0)
128 return r;
6b1dc2bd 129
d5cc4be2
LP
130 r = write_options(f, me->mnt_opts);
131 if (r < 0)
132 return r;
6b1dc2bd 133
47cb901e 134 r = fflush_and_check(f);
23bbb0de 135 if (r < 0)
fb883e75 136 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 137
b3208b66 138 /* use what as where, to have a nicer error message */
bf1d7ba7 139 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
b3208b66
ZJS
140 if (r < 0)
141 return r;
142
da495a03
ZJS
143 if (flags & MAKEFS) {
144 r = generator_hook_up_mkswap(arg_dest, what);
145 if (r < 0)
146 return r;
147 }
148
7f2806d5
ZJS
149 if (flags & GROWFS)
150 /* TODO: swap devices must be wiped and recreated */
151 log_warning("%s: growing swap devices is currently unsupported.", what);
152
4191418b 153 if (!(flags & NOAUTO)) {
630d30d3 154 r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET,
4191418b 155 (flags & NOFAIL) ? "wants" : "requires", name);
630d30d3
ZJS
156 if (r < 0)
157 return r;
4e82fe52
TG
158 }
159
d0aa9ce5 160 return 0;
6b1dc2bd
LP
161}
162
6b1dc2bd
LP
163static bool mount_is_network(struct mntent *me) {
164 assert(me);
165
b9f111b9
ZJS
166 return fstab_test_option(me->mnt_opts, "_netdev\0") ||
167 fstype_is_network(me->mnt_type);
6b1dc2bd
LP
168}
169
3d22d1ab
TG
170static bool mount_in_initrd(struct mntent *me) {
171 assert(me);
172
b9f111b9
ZJS
173 return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
174 streq(me->mnt_dir, "/usr");
3d22d1ab
TG
175}
176
33a4c983
LP
177static int write_timeout(
178 FILE *f,
179 const char *where,
180 const char *opts,
181 const char *filter,
182 const char *variable) {
183
deb0a77c
MO
184 _cleanup_free_ char *timeout = NULL;
185 char timespan[FORMAT_TIMESPAN_MAX];
186 usec_t u;
187 int r;
188
110773f6 189 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL);
deb0a77c
MO
190 if (r < 0)
191 return log_warning_errno(r, "Failed to parse options: %m");
192 if (r == 0)
193 return 0;
194
0004f698 195 r = parse_sec_fix_0(timeout, &u);
deb0a77c
MO
196 if (r < 0) {
197 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
198 return 0;
199 }
200
110773f6 201 fprintf(f, "%s=%s\n", variable, format_timespan(timespan, sizeof(timespan), u, 0));
deb0a77c
MO
202
203 return 0;
204}
2e852276 205
110773f6
CH
206static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
207 return write_timeout(f, where, opts,
208 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
209}
210
211static int write_mount_timeout(FILE *f, const char *where, const char *opts) {
212 return write_timeout(f, where, opts,
213 "x-systemd.mount-timeout\0", "TimeoutSec");
214}
215
33a4c983
LP
216static int write_dependency(
217 FILE *f,
218 const char *opts,
219 const char *filter,
220 const char *format) {
221
3519d230
KZ
222 _cleanup_strv_free_ char **names = NULL, **units = NULL;
223 _cleanup_free_ char *res = NULL;
224 char **s;
225 int r;
226
227 assert(f);
228 assert(opts);
229
ae325185 230 r = fstab_extract_values(opts, filter, &names);
3519d230
KZ
231 if (r < 0)
232 return log_warning_errno(r, "Failed to parse options: %m");
233 if (r == 0)
234 return 0;
235
236 STRV_FOREACH(s, names) {
237 char *x;
238
df7c4eb6 239 r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x);
3519d230
KZ
240 if (r < 0)
241 return log_error_errno(r, "Failed to generate unit name: %m");
33a4c983 242
3519d230
KZ
243 r = strv_consume(&units, x);
244 if (r < 0)
245 return log_oom();
246 }
247
248 if (units) {
249 res = strv_join(units, " ");
250 if (!res)
251 return log_oom();
4a027e19
MP
252#pragma GCC diagnostic push
253#pragma GCC diagnostic ignored "-Wformat-nonliteral"
ae325185 254 fprintf(f, format, res);
4a027e19 255#pragma GCC diagnostic pop
3519d230
KZ
256 }
257
258 return 0;
259}
260
ae325185 261static int write_after(FILE *f, const char *opts) {
33a4c983
LP
262 return write_dependency(f, opts,
263 "x-systemd.after", "After=%1$s\n");
ae325185
RB
264}
265
266static int write_requires_after(FILE *f, const char *opts) {
267 return write_dependency(f, opts,
268 "x-systemd.requires", "After=%1$s\nRequires=%1$s\n");
269}
270
271static int write_before(FILE *f, const char *opts) {
272 return write_dependency(f, opts,
273 "x-systemd.before", "Before=%1$s\n");
274}
275
3519d230 276static int write_requires_mounts_for(FILE *f, const char *opts) {
98bad05e 277 _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL;
3519d230
KZ
278 _cleanup_free_ char *res = NULL;
279 int r;
280
281 assert(f);
282 assert(opts);
283
284 r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths);
285 if (r < 0)
286 return log_warning_errno(r, "Failed to parse options: %m");
287 if (r == 0)
288 return 0;
289
98bad05e
LP
290 r = specifier_escape_strv(paths, &paths_escaped);
291 if (r < 0)
292 return log_error_errno(r, "Failed to escape paths: %m");
293
294 res = strv_join(paths_escaped, " ");
3519d230
KZ
295 if (!res)
296 return log_oom();
297
298 fprintf(f, "RequiresMountsFor=%s\n", res);
299
300 return 0;
301}
302
e8d2f6cd 303static int add_mount(
91214a37 304 const char *dest,
e8d2f6cd
LP
305 const char *what,
306 const char *where,
634735b5 307 const char *original_where,
6db615c1 308 const char *fstype,
e8d2f6cd
LP
309 const char *opts,
310 int passno,
4191418b 311 MountpointFlags flags,
e8d2f6cd 312 const char *post,
e8d2f6cd 313 const char *source) {
e48fdd84 314
7fd1b19b 315 _cleanup_free_ char
da495a03 316 *name = NULL,
fb883e75 317 *automount_name = NULL,
98bad05e
LP
318 *filtered = NULL,
319 *where_escaped = NULL;
be02c1cf 320 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 321 _cleanup_fclose_ FILE *f = NULL;
94192cda 322 int r;
6b1dc2bd
LP
323
324 assert(what);
325 assert(where);
5e398e54 326 assert(opts);
0d3d3be1 327 assert(post);
5e398e54 328 assert(source);
6b1dc2bd 329
6db615c1 330 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
331 return 0;
332
333 if (!is_path(where)) {
334 log_warning("Mount point %s is not a valid path, ignoring.", where);
335 return 0;
336 }
337
338 if (mount_point_is_api(where) ||
339 mount_point_ignore(where))
340 return 0;
341
be02c1cf
AR
342 r = fstab_extract_values(opts, "x-systemd.wanted-by", &wanted_by);
343 if (r < 0)
344 return r;
345
346 r = fstab_extract_values(opts, "x-systemd.required-by", &required_by);
347 if (r < 0)
348 return r;
349
e48fdd84 350 if (path_equal(where, "/")) {
4191418b 351 if (flags & NOAUTO)
2e852276 352 log_warning("Ignoring \"noauto\" for root device");
4191418b 353 if (flags & NOFAIL)
2e852276 354 log_warning("Ignoring \"nofail\" for root device");
4191418b 355 if (flags & AUTOMOUNT)
2e852276 356 log_warning("Ignoring automount option for root device");
be02c1cf
AR
357 if (!strv_isempty(wanted_by))
358 log_warning("Ignoring \"x-systemd.wanted-by=\" for root device");
359 if (!strv_isempty(required_by))
360 log_warning("Ignoring \"x-systemd.required-by=\" for root device");
2e852276 361
be02c1cf
AR
362 required_by = strv_free(required_by);
363 wanted_by = strv_free(wanted_by);
4191418b 364 SET_FLAG(flags, NOAUTO | NOFAIL | AUTOMOUNT, false);
e48fdd84
LP
365 }
366
7410616c
LP
367 r = unit_name_from_path(where, ".mount", &name);
368 if (r < 0)
369 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 370
ed4ad488 371 r = generator_open_unit_file(dest, fstab_path(), name, &f);
fb883e75
ZJS
372 if (r < 0)
373 return r;
0d536673 374
5e398e54 375 fprintf(f,
c3834f9b
LP
376 "[Unit]\n"
377 "SourcePath=%s\n"
378 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
379 source);
6b1dc2bd 380
30fdb896
JL
381 /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW, it's not
382 * technically part of the basic initrd filesystem itself, and so shouldn't inherit the default
383 * Before=local-fs.target dependency. */
384 if (in_initrd() && path_startswith(where, "/sysroot"))
385 fprintf(f, "DefaultDependencies=no\n");
386
4191418b 387 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & AUTOMOUNT) &&
65e1dee7
N
388 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
389 /* The default retry timeout that mount.nfs uses for 'bg' mounts
390 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
391 * As we are making 'bg' mounts look like an 'fg' mount to
392 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
393 * we need to explicitly preserve that default, and also ensure
394 * the systemd mount-timeout doesn't interfere.
395 * By placing these options first, they can be over-ridden by
396 * settings in /etc/fstab. */
397 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts, ",fg");
4191418b 398 SET_FLAG(flags, NOFAIL, true);
65e1dee7
N
399 }
400
4191418b 401 if (!(flags & NOFAIL) && !(flags & AUTOMOUNT))
5ecdcf41 402 fprintf(f, "Before=%s\n", post);
6b1dc2bd 403
4191418b 404 if (!(flags & AUTOMOUNT) && opts) {
ae325185
RB
405 r = write_after(f, opts);
406 if (r < 0)
407 return r;
3519d230 408 r = write_requires_after(f, opts);
ae325185
RB
409 if (r < 0)
410 return r;
411 r = write_before(f, opts);
3519d230
KZ
412 if (r < 0)
413 return r;
414 r = write_requires_mounts_for(f, opts);
415 if (r < 0)
416 return r;
417 }
418
e48fdd84 419 if (passno != 0) {
91214a37 420 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
421 if (r < 0)
422 return r;
423 }
64e70e4b 424
634735b5
CW
425 fprintf(f, "\n[Mount]\n");
426 if (original_where)
427 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
428
429 where_escaped = specifier_escape(where);
430 if (!where_escaped)
431 return log_oom();
432 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 433
19d0833b
LP
434 r = write_what(f, what);
435 if (r < 0)
436 return r;
437
98bad05e
LP
438 if (!isempty(fstype) && !streq(fstype, "auto")) {
439 _cleanup_free_ char *t;
440
441 t = specifier_escape(fstype);
442 if (!t)
443 return -ENOMEM;
444
445 fprintf(f, "Type=%s\n", t);
446 }
6b1dc2bd 447
91214a37 448 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
449 if (r < 0)
450 return r;
451
4195077a
MK
452 r = generator_write_device_deps(dest, what, where, opts);
453 if (r < 0)
454 return r;
455
110773f6
CH
456 r = write_mount_timeout(f, where, opts);
457 if (r < 0)
458 return r;
459
d5cc4be2
LP
460 r = write_options(f, filtered);
461 if (r < 0)
462 return r;
5e398e54 463
4652c56c
ZJS
464 r = fflush_and_check(f);
465 if (r < 0)
fb883e75 466 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 467
da495a03
ZJS
468 if (flags & MAKEFS) {
469 r = generator_hook_up_mkfs(dest, what, where, fstype);
470 if (r < 0)
471 return r;
472 }
473
7f2806d5
ZJS
474 if (flags & GROWFS) {
475 r = generator_hook_up_growfs(dest, where, post);
476 if (r < 0)
477 return r;
478 }
479
be02c1cf
AR
480 if (!FLAGS_SET(flags, AUTOMOUNT)) {
481 if (!FLAGS_SET(flags, NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
482 r = generator_add_symlink(dest, post,
483 (flags & NOFAIL) ? "wants" : "requires", name);
484 if (r < 0)
485 return r;
486 } else {
487 char **s;
488
489 STRV_FOREACH(s, wanted_by) {
490 r = generator_add_symlink(dest, *s, "wants", name);
491 if (r < 0)
492 return r;
493 }
494
495 STRV_FOREACH(s, required_by) {
496 r = generator_add_symlink(dest, *s, "requires", name);
497 if (r < 0)
498 return r;
499 }
500 }
501 } else {
7410616c
LP
502 r = unit_name_from_path(where, ".automount", &automount_name);
503 if (r < 0)
504 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 505
8a7033ac 506 f = safe_fclose(f);
6b1dc2bd 507
ed4ad488 508 r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
fb883e75
ZJS
509 if (r < 0)
510 return r;
0d536673 511
6b1dc2bd 512 fprintf(f,
6b1dc2bd 513 "[Unit]\n"
c3834f9b
LP
514 "SourcePath=%s\n"
515 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
516 source);
517
0d3d3be1 518 fprintf(f, "Before=%s\n", post);
700e07ff 519
3519d230 520 if (opts) {
ae325185
RB
521 r = write_after(f, opts);
522 if (r < 0)
523 return r;
3519d230 524 r = write_requires_after(f, opts);
ae325185
RB
525 if (r < 0)
526 return r;
527 r = write_before(f, opts);
3519d230
KZ
528 if (r < 0)
529 return r;
530 r = write_requires_mounts_for(f, opts);
531 if (r < 0)
532 return r;
533 }
534
700e07ff 535 fprintf(f,
bd10a84b 536 "\n"
6b1dc2bd
LP
537 "[Automount]\n"
538 "Where=%s\n",
98bad05e 539 where_escaped);
6b1dc2bd 540
336b5c61 541 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
542 if (r < 0)
543 return r;
544
4652c56c
ZJS
545 r = fflush_and_check(f);
546 if (r < 0)
fb883e75 547 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
6b1dc2bd 548
630d30d3 549 r = generator_add_symlink(dest, post,
4191418b 550 (flags & NOFAIL) ? "wants" : "requires", automount_name);
630d30d3
ZJS
551 if (r < 0)
552 return r;
6b1dc2bd
LP
553 }
554
d0aa9ce5 555 return 0;
6b1dc2bd
LP
556}
557
e48fdd84 558static int parse_fstab(bool initrd) {
6db615c1 559 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 560 const char *fstab;
6b1dc2bd 561 struct mntent *me;
e48fdd84 562 int r = 0;
6b1dc2bd 563
ed4ad488
ZJS
564 fstab = initrd ? "/sysroot/etc/fstab" : fstab_path();
565 log_debug("Parsing %s...", fstab);
01a0f7d0 566
ed4ad488 567 f = setmntent(fstab, "re");
6b1dc2bd
LP
568 if (!f) {
569 if (errno == ENOENT)
570 return 0;
571
ed4ad488 572 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
573 }
574
575 while ((me = getmntent(f))) {
634735b5 576 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
7f2806d5 577 bool makefs, growfs, noauto, nofail;
6b1dc2bd
LP
578 int k;
579
3d22d1ab
TG
580 if (initrd && !mount_in_initrd(me))
581 continue;
582
6b1dc2bd 583 what = fstab_node_to_udev_node(me->mnt_fsname);
e48fdd84
LP
584 if (!what)
585 return log_oom();
586
a3e7ea02 587 if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
689aede8
LP
588 log_info("Running in a container, ignoring fstab device entry for %s.", what);
589 continue;
590 }
591
98eda38a 592 where = strdup(me->mnt_dir);
e48fdd84 593 if (!where)
5862d652 594 return log_oom();
6b1dc2bd 595
634735b5 596 if (is_path(where)) {
858d36c1 597 path_simplify(where, false);
f1a2c758 598
634735b5
CW
599 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
600 * mount units, but causes problems since it historically worked to have symlinks in e.g.
601 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
602 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
f1a2c758 603 * target is the final directory. */
634735b5
CW
604 r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
605 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
a5648b80 606 &canonical_where, NULL);
f1a2c758
LP
607 if (r < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
608 log_debug_errno(r, "Failed to read symlink target for %s, ignoring: %m", where);
609 else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
610 canonical_where = mfree(canonical_where);
611 else
612 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
634735b5 613 }
6b1dc2bd 614
da495a03 615 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
7f2806d5 616 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
b9f111b9
ZJS
617 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
618 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
f1a2c758 619
ac1d4c79 620 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
5607d856 621 what, where, me->mnt_type,
ac1d4c79 622 yes_no(makefs), yes_no(growfs),
5607d856 623 yes_no(noauto), yes_no(nofail));
6b1dc2bd
LP
624
625 if (streq(me->mnt_type, "swap"))
4191418b 626 k = add_swap(what, me,
7f2806d5 627 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL);
5e398e54 628 else {
5607d856 629 bool automount;
80c3b720 630 const char *post;
5e398e54 631
b9f111b9
ZJS
632 automount = fstab_test_option(me->mnt_opts,
633 "comment=systemd.automount\0"
634 "x-systemd.automount\0");
e48fdd84 635 if (initrd)
700e07ff 636 post = SPECIAL_INITRD_FS_TARGET;
e48fdd84 637 else if (mount_is_network(me))
0c17fbce 638 post = SPECIAL_REMOTE_FS_TARGET;
e48fdd84 639 else
0c17fbce 640 post = SPECIAL_LOCAL_FS_TARGET;
5e398e54 641
91214a37
LP
642 k = add_mount(arg_dest,
643 what,
634735b5
CW
644 canonical_where ?: where,
645 canonical_where ? where: NULL,
6db615c1
LP
646 me->mnt_type,
647 me->mnt_opts,
648 me->mnt_passno,
7f2806d5 649 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT,
6db615c1 650 post,
ed4ad488 651 fstab);
5e398e54 652 }
6b1dc2bd 653
7f2806d5 654 if (r >= 0 && k < 0)
6b1dc2bd
LP
655 r = k;
656 }
657
6b1dc2bd
LP
658 return r;
659}
660
2e852276 661static int add_sysroot_mount(void) {
75a59316
ZJS
662 _cleanup_free_ char *what = NULL;
663 const char *opts;
7163e1ca 664 int r;
5e398e54 665
093c2cfe
TH
666 if (isempty(arg_root_what)) {
667 log_debug("Could not find a root= entry on the kernel command line.");
668 return 0;
135b5212 669 }
5e398e54 670
138f4c69
LP
671 if (streq(arg_root_what, "gpt-auto")) {
672 /* This is handled by the gpt-auto generator */
673 log_debug("Skipping root directory handling, as gpt-auto was requested.");
674 return 0;
675 }
676
e34e72fb 677 if (path_equal(arg_root_what, "/dev/nfs")) {
3ade16c9
HH
678 /* This is handled by the kernel or the initrd */
679 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
680 return 0;
681 }
682
093c2cfe
TH
683 what = fstab_node_to_udev_node(arg_root_what);
684 if (!what)
171181bc 685 return log_oom();
093c2cfe 686
6db615c1 687 if (!arg_root_options)
75a59316
ZJS
688 opts = arg_root_rw > 0 ? "rw" : "ro";
689 else if (arg_root_rw >= 0 ||
d15d0333 690 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 691 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
692 else
693 opts = arg_root_options;
5e398e54 694
6db615c1 695 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
7163e1ca
DD
696
697 if (is_device_path(what)) {
698 r = generator_write_initrd_root_device_deps(arg_dest, what);
699 if (r < 0)
700 return r;
701 }
702
91214a37
LP
703 return add_mount(arg_dest,
704 what,
6db615c1 705 "/sysroot",
634735b5 706 NULL,
6db615c1 707 arg_root_fstype,
75a59316 708 opts,
f113f8e3 709 is_device_path(what) ? 1 : 0, /* passno */
7f2806d5 710 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
6db615c1
LP
711 SPECIAL_INITRD_ROOT_FS_TARGET,
712 "/proc/cmdline");
5e398e54
TG
713}
714
2e852276 715static int add_sysroot_usr_mount(void) {
9f103625
TH
716 _cleanup_free_ char *what = NULL;
717 const char *opts;
718
719 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
720 return 0;
721
722 if (arg_root_what && !arg_usr_what) {
40472036 723 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 724 arg_usr_what = strdup(arg_root_what);
9f103625
TH
725 if (!arg_usr_what)
726 return log_oom();
727 }
728
729 if (arg_root_fstype && !arg_usr_fstype) {
730 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
731 if (!arg_usr_fstype)
732 return log_oom();
733 }
734
735 if (arg_root_options && !arg_usr_options) {
736 arg_usr_options = strdup(arg_root_options);
9f103625
TH
737 if (!arg_usr_options)
738 return log_oom();
739 }
740
eb580002 741 if (!arg_usr_what)
9f103625
TH
742 return 0;
743
744 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
745 if (!what)
746 return log_oom();
9f103625 747
eb580002
MM
748 if (!arg_usr_options)
749 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 750 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 751 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
752 else
753 opts = arg_usr_options;
9f103625
TH
754
755 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
91214a37
LP
756 return add_mount(arg_dest,
757 what,
9f103625 758 "/sysroot/usr",
634735b5 759 NULL,
9f103625
TH
760 arg_usr_fstype,
761 opts,
f113f8e3 762 is_device_path(what) ? 1 : 0, /* passno */
4191418b 763 0,
104bc12f 764 SPECIAL_INITRD_FS_TARGET,
9f103625
TH
765 "/proc/cmdline");
766}
767
91214a37 768static int add_volatile_root(void) {
1de7f825 769
91214a37 770 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 771 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 772
1de7f825 773 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 774 return 0;
91214a37 775
00bb366d 776 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
312da637 777 SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
778}
779
780static int add_volatile_var(void) {
781
782 if (arg_volatile_mode != VOLATILE_STATE)
783 return 0;
784
785 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
786
787 return add_mount(arg_dest_late,
788 "tmpfs",
789 "/var",
634735b5 790 NULL,
91214a37
LP
791 "tmpfs",
792 "mode=0755",
793 0,
4191418b 794 0,
91214a37
LP
795 SPECIAL_LOCAL_FS_TARGET,
796 "/proc/cmdline");
797}
798
96287a49 799static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 800 int r;
94734142 801
9f103625
TH
802 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
803 * instance should take precedence. In the case of multiple rootflags=
804 * or usrflags= the arguments should be concatenated */
6db615c1 805
1d84ad94 806 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 807
1d84ad94 808 r = value ? parse_boolean(value) : 1;
141a79f4 809 if (r < 0)
059cb385 810 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 811 else
e48fdd84 812 arg_fstab_enabled = r;
94734142 813
1d84ad94
LP
814 } else if (streq(key, "root")) {
815
816 if (proc_cmdline_value_missing(key, value))
817 return 0;
6db615c1 818
f88dc3ed 819 if (free_and_strdup(&arg_root_what, value) < 0)
6db615c1
LP
820 return log_oom();
821
1d84ad94
LP
822 } else if (streq(key, "rootfstype")) {
823
824 if (proc_cmdline_value_missing(key, value))
825 return 0;
6db615c1 826
f88dc3ed 827 if (free_and_strdup(&arg_root_fstype, value) < 0)
6db615c1
LP
828 return log_oom();
829
1d84ad94 830 } else if (streq(key, "rootflags")) {
6db615c1 831
1d84ad94
LP
832 if (proc_cmdline_value_missing(key, value))
833 return 0;
834
6c782b7a 835 if (!strextend_with_separator(&arg_root_options, ",", value, NULL))
6db615c1
LP
836 return log_oom();
837
2f3dfc6f
LP
838 } else if (streq(key, "roothash")) {
839
840 if (proc_cmdline_value_missing(key, value))
841 return 0;
842
843 if (free_and_strdup(&arg_root_hash, value) < 0)
844 return log_oom();
6db615c1 845
1d84ad94
LP
846 } else if (streq(key, "mount.usr")) {
847
848 if (proc_cmdline_value_missing(key, value))
849 return 0;
9f103625
TH
850
851 if (free_and_strdup(&arg_usr_what, value) < 0)
852 return log_oom();
853
1d84ad94
LP
854 } else if (streq(key, "mount.usrfstype")) {
855
856 if (proc_cmdline_value_missing(key, value))
857 return 0;
9f103625
TH
858
859 if (free_and_strdup(&arg_usr_fstype, value) < 0)
860 return log_oom();
861
1d84ad94 862 } else if (streq(key, "mount.usrflags")) {
9f103625 863
1d84ad94
LP
864 if (proc_cmdline_value_missing(key, value))
865 return 0;
866
6c782b7a 867 if (!strextend_with_separator(&arg_usr_options, ",", value, NULL))
9f103625
TH
868 return log_oom();
869
6db615c1
LP
870 } else if (streq(key, "rw") && !value)
871 arg_root_rw = true;
872 else if (streq(key, "ro") && !value)
873 arg_root_rw = false;
91214a37
LP
874 else if (streq(key, "systemd.volatile")) {
875 VolatileMode m;
876
877 if (value) {
878 m = volatile_mode_from_string(value);
879 if (m < 0)
880 log_warning("Failed to parse systemd.volatile= argument: %s", value);
881 else
882 arg_volatile_mode = m;
883 } else
884 arg_volatile_mode = VOLATILE_YES;
885 }
94734142 886
d0aa9ce5 887 return 0;
94734142
LP
888}
889
2f3dfc6f
LP
890static int determine_root(void) {
891 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
892
893 if (arg_root_what)
894 return 0;
895
896 if (!arg_root_hash)
897 return 0;
898
899 arg_root_what = strdup("/dev/mapper/root");
900 if (!arg_root_what)
901 return log_oom();
902
903 log_info("Using verity root device %s.", arg_root_what);
904
905 return 1;
906}
907
7a44c7e3 908static int run(const char *dest, const char *dest_early, const char *dest_late) {
2572957e 909 int r, r2 = 0, r3 = 0;
6b1dc2bd 910
7a44c7e3
ZJS
911 assert_se(arg_dest = dest);
912 assert_se(arg_dest_late = dest_late);
6b1dc2bd 913
1d84ad94 914 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 915 if (r < 0)
da927ba9 916 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 917
2f3dfc6f
LP
918 (void) determine_root();
919
9f103625
TH
920 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
921 if (in_initrd()) {
2e852276 922 r = add_sysroot_mount();
003cba39 923
2572957e 924 r2 = add_sysroot_usr_mount();
91214a37 925
2572957e 926 r3 = add_volatile_root();
003cba39 927 } else
91214a37 928 r = add_volatile_var();
5e398e54 929
e48fdd84
LP
930 /* Honour /etc/fstab only when that's enabled */
931 if (arg_fstab_enabled) {
e48fdd84 932 /* Parse the local /etc/fstab, possibly from the initrd */
2572957e 933 r2 = parse_fstab(false);
ac4785b0 934
e48fdd84 935 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e
ZJS
936 if (in_initrd())
937 r3 = parse_fstab(true);
9b69569d
ZJS
938 else
939 r3 = generator_enable_remount_fs_service(arg_dest);
e48fdd84 940 }
6b1dc2bd 941
2572957e 942 return r < 0 ? r : r2 < 0 ? r2 : r3;
6b1dc2bd 943}
a4ef3e4d 944
7a44c7e3 945DEFINE_MAIN_GENERATOR_FUNCTION(run);