I’ve picked up on the dark-arts of ‘architectural chroot‘. Normally chroot is used to restrict a program to a certain directory structure and prevent it from see anything outside of it. For example, you can run an apache server in a chroot for extra security. The advantage of this is that if the apache process is compromised, the damage is limited to the chroot’d environment.

We can also use chroot to emulate an environment for computer architectures other than what the host is running on. In this example, we going to set up a chroot for the Raspberry Pi. The advantage of doing this is that we can compile programs within a ‘native’ environment for the Pi with the speed of the much faster host.
First, we need a static version of qemu. If you are using Debian/Ubuntu, I believe there is a static qemu package available, otherwise you can grab the source from http://wiki.qemu.org/Download.
Building static qemu binary for ARM
Unpack the qemu source and change into the source directory and build qemu:
./configure –disable-kvm –target-list=arm-linux-user –static –disable-werror –prefix=/usr/local/qemu
make
sudo make install
You can set the –prefix option to whatever suits. When the build is complete, you can test the result with this simple “hello world” ARM binary:
/usr/local/qemu/bin/qemu-arm ./test
Preparing the environment
Now comes the interesting part. First we register the qemu-arm interpreter with its binary format with the kernel:
sudo -s
echo ‘: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/local/bin/qemu-arm:’ > /proc/sys/fs/binfmt_misc/register
Note that you should change the ‘/usr/local/bin/qemu-arm’ path to where ever you installed the qemu-arm program. The code was taken from the qemu-binfmt-conf.sh found in the qemu build directory. Mount the Raspberry PI SD card somewhere, eg /media/rpi and copy the qemu-arm program into the same location (relative to /media/rpi) as you did on the host system:
sudo mkdir -p /media/rpi/usr/local/qemu/bin
sudo cp/usr/local/qemu/bin/qemu-arm /media/rpi/usr/local/qemu/bin/qemu-arm
You may also want to mount the following:
sudo mount –rbind /dev /media/rpi/dev
sudo mount –rbind /proc /media/rpi/proc
sudo mount –rbind /sys /media/rpi/sys
Now for the magic part:
chroot /media/rpi
And if all is well, ‘uname -a’ will display:
Linux bofh.localdomain 3.5.1-1.fc17.x86_64 #1 SMP Thu Aug 9 17:50:43 UTC 2012 armv7l GNU/Linux
If you instead get an error like
chroot: failed to run command `/bin/bash’: No such file or directory
It means that you have not placed the ‘qemu-arm’ program in the correct place on the PI SD disk or have given the wrong path when registering it with the kernel.
Recent Comments