]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Replace blockdev --reread-pt by manually adding partitions 1130/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 18 Aug 2022 20:51:53 +0000 (22:51 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 18 Aug 2022 21:05:44 +0000 (23:05 +0200)
Instead of relying on blockdev, let's use our own infra to make
sure all partitions have appeared after we modify the partition
table. This should hopefully reduce the number of race conditions
involved with modifying the partition table and loop devices.

mkosi/__init__.py
mkosi/backend.py

index 892601a822b185f772612ee719d5a7f3f3b0fe4f..a8d6143ff7a3f8e290f3f39d5c53575bca3936de 100644 (file)
@@ -3685,15 +3685,6 @@ def insert_partition(
     with complete_step(f"Inserting partition of {format_bytes(part_size)}{ss}..."):
         args.partition_table.run_sfdisk(loopdev)
 
-        # Required otherwise the partition removal will fail
-        with open(loopdev, 'rb+') as f:
-            ioctl_partition_add(
-                f.fileno(),
-                part.number,
-                args.partition_table.partition_offset(part),
-                args.partition_table.partition_size(part)
-            )
-
     with complete_step("Writing partition..."):
         if ident == PartitionIdentifier.root:
             luks_format_root(args, loopdev, False, False, True)
index abb4da98b7ed38898058f71e333394c8a9d22b67..fcc8ab795802ee313546dce38251ecb2e183fda8 100644 (file)
@@ -36,6 +36,8 @@ from typing import (
     cast,
 )
 
+from .syscall import ioctl_partition_add
+
 PathString = Union[Path, str]
 
 
@@ -415,7 +417,11 @@ class PartitionTable:
 
         if device.is_block_device():
             run(["sync"])
-            run_with_backoff(["blockdev", "--rereadpt", device], attempts=10)
+
+            # Make sure we re-add all partitions after modifying the partition table.
+            with open(device, 'rb+') as f:
+                for p in self.partitions.values():
+                    ioctl_partition_add(f.fileno(), p.number, self.partition_offset(p), self.partition_size(p))
 
 
 @dataclasses.dataclass
@@ -823,29 +829,6 @@ def run(
         die(f"{cmdline[0]} not found in PATH.")
 
 
-def run_with_backoff(
-    cmdline: Sequence[PathString],
-    check: bool = True,
-    delay_interrupt: bool = True,
-    stdout: _FILE = None,
-    stderr: _FILE = None,
-    *,
-    attempts: int,
-    **kwargs: Any,
-) -> CompletedProcess:
-    delay = 0.0
-    for attempt in range(attempts):
-        try:
-            return run(cmdline, check, delay_interrupt, stdout, stderr, **kwargs)
-        except subprocess.CalledProcessError:
-            if attempt == attempts - 1:
-                raise
-            time.sleep(delay)
-            delay = min(delay * 2 + 0.01, 1)
-
-    assert False  # make mypy happy
-
-
 def tmp_dir() -> Path:
     path = os.environ.get("TMPDIR") or "/var/tmp"
     return Path(path)