2 This file is part of systemd.
4 Copyright 2014 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/>.
23 #include "alloc-util.h"
28 #include "fstab-util.h"
29 #include "generator.h"
33 #include "path-util.h"
35 #include "string-util.h"
36 #include "time-util.h"
37 #include "unit-name.h"
40 static int write_fsck_sysroot_service(const char *dir
, const char *what
) {
41 _cleanup_free_
char *device
= NULL
, *escaped
= NULL
;
42 _cleanup_fclose_
FILE *f
= NULL
;
46 escaped
= cescape(what
);
50 unit
= strjoina(dir
, "/systemd-fsck-root.service");
51 log_debug("Creating %s", unit
);
53 r
= unit_name_from_path(what
, ".device", &device
);
55 return log_error_errno(r
, "Failed to convert device \"%s\" to unit name: %m", what
);
57 f
= fopen(unit
, "wxe");
59 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
62 "# Automatically generated by %1$s\n\n"
64 "Documentation=man:systemd-fsck-root.service(8)\n"
65 "Description=File System Check on %2$s\n"
66 "DefaultDependencies=no\n"
68 "After=%3$s local-fs-pre.target\n"
69 "Before=shutdown.target\n"
73 "RemainAfterExit=yes\n"
74 "ExecStart=" SYSTEMD_FSCK_PATH
" %4$s\n"
76 program_invocation_short_name
,
81 r
= fflush_and_check(f
);
83 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
88 int generator_write_fsck_deps(
102 if (!is_device_path(what
)) {
103 log_warning("Checking was requested for \"%s\", but it is not a device.", what
);
107 if (!isempty(fstype
) && !streq(fstype
, "auto")) {
108 r
= fsck_exists(fstype
);
110 log_warning_errno(r
, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what
, fstype
);
112 /* treat missing check as essentially OK */
113 log_debug("Checking was requested for %s, but fsck.%s does not exist.", what
, fstype
);
118 if (path_equal(where
, "/")) {
121 lnk
= strjoina(dir
, "/" SPECIAL_LOCAL_FS_TARGET
".wants/systemd-fsck-root.service");
123 mkdir_parents(lnk
, 0755);
124 if (symlink(SYSTEM_DATA_UNIT_PATH
"/systemd-fsck-root.service", lnk
) < 0)
125 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
128 _cleanup_free_
char *_fsck
= NULL
;
131 if (in_initrd() && path_equal(where
, "/sysroot")) {
132 r
= write_fsck_sysroot_service(dir
, what
);
136 fsck
= "systemd-fsck-root.service";
138 r
= unit_name_from_path_instance("systemd-fsck", what
, ".service", &_fsck
);
140 return log_error_errno(r
, "Failed to create fsck service name: %m");
154 int generator_write_timeouts(
161 /* Allow configuration how long we wait for a device that
162 * backs a mount point to show up. This is useful to support
163 * endless device timeouts for devices that show up only after
164 * user input, like crypto devices. */
166 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *timeout
= NULL
;
170 r
= fstab_filter_options(opts
, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
171 NULL
, &timeout
, filtered
);
175 r
= parse_sec(timeout
, &u
);
177 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
181 node
= fstab_node_to_udev_node(what
);
185 r
= unit_name_from_path(node
, ".device", &unit
);
187 return log_error_errno(r
, "Failed to make unit name from path: %m");
189 return write_drop_in_format(dir
, unit
, 50, "device-timeout",
190 "# Automatically generated by %s\n\n"
191 "[Unit]\nJobTimeoutSec=%s",
192 program_invocation_short_name
, timeout
);