1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
14 #include "fstab-util.h"
15 #include "generator.h"
17 #include "main-func.h"
19 #include "mount-setup.h"
20 #include "mount-util.h"
21 #include "mountpoint-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "proc-cmdline.h"
26 #include "specifier.h"
27 #include "stat-util.h"
28 #include "string-util.h"
30 #include "unit-name.h"
33 #include "volatile-util.h"
35 typedef enum MountpointFlags
{
43 static const char *arg_dest
= NULL
;
44 static const char *arg_dest_late
= NULL
;
45 static bool arg_fstab_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 VolatileMode arg_volatile_mode
= _VOLATILE_MODE_INVALID
;
56 STATIC_DESTRUCTOR_REGISTER(arg_root_what
, freep
);
57 STATIC_DESTRUCTOR_REGISTER(arg_root_fstype
, freep
);
58 STATIC_DESTRUCTOR_REGISTER(arg_root_options
, freep
);
59 STATIC_DESTRUCTOR_REGISTER(arg_root_hash
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_usr_what
, freep
);
61 STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype
, freep
);
62 STATIC_DESTRUCTOR_REGISTER(arg_usr_options
, freep
);
64 static int write_options(FILE *f
, const char *options
) {
65 _cleanup_free_
char *o
= NULL
;
70 if (streq(options
, "defaults"))
73 o
= specifier_escape(options
);
77 fprintf(f
, "Options=%s\n", o
);
81 static int write_what(FILE *f
, const char *what
) {
82 _cleanup_free_
char *w
= NULL
;
84 w
= specifier_escape(what
);
88 fprintf(f
, "What=%s\n", w
);
95 MountpointFlags flags
) {
97 _cleanup_free_
char *name
= NULL
;
98 _cleanup_fclose_
FILE *f
= NULL
;
104 if (access("/proc/swaps", F_OK
) < 0) {
105 log_info("Swap not supported, ignoring fstab swap entry for %s.", what
);
109 if (detect_container() > 0) {
110 log_info("Running in a container, ignoring fstab swap entry for %s.", what
);
114 r
= unit_name_from_path(what
, ".swap", &name
);
116 return log_error_errno(r
, "Failed to generate unit name: %m");
118 r
= generator_open_unit_file(arg_dest
, "/etc/fstab", name
, &f
);
122 fputs("# Automatically generated by systemd-fstab-generator\n\n"
124 "SourcePath=/etc/fstab\n"
125 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
128 r
= write_what(f
, what
);
132 r
= write_options(f
, me
->mnt_opts
);
136 r
= fflush_and_check(f
);
138 return log_error_errno(r
, "Failed to write unit file %s: %m", name
);
140 /* use what as where, to have a nicer error message */
141 r
= generator_write_timeouts(arg_dest
, what
, what
, me
->mnt_opts
, NULL
);
145 if (flags
& MAKEFS
) {
146 r
= generator_hook_up_mkswap(arg_dest
, what
);
152 /* TODO: swap devices must be wiped and recreated */
153 log_warning("%s: growing swap devices is currently unsupported.", what
);
155 if (!(flags
& NOAUTO
)) {
156 r
= generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
,
157 (flags
& NOFAIL
) ? "wants" : "requires", name
);
165 static bool mount_is_network(struct mntent
*me
) {
168 return fstab_test_option(me
->mnt_opts
, "_netdev\0") ||
169 fstype_is_network(me
->mnt_type
);
172 static bool mount_in_initrd(struct mntent
*me
) {
175 return fstab_test_option(me
->mnt_opts
, "x-initrd.mount\0") ||
176 streq(me
->mnt_dir
, "/usr");
179 static int write_timeout(FILE *f
, const char *where
, const char *opts
,
180 const char *filter
, const char *variable
) {
181 _cleanup_free_
char *timeout
= NULL
;
182 char timespan
[FORMAT_TIMESPAN_MAX
];
186 r
= fstab_filter_options(opts
, filter
, NULL
, &timeout
, NULL
);
188 return log_warning_errno(r
, "Failed to parse options: %m");
192 r
= parse_sec_fix_0(timeout
, &u
);
194 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
198 fprintf(f
, "%s=%s\n", variable
, format_timespan(timespan
, sizeof(timespan
), u
, 0));
203 static int write_idle_timeout(FILE *f
, const char *where
, const char *opts
) {
204 return write_timeout(f
, where
, opts
,
205 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
208 static int write_mount_timeout(FILE *f
, const char *where
, const char *opts
) {
209 return write_timeout(f
, where
, opts
,
210 "x-systemd.mount-timeout\0", "TimeoutSec");
213 static int write_dependency(FILE *f
, const char *opts
,
214 const char *filter
, const char *format
) {
215 _cleanup_strv_free_
char **names
= NULL
, **units
= NULL
;
216 _cleanup_free_
char *res
= NULL
;
223 r
= fstab_extract_values(opts
, filter
, &names
);
225 return log_warning_errno(r
, "Failed to parse options: %m");
229 STRV_FOREACH(s
, names
) {
232 r
= unit_name_mangle_with_suffix(*s
, 0, ".mount", &x
);
234 return log_error_errno(r
, "Failed to generate unit name: %m");
235 r
= strv_consume(&units
, x
);
241 res
= strv_join(units
, " ");
244 #pragma GCC diagnostic push
245 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
246 fprintf(f
, format
, res
);
247 #pragma GCC diagnostic pop
253 static int write_after(FILE *f
, const char *opts
) {
254 return write_dependency(f
, opts
, "x-systemd.after", "After=%1$s\n");
257 static int write_requires_after(FILE *f
, const char *opts
) {
258 return write_dependency(f
, opts
,
259 "x-systemd.requires", "After=%1$s\nRequires=%1$s\n");
262 static int write_before(FILE *f
, const char *opts
) {
263 return write_dependency(f
, opts
,
264 "x-systemd.before", "Before=%1$s\n");
267 static int write_requires_mounts_for(FILE *f
, const char *opts
) {
268 _cleanup_strv_free_
char **paths
= NULL
, **paths_escaped
= NULL
;
269 _cleanup_free_
char *res
= NULL
;
275 r
= fstab_extract_values(opts
, "x-systemd.requires-mounts-for", &paths
);
277 return log_warning_errno(r
, "Failed to parse options: %m");
281 r
= specifier_escape_strv(paths
, &paths_escaped
);
283 return log_error_errno(r
, "Failed to escape paths: %m");
285 res
= strv_join(paths_escaped
, " ");
289 fprintf(f
, "RequiresMountsFor=%s\n", res
);
294 static int add_mount(
298 const char *original_where
,
302 MountpointFlags flags
,
304 const char *source
) {
308 *automount_name
= NULL
,
310 *where_escaped
= NULL
;
311 _cleanup_fclose_
FILE *f
= NULL
;
320 if (streq_ptr(fstype
, "autofs"))
323 if (!is_path(where
)) {
324 log_warning("Mount point %s is not a valid path, ignoring.", where
);
328 if (mount_point_is_api(where
) ||
329 mount_point_ignore(where
))
332 if (path_equal(where
, "/")) {
334 log_warning("Ignoring \"noauto\" for root device");
336 log_warning("Ignoring \"nofail\" for root device");
337 if (flags
& AUTOMOUNT
)
338 log_warning("Ignoring automount option for root device");
340 SET_FLAG(flags
, NOAUTO
| NOFAIL
| AUTOMOUNT
, false);
343 r
= unit_name_from_path(where
, ".mount", &name
);
345 return log_error_errno(r
, "Failed to generate unit name: %m");
347 r
= generator_open_unit_file(dest
, "/etc/fstab", name
, &f
);
354 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
357 if (STRPTR_IN_SET(fstype
, "nfs", "nfs4") && !(flags
& AUTOMOUNT
) &&
358 fstab_test_yes_no_option(opts
, "bg\0" "fg\0")) {
359 /* The default retry timeout that mount.nfs uses for 'bg' mounts
360 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
361 * As we are making 'bg' mounts look like an 'fg' mount to
362 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
363 * we need to explicitly preserve that default, and also ensure
364 * the systemd mount-timeout doesn't interfere.
365 * By placing these options first, they can be over-ridden by
366 * settings in /etc/fstab. */
367 opts
= strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts
, ",fg");
368 SET_FLAG(flags
, NOFAIL
, true);
371 if (!(flags
& NOFAIL
) && !(flags
& AUTOMOUNT
))
372 fprintf(f
, "Before=%s\n", post
);
374 if (!(flags
& AUTOMOUNT
) && opts
) {
375 r
= write_after(f
, opts
);
378 r
= write_requires_after(f
, opts
);
381 r
= write_before(f
, opts
);
384 r
= write_requires_mounts_for(f
, opts
);
390 r
= generator_write_fsck_deps(f
, dest
, what
, where
, fstype
);
395 fprintf(f
, "\n[Mount]\n");
397 fprintf(f
, "# Canonicalized from %s\n", original_where
);
399 where_escaped
= specifier_escape(where
);
402 fprintf(f
, "Where=%s\n", where_escaped
);
404 r
= write_what(f
, what
);
408 if (!isempty(fstype
) && !streq(fstype
, "auto")) {
409 _cleanup_free_
char *t
;
411 t
= specifier_escape(fstype
);
415 fprintf(f
, "Type=%s\n", t
);
418 r
= generator_write_timeouts(dest
, what
, where
, opts
, &filtered
);
422 r
= generator_write_device_deps(dest
, what
, where
, opts
);
426 r
= write_mount_timeout(f
, where
, opts
);
430 r
= write_options(f
, filtered
);
434 r
= fflush_and_check(f
);
436 return log_error_errno(r
, "Failed to write unit file %s: %m", name
);
438 if (flags
& MAKEFS
) {
439 r
= generator_hook_up_mkfs(dest
, what
, where
, fstype
);
444 if (flags
& GROWFS
) {
445 r
= generator_hook_up_growfs(dest
, where
, post
);
450 if (!(flags
& NOAUTO
) && !(flags
& AUTOMOUNT
)) {
451 r
= generator_add_symlink(dest
, post
,
452 (flags
& NOFAIL
) ? "wants" : "requires", name
);
457 if (flags
& AUTOMOUNT
) {
458 r
= unit_name_from_path(where
, ".automount", &automount_name
);
460 return log_error_errno(r
, "Failed to generate unit name: %m");
464 r
= generator_open_unit_file(dest
, "/etc/fstab", automount_name
, &f
);
471 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
474 fprintf(f
, "Before=%s\n", post
);
477 r
= write_after(f
, opts
);
480 r
= write_requires_after(f
, opts
);
483 r
= write_before(f
, opts
);
486 r
= write_requires_mounts_for(f
, opts
);
497 r
= write_idle_timeout(f
, where
, opts
);
501 r
= fflush_and_check(f
);
503 return log_error_errno(r
, "Failed to write unit file %s: %m", automount_name
);
505 r
= generator_add_symlink(dest
, post
,
506 (flags
& NOFAIL
) ? "wants" : "requires", automount_name
);
514 static int parse_fstab(bool initrd
) {
515 _cleanup_endmntent_
FILE *f
= NULL
;
516 const char *fstab_path
;
520 fstab_path
= initrd
? "/sysroot/etc/fstab" : "/etc/fstab";
521 log_debug("Parsing %s...", fstab_path
);
523 f
= setmntent(fstab_path
, "re");
528 return log_error_errno(errno
, "Failed to open %s: %m", fstab_path
);
531 while ((me
= getmntent(f
))) {
532 _cleanup_free_
char *where
= NULL
, *what
= NULL
, *canonical_where
= NULL
;
533 bool makefs
, growfs
, noauto
, nofail
;
536 if (initrd
&& !mount_in_initrd(me
))
539 what
= fstab_node_to_udev_node(me
->mnt_fsname
);
543 if (is_device_path(what
) && path_is_read_only_fs("/sys") > 0) {
544 log_info("Running in a container, ignoring fstab device entry for %s.", what
);
548 where
= strdup(me
->mnt_dir
);
552 if (is_path(where
)) {
553 path_simplify(where
, false);
555 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
556 * mount units, but causes problems since it historically worked to have symlinks in e.g.
557 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
558 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
559 * target is the final directory. */
560 r
= chase_symlinks(where
, initrd
? "/sysroot" : NULL
,
561 CHASE_PREFIX_ROOT
| CHASE_NONEXISTENT
,
563 if (r
< 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */
564 log_debug_errno(r
, "Failed to read symlink target for %s, ignoring: %m", where
);
565 else if (streq(canonical_where
, where
)) /* If it was fully canonicalized, suppress the change */
566 canonical_where
= mfree(canonical_where
);
568 log_debug("Canonicalized what=%s where=%s to %s", what
, where
, canonical_where
);
571 makefs
= fstab_test_option(me
->mnt_opts
, "x-systemd.makefs\0");
572 growfs
= fstab_test_option(me
->mnt_opts
, "x-systemd.growfs\0");
573 noauto
= fstab_test_yes_no_option(me
->mnt_opts
, "noauto\0" "auto\0");
574 nofail
= fstab_test_yes_no_option(me
->mnt_opts
, "nofail\0" "fail\0");
576 log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s",
577 what
, where
, me
->mnt_type
,
578 yes_no(makefs
), yes_no(growfs
),
579 yes_no(noauto
), yes_no(nofail
));
581 if (streq(me
->mnt_type
, "swap"))
582 k
= add_swap(what
, me
,
583 makefs
*MAKEFS
| growfs
*GROWFS
| noauto
*NOAUTO
| nofail
*NOFAIL
);
588 automount
= fstab_test_option(me
->mnt_opts
,
589 "comment=systemd.automount\0"
590 "x-systemd.automount\0");
592 post
= SPECIAL_INITRD_FS_TARGET
;
593 else if (mount_is_network(me
))
594 post
= SPECIAL_REMOTE_FS_TARGET
;
596 post
= SPECIAL_LOCAL_FS_TARGET
;
598 k
= add_mount(arg_dest
,
600 canonical_where
?: where
,
601 canonical_where
? where
: NULL
,
605 makefs
*MAKEFS
| growfs
*GROWFS
| noauto
*NOAUTO
| nofail
*NOFAIL
| automount
*AUTOMOUNT
,
617 static int add_sysroot_mount(void) {
618 _cleanup_free_
char *what
= NULL
;
622 if (isempty(arg_root_what
)) {
623 log_debug("Could not find a root= entry on the kernel command line.");
627 if (streq(arg_root_what
, "gpt-auto")) {
628 /* This is handled by the gpt-auto generator */
629 log_debug("Skipping root directory handling, as gpt-auto was requested.");
633 if (path_equal(arg_root_what
, "/dev/nfs")) {
634 /* This is handled by the kernel or the initrd */
635 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
639 what
= fstab_node_to_udev_node(arg_root_what
);
643 if (!arg_root_options
)
644 opts
= arg_root_rw
> 0 ? "rw" : "ro";
645 else if (arg_root_rw
>= 0 ||
646 !fstab_test_option(arg_root_options
, "ro\0" "rw\0"))
647 opts
= strjoina(arg_root_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
649 opts
= arg_root_options
;
651 log_debug("Found entry what=%s where=/sysroot type=%s", what
, strna(arg_root_fstype
));
653 if (is_device_path(what
)) {
654 r
= generator_write_initrd_root_device_deps(arg_dest
, what
);
659 return add_mount(arg_dest
,
665 is_device_path(what
) ? 1 : 0, /* passno */
666 0, /* makefs off, growfs off, noauto off, nofail off, automount off */
667 SPECIAL_INITRD_ROOT_FS_TARGET
,
671 static int add_sysroot_usr_mount(void) {
672 _cleanup_free_
char *what
= NULL
;
675 if (!arg_usr_what
&& !arg_usr_fstype
&& !arg_usr_options
)
678 if (arg_root_what
&& !arg_usr_what
) {
679 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
680 arg_usr_what
= strdup(arg_root_what
);
685 if (arg_root_fstype
&& !arg_usr_fstype
) {
686 arg_usr_fstype
= strdup(arg_root_fstype
);
691 if (arg_root_options
&& !arg_usr_options
) {
692 arg_usr_options
= strdup(arg_root_options
);
693 if (!arg_usr_options
)
700 what
= fstab_node_to_udev_node(arg_usr_what
);
704 if (!arg_usr_options
)
705 opts
= arg_root_rw
> 0 ? "rw" : "ro";
706 else if (!fstab_test_option(arg_usr_options
, "ro\0" "rw\0"))
707 opts
= strjoina(arg_usr_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
709 opts
= arg_usr_options
;
711 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what
, strna(arg_usr_fstype
));
712 return add_mount(arg_dest
,
718 is_device_path(what
) ? 1 : 0, /* passno */
720 SPECIAL_INITRD_FS_TARGET
,
724 static int add_volatile_root(void) {
725 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
726 * requested, leaving only /usr from the root mount inside. */
728 if (arg_volatile_mode
!= VOLATILE_YES
)
731 return generator_add_symlink(arg_dest
, SPECIAL_INITRD_ROOT_FS_TARGET
, "requires",
732 SYSTEM_DATA_UNIT_PATH
"/" SPECIAL_VOLATILE_ROOT_SERVICE
);
735 static int add_volatile_var(void) {
737 if (arg_volatile_mode
!= VOLATILE_STATE
)
740 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
742 return add_mount(arg_dest_late
,
750 SPECIAL_LOCAL_FS_TARGET
,
754 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
757 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
758 * instance should take precedence. In the case of multiple rootflags=
759 * or usrflags= the arguments should be concatenated */
761 if (STR_IN_SET(key
, "fstab", "rd.fstab")) {
763 r
= value
? parse_boolean(value
) : 1;
765 log_warning("Failed to parse fstab switch %s. Ignoring.", value
);
767 arg_fstab_enabled
= r
;
769 } else if (streq(key
, "root")) {
771 if (proc_cmdline_value_missing(key
, value
))
774 if (free_and_strdup(&arg_root_what
, value
) < 0)
777 } else if (streq(key
, "rootfstype")) {
779 if (proc_cmdline_value_missing(key
, value
))
782 if (free_and_strdup(&arg_root_fstype
, value
) < 0)
785 } else if (streq(key
, "rootflags")) {
787 if (proc_cmdline_value_missing(key
, value
))
790 if (!strextend_with_separator(&arg_root_options
, ",", value
, NULL
))
793 } else if (streq(key
, "roothash")) {
795 if (proc_cmdline_value_missing(key
, value
))
798 if (free_and_strdup(&arg_root_hash
, value
) < 0)
801 } else if (streq(key
, "mount.usr")) {
803 if (proc_cmdline_value_missing(key
, value
))
806 if (free_and_strdup(&arg_usr_what
, value
) < 0)
809 } else if (streq(key
, "mount.usrfstype")) {
811 if (proc_cmdline_value_missing(key
, value
))
814 if (free_and_strdup(&arg_usr_fstype
, value
) < 0)
817 } else if (streq(key
, "mount.usrflags")) {
819 if (proc_cmdline_value_missing(key
, value
))
822 if (!strextend_with_separator(&arg_usr_options
, ",", value
, NULL
))
825 } else if (streq(key
, "rw") && !value
)
827 else if (streq(key
, "ro") && !value
)
829 else if (streq(key
, "systemd.volatile")) {
833 m
= volatile_mode_from_string(value
);
835 log_warning("Failed to parse systemd.volatile= argument: %s", value
);
837 arg_volatile_mode
= m
;
839 arg_volatile_mode
= VOLATILE_YES
;
845 static int determine_root(void) {
846 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
854 arg_root_what
= strdup("/dev/mapper/root");
858 log_info("Using verity root device %s.", arg_root_what
);
863 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
864 int r
, r2
= 0, r3
= 0;
866 assert_se(arg_dest
= dest
);
867 assert_se(arg_dest_late
= dest_late
);
869 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
871 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
873 (void) determine_root();
875 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
877 r
= add_sysroot_mount();
879 r2
= add_sysroot_usr_mount();
881 r3
= add_volatile_root();
883 r
= add_volatile_var();
885 /* Honour /etc/fstab only when that's enabled */
886 if (arg_fstab_enabled
) {
887 /* Parse the local /etc/fstab, possibly from the initrd */
888 r2
= parse_fstab(false);
890 /* If running in the initrd also parse the /etc/fstab from the host */
892 r3
= parse_fstab(true);
894 r3
= generator_enable_remount_fs_service(arg_dest
);
897 return r
< 0 ? r
: r2
< 0 ? r2
: r3
;
900 DEFINE_MAIN_GENERATOR_FUNCTION(run
);