From 2aa94bb88a0bceaf60e3e024219384204174bb9f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 14 Oct 2021 15:38:03 +0200 Subject: [PATCH] homework: only do image locks for regular image files If an image file is actually a block device taking a lock on it doesn't really make sense for us: it will interfere with udev's block device probing logic, and it's not going to propagated across the network anyway (which is what we are after here). Hence simply don't do it. Follow-up for 2aaf565a2da8eb0ea9adf84028c9d0ab7a90e53e --- src/home/homework-luks.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c index b38535ddc3..050c9cd6d5 100644 --- a/src/home/homework-luks.c +++ b/src/home/homework-luks.c @@ -1058,6 +1058,21 @@ static int lock_image_fd(int image_fd, const char *ip) { if (r < 0) return log_error_errno(r, "Failed to parse $SYSTEMD_LUKS_LOCK environment variable: %m"); if (r > 0) { + struct stat st; + + if (fstat(image_fd, &st) < 0) + return log_error_errno(errno, "Failed to stat image file: %m"); + if (S_ISBLK(st.st_mode)) { + /* Locking block devices doesn't really make sense, as this might interfear with + * udev's workings, and these locks aren't network propagated anyway, hence not what + * we are after here. */ + log_debug("Not locking image file '%s', since it's a block device.", ip); + return 0; + } + r = stat_verify_regular(&st); + if (r < 0) + return log_error_errno(r, "Image file to lock is not a regular file: %m"); + if (flock(image_fd, LOCK_EX|LOCK_NB) < 0) { if (errno == EWOULDBLOCK)