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