Advertisement
DeaD_EyE

mkroot for arch

Mar 23rd, 2022
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. The target was to create an independent build environment
  5. for micropython-esp32 on Arch-Linux.
  6.  
  7. - create image-file:
  8.  # no sudo
  9.  truncate -- size 16G image.img
  10.  # user still owns the image.img
  11. - sudo mount image-file as block device: losetup -f -P --show image.img
  12. - create a new partition:
  13.  sudo sfdisk /dev/loopXX <<EOF
  14.  2048
  15.  EOF
  16. - create a fs on new created partition:
  17.  mkfs.ext4 /dev/loopXXp1
  18. - sudo mount /dev/loopXXp1 /somewhere...
  19. - run chroot:
  20.  sudo arch-chroot /somewhere... <<EOF
  21.  # shell commands to execute in chroot
  22.  EOF
  23.  
  24. This is the first part.
  25.  
  26. ToDO:
  27.  
  28. - Download micropython and esp32-idf with git
  29.  https://github.com/micropython/micropython/blob/master/ports/esp32/README.md
  30. - compile it in new environment
  31. - flash esp32
  32. - catch all exceptions
  33. """
  34.  
  35.  
  36. import subprocess
  37. from pathlib import Path
  38.  
  39. ARCH_PACKAGES = (
  40.     "base",
  41.     "linux",
  42.     "base-devel",
  43.     "cmake",
  44.     "python",
  45.     "python-pip",
  46.     "python-virtualenv",
  47.     "wget",
  48.     "git",
  49.     "libusb",
  50. )
  51.  
  52.  
  53. def mkimg(image: str | Path, size: str):
  54.     command = ["truncate", "--size", size, image]
  55.     subprocess.run(command, check=True)
  56.  
  57.  
  58. def mkpart(image: str | Path) -> str:
  59.     losetup = ["sudo", "losetup", "-f", "-P", "--show", image]
  60.     loop_dev = subprocess.run(
  61.         losetup, encoding="ascii", check=True, capture_output=True
  62.     ).stdout.strip()
  63.  
  64.     sfdisk_input = "2048\n"
  65.     sfdisk = ["sudo", "sfdisk", loop_dev]
  66.     subprocess.run(sfdisk, input=sfdisk_input, check=True, encoding="ascii")
  67.     return f"{loop_dev}p1"
  68.  
  69.  
  70. def mkfs(partition: str | Path):
  71.     mkfs = ["sudo", "mkfs.ext4", partition]
  72.     subprocess.run(mkfs, check=True)
  73.  
  74.  
  75. def mount(src, dst):
  76.     command = ["sudo", "mount", src, dst]
  77.     subprocess.run(command, check=True)
  78.  
  79.  
  80. def pacstrap(root: str | Path):
  81.     if not root.is_absolute():
  82.         raise RuntimeError("The supplied path is not an absolute path.")
  83.     command = ["sudo", "pacstrap", root, *ARCH_PACKAGES]
  84.     subprocess.run(command, check=True)
  85.  
  86.  
  87. def chroot(root: str | Path, script: str):
  88.     command = ["sudo", "arch-chroot", root]
  89.     subprocess.run(command, encoding="utf8", check=True, input=script)
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     # script-file is on ToDo
  94.     SCRIPT = """
  95.    useradd -m build
  96.    """
  97.     # you have to change the following Paths
  98.     ROOT = Path("/home/deadeye/Downloads/rootfs")
  99.     IMAGE = Path("/home/deadeye/Downloads/TEST.img")
  100.     IMAGE_SIZE = "16G"
  101.     mkimg(IMAGE, IMAGE_SIZE)
  102.     partition = mkpart(IMAGE)
  103.     mkfs(partition)
  104.     mount(partition, ROOT)
  105.     pacstrap(ROOT)
  106.     chroot(ROOT, SCRIPT)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement