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