]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Lock loop device after acquiring 1146/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 29 Aug 2022 10:09:34 +0000 (12:09 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 29 Aug 2022 10:09:34 +0000 (12:09 +0200)
Instructs udevd to not interfere with the device while we're working
on it.

mkosi/__init__.py

index 04bee407758540ea81ee3192aff2f393ac7acae9..d54d0a7030884870407a814818968bbb4fb3d94d 100644 (file)
@@ -936,6 +936,15 @@ def reuse_cache_image(
     return f, True
 
 
+@contextlib.contextmanager
+def flock(file: BinaryIO) -> Iterator[None]:
+    fcntl.flock(file, fcntl.LOCK_EX)
+    try:
+        yield
+    finally:
+        fcntl.flock(file, fcntl.LOCK_UN)
+
+
 @contextlib.contextmanager
 def get_loopdev(f: BinaryIO) -> Iterator[BinaryIO]:
     with complete_step(f"Attaching {f.name} as loopback…", "Detaching {}") as output:
@@ -958,7 +967,7 @@ def attach_image_loopback(image: Optional[BinaryIO], table: Optional[PartitionTa
 
     assert table
 
-    with get_loopdev(image) as loopdev:
+    with get_loopdev(image) as loopdev, flock(loopdev):
         # losetup --partscan instructs the kernel to scan the partition table and add separate partition
         # devices for each of the partitions it finds. However, this operation is asynchronous which
         # means losetup will return before all partition devices have been initialized. This can result