assembler/qemu: install_grub uses partitions

Use the newly available partition information in the install_grub2
method: detect which module to use for the root filesystem and
assert the second stage fits between the MBR and the first partition.
This commit is contained in:
Christian Kellner 2019-12-11 19:42:06 +01:00 committed by Tom Gundersen
parent 9863b5ad10
commit 9688859acf

View file

@ -82,7 +82,7 @@ def mkfs_for_type(device, uuid, fs_type):
elif fs_type == "xfs":
maker = mkfs_xfs
else:
raise ValueError("Unknown filesystem type")
raise ValueError(f"Unknown filesystem type '{fs_type}'")
maker(device, uuid)
@ -112,10 +112,23 @@ def create_partition_table(image, options):
return partitions
def install_grub2(image, fs_module, partition_offset):
def install_grub2(image, partitions):
"""Install grub2 to image"""
grub2_core = "/var/tmp/grub2-core.img"
root_fs_type = "unknown"
for p in partitions:
if p["filesystem"]["mountpoint"] == "/":
root_fs_type = p["filesystem"]["type"]
break
if root_fs_type == "ext4":
fs_module = "ext2"
elif root_fs_type == "xfs":
fs_module = "xfs"
else:
raise ValueError(f"unknown root filesystem type: '{root_fs_type}'")
# Create the level-2 bootloader
# The purpose of this is to find the grub modules and configuration
# to be able to start the level-3 bootloader. It contains the modules
@ -130,6 +143,7 @@ def install_grub2(image, fs_module, partition_offset):
"part_msdos", fs_module, "biosdisk"],
check=True)
partition_offset = partitions[0]["start"]
assert os.path.getsize(grub2_core) < partition_offset - 512
with open(image, "rb+") as image_f:
@ -153,7 +167,6 @@ def main(tree, output_dir, options, loop_client):
fmt = options["format"]
filename = options["filename"]
size = options["size"]
root_fs_type = options.get("root_fs_type", "ext4")
# sfdisk works on sectors of 512 bytes and ignores excess space - be explicit about this
if size % 512 != 0:
@ -162,15 +175,6 @@ def main(tree, output_dir, options, loop_client):
if fmt not in ["raw", "raw.xz", "qcow2", "vdi", "vmdk", "vpc"]:
raise ValueError("`format` must be one of raw, qcow, vdi, vmdk, vpc")
if root_fs_type == "ext4":
mkfs = mkfs_ext4
grub2_fs_module = "ext2"
elif root_fs_type == "xfs":
mkfs = mkfs_xfs
grub2_fs_module = "xfs"
else:
raise ValueError("`root_fs_type` must be either ext4 or xfs")
image = "/var/tmp/osbuild-image.raw"
# Create an empty image file
@ -178,10 +182,9 @@ def main(tree, output_dir, options, loop_client):
# The partition table
partitions = create_partition_table(image, options)
partition_offset = partitions[0]["start"]
# Create the level-2 bootloader
install_grub2(image, grub2_fs_module, partition_offset)
install_grub2(image, partitions)
# Now assemble the filesystem hierarchy and copy the tree into the image
with contextlib.ExitStack() as cm: