From: Daan De Meyer Date: Mon, 29 Aug 2022 10:09:34 +0000 (+0200) Subject: Lock loop device after acquiring X-Git-Tag: v14~61^2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1146%2Fhead;p=thirdparty%2Fmkosi.git Lock loop device after acquiring Instructs udevd to not interfere with the device while we're working on it. --- 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