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