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