-
Notifications
You must be signed in to change notification settings - Fork 699
systemd: lock disks while running filesystem checks #18653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Pawel Winogrodzki (PawelWMS)
wants to merge
4
commits into
microsoft:3.0-dev
Choose a base branch
from
PawelWMS:pawelwi/systemd-fsck-whole-disk-lock
base: 3.0-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−2
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aee4066
fix(systemd): lock disks while running filesystem checks
PawelWMS cd33a9a
docs(systemd): move lock warning into patch
PawelWMS c286104
docs(systemd): move lock warning to patch header
PawelWMS 836fa3c
fix(systemd): bound whole-disk lock wait
PawelWMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| Lock the whole parent disk while fsck runs so udev cannot probe sibling | ||
| partitions during filesystem repair. | ||
|
|
||
| Wait up to 60 seconds for the lock. If the timeout expires, warn and run | ||
| the filesystem check without the lock to preserve the previous behavior. | ||
|
|
||
| Do not combine this implementation with native e2fsprogs (e2fsck) | ||
| whole-disk locking. systemd-fsck retains its lock descriptor while waiting | ||
| for fsck, so an independently opened exclusive lock in the child would | ||
| deadlock. | ||
|
|
||
| diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c | ||
| index 000ed69..8eb209a 100644 | ||
| --- a/src/fsck/fsck.c | ||
| +++ b/src/fsck/fsck.c | ||
| @@ -15,6 +15,7 @@ | ||
| #include "sd-device.h" | ||
|
|
||
| #include "alloc-util.h" | ||
| +#include "blockdev-util.h" | ||
| #include "bus-common-errors.h" | ||
| #include "bus-error.h" | ||
| #include "bus-locator.h" | ||
| @@ -23,6 +24,7 @@ | ||
| #include "fd-util.h" | ||
| #include "fs-util.h" | ||
| #include "fsck-util.h" | ||
| +#include "lock-util.h" | ||
| #include "main-func.h" | ||
| #include "parse-util.h" | ||
| #include "path-util.h" | ||
| @@ -32,6 +34,7 @@ | ||
| #include "socket-util.h" | ||
| #include "special.h" | ||
| #include "stdio-util.h" | ||
| +#include "time-util.h" | ||
|
|
||
| static bool arg_skip = false; | ||
| static bool arg_force = false; | ||
| @@ -233,8 +236,47 @@ static int fsck_progress_socket(void) { | ||
| return TAKE_FD(fd); | ||
| } | ||
|
|
||
| +static int lock_whole_disk(const char *device) { | ||
| + _cleanup_free_ char *whole_disk = NULL; | ||
| + _cleanup_close_ int fd = -EBADF; | ||
| + struct stat st; | ||
| + dev_t devno; | ||
| + int r; | ||
| + | ||
| + assert(device); | ||
| + | ||
| + r = path_get_whole_disk(device, /* backing = */ false, &devno); | ||
| + if (r < 0) | ||
| + return log_error_errno(r, "Failed to find whole block device for '%s': %m", device); | ||
| + | ||
| + r = devname_from_devnum(S_IFBLK, devno, &whole_disk); | ||
| + if (r < 0) | ||
| + return log_error_errno(r, "Failed to resolve whole block device for '%s': %m", device); | ||
| + | ||
| + fd = open(whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | ||
| + if (fd < 0) | ||
| + return log_error_errno(errno, "Failed to open whole block device '%s': %m", whole_disk); | ||
| + | ||
| + if (fstat(fd, &st) < 0) | ||
| + return log_error_errno(errno, "Failed to stat whole block device '%s': %m", whole_disk); | ||
| + if (!S_ISBLK(st.st_mode) || st.st_rdev != devno) | ||
| + return log_error_errno(SYNTHETIC_ERRNO(ENXIO), | ||
| + "Path '%s' no longer refers to block device %u:%u.", | ||
| + whole_disk, major(devno), minor(devno)); | ||
| + | ||
| + r = lock_generic_with_timeout(fd, LOCK_BSD, LOCK_EX, 60 * USEC_PER_SEC); | ||
| + if (r == -ETIMEDOUT) | ||
| + return r; | ||
| + if (r < 0) | ||
| + return log_error_errno(r, "Failed to lock whole block device '%s': %m", whole_disk); | ||
| + | ||
| + log_debug("Locked whole block device %s while checking %s.", whole_disk, device); | ||
| + return TAKE_FD(fd); | ||
| +} | ||
| + | ||
| static int run(int argc, char *argv[]) { | ||
| _cleanup_close_pair_ int progress_pipe[2] = EBADF_PAIR; | ||
| + _cleanup_close_ int lock_fd = -EBADF; | ||
| _cleanup_(sd_device_unrefp) sd_device *dev = NULL; | ||
| _cleanup_free_ char *dpath = NULL; | ||
| _cleanup_fclose_ FILE *console = NULL; | ||
| @@ -333,6 +375,15 @@ static int run(int argc, char *argv[]) { | ||
| } | ||
| } | ||
|
|
||
| + lock_fd = lock_whole_disk(device); | ||
| + if (lock_fd == -ETIMEDOUT) { | ||
| + log_warning( | ||
| + "Timed out waiting 60 seconds for whole block device lock for '%s'; proceeding without lock.", | ||
| + device); | ||
| + lock_fd = -EBADF; | ||
| + } else if (lock_fd < 0) | ||
| + return lock_fd; | ||
| + | ||
| console = fopen("/dev/console", "we"); | ||
| if (console && | ||
| arg_show_progress && | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a constant for the timeout, so we don't repeat "60" in two places in the code and potentially have them go out of sync.