1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
9 #include "bus-locator.h"
10 #include "bus-unit-util.h"
12 #include "creds-util.h"
13 #include "efi-loader.h"
17 #include "fstab-util.h"
18 #include "generator.h"
19 #include "in-addr-util.h"
20 #include "initrd-util.h"
22 #include "main-func.h"
24 #include "mount-setup.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "nulstr-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "proc-cmdline.h"
31 #include "process-util.h"
33 #include "specifier.h"
34 #include "stat-util.h"
35 #include "string-util.h"
37 #include "unit-name.h"
39 #include "volatile-util.h"
41 typedef enum MountPointFlags
{
42 MOUNT_NOAUTO
= 1 << 0,
43 MOUNT_NOFAIL
= 1 << 1,
44 MOUNT_AUTOMOUNT
= 1 << 2,
45 MOUNT_MAKEFS
= 1 << 3,
46 MOUNT_GROWFS
= 1 << 4,
47 MOUNT_RW_ONLY
= 1 << 5,
51 typedef struct Mount
{
59 static void mount_array_free(Mount
*mounts
, size_t n
);
61 static bool arg_sysroot_check
= false;
62 static const char *arg_dest
= NULL
;
63 static const char *arg_dest_late
= NULL
;
64 static bool arg_fstab_enabled
= true;
65 static bool arg_swap_enabled
= true;
66 static char *arg_root_what
= NULL
;
67 static char *arg_root_fstype
= NULL
;
68 static char *arg_root_options
= NULL
;
69 static char *arg_root_hash
= NULL
;
70 static int arg_root_rw
= -1;
71 static char *arg_usr_what
= NULL
;
72 static char *arg_usr_fstype
= NULL
;
73 static char *arg_usr_options
= NULL
;
74 static char *arg_usr_hash
= NULL
;
75 static VolatileMode arg_volatile_mode
= _VOLATILE_MODE_INVALID
;
76 static bool arg_verity
= true;
77 static Mount
*arg_mounts
= NULL
;
78 static size_t arg_n_mounts
= 0;
80 STATIC_DESTRUCTOR_REGISTER(arg_root_what
, freep
);
81 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype
, freep
);
82 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
83 STATIC_DESTRUCTOR_REGISTER(arg_root_hash
, freep
);
84 STATIC_DESTRUCTOR_REGISTER(arg_usr_what
, freep
);
85 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype
, freep
);
86 STATIC_DESTRUCTOR_REGISTER(arg_usr_options
, freep
);
87 STATIC_DESTRUCTOR_REGISTER(arg_usr_hash
, freep
);
88 STATIC_ARRAY_DESTRUCTOR_REGISTER(arg_mounts
, arg_n_mounts
, mount_array_free
);
90 static void mount_done(Mount
*m
) {
99 static void mount_array_free(Mount
*mounts
, size_t n
) {
100 FOREACH_ARRAY(m
, mounts
, n
)
106 static int mount_array_add_internal(
110 const char *in_fstype
,
111 const char *in_options
) {
113 _cleanup_free_
char *what
= NULL
, *where
= NULL
, *fstype
= NULL
, *options
= NULL
;
116 /* This takes what and where. */
118 what
= ASSERT_PTR(in_what
);
121 fstype
= strdup(isempty(in_fstype
) ? "auto" : in_fstype
);
125 if (streq(fstype
, "swap"))
126 where
= mfree(where
);
128 if (!isempty(in_options
)) {
129 _cleanup_strv_free_
char **options_strv
= NULL
;
131 r
= strv_split_full(&options_strv
, in_options
, ",", 0);
135 r
= strv_make_nulstr(options_strv
, &options
, NULL
);
137 r
= strv_make_nulstr(STRV_MAKE("defaults"), &options
, NULL
);
141 if (!GREEDY_REALLOC(arg_mounts
, arg_n_mounts
+ 1))
144 arg_mounts
[arg_n_mounts
++] = (Mount
) {
145 .for_initrd
= for_initrd
,
146 .what
= TAKE_PTR(what
),
147 .where
= TAKE_PTR(where
),
148 .fstype
= TAKE_PTR(fstype
),
149 .options
= TAKE_PTR(options
),
155 static int mount_array_add(bool for_initrd
, const char *str
) {
156 _cleanup_free_
char *what
= NULL
, *where
= NULL
, *fstype
= NULL
, *options
= NULL
;
161 r
= extract_many_words(&str
, ":", EXTRACT_CUNESCAPE
| EXTRACT_DONT_COALESCE_SEPARATORS
,
162 &what
, &where
, &fstype
, &options
, NULL
);
170 return mount_array_add_internal(for_initrd
, TAKE_PTR(what
), TAKE_PTR(where
), fstype
, options
);
173 static int mount_array_add_swap(bool for_initrd
, const char *str
) {
174 _cleanup_free_
char *what
= NULL
, *options
= NULL
;
179 r
= extract_many_words(&str
, ":", EXTRACT_CUNESCAPE
| EXTRACT_DONT_COALESCE_SEPARATORS
,
180 &what
, &options
, NULL
);
188 return mount_array_add_internal(for_initrd
, TAKE_PTR(what
), NULL
, "swap", options
);
191 static int write_options(FILE *f
, const char *options
) {
192 _cleanup_free_
char *o
= NULL
;
194 if (isempty(options
))
197 if (streq(options
, "defaults"))
200 o
= specifier_escape(options
);
204 fprintf(f
, "Options=%s\n", o
);
208 static int write_what(FILE *f
, const char *what
) {
209 _cleanup_free_
char *w
= NULL
;
211 w
= specifier_escape(what
);
215 fprintf(f
, "What=%s\n", w
);
223 MountPointFlags flags
) {
225 _cleanup_free_
char *name
= NULL
;
226 _cleanup_fclose_
FILE *f
= NULL
;
231 if (access("/proc/swaps", F_OK
) < 0) {
232 log_info("Swap not supported, ignoring swap entry for %s.", what
);
236 if (detect_container() > 0) {
237 log_info("Running in a container, ignoring swap entry for %s.", what
);
241 if (arg_sysroot_check
) {
242 log_info("%s should be enabled in the initrd, will request daemon-reload.", what
);
246 log_debug("Found swap entry what=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
248 yes_no(flags
& MOUNT_MAKEFS
), yes_no(flags
& MOUNT_GROWFS
), yes_no(flags
& MOUNT_PCRFS
),
249 yes_no(flags
& MOUNT_NOAUTO
), yes_no(flags
& MOUNT_NOFAIL
));
251 r
= unit_name_from_path(what
, ".swap", &name
);
253 return log_error_errno(r
, "Failed to generate unit name: %m");
255 r
= generator_open_unit_file(arg_dest
, source
, name
, &f
);
261 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
265 r
= generator_write_blockdev_dependency(f
, what
);
273 r
= write_what(f
, what
);
277 r
= write_options(f
, options
);
281 r
= fflush_and_check(f
);
283 return log_error_errno(r
, "Failed to write unit file %s: %m", name
);
285 /* use what as where, to have a nicer error message */
286 r
= generator_write_timeouts(arg_dest
, what
, what
, options
, NULL
);
290 if (flags
& MOUNT_MAKEFS
) {
291 r
= generator_hook_up_mkswap(arg_dest
, what
);
296 if (flags
& MOUNT_GROWFS
)
297 /* TODO: swap devices must be wiped and recreated */
298 log_warning("%s: growing swap devices is currently unsupported.", what
);
299 if (flags
& MOUNT_PCRFS
)
300 log_warning("%s: measuring swap devices is currently unsupported.", what
);
302 if (!(flags
& MOUNT_NOAUTO
)) {
303 r
= generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
,
304 (flags
& MOUNT_NOFAIL
) ? "wants" : "requires", name
);
312 static bool mount_is_network(const char *fstype
, const char *options
) {
313 return fstab_test_option(options
, "_netdev\0") ||
314 (fstype
&& fstype_is_network(fstype
));
317 static bool mount_in_initrd(const char *where
, const char *options
, bool accept_root
) {
318 return fstab_test_option(options
, "x-initrd.mount\0") ||
319 (where
&& PATH_IN_SET(where
, "/usr", accept_root
? "/" : NULL
));
322 static int write_timeout(
327 const char *variable
) {
329 _cleanup_free_
char *timeout
= NULL
;
333 r
= fstab_filter_options(opts
, filter
, NULL
, &timeout
, NULL
, NULL
);
335 return log_warning_errno(r
, "Failed to parse options: %m");
339 r
= parse_sec_fix_0(timeout
, &u
);
341 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
345 fprintf(f
, "%s=%s\n", variable
, FORMAT_TIMESPAN(u
, 0));
350 static int write_idle_timeout(FILE *f
, const char *where
, const char *opts
) {
351 return write_timeout(f
, where
, opts
,
352 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
355 static int write_mount_timeout(FILE *f
, const char *where
, const char *opts
) {
356 return write_timeout(f
, where
, opts
,
357 "x-systemd.mount-timeout\0", "TimeoutSec");
360 static int write_dependency(
364 const char *format
) {
366 _cleanup_strv_free_
char **names
= NULL
, **units
= NULL
;
367 _cleanup_free_
char *res
= NULL
;
373 r
= fstab_filter_options(opts
, filter
, NULL
, NULL
, &names
, NULL
);
375 return log_warning_errno(r
, "Failed to parse options: %m");
379 STRV_FOREACH(s
, names
) {
382 r
= unit_name_mangle_with_suffix(*s
, "as dependency", 0, ".mount", &x
);
384 return log_error_errno(r
, "Failed to generate unit name: %m");
386 r
= strv_consume(&units
, x
);
392 res
= strv_join(units
, " ");
396 DISABLE_WARNING_FORMAT_NONLITERAL
;
397 fprintf(f
, format
, res
);
404 static int write_after(FILE *f
, const char *opts
) {
405 return write_dependency(f
, opts
,
406 "x-systemd.after\0", "After=%1$s\n");
409 static int write_requires_after(FILE *f
, const char *opts
) {
410 return write_dependency(f
, opts
,
411 "x-systemd.requires\0", "After=%1$s\nRequires=%1$s\n");
414 static int write_before(FILE *f
, const char *opts
) {
415 return write_dependency(f
, opts
,
416 "x-systemd.before\0", "Before=%1$s\n");
419 static int write_requires_mounts_for(FILE *f
, const char *opts
) {
420 _cleanup_strv_free_
char **paths
= NULL
, **paths_escaped
= NULL
;
421 _cleanup_free_
char *res
= NULL
;
427 r
= fstab_filter_options(opts
, "x-systemd.requires-mounts-for\0", NULL
, NULL
, &paths
, NULL
);
429 return log_warning_errno(r
, "Failed to parse options: %m");
433 r
= specifier_escape_strv(paths
, &paths_escaped
);
435 return log_error_errno(r
, "Failed to escape paths: %m");
437 res
= strv_join(paths_escaped
, " ");
441 fprintf(f
, "RequiresMountsFor=%s\n", res
);
446 static int write_extra_dependencies(FILE *f
, const char *opts
) {
452 r
= write_after(f
, opts
);
455 r
= write_requires_after(f
, opts
);
458 r
= write_before(f
, opts
);
461 r
= write_requires_mounts_for(f
, opts
);
469 static int add_mount(
474 const char *original_where
,
478 MountPointFlags flags
,
479 const char *target_unit
) {
483 *automount_name
= NULL
,
485 *where_escaped
= NULL
;
486 _cleanup_strv_free_
char **wanted_by
= NULL
, **required_by
= NULL
;
487 _cleanup_fclose_
FILE *f
= NULL
;
496 if (streq_ptr(fstype
, "autofs"))
499 if (!is_path(where
)) {
500 log_warning("Mount point %s is not a valid path, ignoring.", where
);
504 if (mount_point_is_api(where
) ||
505 mount_point_ignore(where
))
508 if (arg_sysroot_check
) {
509 log_info("%s should be mounted in the initrd, will request daemon-reload.", where
);
513 r
= fstab_filter_options(opts
, "x-systemd.wanted-by\0", NULL
, NULL
, &wanted_by
, NULL
);
517 r
= fstab_filter_options(opts
, "x-systemd.required-by\0", NULL
, NULL
, &required_by
, NULL
);
521 if (path_equal(where
, "/")) {
522 if (flags
& MOUNT_NOAUTO
)
523 log_warning("Ignoring \"noauto\" option for root device");
524 if (flags
& MOUNT_NOFAIL
)
525 log_warning("Ignoring \"nofail\" option for root device");
526 if (flags
& MOUNT_AUTOMOUNT
)
527 log_warning("Ignoring \"automount\" option for root device");
528 if (!strv_isempty(wanted_by
))
529 log_warning("Ignoring \"x-systemd.wanted-by=\" option for root device");
530 if (!strv_isempty(required_by
))
531 log_warning("Ignoring \"x-systemd.required-by=\" option for root device");
533 required_by
= strv_free(required_by
);
534 wanted_by
= strv_free(wanted_by
);
535 SET_FLAG(flags
, MOUNT_NOAUTO
| MOUNT_NOFAIL
| MOUNT_AUTOMOUNT
, false);
538 r
= unit_name_from_path(where
, ".mount", &name
);
540 return log_error_errno(r
, "Failed to generate unit name: %m");
542 r
= generator_open_unit_file(dest
, source
, name
, &f
);
548 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n"
552 if (STRPTR_IN_SET(fstype
, "nfs", "nfs4") && !(flags
& MOUNT_AUTOMOUNT
) &&
553 fstab_test_yes_no_option(opts
, "bg\0" "fg\0")) {
554 /* The default retry timeout that mount.nfs uses for 'bg' mounts
555 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
556 * As we are making 'bg' mounts look like an 'fg' mount to
557 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
558 * we need to explicitly preserve that default, and also ensure
559 * the systemd mount-timeout doesn't interfere.
560 * By placing these options first, they can be overridden by
561 * settings in /etc/fstab. */
562 opts
= strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts
, ",fg");
563 SET_FLAG(flags
, MOUNT_NOFAIL
, true);
566 r
= write_extra_dependencies(f
, opts
);
570 /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
571 * target unit won't affect us. */
572 if (!FLAGS_SET(flags
, MOUNT_NOFAIL
))
573 fprintf(f
, "Before=%s\n", target_unit
);
576 r
= generator_write_fsck_deps(f
, dest
, what
, where
, fstype
);
581 r
= generator_write_blockdev_dependency(f
, what
);
589 r
= write_what(f
, what
);
594 fprintf(f
, "# Canonicalized from %s\n", original_where
);
596 where_escaped
= specifier_escape(where
);
599 fprintf(f
, "Where=%s\n", where_escaped
);
601 if (!isempty(fstype
) && !streq(fstype
, "auto")) {
602 _cleanup_free_
char *t
= NULL
;
604 t
= specifier_escape(fstype
);
608 fprintf(f
, "Type=%s\n", t
);
611 r
= generator_write_timeouts(dest
, what
, where
, opts
, &filtered
);
615 r
= generator_write_device_deps(dest
, what
, where
, opts
);
619 if (in_initrd() && path_equal(where
, "/sysroot") && is_device_path(what
)) {
620 r
= generator_write_initrd_root_device_deps(dest
, what
);
625 r
= write_mount_timeout(f
, where
, opts
);
629 r
= write_options(f
, filtered
);
633 if (flags
& MOUNT_RW_ONLY
)
634 fprintf(f
, "ReadWriteOnly=yes\n");
636 r
= fflush_and_check(f
);
638 return log_error_errno(r
, "Failed to write unit file %s: %m", name
);
640 if (flags
& MOUNT_MAKEFS
) {
641 r
= generator_hook_up_mkfs(dest
, what
, where
, fstype
);
646 if (flags
& MOUNT_GROWFS
) {
647 r
= generator_hook_up_growfs(dest
, where
, target_unit
);
652 if (flags
& MOUNT_PCRFS
) {
653 r
= efi_measured_uki(LOG_WARNING
);
655 log_debug("Kernel stub did not measure kernel image into PCR, skipping userspace measurement, too.");
657 r
= generator_hook_up_pcrfs(dest
, where
, target_unit
);
663 if (!FLAGS_SET(flags
, MOUNT_AUTOMOUNT
)) {
664 if (!FLAGS_SET(flags
, MOUNT_NOAUTO
) && strv_isempty(wanted_by
) && strv_isempty(required_by
)) {
665 r
= generator_add_symlink(dest
, target_unit
,
666 (flags
& MOUNT_NOFAIL
) ? "wants" : "requires", name
);
670 STRV_FOREACH(s
, wanted_by
) {
671 r
= generator_add_symlink(dest
, *s
, "wants", name
);
676 STRV_FOREACH(s
, required_by
) {
677 r
= generator_add_symlink(dest
, *s
, "requires", name
);
683 r
= unit_name_from_path(where
, ".automount", &automount_name
);
685 return log_error_errno(r
, "Failed to generate unit name: %m");
689 r
= generator_open_unit_file(dest
, source
, automount_name
, &f
);
696 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
705 r
= write_idle_timeout(f
, where
, opts
);
709 r
= fflush_and_check(f
);
711 return log_error_errno(r
, "Failed to write unit file %s: %m", automount_name
);
713 r
= generator_add_symlink(dest
, target_unit
,
714 (flags
& MOUNT_NOFAIL
) ? "wants" : "requires", automount_name
);
722 static int do_daemon_reload(void) {
723 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
724 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
727 log_debug("Calling org.freedesktop.systemd1.Manager.Reload()...");
729 r
= bus_connect_system_systemd(&bus
);
731 return log_error_errno(r
, "Failed to get D-Bus connection: %m");
733 r
= bus_service_manager_reload(bus
);
737 /* We need to requeue the two targets so that any new units which previously were not part of the
738 * targets, and which we now added, will be started. */
741 FOREACH_STRING(unit
, SPECIAL_INITRD_FS_TARGET
, SPECIAL_SWAP_TARGET
) {
742 log_info("Requesting %s/start/replace...", unit
);
744 k
= bus_call_method(bus
, bus_systemd_mgr
, "StartUnit", &error
, NULL
, "ss", unit
, "replace");
746 log_error_errno(k
, "Failed to (re)start %s: %s", unit
, bus_error_message(&error
, r
));
754 static const char* sysroot_fstab_path(void) {
755 return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab";
758 static bool sysfs_check(void) {
759 static int cached
= -1;
763 r
= getenv_bool_secure("SYSTEMD_SYSFS_CHECK");
764 if (r
< 0 && r
!= -ENXIO
)
765 log_debug_errno(r
, "Failed to parse $SYSTEMD_SYSFS_CHECK, ignoring: %m");
772 static int add_sysusr_sysroot_usr_bind_mount(const char *source
) {
773 return add_mount(source
,
782 SPECIAL_INITRD_FS_TARGET
);
785 static MountPointFlags
fstab_options_to_flags(const char *options
, bool is_swap
) {
786 MountPointFlags flags
= 0;
788 if (fstab_test_option(options
, "x-systemd.makefs\0"))
789 flags
|= MOUNT_MAKEFS
;
790 if (fstab_test_option(options
, "x-systemd.growfs\0"))
791 flags
|= MOUNT_GROWFS
;
792 if (fstab_test_option(options
, "x-systemd.pcrfs\0"))
793 flags
|= MOUNT_PCRFS
;
794 if (fstab_test_yes_no_option(options
, "noauto\0" "auto\0"))
795 flags
|= MOUNT_NOAUTO
;
796 if (fstab_test_yes_no_option(options
, "nofail\0" "fail\0"))
797 flags
|= MOUNT_NOFAIL
;
800 if (fstab_test_option(options
, "x-systemd.rw-only\0"))
801 flags
|= MOUNT_RW_ONLY
;
802 if (fstab_test_option(options
,
803 "comment=systemd.automount\0"
804 "x-systemd.automount\0"))
805 flags
|= MOUNT_AUTOMOUNT
;
811 static int canonicalize_mount_path(const char *path
, const char *type
, bool prefix_sysroot
, char **ret
) {
812 _cleanup_free_
char *p
= NULL
;
818 assert(STR_IN_SET(type
, "where", "what"));
821 // FIXME: when chase() learns to chase non-existent paths, use this here and drop the prefixing with
822 // /sysroot on error below.
823 r
= chase(path
, prefix_sysroot
? "/sysroot" : NULL
, CHASE_PREFIX_ROOT
| CHASE_NONEXISTENT
, &p
, NULL
);
825 log_debug_errno(r
, "Failed to chase '%s', using as-is: %m", path
);
828 p
= path_join("/sysroot", path
);
837 changed
= !streq(path
, p
);
839 log_debug("Canonicalized %s=%s to %s", type
, path
, p
);
845 static int parse_fstab_one(
847 const char *what_original
,
848 const char *where_original
,
853 bool accept_root
, /* This takes an effect only when prefix_sysroot is true. */
854 bool use_swap_enabled
) {
856 _cleanup_free_
char *what
= NULL
, *where
= NULL
, *opts
= NULL
;
857 MountPointFlags flags
;
858 bool is_swap
, where_changed
;
861 assert(what_original
);
865 if (prefix_sysroot
&& !mount_in_initrd(where_original
, options
, accept_root
))
868 is_swap
= streq_ptr(fstype
, "swap");
869 if (is_swap
&& use_swap_enabled
&& !arg_swap_enabled
) {
870 log_info("Swap unit generation disabled on kernel command line, ignoring swap entry for %s.", what_original
);
874 what
= fstab_node_to_udev_node(what_original
);
878 if (path_is_read_only_fs("/sys") > 0 &&
879 (streq(what
, "sysfs") ||
880 (sysfs_check() && is_device_path(what
)))) {
881 log_info("/sys/ is read-only (running in a container?), ignoring mount for %s.", what
);
885 flags
= fstab_options_to_flags(options
, is_swap
);
888 return add_swap(source
, what
, options
, flags
);
891 passno
= is_device_path(what
);
893 assert(where_original
); /* 'where' is not necessary for swap entry. */
895 if (!is_path(where_original
)) {
896 log_warning("Mount point %s is not a valid path, ignoring.", where_original
);
900 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
901 * mount units, but causes problems since it historically worked to have symlinks in e.g.
902 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
903 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
904 * target is the final directory. */
905 r
= canonicalize_mount_path(where_original
, "where", prefix_sysroot
, &where
);
908 where_changed
= r
> 0;
910 if (prefix_sysroot
&& fstab_is_bind(options
, fstype
)) {
911 /* When in initrd, the source of bind mount needs to be prepended with /sysroot as well. */
912 _cleanup_free_
char *p
= NULL
;
914 r
= canonicalize_mount_path(what
, "what", prefix_sysroot
, &p
);
918 free_and_replace(what
, p
);
921 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s",
922 what
, where
, strna(fstype
),
923 yes_no(flags
& MOUNT_MAKEFS
), yes_no(flags
& MOUNT_GROWFS
), yes_no(flags
& MOUNT_PCRFS
),
924 yes_no(flags
& MOUNT_NOAUTO
), yes_no(flags
& MOUNT_NOFAIL
));
926 bool is_sysroot
= in_initrd() && path_equal(where
, "/sysroot");
927 /* See comment from add_sysroot_usr_mount() about the need for extra indirection in case /usr needs
928 * to be mounted in order for the root fs to be synthesized based on configuration included in /usr/,
929 * e.g. systemd-repart. */
930 bool is_sysroot_usr
= in_initrd() && path_equal(where
, "/sysroot/usr");
932 const char *target_unit
=
933 is_sysroot
? SPECIAL_INITRD_ROOT_FS_TARGET
:
934 is_sysroot_usr
? SPECIAL_INITRD_USR_FS_TARGET
:
935 prefix_sysroot
? SPECIAL_INITRD_FS_TARGET
:
936 mount_is_network(fstype
, options
) ? SPECIAL_REMOTE_FS_TARGET
:
937 SPECIAL_LOCAL_FS_TARGET
;
939 /* nofail or noauto don't make sense for critical filesystems we must mount in initrd. */
940 if ((is_sysroot
|| is_sysroot_usr
) && ((flags
& (MOUNT_NOFAIL
|MOUNT_NOAUTO
)) != 0)) {
941 flags
&= ~(MOUNT_NOFAIL
|MOUNT_NOAUTO
);
942 r
= fstab_filter_options(options
, "noauto\0nofail\0", NULL
, NULL
, NULL
, &opts
);
946 log_debug("'noauto' and 'nofail' options are ignored for /sysroot/ and /sysroot/usr/ mounts.");
950 r
= add_mount(source
,
953 is_sysroot_usr
? "/sysusr/usr" : where
,
954 !is_sysroot_usr
&& where_changed
? where_original
: NULL
,
963 if (is_sysroot_usr
) {
964 log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind");
965 r
= add_sysusr_sysroot_usr_bind_mount(source
);
973 static int parse_fstab(bool prefix_sysroot
) {
974 _cleanup_endmntent_
FILE *f
= NULL
;
980 fstab
= sysroot_fstab_path();
982 fstab
= fstab_path();
983 assert(!arg_sysroot_check
);
986 log_debug("Parsing %s...", fstab
);
988 f
= setmntent(fstab
, "re");
993 return log_error_errno(errno
, "Failed to open %s: %m", fstab
);
996 while ((me
= getmntent(f
))) {
997 r
= parse_fstab_one(fstab
,
998 me
->mnt_fsname
, me
->mnt_dir
, me
->mnt_type
, me
->mnt_opts
, me
->mnt_passno
,
1000 /* accept_root = */ false,
1001 /* use_swap_enabled = */ true);
1002 if (r
< 0 && ret
>= 0)
1004 if (arg_sysroot_check
&& r
> 0)
1005 return true; /* We found a mount or swap that would be started… */
1011 static int sysroot_is_nfsroot(void) {
1012 union in_addr_union u
;
1013 const char *sep
, *a
;
1016 assert(arg_root_what
);
1018 /* From dracut.cmdline(7).
1020 * root=[<server-ip>:]<root-dir>[:<nfs-options>]
1021 * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>],
1022 * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>],
1025 * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server.
1026 * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options
1027 * can be appended with the prefix ":" or "," and are separated by ",". */
1029 if (path_equal(arg_root_what
, "/dev/nfs") ||
1030 STR_IN_SET(arg_root_what
, "dhcp", "dhcp6") ||
1031 STARTSWITH_SET(arg_root_what
, "nfs:", "nfs4:"))
1035 if (arg_root_what
[0] == '[') {
1036 sep
= strchr(arg_root_what
+ 1, ']');
1040 a
= strndupa_safe(arg_root_what
+ 1, sep
- arg_root_what
- 1);
1042 r
= in_addr_from_string(AF_INET6
, a
, &u
);
1050 sep
= strchr(arg_root_what
, ':');
1052 a
= strndupa_safe(arg_root_what
, sep
- arg_root_what
);
1054 if (in_addr_from_string(AF_INET
, a
, &u
) >= 0)
1058 /* root directory without address */
1059 return path_is_absolute(arg_root_what
) && !path_startswith(arg_root_what
, "/dev");
1062 static int add_sysroot_mount(void) {
1063 _cleanup_free_
char *what
= NULL
;
1064 const char *opts
, *fstype
;
1065 bool default_rw
, makefs
;
1066 MountPointFlags flags
;
1069 if (isempty(arg_root_what
)) {
1070 log_debug("Could not find a root= entry on the kernel command line.");
1074 if (streq(arg_root_what
, "gpt-auto")) {
1075 /* This is handled by gpt-auto-generator */
1076 log_debug("Skipping root directory handling, as gpt-auto was requested.");
1078 } else if (streq(arg_root_what
, "fstab")) {
1079 /* This is handled by parse_fstab */
1080 log_debug("Using initrd's fstab for /sysroot/ configuration.");
1084 r
= sysroot_is_nfsroot();
1086 log_debug_errno(r
, "Failed to determine if the root directory is on NFS, assuming not: %m");
1088 /* This is handled by the kernel or the initrd */
1089 log_debug("Skipping root directory handling, as root on NFS was requested.");
1093 if (startswith(arg_root_what
, "cifs://")) {
1094 log_debug("Skipping root directory handling, as root on CIFS was requested.");
1098 if (startswith(arg_root_what
, "iscsi:")) {
1099 log_debug("Skipping root directory handling, as root on iSCSI was requested.");
1103 if (startswith(arg_root_what
, "live:")) {
1104 log_debug("Skipping root directory handling, as root on live image was requested.");
1108 if (streq(arg_root_what
, "tmpfs")) {
1109 /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */
1111 what
= strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */
1115 fstype
= arg_root_fstype
?: "tmpfs"; /* tmpfs, unless overridden */
1117 default_rw
= true; /* writable, unless overridden */;
1120 what
= fstab_node_to_udev_node(arg_root_what
);
1124 fstype
= arg_root_fstype
; /* if not specified explicitly, don't default to anything here */
1126 default_rw
= false; /* read-only, unless overridden */
1129 if (!arg_root_options
)
1130 opts
= arg_root_rw
> 0 || (arg_root_rw
< 0 && default_rw
) ? "rw" : "ro";
1131 else if (arg_root_rw
>= 0 ||
1132 !fstab_test_option(arg_root_options
, "ro\0" "rw\0"))
1133 opts
= strjoina(arg_root_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
1135 opts
= arg_root_options
;
1137 log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what
, strna(arg_root_fstype
), strempty(opts
));
1139 makefs
= fstab_test_option(opts
, "x-systemd.makefs\0");
1140 flags
= makefs
* MOUNT_MAKEFS
;
1142 return add_mount("/proc/cmdline",
1149 is_device_path(what
) ? 1 : 0, /* passno */
1150 flags
, /* makefs off, pcrfs off, noauto off, nofail off, automount off */
1151 SPECIAL_INITRD_ROOT_FS_TARGET
);
1154 static int add_sysroot_usr_mount(void) {
1155 _cleanup_free_
char *what
= NULL
;
1158 MountPointFlags flags
;
1161 /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we
1162 * know for sure something else did */
1164 if (!arg_usr_what
&& !arg_usr_fstype
&& !arg_usr_options
)
1167 if (arg_root_what
&& !arg_usr_what
) {
1168 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
1169 arg_usr_what
= strdup(arg_root_what
);
1174 if (arg_root_fstype
&& !arg_usr_fstype
) {
1175 arg_usr_fstype
= strdup(arg_root_fstype
);
1176 if (!arg_usr_fstype
)
1180 if (arg_root_options
&& !arg_usr_options
) {
1181 arg_usr_options
= strdup(arg_root_options
);
1182 if (!arg_usr_options
)
1186 if (isempty(arg_usr_what
)) {
1187 log_debug("Could not find a mount.usr= entry on the kernel command line.");
1191 if (streq(arg_usr_what
, "gpt-auto")) {
1192 /* This is handled by the gpt-auto generator */
1193 log_debug("Skipping /usr/ directory handling, as gpt-auto was requested.");
1194 return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a
1195 * unit file is being created for the host /usr/ mount. */
1196 } else if (streq(arg_usr_what
, "fstab")) {
1197 /* This is handled by parse_fstab */
1198 log_debug("Using initrd's fstab for /sysroot/usr/ configuration.");
1199 return 1; /* parse_fstab will generate a unit for this, hence report that a
1200 * unit file is being created for the host /usr/ mount. */
1203 if (path_equal(arg_usr_what
, "/dev/nfs")) {
1204 /* This is handled by the initrd (if at all supported, that is) */
1205 log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested.");
1206 return 1; /* As above, report that NFS code will create the unit */
1209 what
= fstab_node_to_udev_node(arg_usr_what
);
1213 if (!arg_usr_options
)
1214 opts
= arg_root_rw
> 0 ? "rw" : "ro";
1215 else if (!fstab_test_option(arg_usr_options
, "ro\0" "rw\0"))
1216 opts
= strjoina(arg_usr_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
1218 opts
= arg_usr_options
;
1220 /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/
1221 * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so
1222 * that we can cover for systems that initially only have a /usr/ around and where the root fs needs
1223 * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like
1224 * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should
1225 * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */
1227 log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what
, strna(arg_usr_fstype
), strempty(opts
));
1229 makefs
= fstab_test_option(opts
, "x-systemd.makefs\0");
1230 flags
= makefs
* MOUNT_MAKEFS
;
1232 r
= add_mount("/proc/cmdline",
1239 is_device_path(what
) ? 1 : 0, /* passno */
1241 SPECIAL_INITRD_USR_FS_TARGET
);
1245 log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind");
1247 r
= add_sysusr_sysroot_usr_bind_mount("/proc/cmdline");
1254 static int add_sysroot_usr_mount_or_fallback(void) {
1257 r
= add_sysroot_usr_mount();
1261 /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make
1262 * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order
1263 * themselves after it get the guarantee that /usr/ is definitely mounted somewhere. */
1265 return generator_add_symlink(
1267 SPECIAL_INITRD_USR_FS_TARGET
,
1272 static int add_volatile_root(void) {
1274 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
1275 * requested (or as an overlayfs), leaving only /usr from the root mount inside. */
1277 if (!IN_SET(arg_volatile_mode
, VOLATILE_YES
, VOLATILE_OVERLAY
))
1280 return generator_add_symlink(arg_dest
, SPECIAL_INITRD_ROOT_FS_TARGET
, "requires",
1281 SYSTEM_DATA_UNIT_DIR
"/" SPECIAL_VOLATILE_ROOT_SERVICE
);
1284 static int add_volatile_var(void) {
1286 if (arg_volatile_mode
!= VOLATILE_STATE
)
1289 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
1291 return add_mount("/proc/cmdline",
1297 "mode=0755" TMPFS_LIMITS_VAR
,
1300 SPECIAL_LOCAL_FS_TARGET
);
1303 static int add_mounts_from_cmdline(void) {
1306 /* Handle each entries found in cmdline as a fstab entry. */
1308 FOREACH_ARRAY(m
, arg_mounts
, arg_n_mounts
) {
1309 if (m
->for_initrd
&& !in_initrd())
1312 r
= parse_fstab_one(
1319 /* prefix_sysroot = */ !m
->for_initrd
&& in_initrd(),
1320 /* accept_root = */ true,
1321 /* use_swap_enabled = */ false);
1322 if (r
< 0 && ret
>= 0)
1329 static int add_mounts_from_creds(bool prefix_sysroot
) {
1330 _cleanup_free_
void *b
= NULL
;
1335 assert(in_initrd() || !prefix_sysroot
);
1337 r
= read_credential_with_decryption(
1338 in_initrd() && !prefix_sysroot
? "fstab.extra.initrd" : "fstab.extra",
1343 _cleanup_fclose_
FILE *f
= NULL
;
1344 f
= fmemopen_unlocked(b
, bs
, "r");
1348 while ((me
= getmntent(f
))) {
1349 r
= parse_fstab_one(
1356 /* prefix_sysroot = */ prefix_sysroot
,
1357 /* accept_root = */ true,
1358 /* use_swap_enabled = */ true);
1359 if (r
< 0 && ret
>= 0)
1366 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1371 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
1372 * instance should take precedence. In the case of multiple rootflags=
1373 * or usrflags= the arguments should be concatenated */
1375 if (STR_IN_SET(key
, "fstab", "rd.fstab")) {
1377 r
= value
? parse_boolean(value
) : 1;
1379 log_warning("Failed to parse fstab switch %s. Ignoring.", value
);
1381 arg_fstab_enabled
= fstab_set_enabled(r
);
1383 } else if (streq(key
, "root")) {
1385 if (proc_cmdline_value_missing(key
, value
))
1388 return free_and_strdup_warn(&arg_root_what
, empty_to_null(value
));
1390 } else if (streq(key
, "rootfstype")) {
1392 if (proc_cmdline_value_missing(key
, value
))
1395 return free_and_strdup_warn(&arg_root_fstype
, empty_to_null(value
));
1397 } else if (streq(key
, "rootflags")) {
1399 if (proc_cmdline_value_missing(key
, value
))
1402 if (!strextend_with_separator(&arg_root_options
, ",", value
))
1405 } else if (streq(key
, "roothash")) {
1407 if (proc_cmdline_value_missing(key
, value
))
1410 return free_and_strdup_warn(&arg_root_hash
, empty_to_null(value
));
1412 } else if (streq(key
, "mount.usr")) {
1414 if (proc_cmdline_value_missing(key
, value
))
1417 return free_and_strdup_warn(&arg_usr_what
, empty_to_null(value
));
1419 } else if (streq(key
, "mount.usrfstype")) {
1421 if (proc_cmdline_value_missing(key
, value
))
1424 return free_and_strdup_warn(&arg_usr_fstype
, empty_to_null(value
));
1426 } else if (streq(key
, "mount.usrflags")) {
1428 if (proc_cmdline_value_missing(key
, value
))
1431 if (!strextend_with_separator(&arg_usr_options
, ",", value
))
1434 } else if (streq(key
, "usrhash")) {
1436 if (proc_cmdline_value_missing(key
, value
))
1439 return free_and_strdup_warn(&arg_usr_hash
, empty_to_null(value
));
1441 } else if (streq(key
, "rw") && !value
)
1443 else if (streq(key
, "ro") && !value
)
1444 arg_root_rw
= false;
1445 else if (streq(key
, "systemd.volatile")) {
1449 m
= volatile_mode_from_string(value
);
1451 log_warning_errno(m
, "Failed to parse systemd.volatile= argument: %s", value
);
1453 arg_volatile_mode
= m
;
1455 arg_volatile_mode
= VOLATILE_YES
;
1457 } else if (streq(key
, "systemd.swap")) {
1459 r
= value
? parse_boolean(value
) : 1;
1461 log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value
);
1463 arg_swap_enabled
= r
;
1465 } else if (streq(key
, "systemd.verity")) {
1467 r
= value
? parse_boolean(value
) : 1;
1469 log_warning("Failed to parse systemd.verity= kernel command line switch %s. Ignoring.", value
);
1473 } else if (STR_IN_SET(key
, "systemd.mount-extra", "rd.systemd.mount-extra")) {
1475 if (proc_cmdline_value_missing(key
, value
))
1478 r
= mount_array_add(startswith(key
, "rd."), value
);
1480 log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value
);
1482 } else if (STR_IN_SET(key
, "systemd.swap-extra", "rd.systemd.swap-extra")) {
1484 if (proc_cmdline_value_missing(key
, value
))
1487 r
= mount_array_add_swap(startswith(key
, "rd."), value
);
1489 log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value
);
1495 static int determine_device(
1505 /* If we have a hash but no device then Verity is used, and we use the DM device. */
1515 *what
= path_join("/dev/mapper/", name
);
1519 /* Verity is always read-only */
1522 if (options
&& !strextend_with_separator(options
, ",", "ro"))
1525 log_info("Using verity %s device %s.", name
, *what
);
1529 static int determine_root(void) {
1530 return determine_device(&arg_root_what
, &arg_root_rw
, NULL
, arg_root_hash
, "root");
1533 static int determine_usr(void) {
1534 return determine_device(&arg_usr_what
, NULL
, &arg_usr_options
, arg_usr_hash
, "usr");
1537 /* If arg_sysroot_check is false, run as generator in the usual fashion.
1538 * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount
1539 * in the initrd, and call daemon-reload. We will get reinvoked as a generator,
1540 * with /sysroot/etc/fstab available, and then we can write additional units based
1542 static int run_generator(void) {
1545 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
1547 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
1549 (void) determine_root();
1550 (void) determine_usr();
1552 if (arg_sysroot_check
) {
1553 r
= parse_fstab(/* prefix_sysroot = */ true);
1555 log_debug("Nothing interesting found, not doing daemon-reload.");
1557 r
= do_daemon_reload();
1561 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
1563 r
= add_sysroot_mount();
1564 if (r
< 0 && ret
>= 0)
1567 r
= add_sysroot_usr_mount_or_fallback();
1568 if (r
< 0 && ret
>= 0)
1571 r
= add_volatile_root();
1572 if (r
< 0 && ret
>= 0)
1575 r
= add_volatile_var();
1576 if (r
< 0 && ret
>= 0)
1580 /* Honour /etc/fstab only when that's enabled */
1581 if (arg_fstab_enabled
) {
1582 /* Parse the local /etc/fstab, possibly from the initrd */
1583 r
= parse_fstab(/* prefix_sysroot = */ false);
1584 if (r
< 0 && ret
>= 0)
1587 /* If running in the initrd also parse the /etc/fstab from the host */
1589 r
= parse_fstab(/* prefix_sysroot = */ true);
1591 r
= generator_enable_remount_fs_service(arg_dest
);
1592 if (r
< 0 && ret
>= 0)
1596 r
= add_mounts_from_cmdline();
1597 if (r
< 0 && ret
>= 0)
1600 r
= add_mounts_from_creds(/* prefix_sysroot = */ false);
1601 if (r
< 0 && ret
>= 0)
1605 r
= add_mounts_from_creds(/* prefix_sysroot = */ true);
1606 if (r
< 0 && ret
>= 0)
1613 static int run(int argc
, char **argv
) {
1614 arg_sysroot_check
= invoked_as(argv
, "systemd-sysroot-fstab-check");
1616 if (arg_sysroot_check
) {
1617 /* Run as in systemd-sysroot-fstab-check mode */
1620 if (strv_length(argv
) > 1)
1621 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1622 "This program takes no arguments.");
1624 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1625 "This program is only useful in the initrd.");
1627 /* Run in generator mode */
1628 log_setup_generator();
1630 if (!IN_SET(strv_length(argv
), 2, 4))
1631 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1632 "This program takes one or three arguments.");
1634 arg_dest
= ASSERT_PTR(argv
[1]);
1635 arg_dest_late
= ASSERT_PTR(argv
[argc
> 3 ? 3 : 1]);
1638 return run_generator();
1641 DEFINE_MAIN_FUNCTION(run
);