]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add syscall machinery to call BLKRRPART and use it
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 21 Aug 2022 10:50:56 +0000 (12:50 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 29 Aug 2022 09:57:40 +0000 (11:57 +0200)
mkosi/backend.py
mkosi/syscall.py

index 6b3e42791389dcefcb196227ad3ddaa780f51aed..60c0b88c530736a12640da1af4da352cbb414404 100644 (file)
@@ -37,7 +37,11 @@ from typing import (
     cast,
 )
 
-from .syscall import blkpg_add_partition, blkpg_del_partition
+from .syscall import (
+    blkpg_add_partition,
+    blkpg_del_partition,
+    block_reread_partition_table,
+)
 
 PathString = Union[Path, str]
 
@@ -406,7 +410,7 @@ class PartitionTable:
         if 'disk' in ARG_DEBUG:
             print_between_lines(spec)
 
-        cmd: List[PathString] = ["sfdisk", "--color=never", "--no-reread", device]
+        cmd: List[PathString] = ["sfdisk", "--color=never", "--no-reread", "--no-tell-kernel", device]
         if quiet:
             cmd += ["--quiet"]
 
@@ -433,6 +437,17 @@ class PartitionTable:
                 for p in self.partitions.values():
                     blkpg_add_partition(f.fileno(), p.number, self.partition_offset(p), self.partition_size(p))
 
+                try:
+                    block_reread_partition_table(f.fileno())
+                except OSError as e:
+                    msg = f"Failed to reread partition table of {device}: {e.strerror}"
+                    # BLKRRPART fails with EINVAL if the operation is not supported, let's not fail if that's
+                    # the case.
+                    if e.errno == errno.EINVAL:
+                        warn(msg)
+                    else:
+                        die(msg)
+
 
 @dataclasses.dataclass
 class MkosiArgs:
index e3f364f1125357d35efbd9e0a792b337bb99a5cc..abefd4b55194a22f5a05ad51f02ac60f23129348 100644 (file)
@@ -83,3 +83,10 @@ FICLONE = _IOW(0x94, 9, "int")
 
 def reflink(oldfd: int, newfd: int) -> None:
     fcntl.ioctl(newfd, FICLONE, oldfd)
+
+
+BLKRRPART = _IO(0x12, 95)
+
+
+def block_reread_partition_table(fd: int) -> None:
+    fcntl.ioctl(fd, BLKRRPART, 0)