#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"
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;
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);