]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3d20ed6d | 2 | /*** |
96b2fb93 | 3 | Copyright © 2014 Holger Hans Peter Freyther |
3d20ed6d LP |
4 | ***/ |
5 | ||
3d20ed6d | 6 | #include <errno.h> |
a84f5192 | 7 | #include <fcntl.h> |
cf0fbc49 | 8 | #include <stdbool.h> |
27d340c7 | 9 | #include <sys/file.h> |
96d9117a | 10 | #include <sys/prctl.h> |
cf0fbc49 TA |
11 | #include <sys/stat.h> |
12 | #include <unistd.h> | |
3d20ed6d | 13 | |
0c842e0a | 14 | #include "sd-bus.h" |
9102fdc5 | 15 | #include "sd-device.h" |
3d20ed6d | 16 | |
b5efdb8a | 17 | #include "alloc-util.h" |
96aad8d1 | 18 | #include "bus-common-errors.h" |
3ffd4af2 LP |
19 | #include "bus-error.h" |
20 | #include "bus-util.h" | |
9102fdc5 | 21 | #include "device-util.h" |
3ffd4af2 | 22 | #include "fd-util.h" |
f4f15635 | 23 | #include "fs-util.h" |
b67f05da | 24 | #include "fsck-util.h" |
5e332028 | 25 | #include "main-func.h" |
6bedfcbb | 26 | #include "parse-util.h" |
eb66db55 | 27 | #include "path-util.h" |
4e731273 | 28 | #include "proc-cmdline.h" |
3ffd4af2 | 29 | #include "process-util.h" |
595225af | 30 | #include "rlimit-util.h" |
3ffd4af2 | 31 | #include "signal-util.h" |
ac6e2f0d | 32 | #include "socket-util.h" |
3ffd4af2 | 33 | #include "special.h" |
15a5e950 | 34 | #include "stdio-util.h" |
3d20ed6d LP |
35 | |
36 | static bool arg_skip = false; | |
37 | static bool arg_force = false; | |
96d9117a | 38 | static bool arg_show_progress = false; |
f1f0198c | 39 | static const char *arg_repair = "-a"; |
3d20ed6d | 40 | |
48db40b3 | 41 | static void start_target(const char *target, const char *mode) { |
4afd3348 LP |
42 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
43 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
0c842e0a | 44 | int r; |
3d20ed6d LP |
45 | |
46 | assert(target); | |
47 | ||
266f3e26 | 48 | r = bus_connect_system_systemd(&bus); |
0c842e0a | 49 | if (r < 0) { |
da927ba9 | 50 | log_error_errno(r, "Failed to get D-Bus connection: %m"); |
0c842e0a | 51 | return; |
3d20ed6d LP |
52 | } |
53 | ||
8e34fdbd | 54 | log_info("Requesting %s/start/%s", target, mode); |
3d20ed6d | 55 | |
640dc2f3 | 56 | /* Start this unit only if we can replace basic.target with it */ |
0c842e0a TG |
57 | r = sd_bus_call_method(bus, |
58 | "org.freedesktop.systemd1", | |
59 | "/org/freedesktop/systemd1", | |
60 | "org.freedesktop.systemd1.Manager", | |
61 | "StartUnitReplace", | |
62 | &error, | |
63 | NULL, | |
48db40b3 | 64 | "sss", "basic.target", target, mode); |
3d20ed6d | 65 | |
5220a6f3 LP |
66 | /* Don't print a warning if we aren't called during startup */ |
67 | if (r < 0 && !sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB)) | |
24b52437 | 68 | log_error("Failed to start unit: %s", bus_error_message(&error, r)); |
3d20ed6d LP |
69 | } |
70 | ||
96287a49 | 71 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
24b52437 LP |
72 | int r; |
73 | ||
74 | assert(key); | |
059cb385 | 75 | |
1d84ad94 LP |
76 | if (streq(key, "fsck.mode")) { |
77 | ||
78 | if (proc_cmdline_value_missing(key, value)) | |
79 | return 0; | |
059cb385 LP |
80 | |
81 | if (streq(value, "auto")) | |
82 | arg_force = arg_skip = false; | |
83 | else if (streq(value, "force")) | |
84 | arg_force = true; | |
85 | else if (streq(value, "skip")) | |
86 | arg_skip = true; | |
87 | else | |
85013844 LP |
88 | log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value); |
89 | ||
1d84ad94 LP |
90 | } else if (streq(key, "fsck.repair")) { |
91 | ||
92 | if (proc_cmdline_value_missing(key, value)) | |
93 | return 0; | |
f1f0198c HHPF |
94 | |
95 | if (streq(value, "preen")) | |
96 | arg_repair = "-a"; | |
24b52437 LP |
97 | else { |
98 | r = parse_boolean(value); | |
99 | if (r > 0) | |
100 | arg_repair = "-y"; | |
101 | else if (r == 0) | |
102 | arg_repair = "-n"; | |
103 | else | |
104 | log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value); | |
105 | } | |
85013844 LP |
106 | } |
107 | ||
349cc4a5 | 108 | #if HAVE_SYSV_COMPAT |
059cb385 LP |
109 | else if (streq(key, "fastboot") && !value) { |
110 | log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line."); | |
141a79f4 | 111 | arg_skip = true; |
85013844 | 112 | |
059cb385 LP |
113 | } else if (streq(key, "forcefsck") && !value) { |
114 | log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line."); | |
141a79f4 | 115 | arg_force = true; |
3d20ed6d | 116 | } |
141a79f4 | 117 | #endif |
3d20ed6d | 118 | |
3d20ed6d LP |
119 | return 0; |
120 | } | |
121 | ||
122 | static void test_files(void) { | |
85013844 | 123 | |
349cc4a5 | 124 | #if HAVE_SYSV_COMPAT |
32f992a5 LP |
125 | if (access("/fastboot", F_OK) >= 0) { |
126 | log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system."); | |
3d20ed6d | 127 | arg_skip = true; |
32f992a5 | 128 | } |
3d20ed6d | 129 | |
32f992a5 LP |
130 | if (access("/forcefsck", F_OK) >= 0) { |
131 | log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system."); | |
3d20ed6d | 132 | arg_force = true; |
32f992a5 LP |
133 | } |
134 | #endif | |
27d340c7 | 135 | |
96d9117a | 136 | arg_show_progress = access("/run/systemd/show-status", F_OK) >= 0; |
27d340c7 LP |
137 | } |
138 | ||
96d9117a LP |
139 | static double percent(int pass, unsigned long cur, unsigned long max) { |
140 | /* Values stolen from e2fsck */ | |
141 | ||
142 | static const int pass_table[] = { | |
143 | 0, 70, 90, 92, 95, 100 | |
27d340c7 LP |
144 | }; |
145 | ||
96d9117a LP |
146 | if (pass <= 0) |
147 | return 0.0; | |
148 | ||
149 | if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0) | |
150 | return 100.0; | |
151 | ||
152 | return (double) pass_table[pass-1] + | |
153 | ((double) pass_table[pass] - (double) pass_table[pass-1]) * | |
154 | (double) cur / (double) max; | |
155 | } | |
156 | ||
e4fc7455 BB |
157 | static int process_progress(int fd, FILE* console) { |
158 | _cleanup_fclose_ FILE *f = NULL; | |
96d9117a LP |
159 | usec_t last = 0; |
160 | bool locked = false; | |
161 | int clear = 0, r; | |
162 | ||
163 | /* No progress pipe to process? Then we are a NOP. */ | |
164 | if (fd < 0) | |
165 | return 0; | |
166 | ||
e92aaed3 | 167 | f = fdopen(fd, "r"); |
96d9117a LP |
168 | if (!f) { |
169 | safe_close(fd); | |
8c3dfa7d | 170 | return log_debug_errno(errno, "Failed to use pipe: %m"); |
96d9117a LP |
171 | } |
172 | ||
96d9117a | 173 | for (;;) { |
333ab199 | 174 | int pass; |
96d9117a | 175 | unsigned long cur, max; |
ac6e2f0d | 176 | _cleanup_free_ char *device = NULL; |
96d9117a LP |
177 | double p; |
178 | usec_t t; | |
27d340c7 | 179 | |
96d9117a LP |
180 | if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4) { |
181 | ||
182 | if (ferror(f)) | |
183 | r = log_warning_errno(errno, "Failed to read from progress pipe: %m"); | |
184 | else if (feof(f)) | |
185 | r = 0; | |
8c3dfa7d LP |
186 | else |
187 | r = log_warning_errno(SYNTHETIC_ERRNO(errno), "Failed to parse progress pipe data"); | |
188 | ||
27d340c7 | 189 | break; |
96d9117a LP |
190 | } |
191 | ||
192 | /* Only show one progress counter at max */ | |
193 | if (!locked) { | |
194 | if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) | |
195 | continue; | |
196 | ||
197 | locked = true; | |
198 | } | |
27d340c7 | 199 | |
27d340c7 LP |
200 | /* Only update once every 50ms */ |
201 | t = now(CLOCK_MONOTONIC); | |
e375825d | 202 | if (last + 50 * USEC_PER_MSEC > t) |
27d340c7 | 203 | continue; |
27d340c7 LP |
204 | |
205 | last = t; | |
206 | ||
96d9117a | 207 | p = percent(pass, cur, max); |
333ab199 ZJS |
208 | r = fprintf(console, "\r%s: fsck %3.1f%% complete...\r", device, p); |
209 | if (r < 0) | |
0f1f933b | 210 | return -EIO; /* No point in continuing if something happened to our output stream */ |
96d9117a | 211 | |
333ab199 ZJS |
212 | fflush(console); |
213 | clear = MAX(clear, r); | |
27d340c7 LP |
214 | } |
215 | ||
96d9117a | 216 | if (clear > 0) { |
96d9117a | 217 | fputc('\r', console); |
333ab199 | 218 | for (int j = 0; j < clear; j++) |
96d9117a LP |
219 | fputc(' ', console); |
220 | fputc('\r', console); | |
221 | fflush(console); | |
222 | } | |
223 | ||
224 | return r; | |
225 | } | |
226 | ||
227 | static int fsck_progress_socket(void) { | |
15a3e96f | 228 | _cleanup_close_ int fd = -1; |
1861986a | 229 | int r; |
96d9117a LP |
230 | |
231 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
232 | if (fd < 0) | |
233 | return log_warning_errno(errno, "socket(): %m"); | |
234 | ||
1861986a LP |
235 | r = connect_unix_path(fd, AT_FDCWD, "/run/systemd/fsck.progress"); |
236 | if (r < 0) | |
237 | return log_full_errno(IN_SET(r, -ECONNREFUSED, -ENOENT) ? LOG_DEBUG : LOG_WARNING, | |
238 | r, "Failed to connect to progress socket, ignoring: %m"); | |
96d9117a | 239 | |
15a3e96f | 240 | return TAKE_FD(fd); |
3d20ed6d LP |
241 | } |
242 | ||
9013653e | 243 | static int run(int argc, char *argv[]) { |
96d9117a | 244 | _cleanup_close_pair_ int progress_pipe[2] = { -1, -1 }; |
4afd3348 | 245 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
bd169c2b | 246 | _cleanup_free_ char *dpath = NULL; |
e4fc7455 | 247 | _cleanup_fclose_ FILE *console = NULL; |
94192cda | 248 | const char *device, *type; |
dc8e15c2 | 249 | bool root_directory; |
94192cda | 250 | struct stat st; |
2e87a1fd | 251 | int r, exit_status; |
96d9117a | 252 | pid_t pid; |
3d20ed6d | 253 | |
d2acb93d | 254 | log_setup(); |
3d20ed6d | 255 | |
d7a0f1f4 FS |
256 | if (argc > 2) |
257 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
258 | "This program expects one or no arguments."); | |
9013653e | 259 | |
4c12626c LP |
260 | umask(0022); |
261 | ||
1d84ad94 | 262 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
e7a3aa3d LP |
263 | if (r < 0) |
264 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
b5884878 | 265 | |
3d20ed6d LP |
266 | test_files(); |
267 | ||
9013653e ZJS |
268 | if (!arg_force && arg_skip) |
269 | return 0; | |
a9e1f5ec | 270 | |
dc8e15c2 | 271 | if (argc > 1) { |
bd169c2b LP |
272 | dpath = strdup(argv[1]); |
273 | if (!dpath) | |
274 | return log_oom(); | |
275 | ||
276 | device = dpath; | |
94192cda | 277 | |
9013653e ZJS |
278 | if (stat(device, &st) < 0) |
279 | return log_error_errno(errno, "Failed to stat %s: %m", device); | |
e7a3aa3d | 280 | |
d7a0f1f4 FS |
281 | if (!S_ISBLK(st.st_mode)) |
282 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
283 | "%s is not a block device.", | |
284 | device); | |
94192cda | 285 | |
930aa88f | 286 | r = sd_device_new_from_stat_rdev(&dev, &st); |
9013653e ZJS |
287 | if (r < 0) |
288 | return log_error_errno(r, "Failed to detect device %s: %m", device); | |
e7a3aa3d LP |
289 | |
290 | root_directory = false; | |
dc8e15c2 | 291 | } else { |
a84f5192 | 292 | struct timespec times[2]; |
3d20ed6d | 293 | |
a9e1f5ec LP |
294 | /* Find root device */ |
295 | ||
9013653e ZJS |
296 | if (stat("/", &st) < 0) |
297 | return log_error_errno(errno, "Failed to stat() the root directory: %m"); | |
a9e1f5ec LP |
298 | |
299 | /* Virtual root devices don't need an fsck */ | |
566690fd | 300 | if (major(st.st_dev) == 0) { |
e7a3aa3d | 301 | log_debug("Root directory is virtual or btrfs, skipping check."); |
9013653e | 302 | return 0; |
566690fd | 303 | } |
a9e1f5ec | 304 | |
a84f5192 KS |
305 | /* check if we are already writable */ |
306 | times[0] = st.st_atim; | |
307 | times[1] = st.st_mtim; | |
e7a3aa3d | 308 | |
a84f5192 | 309 | if (utimensat(AT_FDCWD, "/", times, 0) == 0) { |
cf1a1055 | 310 | log_info("Root directory is writable, skipping check."); |
9013653e | 311 | return 0; |
a84f5192 KS |
312 | } |
313 | ||
9102fdc5 | 314 | r = sd_device_new_from_devnum(&dev, 'b', st.st_dev); |
9013653e ZJS |
315 | if (r < 0) |
316 | return log_error_errno(r, "Failed to detect root device: %m"); | |
a9e1f5ec | 317 | |
9102fdc5 | 318 | r = sd_device_get_devname(dev, &device); |
9013653e ZJS |
319 | if (r < 0) |
320 | return log_device_error_errno(dev, r, "Failed to detect device node of root directory: %m"); | |
dc8e15c2 LP |
321 | |
322 | root_directory = true; | |
3d20ed6d LP |
323 | } |
324 | ||
2c740afd | 325 | if (sd_device_get_property_value(dev, "ID_FS_TYPE", &type) >= 0) { |
13556724 | 326 | r = fsck_exists_for_fstype(type); |
85eca92e | 327 | if (r < 0) |
7bd33dbd | 328 | log_device_warning_errno(dev, r, "Couldn't detect if fsck.%s may be used, proceeding: %m", type); |
85eca92e | 329 | else if (r == 0) { |
7bd33dbd | 330 | log_device_info(dev, "fsck.%s doesn't exist, not checking file system.", type); |
9013653e | 331 | return 0; |
85eca92e | 332 | } |
13556724 JK |
333 | } else { |
334 | r = fsck_exists(); | |
335 | if (r < 0) | |
336 | log_device_warning_errno(dev, r, "Couldn't detect if the fsck command may be used, proceeding: %m"); | |
337 | else if (r == 0) { | |
338 | log_device_info(dev, "The fsck command does not exist, not checking file system."); | |
339 | return 0; | |
340 | } | |
94192cda ZJS |
341 | } |
342 | ||
e4fc7455 BB |
343 | console = fopen("/dev/console", "we"); |
344 | if (console && | |
345 | arg_show_progress && | |
9013653e ZJS |
346 | pipe(progress_pipe) < 0) |
347 | return log_error_errno(errno, "pipe(): %m"); | |
27d340c7 | 348 | |
b6e1fff1 LP |
349 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
350 | if (r < 0) | |
9013653e | 351 | return r; |
4c253ed1 | 352 | if (r == 0) { |
fbd0b64f | 353 | char dash_c[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1]; |
96d9117a LP |
354 | int progress_socket = -1; |
355 | const char *cmdline[9]; | |
356 | int i = 0; | |
357 | ||
3d20ed6d | 358 | /* Child */ |
96d9117a | 359 | |
96d9117a | 360 | /* Close the reading side of the progress pipe */ |
1952708a | 361 | progress_pipe[0] = safe_close(progress_pipe[0]); |
96d9117a LP |
362 | |
363 | /* Try to connect to a progress management daemon, if there is one */ | |
364 | progress_socket = fsck_progress_socket(); | |
365 | if (progress_socket >= 0) { | |
366 | /* If this worked we close the progress pipe early, and just use the socket */ | |
367 | progress_pipe[1] = safe_close(progress_pipe[1]); | |
368 | xsprintf(dash_c, "-C%i", progress_socket); | |
369 | } else if (progress_pipe[1] >= 0) { | |
370 | /* Otherwise if we have the progress pipe to our own local handle, we use it */ | |
371 | xsprintf(dash_c, "-C%i", progress_pipe[1]); | |
372 | } else | |
373 | dash_c[0] = 0; | |
374 | ||
375 | cmdline[i++] = "/sbin/fsck"; | |
376 | cmdline[i++] = arg_repair; | |
377 | cmdline[i++] = "-T"; | |
378 | ||
379 | /* | |
380 | * Since util-linux v2.25 fsck uses /run/fsck/<diskname>.lock files. | |
381 | * The previous versions use flock for the device and conflict with | |
382 | * udevd, see https://bugs.freedesktop.org/show_bug.cgi?id=79576#c5 | |
383 | */ | |
384 | cmdline[i++] = "-l"; | |
385 | ||
386 | if (!root_directory) | |
387 | cmdline[i++] = "-M"; | |
388 | ||
389 | if (arg_force) | |
390 | cmdline[i++] = "-f"; | |
391 | ||
392 | if (!isempty(dash_c)) | |
393 | cmdline[i++] = dash_c; | |
394 | ||
395 | cmdline[i++] = device; | |
396 | cmdline[i++] = NULL; | |
397 | ||
595225af LP |
398 | (void) rlimit_nofile_safe(); |
399 | ||
3d20ed6d | 400 | execv(cmdline[0], (char**) cmdline); |
91077af6 | 401 | _exit(FSCK_OPERATIONAL_ERROR); |
3d20ed6d LP |
402 | } |
403 | ||
e4fc7455 BB |
404 | if (console) { |
405 | progress_pipe[1] = safe_close(progress_pipe[1]); | |
406 | (void) process_progress(TAKE_FD(progress_pipe[0]), console); | |
407 | } | |
27d340c7 | 408 | |
2e87a1fd | 409 | exit_status = wait_for_terminate_and_check("fsck", pid, WAIT_LOG_ABNORMAL); |
9013653e ZJS |
410 | if (exit_status < 0) |
411 | return exit_status; | |
cbf13087 | 412 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { |
2e87a1fd | 413 | log_error("fsck failed with exit status %i.", exit_status); |
3d20ed6d | 414 | |
2e87a1fd | 415 | if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory) { |
3d20ed6d | 416 | /* System should be rebooted. */ |
48db40b3 | 417 | start_target(SPECIAL_REBOOT_TARGET, "replace-irreversibly"); |
9013653e | 418 | return -EINVAL; |
4b499063 | 419 | } else if (!(exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED))) |
96d9117a | 420 | log_warning("Ignoring error."); |
9013653e | 421 | } |
3d20ed6d | 422 | |
2e87a1fd | 423 | if (exit_status & FSCK_ERROR_CORRECTED) |
e7a3aa3d | 424 | (void) touch("/run/systemd/quotacheck"); |
3d20ed6d | 425 | |
cac0b957 | 426 | return !!(exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)); |
3d20ed6d | 427 | } |
9013653e ZJS |
428 | |
429 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |