mirror of
https://github.com/rosalinux/image-builder.git
synced 2025-02-23 02:12:52 +00:00
add mount and properly cleanup /dev/loopX devices
This commit is contained in:
parent
4d5bf601ee
commit
d6c3da6a4f
2 changed files with 32 additions and 4 deletions
3
build.py
3
build.py
|
@ -7,7 +7,7 @@ import multiprocessing
|
|||
from utils.bootstrap_setup import setup_bootstrap
|
||||
from utils.common import load_config
|
||||
from utils.make_disk import create_disk_image, setup_loop_device
|
||||
from utils.make_disk import create_partitions
|
||||
from utils.make_disk import create_partitions, mount_partitions
|
||||
|
||||
BASE_DIR = os.getcwd()
|
||||
TMP_DIR = os.path.join(BASE_DIR, "tmp")
|
||||
|
@ -95,6 +95,7 @@ def main():
|
|||
loop_device = setup_loop_device(disk_image_path)
|
||||
print(f"Loop device setup at {loop_device}")
|
||||
create_partitions(loop_device, config)
|
||||
mount_partitions(config, loop_device, TMP_DIR, vendor, device)
|
||||
|
||||
print(f"Build completed for {vendor}/{device} with distro {distro}")
|
||||
|
||||
|
|
|
@ -31,10 +31,19 @@ def create_disk_image(tmp_dir, config, vendor, device):
|
|||
return disk_image_path
|
||||
|
||||
|
||||
def cleanup_loop_devices():
|
||||
result = subprocess.run(["losetup", "-l", "-O", "NAME"], capture_output=True, text=True)
|
||||
active_loops = result.stdout.splitlines()[1:]
|
||||
|
||||
for loop_device in active_loops:
|
||||
loop_device = loop_device.strip()
|
||||
if loop_device:
|
||||
print(f"Detaching {loop_device}")
|
||||
subprocess.run(["sudo", "losetup", "-d", loop_device], check=False)
|
||||
|
||||
|
||||
def setup_loop_device(disk_image_path):
|
||||
print("Cleanup /dev/loopX leftovers")
|
||||
leftovers = ["sudo", "losetup", "-d", "/dev/loop*"]
|
||||
subprocess.run(leftovers, check=False)
|
||||
cleanup_loop_devices()
|
||||
|
||||
cmd = ["sudo", "losetup", "-fP", "--show", disk_image_path]
|
||||
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
||||
|
@ -69,3 +78,21 @@ def create_partitions(loop_device, config):
|
|||
subprocess.run(["sudo", "mkfs.ext4", f"{loop_device}p1"], check=True)
|
||||
|
||||
print("Partitioning and formatting complete.")
|
||||
|
||||
|
||||
def mount_partitions(config, loop_device, tmp_dir, vendor, device):
|
||||
rootfs_dir = os.path.join(tmp_dir, vendor, device, "rootfs")
|
||||
os.makedirs(rootfs_dir, exist_ok=True)
|
||||
boot_partition = f"{loop_device}p1"
|
||||
root_partition = f"{loop_device}p2" if "BOOT_SIZE" in config else f"{loop_device}p1"
|
||||
|
||||
if "BOOT_SIZE" in config:
|
||||
boot_dir = os.path.join(rootfs_dir, "boot")
|
||||
os.makedirs(boot_dir, exist_ok=True)
|
||||
|
||||
print(f"Mounting /boot partition at {boot_dir}")
|
||||
subprocess.run(["sudo", "mount", boot_partition, boot_dir], check=True)
|
||||
|
||||
print(f"Mounting root (/) partition at {rootfs_dir}")
|
||||
subprocess.run(["sudo", "mount", root_partition, rootfs_dir], check=True)
|
||||
print("Mounting complete.")
|
||||
|
|
Loading…
Add table
Reference in a new issue