From 6dc45a80e764eacd6e311c32164b623bbe7f8e08 Mon Sep 17 00:00:00 2001 From: Mohsen Tahmasebi Date: Mon, 3 Apr 2023 11:57:59 +0330 Subject: [PATCH] Register binfmt_misc entry for docker build (#685) --- build-docker.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/build-docker.sh b/build-docker.sh index 01b9f8e..5495dfa 100755 --- a/build-docker.sh +++ b/build-docker.sh @@ -107,6 +107,40 @@ else ) fi +# Check if binfmt_misc is required +binfmt_misc_required=1 +case $(uname -m) in + aarch64) + binfmt_misc_required=0 + ;; + arm*) + binfmt_misc_required=0 + ;; +esac + +# Check if qemu-arm-static and /proc/sys/fs/binfmt_misc are present +if [[ "${binfmt_misc_required}" == "1" ]]; then + if ! qemu_arm=$(which qemu-arm-static) ; then + echo "qemu-arm-static not found (please install qemu-user-static)" + exit 1 + fi + if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then + echo "binfmt_misc required but not mounted, trying to mount it..." + if ! mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc ; then + echo "mounting binfmt_misc failed" + exit 1 + fi + echo "binfmt_misc mounted" + fi + # Register qemu-arm for binfmt_misc (binfmt_misc won't care duplicate entries unless they have common names) + reg="echo ':qemu-arm-rpi:M::"\ +"\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:"\ +"\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:"\ +"$qemu_arm:F' > /proc/sys/fs/binfmt_misc/register" + echo "Registering qemu-arm for binfmt_misc..." + sudo bash -c "$reg" 2>/dev/null || true +fi + trap 'echo "got CTRL+C... please wait 5s" && ${DOCKER} stop -t 5 ${DOCKER_CMDLINE_NAME}' SIGINT SIGTERM time ${DOCKER} run \ "${DOCKER_CMDLINE_PRE[@]}" \