]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fstab-generator/fstab-generator.c
hwdb: Add support for HP ZBook Studio G5 keyboard (#17525)
[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();
56e577c6
LP
266
267 DISABLE_WARNING_FORMAT_NONLITERAL;
ae325185 268 fprintf(f, format, res);
56e577c6 269 REENABLE_WARNING;
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
6371e69b
FB
317static int write_extra_dependencies(FILE *f, const char *opts) {
318 int r;
319
320 assert(f);
321
322 if (opts) {
323 r = write_after(f, opts);
324 if (r < 0)
325 return r;
326 r = write_requires_after(f, opts);
327 if (r < 0)
328 return r;
329 r = write_before(f, opts);
330 if (r < 0)
331 return r;
332 r = write_requires_mounts_for(f, opts);
333 if (r < 0)
334 return r;
335 }
336
337 return 0;
338}
339
e8d2f6cd 340static int add_mount(
91214a37 341 const char *dest,
e8d2f6cd
LP
342 const char *what,
343 const char *where,
634735b5 344 const char *original_where,
6db615c1 345 const char *fstype,
e8d2f6cd
LP
346 const char *opts,
347 int passno,
4191418b 348 MountpointFlags flags,
e8d2f6cd 349 const char *post,
e8d2f6cd 350 const char *source) {
e48fdd84 351
7fd1b19b 352 _cleanup_free_ char
da495a03 353 *name = NULL,
fb883e75 354 *automount_name = NULL,
98bad05e
LP
355 *filtered = NULL,
356 *where_escaped = NULL;
be02c1cf 357 _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL;
7fd1b19b 358 _cleanup_fclose_ FILE *f = NULL;
94192cda 359 int r;
6b1dc2bd
LP
360
361 assert(what);
362 assert(where);
5e398e54 363 assert(opts);
0d3d3be1 364 assert(post);
5e398e54 365 assert(source);
6b1dc2bd 366
6db615c1 367 if (streq_ptr(fstype, "autofs"))
6b1dc2bd
LP
368 return 0;
369
370 if (!is_path(where)) {
371 log_warning("Mount point %s is not a valid path, ignoring.", where);
372 return 0;
373 }
374
375 if (mount_point_is_api(where) ||
376 mount_point_ignore(where))
377 return 0;
378
be02c1cf
AR
379 r = fstab_extract_values(opts, "x-systemd.wanted-by", &wanted_by);
380 if (r < 0)
381 return r;
382
383 r = fstab_extract_values(opts, "x-systemd.required-by", &required_by);
384 if (r < 0)
385 return r;
386
e48fdd84 387 if (path_equal(where, "/")) {
4191418b 388 if (flags & NOAUTO)
2e852276 389 log_warning("Ignoring \"noauto\" for root device");
4191418b 390 if (flags & NOFAIL)
2e852276 391 log_warning("Ignoring \"nofail\" for root device");
4191418b 392 if (flags & AUTOMOUNT)
2e852276 393 log_warning("Ignoring automount option for root device");
be02c1cf
AR
394 if (!strv_isempty(wanted_by))
395 log_warning("Ignoring \"x-systemd.wanted-by=\" for root device");
396 if (!strv_isempty(required_by))
397 log_warning("Ignoring \"x-systemd.required-by=\" for root device");
2e852276 398
be02c1cf
AR
399 required_by = strv_free(required_by);
400 wanted_by = strv_free(wanted_by);
4191418b 401 SET_FLAG(flags, NOAUTO | NOFAIL | AUTOMOUNT, false);
e48fdd84
LP
402 }
403
7410616c
LP
404 r = unit_name_from_path(where, ".mount", &name);
405 if (r < 0)
406 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 407
ed4ad488 408 r = generator_open_unit_file(dest, fstab_path(), name, &f);
fb883e75
ZJS
409 if (r < 0)
410 return r;
0d536673 411
5e398e54 412 fprintf(f,
c3834f9b 413 "[Unit]\n"
a7e88558
LP
414 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
415 "SourcePath=%s\n",
c3834f9b 416 source);
6b1dc2bd 417
4191418b 418 if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & AUTOMOUNT) &&
65e1dee7
N
419 fstab_test_yes_no_option(opts, "bg\0" "fg\0")) {
420 /* The default retry timeout that mount.nfs uses for 'bg' mounts
421 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
422 * As we are making 'bg' mounts look like an 'fg' mount to
423 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
424 * we need to explicitly preserve that default, and also ensure
425 * the systemd mount-timeout doesn't interfere.
426 * By placing these options first, they can be over-ridden by
427 * settings in /etc/fstab. */
e66d2eee 428 opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg");
4191418b 429 SET_FLAG(flags, NOFAIL, true);
65e1dee7
N
430 }
431
6371e69b
FB
432 r = write_extra_dependencies(f, opts);
433 if (r < 0)
434 return r;
3519d230 435
e48fdd84 436 if (passno != 0) {
91214a37 437 r = generator_write_fsck_deps(f, dest, what, where, fstype);
e48fdd84
LP
438 if (r < 0)
439 return r;
440 }
64e70e4b 441
a7e88558
LP
442 r = generator_write_blockdev_dependency(f, what);
443 if (r < 0)
444 return r;
445
446 fprintf(f,
447 "\n"
448 "[Mount]\n");
449
634735b5
CW
450 if (original_where)
451 fprintf(f, "# Canonicalized from %s\n", original_where);
98bad05e
LP
452
453 where_escaped = specifier_escape(where);
454 if (!where_escaped)
455 return log_oom();
456 fprintf(f, "Where=%s\n", where_escaped);
6db615c1 457
19d0833b
LP
458 r = write_what(f, what);
459 if (r < 0)
460 return r;
461
98bad05e
LP
462 if (!isempty(fstype) && !streq(fstype, "auto")) {
463 _cleanup_free_ char *t;
464
465 t = specifier_escape(fstype);
466 if (!t)
467 return -ENOMEM;
468
469 fprintf(f, "Type=%s\n", t);
470 }
6b1dc2bd 471
91214a37 472 r = generator_write_timeouts(dest, what, where, opts, &filtered);
29686440
ZJS
473 if (r < 0)
474 return r;
475
4195077a
MK
476 r = generator_write_device_deps(dest, what, where, opts);
477 if (r < 0)
478 return r;
479
110773f6
CH
480 r = write_mount_timeout(f, where, opts);
481 if (r < 0)
482 return r;
483
d5cc4be2
LP
484 r = write_options(f, filtered);
485 if (r < 0)
486 return r;
5e398e54 487
f42aa416
MH
488 if (flags & RWONLY)
489 fprintf(f, "ReadWriteOnly=yes\n");
490
4652c56c
ZJS
491 r = fflush_and_check(f);
492 if (r < 0)
fb883e75 493 return log_error_errno(r, "Failed to write unit file %s: %m", name);
6b1dc2bd 494
da495a03
ZJS
495 if (flags & MAKEFS) {
496 r = generator_hook_up_mkfs(dest, what, where, fstype);
497 if (r < 0)
498 return r;
499 }
500
7f2806d5
ZJS
501 if (flags & GROWFS) {
502 r = generator_hook_up_growfs(dest, where, post);
503 if (r < 0)
504 return r;
505 }
506
be02c1cf
AR
507 if (!FLAGS_SET(flags, AUTOMOUNT)) {
508 if (!FLAGS_SET(flags, NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
509 r = generator_add_symlink(dest, post,
510 (flags & NOFAIL) ? "wants" : "requires", name);
511 if (r < 0)
512 return r;
513 } else {
514 char **s;
515
516 STRV_FOREACH(s, wanted_by) {
517 r = generator_add_symlink(dest, *s, "wants", name);
518 if (r < 0)
519 return r;
520 }
521
522 STRV_FOREACH(s, required_by) {
523 r = generator_add_symlink(dest, *s, "requires", name);
524 if (r < 0)
525 return r;
526 }
527 }
528 } else {
7410616c
LP
529 r = unit_name_from_path(where, ".automount", &automount_name);
530 if (r < 0)
531 return log_error_errno(r, "Failed to generate unit name: %m");
6b1dc2bd 532
8a7033ac 533 f = safe_fclose(f);
6b1dc2bd 534
ed4ad488 535 r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
fb883e75
ZJS
536 if (r < 0)
537 return r;
0d536673 538
6b1dc2bd 539 fprintf(f,
6b1dc2bd 540 "[Unit]\n"
c3834f9b
LP
541 "SourcePath=%s\n"
542 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
700e07ff
HH
543 source);
544
700e07ff 545 fprintf(f,
bd10a84b 546 "\n"
6b1dc2bd
LP
547 "[Automount]\n"
548 "Where=%s\n",
98bad05e 549 where_escaped);
6b1dc2bd 550
336b5c61 551 r = write_idle_timeout(f, where, opts);
deb0a77c
MO
552 if (r < 0)
553 return r;
554
4652c56c
ZJS
555 r = fflush_and_check(f);
556 if (r < 0)
fb883e75 557 return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
6b1dc2bd 558
630d30d3 559 r = generator_add_symlink(dest, post,
4191418b 560 (flags & NOFAIL) ? "wants" : "requires", automount_name);
630d30d3
ZJS
561 if (r < 0)
562 return r;
6b1dc2bd
LP
563 }
564
d0aa9ce5 565 return 0;
6b1dc2bd
LP
566}
567
e48fdd84 568static int parse_fstab(bool initrd) {
6db615c1 569 _cleanup_endmntent_ FILE *f = NULL;
ed4ad488 570 const char *fstab;
6b1dc2bd 571 struct mntent *me;
e48fdd84 572 int r = 0;
6b1dc2bd 573
ed4ad488
ZJS
574 fstab = initrd ? "/sysroot/etc/fstab" : fstab_path();
575 log_debug("Parsing %s...", fstab);
01a0f7d0 576
ed4ad488 577 f = setmntent(fstab, "re");
6b1dc2bd
LP
578 if (!f) {
579 if (errno == ENOENT)
580 return 0;
581
ed4ad488 582 return log_error_errno(errno, "Failed to open %s: %m", fstab);
6b1dc2bd
LP
583 }
584
585 while ((me = getmntent(f))) {
634735b5 586 _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL;
f42aa416 587 bool makefs, growfs, noauto, nofail, rwonly;
6b1dc2bd
LP
588 int k;
589
3d22d1ab
TG
590 if (initrd && !mount_in_initrd(me))
591 continue;
592
6b1dc2bd 593 what = fstab_node_to_udev_node(me->mnt_fsname);
e48fdd84
LP
594 if (!what)
595 return log_oom();
596
a3e7ea02 597 if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
689aede8
LP
598 log_info("Running in a container, ignoring fstab device entry for %s.", what);
599 continue;
600 }
601
98eda38a 602 where = strdup(me->mnt_dir);
e48fdd84 603 if (!where)
5862d652 604 return log_oom();
6b1dc2bd 605
634735b5 606 if (is_path(where)) {
858d36c1 607 path_simplify(where, false);
f1a2c758 608
634735b5
CW
609 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
610 * mount units, but causes problems since it historically worked to have symlinks in e.g.
611 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
612 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
f1a2c758 613 * target is the final directory. */
634735b5
CW
614 r = chase_symlinks(where, initrd ? "/sysroot" : NULL,
615 CHASE_PREFIX_ROOT | CHASE_NONEXISTENT,
a5648b80 616 &canonical_where, NULL);
f1a2c758
LP
617 if (r < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
618 log_debug_errno(r, "Failed to read symlink target for %s, ignoring: %m", where);
619 else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */
620 canonical_where = mfree(canonical_where);
621 else
622 log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where);
634735b5 623 }
6b1dc2bd 624
da495a03 625 makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0");
7f2806d5 626 growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0");
f42aa416 627 rwonly = fstab_test_option(me->mnt_opts, "x-systemd.rw-only\0");
b9f111b9
ZJS
628 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
629 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
f1a2c758 630
ac1d4c79 631 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
5607d856 632 what, where, me->mnt_type,
ac1d4c79 633 yes_no(makefs), yes_no(growfs),
5607d856 634 yes_no(noauto), yes_no(nofail));
6b1dc2bd
LP
635
636 if (streq(me->mnt_type, "swap"))
4191418b 637 k = add_swap(what, me,
7f2806d5 638 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL);
5e398e54 639 else {
5607d856 640 bool automount;
80c3b720 641 const char *post;
5e398e54 642
b9f111b9
ZJS
643 automount = fstab_test_option(me->mnt_opts,
644 "comment=systemd.automount\0"
645 "x-systemd.automount\0");
e48fdd84 646 if (initrd)
700e07ff 647 post = SPECIAL_INITRD_FS_TARGET;
e48fdd84 648 else if (mount_is_network(me))
0c17fbce 649 post = SPECIAL_REMOTE_FS_TARGET;
e48fdd84 650 else
0c17fbce 651 post = SPECIAL_LOCAL_FS_TARGET;
5e398e54 652
91214a37
LP
653 k = add_mount(arg_dest,
654 what,
634735b5
CW
655 canonical_where ?: where,
656 canonical_where ? where: NULL,
6db615c1
LP
657 me->mnt_type,
658 me->mnt_opts,
659 me->mnt_passno,
f42aa416 660 makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT | rwonly*RWONLY,
6db615c1 661 post,
ed4ad488 662 fstab);
5e398e54 663 }
6b1dc2bd 664
7f2806d5 665 if (r >= 0 && k < 0)
6b1dc2bd
LP
666 r = k;
667 }
668
6b1dc2bd
LP
669 return r;
670}
671
2e852276 672static int add_sysroot_mount(void) {
75a59316
ZJS
673 _cleanup_free_ char *what = NULL;
674 const char *opts;
7163e1ca 675 int r;
5e398e54 676
093c2cfe
TH
677 if (isempty(arg_root_what)) {
678 log_debug("Could not find a root= entry on the kernel command line.");
679 return 0;
135b5212 680 }
5e398e54 681
138f4c69
LP
682 if (streq(arg_root_what, "gpt-auto")) {
683 /* This is handled by the gpt-auto generator */
684 log_debug("Skipping root directory handling, as gpt-auto was requested.");
685 return 0;
686 }
687
e34e72fb 688 if (path_equal(arg_root_what, "/dev/nfs")) {
3ade16c9
HH
689 /* This is handled by the kernel or the initrd */
690 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
691 return 0;
692 }
693
093c2cfe
TH
694 what = fstab_node_to_udev_node(arg_root_what);
695 if (!what)
171181bc 696 return log_oom();
093c2cfe 697
6db615c1 698 if (!arg_root_options)
75a59316
ZJS
699 opts = arg_root_rw > 0 ? "rw" : "ro";
700 else if (arg_root_rw >= 0 ||
d15d0333 701 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
63c372cb 702 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
75a59316
ZJS
703 else
704 opts = arg_root_options;
5e398e54 705
6db615c1 706 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
7163e1ca
DD
707
708 if (is_device_path(what)) {
709 r = generator_write_initrd_root_device_deps(arg_dest, what);
710 if (r < 0)
711 return r;
712 }
713
91214a37
LP
714 return add_mount(arg_dest,
715 what,
6db615c1 716 "/sysroot",
634735b5 717 NULL,
6db615c1 718 arg_root_fstype,
75a59316 719 opts,
f113f8e3 720 is_device_path(what) ? 1 : 0, /* passno */
7f2806d5 721 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
6db615c1
LP
722 SPECIAL_INITRD_ROOT_FS_TARGET,
723 "/proc/cmdline");
5e398e54
TG
724}
725
2e852276 726static int add_sysroot_usr_mount(void) {
9f103625
TH
727 _cleanup_free_ char *what = NULL;
728 const char *opts;
729
730 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
731 return 0;
732
733 if (arg_root_what && !arg_usr_what) {
40472036 734 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
9f103625 735 arg_usr_what = strdup(arg_root_what);
9f103625
TH
736 if (!arg_usr_what)
737 return log_oom();
738 }
739
740 if (arg_root_fstype && !arg_usr_fstype) {
741 arg_usr_fstype = strdup(arg_root_fstype);
9f103625
TH
742 if (!arg_usr_fstype)
743 return log_oom();
744 }
745
746 if (arg_root_options && !arg_usr_options) {
747 arg_usr_options = strdup(arg_root_options);
9f103625
TH
748 if (!arg_usr_options)
749 return log_oom();
750 }
751
eb580002 752 if (!arg_usr_what)
9f103625
TH
753 return 0;
754
755 what = fstab_node_to_udev_node(arg_usr_what);
47be5f07
LP
756 if (!what)
757 return log_oom();
9f103625 758
eb580002
MM
759 if (!arg_usr_options)
760 opts = arg_root_rw > 0 ? "rw" : "ro";
d15d0333 761 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
63c372cb 762 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
eb580002
MM
763 else
764 opts = arg_usr_options;
9f103625
TH
765
766 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
91214a37
LP
767 return add_mount(arg_dest,
768 what,
9f103625 769 "/sysroot/usr",
634735b5 770 NULL,
9f103625
TH
771 arg_usr_fstype,
772 opts,
f113f8e3 773 is_device_path(what) ? 1 : 0, /* passno */
4191418b 774 0,
104bc12f 775 SPECIAL_INITRD_FS_TARGET,
9f103625
TH
776 "/proc/cmdline");
777}
778
91214a37 779static int add_volatile_root(void) {
1de7f825 780
91214a37 781 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1de7f825 782 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
91214a37 783
1de7f825 784 if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY))
00bb366d 785 return 0;
91214a37 786
00bb366d 787 return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires",
312da637 788 SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE);
91214a37
LP
789}
790
791static int add_volatile_var(void) {
792
793 if (arg_volatile_mode != VOLATILE_STATE)
794 return 0;
795
796 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
797
798 return add_mount(arg_dest_late,
799 "tmpfs",
800 "/var",
634735b5 801 NULL,
91214a37 802 "tmpfs",
7d85383e 803 "mode=0755" TMPFS_LIMITS_VAR,
91214a37 804 0,
4191418b 805 0,
91214a37
LP
806 SPECIAL_LOCAL_FS_TARGET,
807 "/proc/cmdline");
808}
809
96287a49 810static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 811 int r;
94734142 812
9f103625
TH
813 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
814 * instance should take precedence. In the case of multiple rootflags=
815 * or usrflags= the arguments should be concatenated */
6db615c1 816
1d84ad94 817 if (STR_IN_SET(key, "fstab", "rd.fstab")) {
e48fdd84 818
1d84ad94 819 r = value ? parse_boolean(value) : 1;
141a79f4 820 if (r < 0)
059cb385 821 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
141a79f4 822 else
e48fdd84 823 arg_fstab_enabled = r;
94734142 824
1d84ad94
LP
825 } else if (streq(key, "root")) {
826
827 if (proc_cmdline_value_missing(key, value))
828 return 0;
6db615c1 829
f88dc3ed 830 if (free_and_strdup(&arg_root_what, value) < 0)
6db615c1
LP
831 return log_oom();
832
1d84ad94
LP
833 } else if (streq(key, "rootfstype")) {
834
835 if (proc_cmdline_value_missing(key, value))
836 return 0;
6db615c1 837
f88dc3ed 838 if (free_and_strdup(&arg_root_fstype, value) < 0)
6db615c1
LP
839 return log_oom();
840
1d84ad94 841 } else if (streq(key, "rootflags")) {
6db615c1 842
1d84ad94
LP
843 if (proc_cmdline_value_missing(key, value))
844 return 0;
845
6c782b7a 846 if (!strextend_with_separator(&arg_root_options, ",", value, NULL))
6db615c1
LP
847 return log_oom();
848
2f3dfc6f
LP
849 } else if (streq(key, "roothash")) {
850
851 if (proc_cmdline_value_missing(key, value))
852 return 0;
853
854 if (free_and_strdup(&arg_root_hash, value) < 0)
855 return log_oom();
6db615c1 856
1d84ad94
LP
857 } else if (streq(key, "mount.usr")) {
858
859 if (proc_cmdline_value_missing(key, value))
860 return 0;
9f103625
TH
861
862 if (free_and_strdup(&arg_usr_what, value) < 0)
863 return log_oom();
864
1d84ad94
LP
865 } else if (streq(key, "mount.usrfstype")) {
866
867 if (proc_cmdline_value_missing(key, value))
868 return 0;
9f103625
TH
869
870 if (free_and_strdup(&arg_usr_fstype, value) < 0)
871 return log_oom();
872
1d84ad94 873 } else if (streq(key, "mount.usrflags")) {
9f103625 874
1d84ad94
LP
875 if (proc_cmdline_value_missing(key, value))
876 return 0;
877
6c782b7a 878 if (!strextend_with_separator(&arg_usr_options, ",", value, NULL))
9f103625
TH
879 return log_oom();
880
6db615c1
LP
881 } else if (streq(key, "rw") && !value)
882 arg_root_rw = true;
883 else if (streq(key, "ro") && !value)
884 arg_root_rw = false;
91214a37
LP
885 else if (streq(key, "systemd.volatile")) {
886 VolatileMode m;
887
888 if (value) {
889 m = volatile_mode_from_string(value);
890 if (m < 0)
891 log_warning("Failed to parse systemd.volatile= argument: %s", value);
892 else
893 arg_volatile_mode = m;
894 } else
895 arg_volatile_mode = VOLATILE_YES;
567a5307 896
897 } else if (streq(key, "systemd.swap")) {
898
899 r = value ? parse_boolean(value) : 1;
900 if (r < 0)
901 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value);
902 else
903 arg_swap_enabled = r;
91214a37 904 }
94734142 905
d0aa9ce5 906 return 0;
94734142
LP
907}
908
2f3dfc6f
LP
909static int determine_root(void) {
910 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
911
912 if (arg_root_what)
913 return 0;
914
915 if (!arg_root_hash)
916 return 0;
917
918 arg_root_what = strdup("/dev/mapper/root");
919 if (!arg_root_what)
920 return log_oom();
921
922 log_info("Using verity root device %s.", arg_root_what);
923
924 return 1;
925}
926
7a44c7e3 927static int run(const char *dest, const char *dest_early, const char *dest_late) {
2572957e 928 int r, r2 = 0, r3 = 0;
6b1dc2bd 929
7a44c7e3
ZJS
930 assert_se(arg_dest = dest);
931 assert_se(arg_dest_late = dest_late);
6b1dc2bd 932
1d84ad94 933 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 934 if (r < 0)
da927ba9 935 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
94734142 936
2f3dfc6f
LP
937 (void) determine_root();
938
9f103625
TH
939 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
940 if (in_initrd()) {
2e852276 941 r = add_sysroot_mount();
003cba39 942
2572957e 943 r2 = add_sysroot_usr_mount();
91214a37 944
2572957e 945 r3 = add_volatile_root();
003cba39 946 } else
91214a37 947 r = add_volatile_var();
5e398e54 948
e48fdd84
LP
949 /* Honour /etc/fstab only when that's enabled */
950 if (arg_fstab_enabled) {
e48fdd84 951 /* Parse the local /etc/fstab, possibly from the initrd */
2572957e 952 r2 = parse_fstab(false);
ac4785b0 953
e48fdd84 954 /* If running in the initrd also parse the /etc/fstab from the host */
2572957e
ZJS
955 if (in_initrd())
956 r3 = parse_fstab(true);
9b69569d
ZJS
957 else
958 r3 = generator_enable_remount_fs_service(arg_dest);
e48fdd84 959 }
6b1dc2bd 960
2572957e 961 return r < 0 ? r : r2 < 0 ? r2 : r3;
6b1dc2bd 962}
a4ef3e4d 963
7a44c7e3 964DEFINE_MAIN_GENERATOR_FUNCTION(run);