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"
29 #include "fstab-util.h"
30 #include "generator.h"
33 #include "mount-setup.h"
34 #include "mount-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "proc-cmdline.h"
39 #include "stat-util.h"
40 #include "string-util.h"
42 #include "unit-name.h"
46 static const char *arg_dest
= "/tmp";
47 static bool arg_fstab_enabled
= true;
48 static char *arg_root_what
= NULL
;
49 static char *arg_root_fstype
= NULL
;
50 static char *arg_root_options
= NULL
;
51 static int arg_root_rw
= -1;
52 static char *arg_usr_what
= NULL
;
53 static char *arg_usr_fstype
= NULL
;
54 static char *arg_usr_options
= NULL
;
62 _cleanup_free_
char *name
= NULL
, *unit
= NULL
, *lnk
= NULL
;
63 _cleanup_fclose_
FILE *f
= NULL
;
69 if (access("/proc/swaps", F_OK
) < 0) {
70 log_info("Swap not supported, ignoring fstab swap entry for %s.", what
);
74 if (detect_container() > 0) {
75 log_info("Running in a container, ignoring fstab swap entry for %s.", what
);
79 r
= unit_name_from_path(what
, ".swap", &name
);
81 return log_error_errno(r
, "Failed to generate unit name: %m");
83 unit
= strjoin(arg_dest
, "/", name
, NULL
);
87 f
= fopen(unit
, "wxe");
90 log_error("Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit
);
92 log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
97 "# Automatically generated by systemd-fstab-generator\n\n"
99 "SourcePath=/etc/fstab\n"
100 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
105 if (!isempty(me
->mnt_opts
) && !streq(me
->mnt_opts
, "defaults"))
106 fprintf(f
, "Options=%s\n", me
->mnt_opts
);
108 r
= fflush_and_check(f
);
110 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
112 /* use what as where, to have a nicer error message */
113 r
= generator_write_timeouts(arg_dest
, what
, what
, me
->mnt_opts
, NULL
);
118 lnk
= strjoin(arg_dest
, "/" SPECIAL_SWAP_TARGET
,
119 nofail
? ".wants/" : ".requires/", name
, NULL
);
123 mkdir_parents_label(lnk
, 0755);
124 if (symlink(unit
, lnk
) < 0)
125 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
131 static bool mount_is_network(struct mntent
*me
) {
134 return fstab_test_option(me
->mnt_opts
, "_netdev\0") ||
135 fstype_is_network(me
->mnt_type
);
138 static bool mount_in_initrd(struct mntent
*me
) {
141 return fstab_test_option(me
->mnt_opts
, "x-initrd.mount\0") ||
142 streq(me
->mnt_dir
, "/usr");
145 static int write_idle_timeout(FILE *f
, const char *where
, const char *opts
) {
146 _cleanup_free_
char *timeout
= NULL
;
147 char timespan
[FORMAT_TIMESPAN_MAX
];
151 r
= fstab_filter_options(opts
, "x-systemd.idle-timeout\0", NULL
, &timeout
, NULL
);
153 return log_warning_errno(r
, "Failed to parse options: %m");
157 r
= parse_sec(timeout
, &u
);
159 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
163 fprintf(f
, "TimeoutIdleSec=%s\n", format_timespan(timespan
, sizeof(timespan
), u
, 0));
168 static int write_requires_after(FILE *f
, const char *opts
) {
169 _cleanup_strv_free_
char **names
= NULL
, **units
= NULL
;
170 _cleanup_free_
char *res
= NULL
;
177 r
= fstab_extract_values(opts
, "x-systemd.requires", &names
);
179 return log_warning_errno(r
, "Failed to parse options: %m");
183 STRV_FOREACH(s
, names
) {
186 r
= unit_name_mangle_with_suffix(*s
, UNIT_NAME_NOGLOB
, ".mount", &x
);
188 return log_error_errno(r
, "Failed to generate unit name: %m");
189 r
= strv_consume(&units
, x
);
195 res
= strv_join(units
, " ");
198 fprintf(f
, "After=%1$s\nRequires=%1$s\n", res
);
204 static int write_requires_mounts_for(FILE *f
, const char *opts
) {
205 _cleanup_strv_free_
char **paths
= NULL
;
206 _cleanup_free_
char *res
= NULL
;
212 r
= fstab_extract_values(opts
, "x-systemd.requires-mounts-for", &paths
);
214 return log_warning_errno(r
, "Failed to parse options: %m");
218 res
= strv_join(paths
, " ");
222 fprintf(f
, "RequiresMountsFor=%s\n", res
);
227 static int add_mount(
237 const char *source
) {
240 *name
= NULL
, *unit
= NULL
, *lnk
= NULL
,
241 *automount_name
= NULL
, *automount_unit
= NULL
,
243 _cleanup_fclose_
FILE *f
= NULL
;
252 if (streq_ptr(fstype
, "autofs"))
255 if (!is_path(where
)) {
256 log_warning("Mount point %s is not a valid path, ignoring.", where
);
260 if (mount_point_is_api(where
) ||
261 mount_point_ignore(where
))
264 if (path_equal(where
, "/")) {
266 log_warning("Ignoring \"noauto\" for root device");
268 log_warning("Ignoring \"nofail\" for root device");
270 log_warning("Ignoring automount option for root device");
272 noauto
= nofail
= automount
= false;
275 r
= unit_name_from_path(where
, ".mount", &name
);
277 return log_error_errno(r
, "Failed to generate unit name: %m");
279 unit
= strjoin(arg_dest
, "/", name
, NULL
);
283 f
= fopen(unit
, "wxe");
286 log_error("Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit
);
288 log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
293 "# Automatically generated by systemd-fstab-generator\n\n"
296 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
299 if (!noauto
&& !nofail
&& !automount
)
300 fprintf(f
, "Before=%s\n", post
);
302 if (!automount
&& opts
) {
303 r
= write_requires_after(f
, opts
);
306 r
= write_requires_mounts_for(f
, opts
);
312 r
= generator_write_fsck_deps(f
, arg_dest
, what
, where
, fstype
);
325 if (!isempty(fstype
) && !streq(fstype
, "auto"))
326 fprintf(f
, "Type=%s\n", fstype
);
328 r
= generator_write_timeouts(arg_dest
, what
, where
, opts
, &filtered
);
332 if (!isempty(filtered
) && !streq(filtered
, "defaults"))
333 fprintf(f
, "Options=%s\n", filtered
);
335 r
= fflush_and_check(f
);
337 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
339 if (!noauto
&& !automount
) {
340 lnk
= strjoin(arg_dest
, "/", post
, nofail
? ".wants/" : ".requires/", name
, NULL
);
344 mkdir_parents_label(lnk
, 0755);
345 if (symlink(unit
, lnk
) < 0)
346 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
350 r
= unit_name_from_path(where
, ".automount", &automount_name
);
352 return log_error_errno(r
, "Failed to generate unit name: %m");
354 automount_unit
= strjoin(arg_dest
, "/", automount_name
, NULL
);
359 f
= fopen(automount_unit
, "wxe");
361 return log_error_errno(errno
, "Failed to create unit file %s: %m", automount_unit
);
364 "# Automatically generated by systemd-fstab-generator\n\n"
367 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
370 fprintf(f
, "Before=%s\n", post
);
373 r
= write_requires_after(f
, opts
);
376 r
= write_requires_mounts_for(f
, opts
);
387 r
= write_idle_timeout(f
, where
, opts
);
391 r
= fflush_and_check(f
);
393 return log_error_errno(r
, "Failed to write unit file %s: %m", automount_unit
);
396 lnk
= strjoin(arg_dest
, "/", post
, nofail
? ".wants/" : ".requires/", automount_name
, NULL
);
400 mkdir_parents_label(lnk
, 0755);
401 if (symlink(automount_unit
, lnk
) < 0)
402 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
408 static int parse_fstab(bool initrd
) {
409 _cleanup_endmntent_
FILE *f
= NULL
;
410 const char *fstab_path
;
414 fstab_path
= initrd
? "/sysroot/etc/fstab" : "/etc/fstab";
415 f
= setmntent(fstab_path
, "re");
420 log_error_errno(errno
, "Failed to open %s: %m", fstab_path
);
424 while ((me
= getmntent(f
))) {
425 _cleanup_free_
char *where
= NULL
, *what
= NULL
;
429 if (initrd
&& !mount_in_initrd(me
))
432 what
= fstab_node_to_udev_node(me
->mnt_fsname
);
436 if (is_device_path(what
) && path_is_read_only_fs("sys") > 0) {
437 log_info("Running in a container, ignoring fstab device entry for %s.", what
);
441 where
= initrd
? strappend("/sysroot/", me
->mnt_dir
) : strdup(me
->mnt_dir
);
446 path_kill_slashes(where
);
448 noauto
= fstab_test_yes_no_option(me
->mnt_opts
, "noauto\0" "auto\0");
449 nofail
= fstab_test_yes_no_option(me
->mnt_opts
, "nofail\0" "fail\0");
450 log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
451 what
, where
, me
->mnt_type
,
452 yes_no(noauto
), yes_no(nofail
));
454 if (streq(me
->mnt_type
, "swap"))
455 k
= add_swap(what
, me
, noauto
, nofail
);
460 automount
= fstab_test_option(me
->mnt_opts
,
461 "comment=systemd.automount\0"
462 "x-systemd.automount\0");
464 post
= SPECIAL_INITRD_FS_TARGET
;
465 else if (mount_is_network(me
))
466 post
= SPECIAL_REMOTE_FS_TARGET
;
468 post
= SPECIAL_LOCAL_FS_TARGET
;
489 static int add_sysroot_mount(void) {
490 _cleanup_free_
char *what
= NULL
;
494 if (isempty(arg_root_what
)) {
495 log_debug("Could not find a root= entry on the kernel command line.");
499 if (streq(arg_root_what
, "gpt-auto")) {
500 /* This is handled by the gpt-auto generator */
501 log_debug("Skipping root directory handling, as gpt-auto was requested.");
505 what
= fstab_node_to_udev_node(arg_root_what
);
509 if (!arg_root_options
)
510 opts
= arg_root_rw
> 0 ? "rw" : "ro";
511 else if (arg_root_rw
>= 0 ||
512 !fstab_test_option(arg_root_options
, "ro\0" "rw\0"))
513 opts
= strjoina(arg_root_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
515 opts
= arg_root_options
;
517 log_debug("Found entry what=%s where=/sysroot type=%s", what
, strna(arg_root_fstype
));
519 if (is_device_path(what
)) {
520 r
= generator_write_initrd_root_device_deps(arg_dest
, what
);
525 return add_mount(what
,
529 is_device_path(what
) ? 1 : 0,
533 SPECIAL_INITRD_ROOT_FS_TARGET
,
537 static int add_sysroot_usr_mount(void) {
538 _cleanup_free_
char *what
= NULL
;
541 if (!arg_usr_what
&& !arg_usr_fstype
&& !arg_usr_options
)
544 if (arg_root_what
&& !arg_usr_what
) {
545 arg_usr_what
= strdup(arg_root_what
);
551 if (arg_root_fstype
&& !arg_usr_fstype
) {
552 arg_usr_fstype
= strdup(arg_root_fstype
);
558 if (arg_root_options
&& !arg_usr_options
) {
559 arg_usr_options
= strdup(arg_root_options
);
561 if (!arg_usr_options
)
568 what
= fstab_node_to_udev_node(arg_usr_what
);
569 if (!path_is_absolute(what
)) {
570 log_debug("Skipping entry what=%s where=/sysroot/usr type=%s", what
, strna(arg_usr_fstype
));
574 if (!arg_usr_options
)
575 opts
= arg_root_rw
> 0 ? "rw" : "ro";
576 else if (!fstab_test_option(arg_usr_options
, "ro\0" "rw\0"))
577 opts
= strjoina(arg_usr_options
, ",", arg_root_rw
> 0 ? "rw" : "ro");
579 opts
= arg_usr_options
;
581 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what
, strna(arg_usr_fstype
));
582 return add_mount(what
,
590 SPECIAL_INITRD_FS_TARGET
,
594 static int parse_proc_cmdline_item(const char *key
, const char *value
) {
597 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
598 * instance should take precedence. In the case of multiple rootflags=
599 * or usrflags= the arguments should be concatenated */
601 if (STR_IN_SET(key
, "fstab", "rd.fstab") && value
) {
603 r
= parse_boolean(value
);
605 log_warning("Failed to parse fstab switch %s. Ignoring.", value
);
607 arg_fstab_enabled
= r
;
609 } else if (streq(key
, "root") && value
) {
611 if (free_and_strdup(&arg_root_what
, value
) < 0)
614 } else if (streq(key
, "rootfstype") && value
) {
616 if (free_and_strdup(&arg_root_fstype
, value
) < 0)
619 } else if (streq(key
, "rootflags") && value
) {
622 o
= arg_root_options
?
623 strjoin(arg_root_options
, ",", value
, NULL
) :
628 free(arg_root_options
);
629 arg_root_options
= o
;
631 } else if (streq(key
, "mount.usr") && value
) {
633 if (free_and_strdup(&arg_usr_what
, value
) < 0)
636 } else if (streq(key
, "mount.usrfstype") && value
) {
638 if (free_and_strdup(&arg_usr_fstype
, value
) < 0)
641 } else if (streq(key
, "mount.usrflags") && value
) {
644 o
= arg_usr_options
?
645 strjoin(arg_usr_options
, ",", value
, NULL
) :
650 free(arg_usr_options
);
653 } else if (streq(key
, "rw") && !value
)
655 else if (streq(key
, "ro") && !value
)
661 int main(int argc
, char *argv
[]) {
664 if (argc
> 1 && argc
!= 4) {
665 log_error("This program takes three or no arguments.");
672 log_set_target(LOG_TARGET_SAFE
);
673 log_parse_environment();
678 r
= parse_proc_cmdline(parse_proc_cmdline_item
);
680 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
682 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
684 r
= add_sysroot_mount();
686 r
= add_sysroot_usr_mount();
689 /* Honour /etc/fstab only when that's enabled */
690 if (arg_fstab_enabled
) {
693 log_debug("Parsing /etc/fstab");
695 /* Parse the local /etc/fstab, possibly from the initrd */
696 k
= parse_fstab(false);
700 /* If running in the initrd also parse the /etc/fstab from the host */
702 log_debug("Parsing /sysroot/etc/fstab");
704 k
= parse_fstab(true);
711 free(arg_root_fstype
);
712 free(arg_root_options
);
715 free(arg_usr_fstype
);
716 free(arg_usr_options
);
718 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;