Raspberry Pi のセルフビルド環境を Docker で作る

拙稿「[Raspberry Pi のセルフビルド環境を QEMU で作る](https://qiita.com/autch/items/c8c9cdc7b8e5821e81a4)」を Docker で動かす。

上記記事で `raspbian-stretch-lite.tar.bz2` を入手するところまで進める。


```dockerfile:Dockerfile
FROM debian:stretch

RUN apt-get update && apt-get install -y \
   qemu-user-static binfmt-support \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

RUN mkdir /raspbian

ADD ./raspbian-stretch-lite.tar.bz2 /raspbian

RUN cp /usr/bin/qemu-arm-static /raspbian/usr/bin/

ADD ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/bin/bash"]
```

```bash:entrypoint.sh
#!/bin/bash
set -e

if [ ! -f /tmp/CHROOT_is_mounted ]; then

mount -t devtmpfs devfs /raspbian/dev
mount -t devpts devpts /raspbian/dev/pts
mount -t proc proc /raspbian/proc
mount -t sysfs sys /raspbian/sys

touch /tmp/CHROOT_is_mounted

fi

exec chroot /raspbian $@
```

これらファイルを一箇所にまとめてビルド

```shell-session
$ ls
Dockerfile  entrypoint.sh  raspbian-stretch-lite.tar.bz2

$ docker build -t raspbian-cross .
Sending build context to Docker daemon  242.8MB
Step 1/9 : FROM debian:stretch
 ---> d508d16c64cd
Step 2/9 : RUN apt-get update && apt-get install -y    qemu-user-static binfmt-support  && apt-get clean  && rm -rf /var/lib/apt/lists/*
 ---> Using cache
 ---> e83500c439db
Step 3/9 : RUN mkdir /raspbian
 ---> Using cache
 ---> 66f55064916f
Step 4/9 : ADD ./raspbian-stretch-lite.tar.xz /raspbian
 ---> Using cache
 ---> 7e25464c70df
Step 5/9 : RUN cp /usr/bin/qemu-arm-static /raspbian/usr/bin/
 ---> Using cache
 ---> 394f47dd0040
Step 6/9 : ADD ./entrypoint.sh /entrypoint.sh
 ---> d44e268532e2
Step 7/9 : RUN chmod +x /entrypoint.sh
 ---> Running in fdb82f0b50ae
Removing intermediate container fdb82f0b50ae
 ---> 058fcfcecbc7
Step 8/9 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Running in e67c2a8088c1
Removing intermediate container e67c2a8088c1
 ---> 2d42c30100dc
Step 9/9 : CMD ["/bin/bash"]
 ---> Running in b8e05d85bcf3
Removing intermediate container b8e05d85bcf3
 ---> 36a9cecc30a7
Successfully built 36a9cecc30a7
Successfully tagged raspbian-cross:latest
```

出来上がったイメージは `--privileged` をつけて実行しないと失敗する。

```shell-session
$ docker run --rm -ti --privileged raspbian-cross:latest
root@3cbc0d7686c3:/# uname -a
Linux 3cbc0d7686c3 4.18.0-0.bpo.1-amd64 #1 SMP Debian 4.18.6-1~bpo9+1 (2018-09-13) armv7l GNU/Linux

root@3cbc0d7686c3:/# gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 6.3.0-18+rpi1+deb9u1' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1+deb9u1) 

root@3cbc0d7686c3:/# file /bin/bash
/bin/bash: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=b1e8e19f3b7b388468f2914bfb2ae8ac6b9f0478, stripped
```