Skip to content

Commit 02c2e30

Browse files
committed
T7453: handle dynamic partition mapping in raw image build
Enhanced the raw image creation logic to dynamically detect and assign EFI and root partitions based on the number of partitions created by kpartx. - Supports both 2-partition and 3-partition layouts - Adds debug output for mapped partitions - Avoids hardcoded assumptions about partition order - Improves resilience in cloud-init and containerized build contexts Fixes build failure when /dev/loopXp3 is missing or not mapped properly. Signed-off-by: Gabin-CC <[email protected]>
1 parent 1cda2d4 commit 02c2e30

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

scripts/image-build/raw_image.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,34 @@ def create_raw_image(build_config, iso_file, work_dir):
232232
disk_details = parttable_create(con.loop_device, (int(build_config["disk_size"]) - 1) * 1024 * 1024)
233233

234234
# Map partitions using kpartx
235+
print("I: Mapping partitions using kpartx...")
235236
cmd(f"kpartx -av {con.loop_device}")
236237
cmd("udevadm settle")
237238

238-
# Resolve mapper names (example: /dev/mapper/loop0p2)
239+
cmd("ls -l /dev/mapper") # debug output
240+
241+
# Detect mapped partitions
239242
from glob import glob
243+
import time
244+
240245
mapper_base = os.path.basename(con.loop_device).replace("/dev/", "")
241246
mapped_parts = sorted(glob(f"/dev/mapper/{mapper_base}p*"))
242247

243-
if len(mapped_parts) < 3:
244-
raise RuntimeError("E: Expected at least 3 partitions created by kpartx")
245-
246-
disk_details.partition['efi'] = mapped_parts[1]
247-
disk_details.partition['root'] = mapped_parts[2]
248+
if not mapped_parts:
249+
raise RuntimeError(f"E: No partitions were found in /dev/mapper for {mapper_base}")
250+
251+
print(f"I: Found mapped partitions: {mapped_parts}")
252+
253+
if len(mapped_parts) == 2:
254+
# Assume [0] = EFI, [1] = root
255+
disk_details.partition['efi'] = mapped_parts[0]
256+
disk_details.partition['root'] = mapped_parts[1]
257+
elif len(mapped_parts) >= 3:
258+
# Common layout: [1] = EFI, [2] = root (skip 0 if it's BIOS boot)
259+
disk_details.partition['efi'] = mapped_parts[1]
260+
disk_details.partition['root'] = mapped_parts[2]
261+
else:
262+
raise RuntimeError(f"E: Unexpected partition layout: {mapped_parts}")
248263

249264
con.disk_details = disk_details
250265
mount_image(con)

0 commit comments

Comments
 (0)