# Petr Cizmar's Wiki

### Site Tools

linux:chroot_environment_for_cross-compilation_of_debian_packages

# Chroot environment for cross-compilation of Debian packages

For cross-compilation, only cross-compiler and a cross-toolchain is needed. However, in order to build a debian package, it is very suitable to have a debian system for the target platform installed in the system. One way to achieve this is to use a combination of Debian bootstrap, statically linked qemu, and binfmt. This little how-to shows, how to make the environemnt for the armel platform.

### Qemu

For the arm platform, the static user qemu is suitable. The best is to compile it from sources. In the case of the version 0.11.0, the compilation looks like this:

tar xvzf qemu-0.11.0.tar.gz
cd qemu-0.11.0
./configure --target-list=arm-linux-user --static
make
su
cp arm-linux-user/qemu-arm /usr/local/bin

### Binfmt

From Wikipedia: binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.

It's necessary to have loaded the binfmt_misc kernel module. In Debian, there is the binfmt-support package, which is supposed to take care of the setting of the kernel module (Unfortunately, I myself had no luck). The support for the arm platform can also be configured manually:

if [ ! -d /proc/sys/fs/binfmt_misc ]; then
/sbin/modprobe binfmt_misc
fi
if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
fi
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

This manual configuration has to be renewed after reboot, so putting this to /etc/rc.local may come in handy. Then, if an arm binary is invoked, it's “interpreted” by /usr/local/bin/qemu-arm.

### Debian bootstrap

Next, in a selected directory, the Debian bootstrap can be installed like this:

mkdir -p debian_arm_lenny/usr/local/bin
cp /usr/local/bin/qemu-arm debian_arm_lenny/usr/local/bin/
debootstrap --arch arm lenny debian_arm_lenny http://ftp.debian.org/debian/

Then it is also good to mount the /proc and /dev filesystem (possibly also at the boot time):

mount -t proc proc $BASEDIR/debian_arm_lenny/proc mount -t sysfs sysfs$BASEDIR/debian_arm_lenny/sys
mount -o bind /dev \$BASEDIR/debian_arm_lenny/dev

That's it; the environment is ready. Invoking chroot gets us there:

LC_ALL=C chroot /cross/arm/debian_arm_lenny/ /bin/bash

One may also consider creating their account with the same uid number. This number it output by the uid command (as yourself outside the chroot).

The following creates the new account:

useradd -m -uXXXX username

XXX is the user id obtained above.

This is generally useful for packaging. Importing your gpg key is also very suitable, especially when one wants to sign the created debian packages. To get the private key invoke (outside chroot):

gpg -a --export-secret-key KEYID

KEYID is the ID of the secret key to extract.

To add key (inside chroot as userid) invoke:

gpg --import

and then paste the secret key obtained from the command above.

At this time, the chroot environment is nearly ready. One still needs to install appropriate packages to be able to compile programs and create Debian packages.