From: Lennart Poettering Date: Mon, 18 May 2020 16:31:45 +0000 (+0200) Subject: makefs: lock device while we operate X-Git-Tag: v246-rc1~336^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0181ad85b37d37785787b4eb8aa8c72d2e4c76b4;p=thirdparty%2Fsystemd.git makefs: lock device while we operate Let's implement our own specs, i.e. https://systemd.io/BLOCK_DEVICE_LOCKING/ This should address issues like this: #13162 --- diff --git a/src/partition/makefs.c b/src/partition/makefs.c index df08a5fea65..128aa41044a 100644 --- a/src/partition/makefs.c +++ b/src/partition/makefs.c @@ -7,7 +7,9 @@ #include #include "alloc-util.h" +#include "blockdev-util.h" #include "dissect-image.h" +#include "fd-util.h" #include "main-func.h" #include "process-util.h" #include "signal-util.h" @@ -42,6 +44,7 @@ static int makefs(const char *type, const char *device) { static int run(int argc, char *argv[]) { _cleanup_free_ char *device = NULL, *type = NULL, *detected = NULL; + _cleanup_close_ int lock_fd = -1; struct stat st; int r; @@ -63,7 +66,13 @@ static int run(int argc, char *argv[]) { if (stat(device, &st) < 0) return log_error_errno(errno, "Failed to stat \"%s\": %m", device); - if (!S_ISBLK(st.st_mode)) + if (S_ISBLK(st.st_mode)) { + /* Lock the device so that udev doesn't interfere with our work */ + + lock_fd = lock_whole_block_device(st.st_rdev, LOCK_EX); + if (lock_fd < 0) + return log_error_errno(lock_fd, "Failed to lock whole block device of \"%s\": %m", device); + } else log_info("%s is not a block device.", device); r = probe_filesystem(device, &detected);