Skip to content

Instantly share code, notes, and snippets.

@linki
Forked from ArturKlauser/check-qemu-binfmt.sh
Created March 22, 2022 10:44
Show Gist options
  • Select an option

  • Save linki/fcc57bc4fb2bc8e72906d507ce712f22 to your computer and use it in GitHub Desktop.

Select an option

Save linki/fcc57bc4fb2bc8e72906d507ce712f22 to your computer and use it in GitHub Desktop.

Revisions

  1. linki revised this gist Mar 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check-qemu-binfmt.sh
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ function check_qemu_binfmt() {
    # Kernel
    kernel_version="$(uname -r)"
    if [[ "$(version "$kernel_version")" < "$(version '4.8')" ]]; then
    error "Kernel $kernel_version too old - need >= 4.8." \
    echo "Kernel $kernel_version too old - need >= 4.8." \
    " Install a newer kernel."
    else
    ok "kernel $kernel_version has binfmt_misc fix-binary (F) support."
  2. @ArturKlauser ArturKlauser revised this gist Nov 6, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion check-qemu-binfmt.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/bin/bash
    # (c) [email protected]
    # (c) 2020 [email protected]
    # SPDX-License-Identifier: Apache-2.0 OR MIT
    #
    # This script checks if all software requirements are met in a Linux environment
    # in order to use 'docker buildx' to build multi-architecture images.
  3. @ArturKlauser ArturKlauser created this gist Jan 18, 2020.
    97 changes: 97 additions & 0 deletions check-qemu-binfmt.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    #!/bin/bash
    # (c) [email protected]
    #
    # This script checks if all software requirements are met in a Linux environment
    # in order to use 'docker buildx' to build multi-architecture images.
    # For more information see:
    # https://nexus.eddiesinentropy.net/2020/01/12/Building-Multi-architecture-Docker-Images-With-Buildx/

    function error() {
    echo "ERROR: $*"
    exit 1
    }

    function ok() {
    echo "OK: $*"
    }

    function version() {
    printf '%02d' $(echo "$1" | tr . ' ' | sed -e 's/ 0*/ /g') 2>/dev/null
    }

    function check_qemu_binfmt() {
    # Docker
    if ! command -v docker >/dev/null 2>&1; then
    error "Can't find docker." \
    "Install with 'sudo apt-get install docker-ce' or docker.io."
    fi
    docker_version="$(docker --version | cut -d' ' -f3 | tr -cd '0-9.')"
    if [[ "$(version "$docker_version")" < "$(version '19.03')" ]]; then
    error "docker $docker_version too old. Need >= 19.03"
    fi
    docker_experimental="$(docker version | \
    awk '/^ *Experimental:/ {print $2 ; exit}')"
    if [[ "$docker_experimental" != 'true' ]]; then
    error "docker experimental flag not enabled:"\
    "Set with 'export DOCKER_CLI_EXPERIMENTAL=enabled'"
    else
    ok "docker $docker_version supports buildx experimental feature."
    fi

    # Kernel
    kernel_version="$(uname -r)"
    if [[ "$(version "$kernel_version")" < "$(version '4.8')" ]]; then
    error "Kernel $kernel_version too old - need >= 4.8." \
    " Install a newer kernel."
    else
    ok "kernel $kernel_version has binfmt_misc fix-binary (F) support."
    fi

    # binfmt_misc file system
    if [[ "$(mount | grep -c '/proc/sys/fs/binfmt_misc')" == '0' ]]; then
    error '/proc/sys/fs/binfmt_misc not mounted. Mount with' \
    "'sudo mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc'"
    else
    ok "/proc/sys/fs/binfmt_misc is mounted"
    fi

    # binfmt-support
    if ! command -v update-binfmts >/dev/null 2>&1; then
    error "Can't find update-binfmts." \
    "Install with 'sudo apt-get install binfmt-support'."
    fi
    binfmt_version="$(update-binfmts --version | awk '{print $NF}')"
    if [[ "$(version "$binfmt_version")" < "$(version '2.1.7')" ]]; then
    error "update-binfmts $binfmt_version too old. Need >= 2.1.7"
    else
    ok "update-binfmts $binfmt_version has fix-binary (F) support."
    fi

    # QEMU
    if [[ ! -e '/proc/sys/fs/binfmt_misc/qemu-aarch64' ]]; then
    # Skip this test if QEMU isn't registered with binfmt_misc. It might
    # come from a docker image rather than the host file system.
    if [[ ! -e '/usr/bin/qemu-aarch64-static' ]]; then
    error "Missing QEMU." \
    " Install with 'sudo apt-get install qemu-user-static'."
    else
    ok "QEMU installed"
    fi
    fi

    if [[ ! -e '/proc/sys/fs/binfmt_misc/qemu-aarch64' ]]; then
    error 'QEMU not registered in binfmt_misc.'
    fi
    flags="$(grep 'flags:' /proc/sys/fs/binfmt_misc/qemu-aarch64 | \
    cut -d' ' -f2)"
    if [[ "$(echo "$flags" | grep -c F)" == '0' ]]; then
    error 'QEMU not registered in binfmt_misc with fix-binary (F) flag.'
    else
    ok "QEMU registered in binfmt_misc with flags $flags (F is required)."
    fi

    echo "Host looks good for docker buildx multi-architecture support".
    }

    set -e
    check_qemu_binfmt "$@"