diff --git a/.circleci/config.yml b/.circleci/config.yml index cce7a92..85772ed 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,7 @@ jobs: - run: name: Build image command: | - ./build.sh + docker build -t hypriot/qemu-register . - deploy: name: Push image diff --git a/Dockerfile b/Dockerfile index bf4140f..16b205c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,3 @@ -# Fetch base image FROM debian:stretch as qemu ENV QEMU_VERSION 4.2.0 @@ -27,14 +26,15 @@ RUN curl -sSL https://github.com/resin-io/qemu/commit/db186a3f83454268c43fc793a4 # - resulting binaries are located in "/usr/local/bin/qemu-*" ARG TARGET_ARCH=arm-linux-user,aarch64-linux-user,ppc64le-linux-user,riscv64-linux-user RUN mkdir build \ - && cd build \ - && ../configure --static --target-list=$TARGET_ARCH \ - && make -j $(nproc) install + && cd build \ + && ../configure --static --target-list=$TARGET_ARCH \ + && make -j $(nproc) install FROM busybox COPY --from=qemu /usr/local/bin/qemu-arm /qemu-arm COPY --from=qemu /usr/local/bin/qemu-aarch64 /qemu-aarch64 COPY --from=qemu /usr/local/bin/qemu-ppc64le /qemu-ppc64le COPY --from=qemu /usr/local/bin/qemu-riscv64 /qemu-riscv64 + ADD register.sh /register.sh CMD ["/register.sh", "--reset"] diff --git a/README.md b/README.md index 3b7a4e0..ee7607e 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,76 @@ +# qemu-register + [![Build Status](https://circleci.com/gh/hypriot/qemu-register.svg?style=svg)](https://circleci.com/gh/hypriot/qemu-register) -### Build Docker image -``` -./build.sh +qemu-register – is the docker-image providing `qemu-static` binaries for your cross-platform projects. + +A simple way to register `qemu` usermode emulation binaries for your kernel: + +```docker +FROM hypriot/qemu-register as qemu + +# Fetching your base image (you can select another one) +FROM alpine + +# Coping compiled binaries to your image +COPY --from=qemu /qemu-aarch64 /qemu-arm +COPY --from=qemu /qemu-aarch64 /qemu-aarch64 +COPY --from=qemu /qemu-aarch64 /qemu-ppc64le +COPY --from=qemu /qemu-aarch64 /qemu-riscv64 + +# Coping & executing script for registering qemu in the kernel +COPY --from=qemu /register.sh /register.sh +RUN /register.sh --reset + +# PUT HERE YOUR INSTRUCTIONS!!! + ``` +## How this works -### Check Qemu binaries and versions +Starting with the Linux kernel 4.8, there's a mechanism in the kernel to load the interpreter specified in your `binfmt_misc` ahead of time (and make sure it stays in memory even if the interpreter file is deleted or moved). This is described in better detail in [this blog post by npmccallum](https://npmccallum.gitlab.io/post/foreign-architecture-docker/), [nmpccallum`s github](https://github.com/npmccallum/qemu-register) and in the [kernel binfmt_misc docs](https://www.kernel.org/doc/Documentation/admin-guide/binfmt-misc.rst). + +### Build Docker image + +If you wanna build your own qemu-register using next instruction: + +```bash +git clone https://github.com/hypriot/qemu-register.git +cd qemu-register +docker build -t hypriot/qemu-register:local . ``` -docker run --rm hypriot/qemu-register sh -c 'ls -al /qemu*' + +### Check Qemu binaries and versions + +```bash +$ docker run --rm hypriot/qemu-register sh -c 'ls -al /qemu*' -rwxr-xr-x 1 root root 6192520 Apr 27 11:25 /qemu-aarch64 -rwxr-xr-x 1 root root 5606984 Apr 27 11:25 /qemu-arm -rwxr-xr-x 1 root root 5987464 Apr 27 11:25 /qemu-ppc64le -docker run --rm hypriot/qemu-register /qemu-arm --version +$ docker run --rm hypriot/qemu-register /qemu-arm --version qemu-arm version 4.2.0 Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers -docker run --rm hypriot/qemu-register /qemu-aarch64 --version +$ docker run --rm hypriot/qemu-register /qemu-aarch64 --version qemu-aarch64 version 4.2.0 Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers -docker run --rm hypriot/qemu-register /qemu-ppc64le --version +$ docker run --rm hypriot/qemu-register /qemu-ppc64le --version qemu-ppc64le version 4.2.0 Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers -docker run --rm hypriot/qemu-register /qemu-riscv64 --version +$ docker run --rm hypriot/qemu-register /qemu-riscv64 --version qemu-riscv64 version 4.2.0 Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers ``` - ### Run the resulting Docker image to register the Qemu interpreters + ``` docker run --rm --privileged hypriot/qemu-register ``` - ### Buy us a beer! This FLOSS software is funded by donations only. Please support us to maintain and further improve it! diff --git a/build.sh b/build.sh deleted file mode 100755 index 1e5016b..0000000 --- a/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -# Build Docker image -docker build -t hypriot/qemu-register . diff --git a/register.sh b/register.sh index dcbfb18..bbd11f2 100755 --- a/register.sh +++ b/register.sh @@ -3,12 +3,12 @@ set -e # Mount if neccessary if [ ! -d /proc/sys/fs/binfmt_misc ]; then - echo "No binfmt support in the kernel." - echo " Try: '/sbin/modprobe binfmt_misc' from the host" - exit 1 + echo "No binfmt support in the kernel." + echo " Try: '/sbin/modprobe binfmt_misc' from the host" + exit 1 fi if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then - mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc + mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc fi # Reset all pre-registered interpreters, if requested @@ -16,13 +16,13 @@ if [ "$1" = "--reset" ]; then ( cd /proc/sys/fs/binfmt_misc for file in *; do - case "${file}" in + case "${file}" in status|register) - ;; + ;; *) - echo -1 > "${file}" - ;; - esac + echo -1 > "${file}" + ;; + esac done ) fi @@ -43,11 +43,11 @@ cd /proc/sys/fs/binfmt_misc for file in *; do case "${file}" in status|register) - ;; + ;; *) - echo "Registered interpreter=${file}" - cat ${file} - echo "---" - ;; + echo "Registered interpreter=${file}" + cat ${file} + echo "---" + ;; esac done