10.08.2021

Docker to the rescue in an unexpected way

Imagine you find yourself in a restricted environment and you need some Linux rootfs that runs on the embedded system you just managed get access to. Of course the CPU architecture of the embedded system is not the same as your workstation. The circumstances are further complicated by the fact that the userspace should offer enough tooling to build a C/C++ application. In such a situation docker can help, but in an unexpected way.

Common solutions to deal with such a situation include manually bootstrapping a Linux distribution, installing it in an emulator such as QEMU, building a rootfs from scratch or trying to cross build the targeted application. In any case, there is huge potential to waste a full day or more.

Here, rescue comes in form of docker. Actually the great service to the community by major Linux distributions, docker just offers the tooling. Especially Debian supports a wide range of CPU architectures. So let’s ask docker to pull a Debian image for the desired architecture and instead of starting a container we export the image to use it on the target device.

In this example, Debian stable built for arm32v7 turned out to be useful. Other useful images to deal with embedded systems are arm32v5/debian, amd64v8/debian or riscv64/debian. Linux distributions such as Fedora also offer root filesystems for various CPU architectures.

docker image pull arm32v7/debian:stable 
docker image save arm32v7/debian:stable | tar --wildcards --to-stdout -xf - "*/layer.tar" | tar xf - -C myroot/

After transferring myroot/ to the device and a few apt-get commands the application could be compiled and run.

  • Is it an ugly hack? Of course!
  • Is it unorthodox? True!
  • Are there better/other ways? Sure!
  • Can it save a lot of time? YES!

There is one drawback to consider: these root filesystems usually come without an init system such as SysV init or systemd. If you plan to boot from it, better ask Linux to directly start into a shell using init=/bin/sh.