#!/usr/bin/env python3 """ The target was to create an independent build environment for micropython-esp32 on Arch-Linux. - create image-file: # no sudo truncate -- size 16G image.img # user still owns the image.img - sudo mount image-file as block device: losetup -f -P --show image.img - create a new partition: sudo sfdisk /dev/loopXX < str: losetup = ["sudo", "losetup", "-f", "-P", "--show", image] loop_dev = subprocess.run( losetup, encoding="ascii", check=True, capture_output=True ).stdout.strip() sfdisk_input = "2048\n" sfdisk = ["sudo", "sfdisk", loop_dev] subprocess.run(sfdisk, input=sfdisk_input, check=True, encoding="ascii") return f"{loop_dev}p1" def mkfs(partition: str | Path): mkfs = ["sudo", "mkfs.ext4", partition] subprocess.run(mkfs, check=True) def mount(src, dst): command = ["sudo", "mount", src, dst] subprocess.run(command, check=True) def pacstrap(root: str | Path): if not root.is_absolute(): raise RuntimeError("The supplied path is not an absolute path.") command = ["sudo", "pacstrap", root, *ARCH_PACKAGES] subprocess.run(command, check=True) def chroot(root: str | Path, script: str): command = ["sudo", "arch-chroot", root] subprocess.run(command, encoding="utf8", check=True, input=script) if __name__ == "__main__": # script-file is on ToDo SCRIPT = """ useradd -m build """ # you have to change the following Paths ROOT = Path("/home/deadeye/Downloads/rootfs") IMAGE = Path("/home/deadeye/Downloads/TEST.img") IMAGE_SIZE = "16G" mkimg(IMAGE, IMAGE_SIZE) partition = mkpart(IMAGE) mkfs(partition) mount(partition, ROOT) pacstrap(ROOT) chroot(ROOT, SCRIPT)