diff --git a/assemblers/org.osbuild.qemu b/assemblers/org.osbuild.qemu index f105775d..99e772f0 100755 --- a/assemblers/org.osbuild.qemu +++ b/assemblers/org.osbuild.qemu @@ -5,6 +5,7 @@ import json import os import socket import shutil +import struct import subprocess import sys import tempfile @@ -269,6 +270,14 @@ class PartitionTable: return part return None + def find_bios_boot_partition(self) -> Partition: + """Find the BIOS-boot Partition""" + bb_type = "21686148-6449-6E6F-744E-656564454649" + for part in self.partitions: + if part.type.upper() == bb_type: + return part + return None + def write_to(self, target, sync=True): """Write the partition table to disk""" # generate the command for sfdisk to create the table @@ -394,6 +403,51 @@ def grub2_write_core_prep_part(core_f: BinaryIO, shutil.copyfileobj(core_f, image_f) +def grub2_write_core_bios_boot(core_f: BinaryIO, + image_f: BinaryIO, + pt: PartitionTable): + """Write the core to the bios boot partition""" + bb = pt.find_bios_boot_partition() + if bb is None: + raise ValueError("BIOS-boot partition missing") + core_size = os.fstat(core_f.fileno()).st_size + if bb.size_in_bytes < core_size: + raise ValueError("BIOS-boot partition too small") + + image_f.seek(bb.start_in_bytes) + shutil.copyfileobj(core_f, image_f) + + # The core image needs to know from where to load its + # second sector so that information needs to be embedded + # into the image itself at the right location, i.e. + # the "sector start parameter" ("size .long 2, 0"): + # 0x200 - GRUB_BOOT_MACHINE_LIST_SIZE (12) = 0x1F4 = 500 + image_f.seek(bb.start_in_bytes + 500) + image_f.write(struct.pack("