From d65a7701d82152d33066c74f9ca6fc8e50995607 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Mon, 29 Aug 2022 12:09:34 +0200 Subject: [PATCH] Lock loop device after acquiring Instructs udevd to not interfere with the device while we're working on it. --- mkosi/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 04bee4077..d54d0a703 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -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 -- 2.47.2