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