DeaD_EyE icon

mkroot for arch 2 + esp32 + rp2

DeaD_EyE | PRO | 03/29/22 08:47:27 AM UTC (Edited) | 0 ⭐ | 1640 👁️ | Never ⏰ | []
Python |

3.95 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env python3
 
"""
The target was to create an independent build environment
for micropython-esp32 (master branch) 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
- Download micropython and esp32-idf with git
  https://github.com/micropython/micropython/blob/master/ports/esp32/README.md
- compile it in new environment
 
ToDO:
- flash esp32
- catch all exceptions
"""
 
import subprocess
from pathlib import Path
from textwrap import dedent
 
 
ARCH_PACKAGES = (
    "base",
    "linux",
    "base-devel",
    "arm-none-eabi-gcc",
    "arm-none-eabi-newlib",
    "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 = dedent("""
    #!/bin/bash
    useradd -m build
    usermod -a -G uucp build
    cat > /home/build/compile.sh <<EOF
    #!/bin/bash
 
    set -e
 
    git clone --depth 1 https://github.com/micropython/micropython.git
    git clone --depth 1 -b v4.4 --recursive https://github.com/espressif/esp-idf.git
    pushd esp-idf
    git checkout v4.4
    git submodule update --depth 1 --init --recursive
    ./install.sh
    source export.sh
    popd
 
    cd micropython
    make -C mpy-cross
    cd ports/esp32
    make submodules
    make
    EOF
 
    chmod +x /home/build/compile.sh
    su - build /home/build/compile.sh
    """)
 
    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)
 
 
# OUTPUT
# (seems to work)
 
# Project build complete. To flash, run this command:
# /home/build/.espressif/python_env/idf4.4_py3.10_env/bin/python ../../../esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 460800 --before default_reset --after hard_reset --chip esp32  write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build-GENERIC/bootloader/bootloader.bin 0x8000 build-GENERIC/partition_table/partition-table.bin 0x10000 build-GENERIC/micropython.bin
# or run 'idf.py -p (PORT) flash'
# bootloader  @0x001000    21584  (    7088 remaining)
# partitions  @0x008000     3072  (    1024 remaining)
# application @0x010000  1466208  (  565408 remaining)
# total                  1531744

Comments

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

    0 B

    |

    👍

    /

    👎