[elbe-devel] [PATCH v2] elbepack: imgutils: protect against race between losetup() and udev
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Wed Feb 19 11:02:46 CET 2025
After creation of the loop device through losetup, udev reconfigures it.
Partitions are rescanned, removing and then readding their device nodes.
This introduces a race window where ELBE tries to access the non-existent
partition device nodes after creating the loop device.
The involvment of udev is unnecessary anyways as devtmpfs will properly create
the partition device nodes on its won.
Use a file lock to synchronize the accesses between udev and ELBE.
The lock will be dropped automatically when the filedescriptor is closed
at the end of the contextmanager.
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>
---
Changes in v2:
- Make commit message clearer
- Link to v1: https://lore.kernel.org/r/20250219-losetup-wait-v1-1-94b35fa1ad0f@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