Por circunstancias, hay veces que necesito compilar algo para una arquitectura para la que no tengo disponible un equipo físico (por ejemplo, si quiero compilar algo para una SetTopBox con un procesador… MIPS o PowerPC). Para resolver ese problema, he escrito este pequeño script que permite lanzar una Debian de cualquier arquitectura soportada.
#!/usr/bin/env python3
import sys
import os
import shutil
import subprocess
import logging
versions = {
4: "etch",
5: "lenny",
6: "squeeze",
7: "wheezy",
8: "jessie",
9: "stretch",
10: "buster",
11: "bullseye",
12: "bookworm",
13: "trixie",
14: "forky",
15: "duke"
}
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
def usage():
print("Usage: multiarch-chroot ARCH DEBIAN-VERSION PATH")
print(" Creates a chroot environment")
print(" ARCH can be powerpc, arm, arm64...")
print(" DEBIAN-VERSION can be either a version number, or the version name (like trixie, or buster)")
print(" PATH is the path where to create the CHROOT environment with the operating system files et al.")
print("")
print("multiarch-chroot launch PATH")
print(" Launches a chroot environment in the specified path")
sys.exit(-1)
def run_external_program(command, show_msg = True):
if show_msg:
log.info(f"Launching external command {command}")
return subprocess.run(command, shell=True).returncode
def download_debian(arch, version, path):
try:
version_number = int(version)
version = versions[version_number]
except:
pass
command = "apt install -y qemu-user"
run_external_program(command)
command = f"debootstrap --foreign --variant=buildd --arch={arch} {version} {path} http://http.debian.net/debian/"
run_external_program(command)
shutil.copy(f"/usr/bin/qemu-{arch}", f"{os.path.join(path, 'usr/bin')}")
command = f"chroot {path} /debootstrap/debootstrap --second-stage"
run_external_program(command)
if os.getuid() != 0:
print("This program must be run as root")
sys.exit(-1)
if (len(sys.argv) != 4) and (len(sys.argv) != 3):
usage()
if len(sys.argv) == 4:
download_debian(sys.argv[1], sys.argv[2], sys.argv[3])
sys.exit(0)
if sys.argv[1] != "launch":
usage()
run_external_program(f"chroot {sys.argv[2]} /bin/sh")
Para usarlo, primero se debe ejecutar con tres parámetros: la arquitectura (por ejemplo arm64), la versión de Debian a usar (puede ser tanto en número como en nombre, por ejemplo jessie), y la ruta donde crear el sistema que se ejecutará dentro del chroot.
Una vez que ya tenemos el sistema operativo generado, sólo tenemos que ejecutarlo, también como root, con el parámetro launch y la ruta, y entraremos dentro de la máquina virtual donde podremos instalar paquetes, compilar cosas, etc.
CHROOT en otras arquitecturas por A cuadros está licenciado bajo una Licencia Creative Commons Atribución-CompartirIgual 4.0 Internacional.