pmbootstrap/pmb/parse/arch.py

177 lines
5.0 KiB
Python
Raw Normal View History

2017-05-26 20:08:45 +00:00
"""
Copyright 2018 Oliver Smith
2017-05-26 20:08:45 +00:00
This file is part of pmbootstrap.
pmbootstrap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pmbootstrap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pmbootstrap. If not, see <http://www.gnu.org/licenses/>.
"""
import platform
import fnmatch
def alpine_native():
machine = platform.machine()
ret = ""
mapping = {
"i686": "x86",
"x86_64": "x86_64",
"aarch64": "aarch64",
"armv7l": "armhf"
}
if machine in mapping:
return mapping[machine]
raise ValueError("Can not map platform.machine '" + machine + "'"
" to the right Alpine Linux architecture")
2017-05-26 20:08:45 +00:00
return ret
def from_chroot_suffix(args, suffix):
if suffix == "native":
return args.arch_native
if suffix == "rootfs_" + args.device:
return args.deviceinfo["arch"]
if suffix.startswith("buildroot_"):
return suffix.split("_", 1)[1]
2017-05-26 20:08:45 +00:00
raise ValueError("Invalid chroot suffix: " + suffix +
" (wrong device chosen in 'init' step?)")
def alpine_to_debian(arch):
"""
Convert the architecture to the string used in the binfmt info
(aka. the Debian architecture format).
"""
mapping = {
"x86": "i386",
"x86_64": "x86_64",
2017-05-26 20:08:45 +00:00
"armhf": "arm",
"aarch64": "aarch64",
2017-05-26 20:08:45 +00:00
}
for pattern, arch_debian in mapping.items():
if fnmatch.fnmatch(arch, pattern):
return arch_debian
raise ValueError("Can not map Alpine architecture '" + arch + "'"
2017-05-26 20:08:45 +00:00
" to the right Debian architecture.")
def alpine_to_kernel(arch):
"""
Convert the architecture to the string used inside the kernel sources.
You can read the mapping from the linux-vanilla APKBUILD for example.
"""
mapping = {
"aarch64*": "arm64",
"arm*": "arm",
"ppc*": "powerpc",
"s390*": "s390"
}
for pattern, arch_kernel in mapping.items():
if fnmatch.fnmatch(arch, pattern):
return arch_kernel
return arch
def alpine_to_hostspec(arch):
"""
See: abuild source code/functions.sh.in: arch_to_hostspec()
"""
mapping = {
"aarch64": "aarch64-alpine-linux-musl",
"armhf": "armv6-alpine-linux-muslgnueabihf",
"armv7": "armv7-alpine-linux-musleabihf",
"ppc": "powerpc-alpine-linux-musl",
"ppc64": "powerpc64-alpine-linux-musl",
"ppc64le": "powerpc64le-alpine-linux-musl",
"s390x": "s390x-alpine-linux-musl",
"x86": "i586-alpine-linux-musl",
"x86_64": "x86_64-alpine-linux-musl",
2017-05-26 20:08:45 +00:00
}
if arch in mapping:
return mapping[arch]
raise ValueError("Can not map Alpine architecture '" + arch + "'"
2017-05-26 20:08:45 +00:00
" to the right hostspec value")
def cpu_emulation_required(args, arch):
# Obvious case: host arch is target arch
if args.arch_native == arch:
return False
# Other cases: host arch on the left, target archs on the right
not_required = {
"x86_64": ["x86"],
"aarch64": ["armel", "armhf", "armv7"],
}
if args.arch_native in not_required:
if arch in not_required[args.arch_native]:
return False
# No match: then it's required
return True
Close #226: Launch postmarketOS in a qemu virtual machine (#350) Thanks to Pablo Castellano and Martijn Braam! In postmarketOS we are now able to generate system images with the correct configuration so that they can boot already using qemu This commit brings the `pmbootstrap qemu` action. This command is very handy because you don't have to set all the qemu parameters, pmbootstrap does it for you. * device-qemu-vexpress: Added kernel command line according to wiki * qemu: Added workaround for image writing permissions * qemu: Added support to launch postmarketOS in a QEMU virtual machine - Support for emulating these architectures in QEMU: arm, aarch64, x86_84 - Generate QEMU command correctly depending no guest architecture (arm/x86) - Run QEMU in the same architecture as the host by default - Refactoring in pmb.parse.arch and pmb.qemu.run - Raise exception if DTB file or system image are not present - Display more useful information when something fails (e.g. image not found) - Run qemu version depending on arch (host or argument), not device configured * device-qemu-amd64: set deviceinfo_kernel_cmdline to "PMOS_NO_OUTPUT_REDIRECT" * qemu: added --memory argument to specific guest RAM * device-qemu-amd64: adjusted deviceinfo_kernel_cmdline (console=tty1) * Added /etc/network/interfaces for qemu-amd64 * qemu: Added KVM support if /dev/kvm if present * Specify separate machines for architecture * qemu: Check if QEMU is installed instead of crashing * Added graphics driver to qemu-aarch64 - Use arm (as used in qemu) instead of armhf (used in Alpine) - qemu argument is -dtb - Follow same style to build the command + arguments * qemu: Added SSH port redirection: ./pmbootstrap.py qemu -p 2222
2017-08-09 20:26:40 +00:00
def uname_to_qemu(arch):
"""
Convert the most common architectures returned by 'uname' to those
used by the QEMU binary
"""
mapping = {
"aarch64": "aarch64",
"arm": "arm",
"armeb": "arm",
"armel": "arm",
"armhf": "arm",
"x86_64": "x86_64",
"amd64": "x86_64",
}
if arch in mapping:
return mapping[arch]
raise ValueError("Can not map host architecture '" + arch + "'"
" to the right QEMU value")
def qemu_to_pmos_device(arch):
"""
Convert the architecture used in the QEMU binary to the aport name in
postmarketOS defining the device
"""
mapping = {
"arm": "qemu-vexpress",
"aarch64": "qemu-aarch64",
"x86_64": "qemu-amd64",
}
if arch in mapping:
return mapping[arch]
raise ValueError("Can not map QEMU value '" + arch + "'"
" to the right postmarketOS device")
def qemu_check_device(device, arch):
"""
Check whether a device has a specific architecture.
Examples:
qemu_check_device("qemu-amd64", "x86_64") is True
qemu_check_device("qemu-vexpress", "armel") is True
qemu_check_device("qemu-vexpress", "aarch64") is False
"""
arch_qemu = uname_to_qemu(arch)
return device == qemu_to_pmos_device(arch_qemu)