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