Skip to content

Instantly share code, notes, and snippets.

@mikkeloscar
Created June 14, 2014 20:44
Show Gist options
  • Save mikkeloscar/a85b08881c437795c1b9 to your computer and use it in GitHub Desktop.
Save mikkeloscar/a85b08881c437795c1b9 to your computer and use it in GitHub Desktop.
Setup armv7h chroot under x86_64 host (Archlinux/Archlinuxarm biased)

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

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.

$ 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
# 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-sun7i-latest.tar.gz
# tar -xf ArchLinuxArm-sun7i-latest.tar.gz -C arm-chroot

chroot with QEMU

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

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

# 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) [1].

# 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.

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

When in the chroot check that it is working

[chroot]# uname -a
Linux saturn 3.14.6-1-ARCH #1 SMP PREEMPT Sun Jun 8 10:08:38 CEST 2014 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
# umount arm-chroot/{sys,proc,dev/pts,dev}
# umount arm-chroot
References

[1] http://www.darrinhodges.com/chroot-voodoo/

Copy link

ghost commented May 29, 2018

Why I can't ping from alarm user?
[root@mypc /]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
Unsupported ancillary data: 1/29
Warning: time of day goes back (-98945us), taking countermeasures.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=6.69 ms
Unsupported ancillary data: 1/29
64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=4.03 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 4.036/5.364/6.693/1.330 ms
[root@mypc /]# su alarm
[alarm@mypc /]$ ping 8.8.8.8
ping: socket: Operation not permitted

@Danct12
Copy link

Danct12 commented Jul 20, 2018

For the first step (creating raw disk), you can use truncate instead of dd.
truncate archlinuxarm.img -s 8G (creates 8GB raw disk)

@WarheadsSE
Copy link

You can make this simpler, once you have qemu-arm-static AUR package.

systemd-binfmt.service is a built-in that will activate once the qemu-arm-static is installed.

With qemu in play, arch-chroot from arch-install-scripts doesn't care and does all the special mounts you need.

pacman -U 
systemctl start systemd-binfmt.service
arch-chroot arm-chroot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment