Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- run:
name: Build image
command: |
./build.sh
docker build -t hypriot/qemu-register .

- deploy:
name: Push image
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Fetch base image
FROM debian:stretch as qemu

ENV QEMU_VERSION 4.2.0
Expand Down Expand Up @@ -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 link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the much smaller final stage?

Copy link
Copy Markdown
Author

@urpylka urpylka Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was guided by idea of gap between qemu-register and user-implementation. And if you wanna build your own implementation you can use large or smaller base image (qemu-register) and later copy binary files & register.sh. It's just one docker-layer. And it can be did before (as in your solution) or later (as in mine).

You are right. busybox is smaller than debian. And if it does matter, I can fixed it in PR (return previous structure).

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"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break the usage of this image if there is no CMD any more.

Copy link
Copy Markdown
Author

@urpylka urpylka Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that is not necessary for users, for example I can't using busybox in my project. I need debian, or alpine, or smthn else w package manager. Therefore CMD operation should using in user-implementation as I documented in README.md.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think about that a bit more and I has concluded that it can useful for tests. I return it back

56 changes: 45 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
4 changes: 0 additions & 4 deletions build.sh

This file was deleted.

28 changes: 14 additions & 14 deletions register.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ 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
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
Expand All @@ -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