1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "fstab-util.h"
29 #include "generator.h"
31 #include "mount-util.h"
32 #include "path-util.h"
34 #include "string-util.h"
35 #include "unit-name.h"
38 static int write_fsck_sysroot_service(const char *dir
, const char *what
) {
39 _cleanup_free_
char *device
= NULL
, *escaped
= NULL
;
40 _cleanup_fclose_
FILE *f
= NULL
;
44 escaped
= cescape(what
);
48 unit
= strjoina(dir
, "/systemd-fsck-root.service");
49 log_debug("Creating %s", unit
);
51 r
= unit_name_from_path(what
, ".device", &device
);
53 return log_error_errno(r
, "Failed to convert device \"%s\" to unit name: %m", what
);
55 f
= fopen(unit
, "wxe");
57 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
60 "# Automatically generated by %1$s\n\n"
62 "Documentation=man:systemd-fsck-root.service(8)\n"
63 "Description=File System Check on %2$s\n"
64 "DefaultDependencies=no\n"
67 "Before=shutdown.target\n"
71 "RemainAfterExit=yes\n"
72 "ExecStart=" SYSTEMD_FSCK_PATH
" %4$s\n"
74 program_invocation_short_name
,
79 r
= fflush_and_check(f
);
81 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
86 int generator_write_fsck_deps(
100 if (!is_device_path(what
)) {
101 log_warning("Checking was requested for \"%s\", but it is not a device.", what
);
105 if (!isempty(fstype
) && !streq(fstype
, "auto")) {
106 r
= fsck_exists(fstype
);
108 log_warning_errno(r
, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what
, fstype
);
110 /* treat missing check as essentially OK */
111 log_debug("Checking was requested for %s, but fsck.%s does not exist.", what
, fstype
);
116 if (path_equal(where
, "/")) {
119 lnk
= strjoina(dir
, "/" SPECIAL_LOCAL_FS_TARGET
".wants/systemd-fsck-root.service");
121 mkdir_parents(lnk
, 0755);
122 if (symlink(SYSTEM_DATA_UNIT_PATH
"/systemd-fsck-root.service", lnk
) < 0)
123 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
126 _cleanup_free_
char *_fsck
= NULL
;
129 if (in_initrd() && path_equal(where
, "/sysroot")) {
130 r
= write_fsck_sysroot_service(dir
, what
);
134 fsck
= "systemd-fsck-root.service";
136 r
= unit_name_from_path_instance("systemd-fsck", what
, ".service", &_fsck
);
138 return log_error_errno(r
, "Failed to create fsck service name: %m");
144 "RequiresOverridable=%1$s\n"
152 int generator_write_timeouts(
159 /* Allow configuration how long we wait for a device that
160 * backs a mount point to show up. This is useful to support
161 * endless device timeouts for devices that show up only after
162 * user input, like crypto devices. */
164 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *timeout
= NULL
;
168 r
= fstab_filter_options(opts
, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
169 NULL
, &timeout
, filtered
);
173 r
= parse_sec(timeout
, &u
);
175 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
179 node
= fstab_node_to_udev_node(what
);
183 r
= unit_name_from_path(node
, ".device", &unit
);
185 return log_error_errno(r
, "Failed to make unit name from path: %m");
187 return write_drop_in_format(dir
, unit
, 50, "device-timeout",
188 "# Automatically generated by %s\n\n"
189 "[Unit]\nJobTimeoutSec=" USEC_FMT
,
190 program_invocation_short_name
,