DeaD_EyE icon

mkroot for arch

DeaD_EyE | PRO | 03/23/22 08:22:07 PM UTC | 0 ⭐ | 1710 👁️ | Never ⏰ | []
Python |

2.63 KB

|

None

|

0 👍

/

0 👎

#!/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 <<EOF
  2048
  EOF
- create a fs on new created partition:
  mkfs.ext4 /dev/loopXXp1
- sudo mount /dev/loopXXp1 /somewhere...
- run chroot:
  sudo arch-chroot /somewhere... <<EOF
  # shell commands to execute in chroot
  EOF
 
This is the first part.
 
ToDO:
 
- Download micropython and esp32-idf with git
  https://github.com/micropython/micropython/blob/master/ports/esp32/README.md
- compile it in new environment
- flash esp32
- catch all exceptions
"""
 
 
import subprocess
from pathlib import Path
 
ARCH_PACKAGES = (
    "base",
    "linux",
    "base-devel",
    "cmake",
    "python",
    "python-pip",
    "python-virtualenv",
    "wget",
    "git",
    "libusb",
)
 
 
def mkimg(image: str | Path, size: str):
    command = ["truncate", "--size", size, image]
    subprocess.run(command, check=True)
 
 
def mkpart(image: str | Path) -> 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)
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎