]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
makefs: lock device while we operate
authorLennart Poettering <lennart@poettering.net>
Mon, 18 May 2020 16:31:45 +0000 (18:31 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 18 May 2020 18:50:03 +0000 (20:50 +0200)
Let's implement our own specs, i.e.

https://systemd.io/BLOCK_DEVICE_LOCKING/

This should address issues like this: #13162

src/partition/makefs.c

index df08a5fea654c2ede00c5671d3eccd82316cdc5b..128aa41044a31269b73ce945e55e2d0baf9e6e5f 100644 (file)
@@ -7,7 +7,9 @@
 #include <unistd.h>
 
 #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);