[elbe-devel] [PATCH] elbepack: imgutils: protect against race between losetup() and udev

Thomas Weißschuh thomas.weissschuh at linutronix.de
Wed Feb 19 08:11:14 CET 2025


Udev or on of its plugins reconfigures new loop devices.
Partitions are rescanned, leading to the removal and readding of
their dev nodes.
Depending on the timing, ELBE tries to access the partion devnode
while it has been removed but not yet recreated.

Use a file lock to synchronize the accesses between udev and ELBE.
It's not necessary to wait for the udev updates as devtmpfs will create
the device nodes on its own.

Reported-by: John Ogness <jogness at linutronix.de>
Link: https://systemd.io/BLOCK_DEVICE_LOCKING
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/imgutils.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/elbepack/imgutils.py b/elbepack/imgutils.py
index 635b3e68e7a906e1acc2b5e4f145f7aadcc9dcff..b060121c4590d4235a0fbc65c97c0b0c82cd95ce 100644
--- a/elbepack/imgutils.py
+++ b/elbepack/imgutils.py
@@ -3,6 +3,7 @@
 # SPDX-FileCopyrightText: 2024 Linutronix GmbH
 
 import contextlib
+import fcntl
 import subprocess
 
 from elbepack.shellhelper import ELBE_LOGGING, do, run
@@ -16,7 +17,10 @@ def losetup(dev, extra_args=[]):
     ).stdout.decode('ascii').rstrip('\n')
 
     try:
-        yield loopdev
+        with open(loopdev) as f:
+            # protect against races with udev
+            fcntl.flock(f, fcntl.LOCK_EX)
+            yield loopdev
     finally:
         do(['losetup', '--detach', loopdev], check=False)
 

---
base-commit: b5e67c0758c98adfc762969bfcd190c319e9fd78
change-id: 20250218-losetup-wait-d79b5ff4b68f

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh at linutronix.de>



More information about the elbe-devel mailing list