Skip to content

Instantly share code, notes, and snippets.

@ogarcia
Forked from mikkeloscar/guide.md
Last active February 14, 2016 11:45
Show Gist options
  • Save ogarcia/0a4af2d5c914121d8486 to your computer and use it in GitHub Desktop.
Save ogarcia/0a4af2d5c914121d8486 to your computer and use it in GitHub Desktop.
Setup armv7h chroot under x86_64 host (Archlinux/Archlinuxarm based)

Setup armv7h chroot under x86_64 host (Archlinux/Archlinuxarm based)

Simple way to setup an arm chroot for building packages for your arm devices. This is an alternative to cross-compiling where you are limited to only linking against the libs in your toolchain.

Setup chroot-fs

You can store the chroot wherever you like. I choose to store it in a disk-image which I mount to my filesystem.

First create a raw disk.

qemu-img create -f raw archlinuxarm.img 4G

If you don't have installed qemu-img, you can use dd to make the work.

$ dd of=archlinuxarm.img bs=1 seek=4G count=0

Make a ext4 filesystem of the whole disk-image

$ mkfs.ext4 -F archlinuxarm.img

Mount the disk-image somewhere

$ mkdir arm-chroot
$ sudo mount archlinuxarm.img arm-chroot

Get the arm root-fs you want to use and copy it to the chroot folder. I use the sun7i archlinuxarm root-fs (same as I have on my Cubietruck).

$ wget http://archlinuxarm.org/os/ArchLinuxARM-armv7-latest.tar.gz
$ sudo tar -xf ArchLinuxARM-armv7-latest.tar.gz -C arm-chroot

chroot with QEMU

You can use qemu-user-static to interpret the ARM instructions in the chroot. Get it from AUR if you are on Archlinux.

Copy the qemu-arm-static binary to the chroot.

$ sudo cp /usr/bin/qemu-arm-static arm-chroot/usr/bin

Register the qemu-arm-static as an ARM interpreter in the kernel (using binfmt_misc kernel module).

$ sudo sh -c "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/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register"

I have made a tool to make this easier, available here.

Finally bind {/proc,/dev,/dev/pts,/sys} and chroot into the chroot.

$ sudo mount -t proc /proc arm-chroot/proc
$ sudo mount -o bind /dev arm-chroot/dev
$ sudo mount -o bind /dev/pts arm-chroot/dev/pts
$ sudo mount -o bind /sys arm-chroot/sys
$ sudo chroot arm-chroot /bin/bash

When in the chroot check that it is working

[chroot]# uname -a
Linux hell 4.4.1-2-ARCH #1 SMP PREEMPT Wed Feb 3 13:12:33 UTC 2016 armv7l GNU/Linux

Depending on the rootfs you used you might want to set the nameserver to something like 8.8.8.8 in /ect/resolv.conf in order to lookup services by domainname (or copy the /etc/resolv.conf from your host system).

When you are done, exit the chroot and unmount everything

[chroot]# exit
$ sudo umount -R arm-chroot

You can use this small script to enter in the chroot.

#! /bin/bash
#
# arm_chroot.sh
#

sudo mount archlinuxarm.img arm-chroot
sudo mount -t proc /proc arm-chroot/proc
sudo mount -o bind /dev arm-chroot/dev
sudo mount -o bind /dev/pts arm-chroot/dev/pts
sudo mount -o bind /sys arm-chroot/sys
sudo chroot arm-chroot /bin/bash
sudo umount -R arm-chroot

Remember register qemu-arm-static as ARM interpreter after reboot.

$ sudo sh -c "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/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment