2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
30 #include "fstab-util.h"
31 #include "generator.h"
34 #include "mount-setup.h"
35 #include "mount-util.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "proc-cmdline.h"
40 #include "stat-util.h"
41 #include "string-util.h"
43 #include "unit-name.h"
46 #include "volatile-util.h"
48 static const char *arg_dest
= "/tmp";
49 static const char *arg_dest_late
= "/tmp";
50 static bool arg_fstab_enabled
= true;
51 static char *arg_root_what
= NULL
;
52 static char *arg_root_fstype
= NULL
;
53 static char *arg_root_options
= NULL
;
54 static char *arg_root_hash
= NULL
;
55 static int arg_root_rw
= -1;
56 static char *arg_usr_what
= NULL
;
57 static char *arg_usr_fstype
= NULL
;
58 static char *arg_usr_options
= NULL
;
59 static VolatileMode arg_volatile_mode
= _VOLATILE_MODE_INVALID
;
61 static int write_options(FILE *f
, const char *options
) {
62 _cleanup_free_
char *o
= NULL
;
67 if (streq(options
, "defaults"))
70 o
= strreplace(options
, "%", "%%");
74 fprintf(f
, "Options=%s\n", o
);
78 static int write_what(FILE *f
, const char *what
) {
79 _cleanup_free_
char *w
= NULL
;
81 w
= strreplace(what
, "%", "%%");
85 fprintf(f
, "What=%s\n", w
);
95 _cleanup_free_
char *name
= NULL
, *unit
= NULL
;
96 _cleanup_fclose_
FILE *f
= NULL
;
102 if (access("/proc/swaps", F_OK
) < 0) {
103 log_info("Swap not supported, ignoring fstab swap entry for %s.", what
);
107 if (detect_container() > 0) {
108 log_info("Running in a container, ignoring fstab swap entry for %s.", what
);
112 r
= unit_name_from_path(what
, ".swap", &name
);
114 return log_error_errno(r
, "Failed to generate unit name: %m");
116 unit
= strjoin(arg_dest
, "/", name
);
120 f
= fopen(unit
, "wxe");
122 return log_error_errno(errno
,
124 "Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
125 "Failed to create unit file %s: %m",
128 fputs("# Automatically generated by systemd-fstab-generator\n\n"
130 "SourcePath=/etc/fstab\n"
131 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
134 r
= write_what(f
, what
);
138 r
= write_options(f
, me
->mnt_opts
);
142 r
= fflush_and_check(f
);
144 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
146 /* use what as where, to have a nicer error message */
147 r
= generator_write_timeouts(arg_dest
, what
, what
, me
->mnt_opts
, NULL
);
152 r
= generator_add_symlink(arg_dest
, SPECIAL_SWAP_TARGET
,
153 nofail
? "wants" : "requires", name
);
161 static bool mount_is_network(struct mntent
*me
) {
164 return fstab_test_option(me
->mnt_opts
, "_netdev\0") ||
165 fstype_is_network(me
->mnt_type
);
168 static bool mount_in_initrd(struct mntent
*me
) {
171 return fstab_test_option(me
->mnt_opts
, "x-initrd.mount\0") ||
172 streq(me
->mnt_dir
, "/usr");
175 static int write_timeout(FILE *f
, const char *where
, const char *opts
,
176 const char *filter
, const char *variable
) {
177 _cleanup_free_
char *timeout
= NULL
;
178 char timespan
[FORMAT_TIMESPAN_MAX
];
182 r
= fstab_filter_options(opts
, filter
, NULL
, &timeout
, NULL
);
184 return log_warning_errno(r
, "Failed to parse options: %m");
188 r
= parse_sec_fix_0(timeout
, &u
);
190 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
194 fprintf(f
, "%s=%s\n", variable
, format_timespan(timespan
, sizeof(timespan
), u
, 0));
199 static int write_idle_timeout(FILE *f
, const char *where
, const char *opts
) {
200 return write_timeout(f
, where
, opts
,
201 "x-systemd.idle-timeout\0", "TimeoutIdleSec");
204 static int write_mount_timeout(FILE *f
, const char *where
, const char *opts
) {
205 return write_timeout(f
, where
, opts
,
206 "x-systemd.mount-timeout\0", "TimeoutSec");
209 static int write_dependency(FILE *f
, const char *opts
,
210 const char *filter
, const char *format
) {
211 _cleanup_strv_free_
char **names
= NULL
, **units
= NULL
;
212 _cleanup_free_
char *res
= NULL
;
219 r
= fstab_extract_values(opts
, filter
, &names
);
221 return log_warning_errno(r
, "Failed to parse options: %m");
225 STRV_FOREACH(s
, names
) {
228 r
= unit_name_mangle_with_suffix(*s
, UNIT_NAME_NOGLOB
, ".mount", &x
);
230 return log_error_errno(r
, "Failed to generate unit name: %m");
231 r
= strv_consume(&units
, x
);
237 res
= strv_join(units
, " ");
240 #pragma GCC diagnostic push
241 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
242 fprintf(f
, format
, res
);
243 #pragma GCC diagnostic pop
249 static int write_after(FILE *f
, const char *opts
) {
250 return write_dependency(f
, opts
, "x-systemd.after", "After=%1$s\n");
253 static int write_requires_after(FILE *f
, const char *opts
) {
254 return write_dependency(f
, opts
,
255 "x-systemd.requires", "After=%1$s\nRequires=%1$s\n");
258 static int write_before(FILE *f
, const char *opts
) {
259 return write_dependency(f
, opts
,
260 "x-systemd.before", "Before=%1$s\n");
263 static int write_requires_mounts_for(FILE *f
, const char *opts
) {
264 _cleanup_strv_free_
char **paths
= NULL
;
265 _cleanup_free_
char *res
= NULL
;
271 r
= fstab_extract_values(opts
, "x-systemd.requires-mounts-for", &paths
);
273 return log_warning_errno(r
, "Failed to parse options: %m");
277 res
= strv_join(paths
, " ");
281 fprintf(f
, "RequiresMountsFor=%s\n", res
);
286 static int add_mount(
290 const char *original_where
,
298 const char *source
) {
301 *name
= NULL
, *unit
= NULL
,
302 *automount_name
= NULL
, *automount_unit
= NULL
,
304 _cleanup_fclose_
FILE *f
= NULL
;
313 if (streq_ptr(fstype
, "autofs"))
316 if (!is_path(where
)) {
317 log_warning("Mount point %s is not a valid path, ignoring.", where
);
321 if (mount_point_is_api(where
) ||
322 mount_point_ignore(where
))
325 if (path_equal(where
, "/")) {
327 log_warning("Ignoring \"noauto\" for root device");
329 log_warning("Ignoring \"nofail\" for root device");
331 log_warning("Ignoring automount option for root device");
333 noauto
= nofail
= automount
= false;
336 r
= unit_name_from_path(where
, ".mount", &name
);
338 return log_error_errno(r
, "Failed to generate unit name: %m");
340 unit
= strjoin(dest
, "/", name
);
344 f
= fopen(unit
, "wxe");
346 return log_error_errno(errno
,
348 "Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
349 "Failed to create unit file %s: %m",
353 "# Automatically generated by systemd-fstab-generator\n\n"
356 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
359 if (STRPTR_IN_SET(fstype
, "nfs", "nfs4") && !automount
&&
360 fstab_test_yes_no_option(opts
, "bg\0" "fg\0")) {
361 /* The default retry timeout that mount.nfs uses for 'bg' mounts
362 * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts.
363 * As we are making 'bg' mounts look like an 'fg' mount to
364 * mount.nfs (so systemd can manage the job-control aspects of 'bg'),
365 * we need to explicitly preserve that default, and also ensure
366 * the systemd mount-timeout doesn't interfere.
367 * By placing these options first, they can be over-ridden by
368 * settings in /etc/fstab. */
369 opts
= strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts
, ",fg");
373 if (!nofail
&& !automount
)
374 fprintf(f
, "Before=%s\n", post
);
376 if (!automount
&& opts
) {
377 r
= write_after(f
, opts
);
380 r
= write_requires_after(f
, opts
);
383 r
= write_before(f
, opts
);
386 r
= write_requires_mounts_for(f
, opts
);
392 r
= generator_write_fsck_deps(f
, dest
, what
, where
, fstype
);
397 fprintf(f
, "\n[Mount]\n");
399 fprintf(f
, "# Canonicalized from %s\n", original_where
);
400 fprintf(f
, "Where=%s\n", where
);
402 r
= write_what(f
, what
);
406 if (!isempty(fstype
) && !streq(fstype
, "auto"))
407 fprintf(f
, "Type=%s\n", fstype
);
409 r
= generator_write_timeouts(dest
, what
, where
, opts
, &filtered
);
413 r
= generator_write_device_deps(dest
, what
, where
, opts
);
417 r
= write_mount_timeout(f
, where
, opts
);
421 r
= write_options(f
, filtered
);
425 r
= fflush_and_check(f
);
427 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
429 if (!noauto
&& !automount
) {
430 r
= generator_add_symlink(dest
, post
,
431 nofail
? "wants" : "requires", name
);
437 r
= unit_name_from_path(where
, ".automount", &automount_name
);
439 return log_error_errno(r
, "Failed to generate unit name: %m");
441 automount_unit
= strjoin(dest
, "/", automount_name
);
446 f
= fopen(automount_unit
, "wxe");
448 return log_error_errno(errno
, "Failed to create unit file %s: %m", automount_unit
);
451 "# Automatically generated by systemd-fstab-generator\n\n"
454 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
457 fprintf(f
, "Before=%s\n", post
);
460 r
= write_after(f
, opts
);
463 r
= write_requires_after(f
, opts
);
466 r
= write_before(f
, opts
);
469 r
= write_requires_mounts_for(f
, opts
);
480 r
= write_idle_timeout(f
, where
, opts
);
484 r
= fflush_and_check(f
);
486 return log_error_errno(r
, "Failed to write unit file %s: %m", automount_unit
);
488 r
= generator_add_symlink(dest
, post
,
489 nofail
? "wants" : "requires", automount_name
);
497 static int parse_fstab(bool initrd
) {
498 _cleanup_endmntent_
FILE *f
= NULL
;
499 const char *fstab_path
;
503 fstab_path
= initrd
? "/sysroot/etc/fstab" : "/etc/fstab";
504 f
= setmntent(fstab_path
, "re");
509 return log_error_errno(errno
, "Failed to open %s: %m", fstab_path
);
512 while ((me
= getmntent(f
))) {
513 _cleanup_free_
char *where
= NULL
, *what
= NULL
, *canonical_where
= NULL
;
517 if (initrd
&& !mount_in_initrd(me
))
520 what
= fstab_node_to_udev_node(me
->mnt_fsname
);
524 if (is_device_path(what
) && path_is_read_only_fs("sys") > 0) {
525 log_info("Running in a container, ignoring fstab device entry for %s.", what
);
529 where
= strdup(me
->mnt_dir
);
533 if (is_path(where
)) {
534 path_kill_slashes(where
);
535 /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for
536 * mount units, but causes problems since it historically worked to have symlinks in e.g.
537 * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case
538 * where a symlink refers to another mount target; this works assuming the sub-mountpoint
539 * target is the final directory.
541 r
= chase_symlinks(where
, initrd
? "/sysroot" : NULL
,
542 CHASE_PREFIX_ROOT
| CHASE_NONEXISTENT
,
545 /* In this case for now we continue on as if it wasn't a symlink */
546 log_warning_errno(r
, "Failed to read symlink target for %s: %m", where
);
548 if (streq(canonical_where
, where
))
549 canonical_where
= mfree(canonical_where
);
551 log_debug("Canonicalized what=%s where=%s to %s",
552 what
, where
, canonical_where
);
556 noauto
= fstab_test_yes_no_option(me
->mnt_opts
, "noauto\0" "auto\0");
557 nofail
= fstab_test_yes_no_option(me
->mnt_opts
, "nofail\0" "fail\0");
558 log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
559 what
, where
, me
->mnt_type
,
560 yes_no(noauto
), yes_no(nofail
));
562 if (streq(me
->mnt_type
, "swap"))
563 k
= add_swap(what
, me
, noauto
, nofail
);
568 automount
= fstab_test_option(me
->mnt_opts
,
569 "comment=systemd.automount\0"
570 "x-systemd.automount\0");
572 post
= SPECIAL_INITRD_FS_TARGET
;
573 else if (mount_is_network(me
))
574 post
= SPECIAL_REMOTE_FS_TARGET
;
576 post
= SPECIAL_LOCAL_FS_TARGET
;
578 k
= add_mount(arg_dest
,
580 canonical_where
?: where
,
581 canonical_where
? where
: NULL
,
599 static int add_sysroot_mount(void) {
600 _cleanup_free_
char *what
= NULL
;
604 if (isempty(arg_root_what
)) {
605 log_debug("Could not find a root= entry on the kernel command line.");
609 if (streq(arg_root_what
, "gpt-auto")) {
610 /* This is handled by the gpt-auto generator */
611 log_debug("Skipping root directory handling, as gpt-auto was requested.");
615 if (path_equal(arg_root_what
, "/dev/nfs")) {
616 /* This is handled by the kernel or the initrd */
617 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
621 what
= fstab_node_to_udev_node(arg_root_what
);
625 if (!arg_root_options
)
626 opts
= arg_root_rw
> 0 ? "rw" : "ro";
627 else if (arg_root_rw
>= 0 ||
628 !fstab_test_option(arg_root_options
, "ro\0" "rw\0"))
629 opts
= strjoina(arg_root_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
631 opts
= arg_root_options
;
633 log_debug("Found entry what=%s where=/sysroot type=%s", what
, strna(arg_root_fstype
));
635 if (is_device_path(what
)) {
636 r
= generator_write_initrd_root_device_deps(arg_dest
, what
);
641 return add_mount(arg_dest
,
647 is_device_path(what
) ? 1 : 0, /* passno */
648 false, /* noauto off */
649 false, /* nofail off */
650 false, /* automount off */
651 SPECIAL_INITRD_ROOT_FS_TARGET
,
655 static int add_sysroot_usr_mount(void) {
656 _cleanup_free_
char *what
= NULL
;
659 if (!arg_usr_what
&& !arg_usr_fstype
&& !arg_usr_options
)
662 if (arg_root_what
&& !arg_usr_what
) {
663 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
664 arg_usr_what
= strdup(arg_root_what
);
669 if (arg_root_fstype
&& !arg_usr_fstype
) {
670 arg_usr_fstype
= strdup(arg_root_fstype
);
675 if (arg_root_options
&& !arg_usr_options
) {
676 arg_usr_options
= strdup(arg_root_options
);
677 if (!arg_usr_options
)
684 what
= fstab_node_to_udev_node(arg_usr_what
);
688 if (!arg_usr_options
)
689 opts
= arg_root_rw
> 0 ? "rw" : "ro";
690 else if (!fstab_test_option(arg_usr_options
, "ro\0" "rw\0"))
691 opts
= strjoina(arg_usr_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
693 opts
= arg_usr_options
;
695 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what
, strna(arg_usr_fstype
));
696 return add_mount(arg_dest
,
702 is_device_path(what
) ? 1 : 0, /* passno */
703 false, /* noauto off */
704 false, /* nofail off */
705 false, /* automount off */
706 SPECIAL_INITRD_FS_TARGET
,
710 static int add_volatile_root(void) {
711 const char *from
, *to
;
713 if (arg_volatile_mode
!= VOLATILE_YES
)
716 /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is
717 * requested, leaving only /usr from the root mount inside. */
719 from
= strjoina(SYSTEM_DATA_UNIT_PATH
"/systemd-volatile-root.service");
720 to
= strjoina(arg_dest
, "/" SPECIAL_INITRD_ROOT_FS_TARGET
, ".requires/systemd-volatile-root.service");
722 (void) mkdir_parents(to
, 0755);
724 if (symlink(from
, to
) < 0)
725 return log_error_errno(errno
, "Failed to hook in volatile remount service: %m");
730 static int add_volatile_var(void) {
732 if (arg_volatile_mode
!= VOLATILE_STATE
)
735 /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */
737 return add_mount(arg_dest_late
,
747 SPECIAL_LOCAL_FS_TARGET
,
751 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
754 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
755 * instance should take precedence. In the case of multiple rootflags=
756 * or usrflags= the arguments should be concatenated */
758 if (STR_IN_SET(key
, "fstab", "rd.fstab")) {
760 r
= value
? parse_boolean(value
) : 1;
762 log_warning("Failed to parse fstab switch %s. Ignoring.", value
);
764 arg_fstab_enabled
= r
;
766 } else if (streq(key
, "root")) {
768 if (proc_cmdline_value_missing(key
, value
))
771 if (free_and_strdup(&arg_root_what
, value
) < 0)
774 } else if (streq(key
, "rootfstype")) {
776 if (proc_cmdline_value_missing(key
, value
))
779 if (free_and_strdup(&arg_root_fstype
, value
) < 0)
782 } else if (streq(key
, "rootflags")) {
785 if (proc_cmdline_value_missing(key
, value
))
788 o
= arg_root_options
?
789 strjoin(arg_root_options
, ",", value
) :
794 free(arg_root_options
);
795 arg_root_options
= o
;
796 } else if (streq(key
, "roothash")) {
798 if (proc_cmdline_value_missing(key
, value
))
801 if (free_and_strdup(&arg_root_hash
, value
) < 0)
804 } else if (streq(key
, "mount.usr")) {
806 if (proc_cmdline_value_missing(key
, value
))
809 if (free_and_strdup(&arg_usr_what
, value
) < 0)
812 } else if (streq(key
, "mount.usrfstype")) {
814 if (proc_cmdline_value_missing(key
, value
))
817 if (free_and_strdup(&arg_usr_fstype
, value
) < 0)
820 } else if (streq(key
, "mount.usrflags")) {
823 if (proc_cmdline_value_missing(key
, value
))
826 o
= arg_usr_options
?
827 strjoin(arg_usr_options
, ",", value
) :
832 free(arg_usr_options
);
835 } else if (streq(key
, "rw") && !value
)
837 else if (streq(key
, "ro") && !value
)
839 else if (streq(key
, "systemd.volatile")) {
843 m
= volatile_mode_from_string(value
);
845 log_warning("Failed to parse systemd.volatile= argument: %s", value
);
847 arg_volatile_mode
= m
;
849 arg_volatile_mode
= VOLATILE_YES
;
855 static int determine_root(void) {
856 /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */
864 arg_root_what
= strdup("/dev/mapper/root");
868 log_info("Using verity root device %s.", arg_root_what
);
873 int main(int argc
, char *argv
[]) {
876 if (argc
> 1 && argc
!= 4) {
877 log_error("This program takes three or no arguments.");
884 arg_dest_late
= argv
[3];
886 log_set_target(LOG_TARGET_SAFE
);
887 log_parse_environment();
892 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
894 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
896 (void) determine_root();
898 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
902 r
= add_sysroot_mount();
904 k
= add_sysroot_usr_mount();
908 k
= add_volatile_root();
912 r
= add_volatile_var();
914 /* Honour /etc/fstab only when that's enabled */
915 if (arg_fstab_enabled
) {
918 log_debug("Parsing /etc/fstab");
920 /* Parse the local /etc/fstab, possibly from the initrd */
921 k
= parse_fstab(false);
925 /* If running in the initrd also parse the /etc/fstab from the host */
927 log_debug("Parsing /sysroot/etc/fstab");
929 k
= parse_fstab(true);
936 free(arg_root_fstype
);
937 free(arg_root_options
);
941 free(arg_usr_fstype
);
942 free(arg_usr_options
);
944 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;