From 898aab8942331994e967d5fbbf858c568a654ad9 Mon Sep 17 00:00:00 2001 From: Alexander Stefanov Date: Wed, 13 Nov 2024 20:05:50 +0000 Subject: [PATCH] add make_disk include --- build.py | 7 +++++-- utils/make_disk.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 utils/make_disk.py diff --git a/build.py b/build.py index f48b34f..462f53a 100755 --- a/build.py +++ b/build.py @@ -6,7 +6,7 @@ import subprocess import multiprocessing from utils.bootstrap_setup import setup_bootstrap from utils.common import load_config -from utils.make_disk import create_disk_image +from utils.make_disk import create_disk_image, setup_loop_device BASE_DIR = os.getcwd() TMP_DIR = os.path.join(BASE_DIR, "tmp") @@ -85,7 +85,10 @@ def main(): else: print("Skipping rootfs bootstrap") - create_disk_image(TMP_DIR, config, vendor, device) + disk_image_path = create_disk_image(TMP_DIR, config, vendor, device) + if disk_image_path: + loop_device = setup_loop_device(disk_image_path) + print(f"Loop device setup at {loop_device}") print(f"Build completed for {vendor}/{device} with distro {distro}") diff --git a/utils/make_disk.py b/utils/make_disk.py new file mode 100644 index 0000000..77da31a --- /dev/null +++ b/utils/make_disk.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +import os +import subprocess + + +def create_disk_image(tmp_dir, config, vendor, device): + + boot_size = config.get("BOOT_SIZE").rstrip("MB") + root_size = config.get("ROOT_SIZE").rstrip("MB") + + if not root_size: + print("Error: ROOT_SIZE is not defined in the configuration.") + return + + if not boot_size: + boot_size = "0" + + disk_image_path = os.path.join(tmp_dir, vendor, device, "disk.img") + cmd = [ + "dd", + "if=/dev/zero", + f"of={disk_image_path}", + "bs=1M", + f"count={int(boot_size) + int(root_size)}" + ] + + print(f"Creating disk image: {disk_image_path} size {root_size} MB") + subprocess.run(cmd, check=True) + print(f"Disk image created at {disk_image_path}") + return disk_image_path + + +def setup_loop_device(disk_image_path): + print("Cleanup /dev/loopX leftovers") + leftovers = ["sudo", "losetup", "-d", "/dev/loop*"] + subprocess.run(leftovers, check=False) + + cmd = ["sudo", "losetup", "-fP", "--show", disk_image_path] + result = subprocess.run(cmd, check=True, capture_output=True, text=True) + loop_device = result.stdout.strip() + print(f"Disk image mounted to loop device {loop_device}") + return loop_device