From 7ea901928d347a1d9520d59611ced16d6ad685b1 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Thu, 11 Jul 2019 00:03:34 +0200 Subject: [PATCH] assembler/qcow2: only require loopback for grub2 Create and instantiate the ext4 filesystem directly on the device, without ever mounting it. This means that only grub2 now requires loopback devices to function properly. Signed-off-by: Tom Gundersen --- assemblers/io.weldr.qcow2 | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/assemblers/io.weldr.qcow2 b/assemblers/io.weldr.qcow2 index aea8096b..74f6d689 100755 --- a/assemblers/io.weldr.qcow2 +++ b/assemblers/io.weldr.qcow2 @@ -61,18 +61,22 @@ def main(tree, output_dir, options): # Set up the partition table of the image partition_table = "label: mbr\nlabel-id: {partition_table_id}\nbootable, type=83" subprocess.run(["sfdisk", "-q", image], input=partition_table, encoding='utf-8', check=True) + r = subprocess.run(["sfdisk", "--json", image], stdout=subprocess.PIPE, encoding='utf-8', check=True) + partition_table = json.loads(r.stdout) + partition = partition_table["partitiontable"]["partitions"][0] + partition_offset = partition["start"] * 512 + partition_size = partition["size"] * 512 + + # Populate the first partition of the image with an ext4 fs and fill it with the contents of the + # tree we are operating on. + subprocess.run(["mkfs.ext4", "-d", tree, "-U", root_fs_uuid, "-E", f"offset={partition_offset}", image, f"{int(partition_size / 1024)}k"], input="y", encoding='utf-8', check=True) # Mount the created image as a loopback device - with loop_device(image, size) as loop: - # Populate the first partition of the image with an ext4 fs and fill it with the contents of the - # tree we are operating on. - subprocess.run(["mkfs.ext4", "-d", tree, "-U", root_fs_uuid, f"{loop}p1"], check=True) - - # Install grub2 into the boot sector of the image - # It is unclear why chrooting into the image is required. - with mount(f"{loop}p1", mountpoint): - with mount_api(mountpoint): - subprocess.run(["chroot", mountpoint, "grub2-install", "--no-floppy", "--target", "i386-pc", loop], check=True) + with loop_device(image, size) as loop, \ + mount(f"{loop}p1", mountpoint): + # Install grub2 into the boot sector of the image, and copy the grub2 imagise into /boot/grub2 + with mount_api(mountpoint): + subprocess.run(["chroot", mountpoint, "grub2-install", "--no-floppy", "--target", "i386-pc", loop], check=True) subprocess.run(["qemu-img", "convert", "-O" "qcow2", "-c", image, f"{output_dir}/{filename}"], check=True)