]>
Commit | Line | Data |
---|---|---|
e48fdd84 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <unistd.h> | |
23 | ||
b5efdb8a | 24 | #include "alloc-util.h" |
4f5dd394 LP |
25 | #include "dropin.h" |
26 | #include "escape.h" | |
3ffd4af2 | 27 | #include "fd-util.h" |
4f5dd394 LP |
28 | #include "fileio.h" |
29 | #include "fstab-util.h" | |
3ffd4af2 | 30 | #include "generator.h" |
e48fdd84 | 31 | #include "mkdir.h" |
4349cd7c | 32 | #include "mount-util.h" |
4f5dd394 LP |
33 | #include "path-util.h" |
34 | #include "special.h" | |
07630cea | 35 | #include "string-util.h" |
e48fdd84 | 36 | #include "unit-name.h" |
4f5dd394 | 37 | #include "util.h" |
e48fdd84 | 38 | |
4dda4e63 | 39 | static int write_fsck_sysroot_service(const char *dir, const char *what) { |
85eca92e | 40 | _cleanup_free_ char *device = NULL, *escaped = NULL; |
4dda4e63 | 41 | _cleanup_fclose_ FILE *f = NULL; |
85eca92e | 42 | const char *unit; |
4dda4e63 ZJS |
43 | int r; |
44 | ||
fa05e972 AB |
45 | escaped = cescape(what); |
46 | if (!escaped) | |
47 | return log_oom(); | |
48 | ||
4dda4e63 ZJS |
49 | unit = strjoina(dir, "/systemd-fsck-root.service"); |
50 | log_debug("Creating %s", unit); | |
51 | ||
52 | r = unit_name_from_path(what, ".device", &device); | |
53 | if (r < 0) | |
54 | return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what); | |
55 | ||
56 | f = fopen(unit, "wxe"); | |
57 | if (!f) | |
58 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
59 | ||
60 | fprintf(f, | |
61 | "# Automatically generated by %1$s\n\n" | |
62 | "[Unit]\n" | |
63 | "Documentation=man:systemd-fsck-root.service(8)\n" | |
64 | "Description=File System Check on %2$s\n" | |
65 | "DefaultDependencies=no\n" | |
66 | "BindsTo=%3$s\n" | |
5fd534d9 | 67 | "After=%3$s local-fs-pre.target\n" |
4dda4e63 ZJS |
68 | "Before=shutdown.target\n" |
69 | "\n" | |
70 | "[Service]\n" | |
71 | "Type=oneshot\n" | |
72 | "RemainAfterExit=yes\n" | |
fa05e972 | 73 | "ExecStart=" SYSTEMD_FSCK_PATH " %4$s\n" |
4dda4e63 ZJS |
74 | "TimeoutSec=0\n", |
75 | program_invocation_short_name, | |
76 | what, | |
fa05e972 AB |
77 | device, |
78 | escaped); | |
4dda4e63 | 79 | |
2929b4a6 LP |
80 | r = fflush_and_check(f); |
81 | if (r < 0) | |
82 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
4dda4e63 ZJS |
83 | |
84 | return 0; | |
85 | } | |
86 | ||
e48fdd84 LP |
87 | int generator_write_fsck_deps( |
88 | FILE *f, | |
2e852276 | 89 | const char *dir, |
e48fdd84 LP |
90 | const char *what, |
91 | const char *where, | |
6db615c1 | 92 | const char *fstype) { |
e48fdd84 | 93 | |
7410616c LP |
94 | int r; |
95 | ||
e48fdd84 | 96 | assert(f); |
2e852276 | 97 | assert(dir); |
6db615c1 LP |
98 | assert(what); |
99 | assert(where); | |
e48fdd84 LP |
100 | |
101 | if (!is_device_path(what)) { | |
102 | log_warning("Checking was requested for \"%s\", but it is not a device.", what); | |
103 | return 0; | |
104 | } | |
105 | ||
6db615c1 | 106 | if (!isempty(fstype) && !streq(fstype, "auto")) { |
eb66db55 | 107 | r = fsck_exists(fstype); |
85eca92e LP |
108 | if (r < 0) |
109 | log_warning_errno(r, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what, fstype); | |
110 | else if (r == 0) { | |
e48fdd84 | 111 | /* treat missing check as essentially OK */ |
85eca92e | 112 | log_debug("Checking was requested for %s, but fsck.%s does not exist.", what, fstype); |
571d0134 | 113 | return 0; |
85eca92e | 114 | } |
e48fdd84 LP |
115 | } |
116 | ||
2e852276 | 117 | if (path_equal(where, "/")) { |
85eca92e | 118 | const char *lnk; |
e48fdd84 | 119 | |
2e852276 | 120 | lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service"); |
e48fdd84 LP |
121 | |
122 | mkdir_parents(lnk, 0755); | |
4a62c710 MS |
123 | if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0) |
124 | return log_error_errno(errno, "Failed to create symlink %s: %m", lnk); | |
e48fdd84 LP |
125 | |
126 | } else { | |
4dda4e63 ZJS |
127 | _cleanup_free_ char *_fsck = NULL; |
128 | const char *fsck; | |
129 | ||
130 | if (in_initrd() && path_equal(where, "/sysroot")) { | |
131 | r = write_fsck_sysroot_service(dir, what); | |
132 | if (r < 0) | |
133 | return r; | |
134 | ||
135 | fsck = "systemd-fsck-root.service"; | |
136 | } else { | |
137 | r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck); | |
138 | if (r < 0) | |
139 | return log_error_errno(r, "Failed to create fsck service name: %m"); | |
140 | ||
141 | fsck = _fsck; | |
142 | } | |
e48fdd84 LP |
143 | |
144 | fprintf(f, | |
f32b43bd | 145 | "Requires=%1$s\n" |
2e852276 | 146 | "After=%1$s\n", |
e48fdd84 LP |
147 | fsck); |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
29686440 | 152 | |
2e852276 ZJS |
153 | int generator_write_timeouts( |
154 | const char *dir, | |
155 | const char *what, | |
156 | const char *where, | |
157 | const char *opts, | |
158 | char **filtered) { | |
29686440 ZJS |
159 | |
160 | /* Allow configuration how long we wait for a device that | |
161 | * backs a mount point to show up. This is useful to support | |
162 | * endless device timeouts for devices that show up only after | |
163 | * user input, like crypto devices. */ | |
164 | ||
d15d0333 | 165 | _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL; |
29686440 ZJS |
166 | usec_t u; |
167 | int r; | |
29686440 | 168 | |
d15d0333 ZJS |
169 | r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0", |
170 | NULL, &timeout, filtered); | |
171 | if (r <= 0) | |
172 | return r; | |
b3208b66 | 173 | |
d15d0333 | 174 | r = parse_sec(timeout, &u); |
29686440 | 175 | if (r < 0) { |
7410616c | 176 | log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout); |
29686440 ZJS |
177 | return 0; |
178 | } | |
179 | ||
180 | node = fstab_node_to_udev_node(what); | |
181 | if (!node) | |
182 | return log_oom(); | |
183 | ||
7410616c LP |
184 | r = unit_name_from_path(node, ".device", &unit); |
185 | if (r < 0) | |
186 | return log_error_errno(r, "Failed to make unit name from path: %m"); | |
29686440 | 187 | |
8eea8687 ZJS |
188 | return write_drop_in_format(dir, unit, 50, "device-timeout", |
189 | "# Automatically generated by %s\n\n" | |
de48d5ab | 190 | "[Unit]\nJobTimeoutSec=" USEC_FMT, |
8eea8687 ZJS |
191 | program_invocation_short_name, |
192 | u / USEC_PER_SEC); | |
29686440 | 193 | } |