]>
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" |
3ffd4af2 | 35 | #include "util.h" |
3d20ed6d LP |
36 | |
37 | static bool arg_skip = false; | |
38 | static bool arg_force = false; | |
96d9117a | 39 | static bool arg_show_progress = false; |
f1f0198c | 40 | static const char *arg_repair = "-a"; |
3d20ed6d | 41 | |
48db40b3 | 42 | static void start_target(const char *target, const char *mode) { |
4afd3348 LP |
43 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
44 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
0c842e0a | 45 | int r; |
3d20ed6d LP |
46 | |
47 | assert(target); | |
48 | ||
266f3e26 | 49 | r = bus_connect_system_systemd(&bus); |
0c842e0a | 50 | if (r < 0) { |
da927ba9 | 51 | log_error_errno(r, "Failed to get D-Bus connection: %m"); |
0c842e0a | 52 | return; |
3d20ed6d LP |
53 | } |
54 | ||
4b499063 | 55 | log_info("Running request %s/start/%s", target, mode); |
3d20ed6d | 56 | |
90bb85e1 | 57 | /* Start these units only if we can replace base.target with it */ |
0c842e0a TG |
58 | r = sd_bus_call_method(bus, |
59 | "org.freedesktop.systemd1", | |
60 | "/org/freedesktop/systemd1", | |
61 | "org.freedesktop.systemd1.Manager", | |
62 | "StartUnitReplace", | |
63 | &error, | |
64 | NULL, | |
48db40b3 | 65 | "sss", "basic.target", target, mode); |
3d20ed6d | 66 | |
5220a6f3 LP |
67 | /* Don't print a warning if we aren't called during startup */ |
68 | if (r < 0 && !sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB)) | |
24b52437 | 69 | log_error("Failed to start unit: %s", bus_error_message(&error, r)); |
3d20ed6d LP |
70 | } |
71 | ||
96287a49 | 72 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
24b52437 LP |
73 | int r; |
74 | ||
75 | assert(key); | |
059cb385 | 76 | |
1d84ad94 LP |
77 | if (streq(key, "fsck.mode")) { |
78 | ||
79 | if (proc_cmdline_value_missing(key, value)) | |
80 | return 0; | |
059cb385 LP |
81 | |
82 | if (streq(value, "auto")) | |
83 | arg_force = arg_skip = false; | |
84 | else if (streq(value, "force")) | |
85 | arg_force = true; | |
86 | else if (streq(value, "skip")) | |
87 | arg_skip = true; | |
88 | else | |
85013844 LP |
89 | log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value); |
90 | ||
1d84ad94 LP |
91 | } else if (streq(key, "fsck.repair")) { |
92 | ||
93 | if (proc_cmdline_value_missing(key, value)) | |
94 | return 0; | |
f1f0198c HHPF |
95 | |
96 | if (streq(value, "preen")) | |
97 | arg_repair = "-a"; | |
24b52437 LP |
98 | else { |
99 | r = parse_boolean(value); | |
100 | if (r > 0) | |
101 | arg_repair = "-y"; | |
102 | else if (r == 0) | |
103 | arg_repair = "-n"; | |
104 | else | |
105 | log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value); | |
106 | } | |
85013844 LP |
107 | } |
108 | ||
349cc4a5 | 109 | #if HAVE_SYSV_COMPAT |
059cb385 LP |
110 | else if (streq(key, "fastboot") && !value) { |
111 | log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line."); | |
141a79f4 | 112 | arg_skip = true; |
85013844 | 113 | |
059cb385 LP |
114 | } else if (streq(key, "forcefsck") && !value) { |
115 | log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line."); | |
141a79f4 | 116 | arg_force = true; |
3d20ed6d | 117 | } |
141a79f4 | 118 | #endif |
3d20ed6d | 119 | |
3d20ed6d LP |
120 | return 0; |
121 | } | |
122 | ||
123 | static void test_files(void) { | |
85013844 | 124 | |
349cc4a5 | 125 | #if HAVE_SYSV_COMPAT |
32f992a5 LP |
126 | if (access("/fastboot", F_OK) >= 0) { |
127 | log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system."); | |
3d20ed6d | 128 | arg_skip = true; |
32f992a5 | 129 | } |
3d20ed6d | 130 | |
32f992a5 LP |
131 | if (access("/forcefsck", F_OK) >= 0) { |
132 | log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system."); | |
3d20ed6d | 133 | arg_force = true; |
32f992a5 LP |
134 | } |
135 | #endif | |
27d340c7 | 136 | |
96d9117a | 137 | arg_show_progress = access("/run/systemd/show-status", F_OK) >= 0; |
27d340c7 LP |
138 | } |
139 | ||
96d9117a LP |
140 | static double percent(int pass, unsigned long cur, unsigned long max) { |
141 | /* Values stolen from e2fsck */ | |
142 | ||
143 | static const int pass_table[] = { | |
144 | 0, 70, 90, 92, 95, 100 | |
27d340c7 LP |
145 | }; |
146 | ||
96d9117a LP |
147 | if (pass <= 0) |
148 | return 0.0; | |
149 | ||
150 | if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0) | |
151 | return 100.0; | |
152 | ||
153 | return (double) pass_table[pass-1] + | |
154 | ((double) pass_table[pass] - (double) pass_table[pass-1]) * | |
155 | (double) cur / (double) max; | |
156 | } | |
157 | ||
e4fc7455 BB |
158 | static int process_progress(int fd, FILE* console) { |
159 | _cleanup_fclose_ FILE *f = NULL; | |
96d9117a LP |
160 | usec_t last = 0; |
161 | bool locked = false; | |
162 | int clear = 0, r; | |
163 | ||
164 | /* No progress pipe to process? Then we are a NOP. */ | |
165 | if (fd < 0) | |
166 | return 0; | |
167 | ||
e92aaed3 | 168 | f = fdopen(fd, "r"); |
96d9117a LP |
169 | if (!f) { |
170 | safe_close(fd); | |
8c3dfa7d | 171 | return log_debug_errno(errno, "Failed to use pipe: %m"); |
96d9117a LP |
172 | } |
173 | ||
96d9117a | 174 | for (;;) { |
333ab199 | 175 | int pass; |
96d9117a | 176 | unsigned long cur, max; |
ac6e2f0d | 177 | _cleanup_free_ char *device = NULL; |
96d9117a LP |
178 | double p; |
179 | usec_t t; | |
27d340c7 | 180 | |
96d9117a LP |
181 | if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4) { |
182 | ||
183 | if (ferror(f)) | |
184 | r = log_warning_errno(errno, "Failed to read from progress pipe: %m"); | |
185 | else if (feof(f)) | |
186 | r = 0; | |
8c3dfa7d LP |
187 | else |
188 | r = log_warning_errno(SYNTHETIC_ERRNO(errno), "Failed to parse progress pipe data"); | |
189 | ||
27d340c7 | 190 | break; |
96d9117a LP |
191 | } |
192 | ||
193 | /* Only show one progress counter at max */ | |
194 | if (!locked) { | |
195 | if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) | |
196 | continue; | |
197 | ||
198 | locked = true; | |
199 | } | |
27d340c7 | 200 | |
27d340c7 LP |
201 | /* Only update once every 50ms */ |
202 | t = now(CLOCK_MONOTONIC); | |
e375825d | 203 | if (last + 50 * USEC_PER_MSEC > t) |
27d340c7 | 204 | continue; |
27d340c7 LP |
205 | |
206 | last = t; | |
207 | ||
96d9117a | 208 | p = percent(pass, cur, max); |
333ab199 ZJS |
209 | r = fprintf(console, "\r%s: fsck %3.1f%% complete...\r", device, p); |
210 | if (r < 0) | |
0f1f933b | 211 | return -EIO; /* No point in continuing if something happened to our output stream */ |
96d9117a | 212 | |
333ab199 ZJS |
213 | fflush(console); |
214 | clear = MAX(clear, r); | |
27d340c7 LP |
215 | } |
216 | ||
96d9117a | 217 | if (clear > 0) { |
96d9117a | 218 | fputc('\r', console); |
333ab199 | 219 | for (int j = 0; j < clear; j++) |
96d9117a LP |
220 | fputc(' ', console); |
221 | fputc('\r', console); | |
222 | fflush(console); | |
223 | } | |
224 | ||
225 | return r; | |
226 | } | |
227 | ||
228 | static int fsck_progress_socket(void) { | |
15a3e96f | 229 | _cleanup_close_ int fd = -1; |
1861986a | 230 | int r; |
96d9117a LP |
231 | |
232 | fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
233 | if (fd < 0) | |
234 | return log_warning_errno(errno, "socket(): %m"); | |
235 | ||
1861986a LP |
236 | r = connect_unix_path(fd, AT_FDCWD, "/run/systemd/fsck.progress"); |
237 | if (r < 0) | |
238 | return log_full_errno(IN_SET(r, -ECONNREFUSED, -ENOENT) ? LOG_DEBUG : LOG_WARNING, | |
239 | r, "Failed to connect to progress socket, ignoring: %m"); | |
96d9117a | 240 | |
15a3e96f | 241 | return TAKE_FD(fd); |
3d20ed6d LP |
242 | } |
243 | ||
9013653e | 244 | static int run(int argc, char *argv[]) { |
96d9117a | 245 | _cleanup_close_pair_ int progress_pipe[2] = { -1, -1 }; |
4afd3348 | 246 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
bd169c2b | 247 | _cleanup_free_ char *dpath = NULL; |
e4fc7455 | 248 | _cleanup_fclose_ FILE *console = NULL; |
94192cda | 249 | const char *device, *type; |
dc8e15c2 | 250 | bool root_directory; |
94192cda | 251 | struct stat st; |
2e87a1fd | 252 | int r, exit_status; |
96d9117a | 253 | pid_t pid; |
3d20ed6d | 254 | |
d2acb93d | 255 | log_setup(); |
3d20ed6d | 256 | |
d7a0f1f4 FS |
257 | if (argc > 2) |
258 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
259 | "This program expects one or no arguments."); | |
9013653e | 260 | |
4c12626c LP |
261 | umask(0022); |
262 | ||
1d84ad94 | 263 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
e7a3aa3d LP |
264 | if (r < 0) |
265 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); | |
b5884878 | 266 | |
3d20ed6d LP |
267 | test_files(); |
268 | ||
9013653e ZJS |
269 | if (!arg_force && arg_skip) |
270 | return 0; | |
a9e1f5ec | 271 | |
dc8e15c2 | 272 | if (argc > 1) { |
bd169c2b LP |
273 | dpath = strdup(argv[1]); |
274 | if (!dpath) | |
275 | return log_oom(); | |
276 | ||
277 | device = dpath; | |
94192cda | 278 | |
9013653e ZJS |
279 | if (stat(device, &st) < 0) |
280 | return log_error_errno(errno, "Failed to stat %s: %m", device); | |
e7a3aa3d | 281 | |
d7a0f1f4 FS |
282 | if (!S_ISBLK(st.st_mode)) |
283 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
284 | "%s is not a block device.", | |
285 | device); | |
94192cda | 286 | |
930aa88f | 287 | r = sd_device_new_from_stat_rdev(&dev, &st); |
9013653e ZJS |
288 | if (r < 0) |
289 | return log_error_errno(r, "Failed to detect device %s: %m", device); | |
e7a3aa3d LP |
290 | |
291 | root_directory = false; | |
dc8e15c2 | 292 | } else { |
a84f5192 | 293 | struct timespec times[2]; |
3d20ed6d | 294 | |
a9e1f5ec LP |
295 | /* Find root device */ |
296 | ||
9013653e ZJS |
297 | if (stat("/", &st) < 0) |
298 | return log_error_errno(errno, "Failed to stat() the root directory: %m"); | |
a9e1f5ec LP |
299 | |
300 | /* Virtual root devices don't need an fsck */ | |
566690fd | 301 | if (major(st.st_dev) == 0) { |
e7a3aa3d | 302 | log_debug("Root directory is virtual or btrfs, skipping check."); |
9013653e | 303 | return 0; |
566690fd | 304 | } |
a9e1f5ec | 305 | |
a84f5192 KS |
306 | /* check if we are already writable */ |
307 | times[0] = st.st_atim; | |
308 | times[1] = st.st_mtim; | |
e7a3aa3d | 309 | |
a84f5192 | 310 | if (utimensat(AT_FDCWD, "/", times, 0) == 0) { |
cf1a1055 | 311 | log_info("Root directory is writable, skipping check."); |
9013653e | 312 | return 0; |
a84f5192 KS |
313 | } |
314 | ||
9102fdc5 | 315 | r = sd_device_new_from_devnum(&dev, 'b', st.st_dev); |
9013653e ZJS |
316 | if (r < 0) |
317 | return log_error_errno(r, "Failed to detect root device: %m"); | |
a9e1f5ec | 318 | |
9102fdc5 | 319 | r = sd_device_get_devname(dev, &device); |
9013653e ZJS |
320 | if (r < 0) |
321 | return log_device_error_errno(dev, r, "Failed to detect device node of root directory: %m"); | |
dc8e15c2 LP |
322 | |
323 | root_directory = true; | |
3d20ed6d LP |
324 | } |
325 | ||
2c740afd | 326 | if (sd_device_get_property_value(dev, "ID_FS_TYPE", &type) >= 0) { |
eb66db55 | 327 | r = fsck_exists(type); |
85eca92e | 328 | if (r < 0) |
7bd33dbd | 329 | log_device_warning_errno(dev, r, "Couldn't detect if fsck.%s may be used, proceeding: %m", type); |
85eca92e | 330 | else if (r == 0) { |
7bd33dbd | 331 | log_device_info(dev, "fsck.%s doesn't exist, not checking file system.", type); |
9013653e | 332 | return 0; |
85eca92e | 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 | |
b6e1fff1 LP |
342 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
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 | ||
368 | cmdline[i++] = "/sbin/fsck"; | |
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 | ||
595225af LP |
391 | (void) rlimit_nofile_safe(); |
392 | ||
3d20ed6d | 393 | execv(cmdline[0], (char**) cmdline); |
91077af6 | 394 | _exit(FSCK_OPERATIONAL_ERROR); |
3d20ed6d LP |
395 | } |
396 | ||
e4fc7455 BB |
397 | if (console) { |
398 | progress_pipe[1] = safe_close(progress_pipe[1]); | |
399 | (void) process_progress(TAKE_FD(progress_pipe[0]), console); | |
400 | } | |
27d340c7 | 401 | |
2e87a1fd | 402 | exit_status = wait_for_terminate_and_check("fsck", pid, WAIT_LOG_ABNORMAL); |
9013653e ZJS |
403 | if (exit_status < 0) |
404 | return exit_status; | |
cbf13087 | 405 | if ((exit_status & ~FSCK_ERROR_CORRECTED) != FSCK_SUCCESS) { |
2e87a1fd | 406 | log_error("fsck failed with exit status %i.", exit_status); |
3d20ed6d | 407 | |
2e87a1fd | 408 | if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory) { |
3d20ed6d | 409 | /* System should be rebooted. */ |
48db40b3 | 410 | start_target(SPECIAL_REBOOT_TARGET, "replace-irreversibly"); |
9013653e | 411 | return -EINVAL; |
4b499063 | 412 | } else if (!(exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED))) |
96d9117a | 413 | log_warning("Ignoring error."); |
9013653e | 414 | } |
3d20ed6d | 415 | |
2e87a1fd | 416 | if (exit_status & FSCK_ERROR_CORRECTED) |
e7a3aa3d | 417 | (void) touch("/run/systemd/quotacheck"); |
3d20ed6d | 418 | |
cac0b957 | 419 | return !!(exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)); |
3d20ed6d | 420 | } |
9013653e ZJS |
421 | |
422 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |