Skip to content

Commit

Permalink
Fix qemu container integration
Browse files Browse the repository at this point in the history
  • Loading branch information
dbast committed Dec 12, 2022
1 parent 6636c68 commit a9af016
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.git*
boards
.packer_cache
packer_cache
scripts
*.img
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
uses: actions/checkout@v3.1.0

- name: Set up QEMU
# Required only for the multi-arch container builds that are pushed to Dockerhub.
# Setting up QEMU during normal PRs prevents testing of the container included
# QEMU setup.
if: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags') }}
uses: docker/setup-qemu-action@v2
with:
platforms: linux/amd64,linux/arm64
Expand Down
11 changes: 9 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
FROM tonistiigi/binfmt:qemu-v7.0.0 AS binfmt
# Source for qemu-user-static >7.0
FROM public.ecr.aws/ubuntu/ubuntu:kinetic as qemu_binaries

# hadolint ignore=DL3008
RUN apt-get update -qq \
&& apt-get install -qqy --no-install-recommends qemu-user-static

FROM golang:1.19-bullseye AS builder

# hadolint ignore=DL3008
Expand Down Expand Up @@ -49,7 +55,8 @@ WORKDIR /build

COPY docker/entrypoint.sh /entrypoint.sh
COPY --from=builder /build/packer-builder-arm /bin/packer /bin/
COPY --from=binfmt /usr/bin/ /usr/bin
# Only copy relevant qemu binaries to save container space
COPY --from=qemu_binaries /usr/bin/qemu-arm-static /usr/bin/qemu-aarch64-static /usr/bin/

# Enable detailed logging
ENV PACKER_LOG=1
Expand Down
28 changes: 26 additions & 2 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@ set -o errtrace -o nounset -o pipefail -o errexit

echo "uname -a: $(uname -a)"

/usr/bin/binfmt --install all

PACKER=/bin/packer

setup_qemu() {
# See also:
# * https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
# * https://github.com/tonistiigi/binfmt/blob/master/cmd/binfmt/main.go
# * https://docs.kernel.org/admin-guide/binfmt-misc.html

# mount binfmt_misc to be able to register qemu binaries
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc

# reset
find /proc/sys/fs/binfmt_misc -type f -name 'qemu-*' -exec sh -c 'echo -1 > "$1"' shell {} \;

uname_m="$(uname -m)"
if [ "$uname_m" != "aarch64" ]; then
echo "Register qemu-aarch64"
echo ":qemu-aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-aarch64-static:F" > /proc/sys/fs/binfmt_misc/register
fi
echo "Register qemu-arm"
echo ":qemu-arm: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:/usr/bin/qemu-arm-static:F" > /proc/sys/fs/binfmt_misc/register
}

do_qemu_setup=${SETUP_QEMU:-true}
if [ "$do_qemu_setup" = true ]; then
setup_qemu
fi

declare -a EXTRA_SYSTEM_PACKAGES=()
for arg do
shift
Expand Down

0 comments on commit a9af016

Please sign in to comment.