lib.sh: update binfmt handling, add riscv

recent qemu updates mean we don't need to hardcode the binfmts, as they
are shipped as files generated by qemu, importable by update-binfmts.

ref: #386
This commit is contained in:
classabbyamp 2024-12-26 12:13:26 -05:00 committed by classabbyamp
parent 20b42c6218
commit 66d1c54261
2 changed files with 9 additions and 18 deletions

26
lib.sh
View File

@ -210,52 +210,39 @@ register_binfmt() {
return return
fi fi
_cpu=arm _cpu=arm
_magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
;; ;;
aarch64) aarch64)
_cpu=aarch64 _cpu=aarch64
_magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
;; ;;
ppc64le) ppc64le)
_cpu=ppc64le _cpu=ppc64le
_magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00"
;; ;;
ppc64) ppc64)
_cpu=ppc64 _cpu=ppc64
_magic="\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
;; ;;
ppc) ppc)
if [ "$_hostarch" = "ppc64" ] ; then if [ "$_hostarch" = "ppc64" ] ; then
return return
fi fi
_cpu=ppc _cpu=ppc
_magic="\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
;; ;;
mipsel) mipsel)
if [ "$_hostarch" = "mips64el" ] ; then if [ "$_hostarch" = "mips64el" ] ; then
return return
fi fi
_cpu=mipsel _cpu=mipsel
_magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
_mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
;; ;;
x86_64) x86_64)
_cpu=x86_64 _cpu=x86_64
_magic="\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00"
_mask="\xff\xff\xff\xff\xff\xfe\xfe\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
;; ;;
i686) i686)
if [ "$_hostarch" = "x86_64" ] ; then if [ "$_hostarch" = "x86_64" ] ; then
return return
fi fi
_cpu=i386 _cpu=i386
_magic="\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00" ;;
_mask="\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff" riscv64)
_cpu=riscv64
;; ;;
*) *)
die "Unknown target architecture!" die "Unknown target architecture!"
@ -264,7 +251,7 @@ register_binfmt() {
# For builds that do not match the host architecture, the correct # For builds that do not match the host architecture, the correct
# qemu binary will be required. # qemu binary will be required.
QEMU_BIN="qemu-${_cpu}-static" QEMU_BIN="qemu-${_cpu}"
if ! $QEMU_BIN -version >/dev/null 2>&1; then if ! $QEMU_BIN -version >/dev/null 2>&1; then
die "$QEMU_BIN binary is missing in your system, exiting." die "$QEMU_BIN binary is missing in your system, exiting."
fi fi
@ -278,7 +265,10 @@ register_binfmt() {
# Only register if the map is incomplete # Only register if the map is incomplete
if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
echo ":qemu-$_cpu:M::$_magic:$_mask:/usr/bin/$QEMU_BIN:F" > /proc/sys/fs/binfmt_misc/register 2>/dev/null if ! command -v update-binfmts >/dev/null 2>&1; then
die "could not add binfmt: update-binfmts binary is missing in your system"
fi
update-binfmts --import "qemu-$_cpu"
fi fi
} }

View File

@ -57,6 +57,7 @@ usage() {
aarch64, aarch64-musl, aarch64, aarch64-musl,
mipsel, mipsel-musl, mipsel, mipsel-musl,
ppc, ppc-musl, ppc64le, ppc64le-musl, ppc64, ppc64-musl ppc, ppc-musl, ppc64le, ppc64le-musl, ppc64, ppc64-musl
riscv64, riscv64-musl
OPTIONS OPTIONS
-b <system-pkg> Set an alternative base-system package (default: base-container-full) -b <system-pkg> Set an alternative base-system package (default: base-container-full)