How to make a cross architecture chroot: Difference between revisions

From Alpine Linux
(Add openrc package, which is needed to run `rc-service`)
(qemu-binfmt -> binfmt)
Line 17: Line 17:
Please note there are the <code>qemu-system-*</code> packages, you don't want those since they are used for full virtual machine emulation.
Please note there are the <code>qemu-system-*</code> packages, you don't want those since they are used for full virtual machine emulation.


== Enable the <code>qemu-binfmt</code> service and start it ==
== Enable the <code>binfmt</code> service and start it ==


The qemu-binfmt script is an OpenRC init script that registers the binfmt_misc handlers when the service starts and unregisters them when the service stops. Add the service to the default runlevel and start it immediately:
{{Tip|This section applies to Alpine 3.24 and later. In earlier releases, enable <code>qemu-binfmt</code> instead of <code>binfmt</code>.}}


{{Cmd|# rc-update add qemu-binfmt default
The binfmt service registers binfmt_misc handlers from configuration files in binfmt.d directories when it starts. The qemu-* static packages include the necessary binfmt.d configuration files. Add the service to the default runlevel and start it:
&#35; rc-service qemu-binfmt start}}
 
{{Cmd|# rc-update add binfmt
&#35; rc-service binfmt start}}


== Make the chroot folder ==
== Make the chroot folder ==

Revision as of 12:56, 4 February 2026

If you have an ARM computer, you will find that some software is only available for x86_64. One possible solution is to have a full x86_64 virtual machine, but that has quite a lot of overhead from having to emulate the entire system. Fortunately, there is QEMU User space emulation which still uses the host kernel, so there is less overhead. This guide will show you how to make a chroot and enable transparent x86_64 binary execution using qemu and binfmt_misc.

Prerequisites

You should be familiar with the Linux command line.

This guide is written assuming you have Alpine as the main install as well as putting Alpine in the chroot.

Also, the commands shown assume you have root privileges. Either use sudo/doas as needed (or su first).

Installing necessary software

Enable the Community Repository and install the qemu user mode binaries for the architectures you want to use, in this case x86_64 and i386:

# apk add qemu-x86_64 qemu-i386 qemu-openrc openrc

Please note there are the qemu-system-* packages, you don't want those since they are used for full virtual machine emulation.

Enable the binfmt service and start it

Tip: This section applies to Alpine 3.24 and later. In earlier releases, enable qemu-binfmt instead of binfmt.

The binfmt service registers binfmt_misc handlers from configuration files in binfmt.d directories when it starts. The qemu-* static packages include the necessary binfmt.d configuration files. Add the service to the default runlevel and start it:

# rc-update add binfmt # rc-service binfmt start

Make the chroot folder

To keep it simple, we're just going to use the apk repositories specified on the host system. This guide assumes the $CHROOT environment variable holds the directory where you want to make the chroot. Make the chroot directory and the etc/apk subdirectory (which we'll copy the apk repositories file to):

# mkdir -p $CHROOT/etc/apk/

Now copy the host's apk repositories into the chroot folder:

# cp /etc/apk/repositories $CHROOT/etc/apk/

You'll also need to copy the resolv.conf file into the chroot so it can have internet access:

# cp /etc/resolv.conf $CHROOT/etc/

Install alpine-base into the chroot folder

The alpine-base meta package is all that's needed to have a fully working alpine system. The apk invocation used here has a few more options than what you might be used to:

# apk add -p $CHROOT --initdb -U --arch x86_64 --allow-untrusted alpine-base

Here's what all the options do:

  • -p $CHROOT: Sets the root of the install to $CHROOT instead of /
  • --initdb: Initialize a new package database.
  • -U: Alias for --cache-max-age 1. Basically means don't install from cache.
  • --arch x86_64: Install x86_64 packages instead of native packages. Replace this with whichever architecture you want to emulate instead (e.g. --arch x86).
  • --allow-untrusted: Install packages with untrusted signature or no signature. (This is needed since the keys for different architectures are different than what will be on your host. If you have the correct keys, you can copy them into the $CHROOT/etc/apk/keys/ directory and remove this flag).

Mount the virtual file systems into the chroot

# mount -t proc proc $CHROOT/proc/ # mount -t sysfs sys $CHROOT/sys/ # mount -o bind /dev/ $CHROOT/dev/ # mount -o bind /dev/pts/ $CHROOT/dev/pts/ # mount -o bind /run $CHROOT/run/

The exciting part: Enter the chroot

If you've followed all the steps, this part should be uneventful. The chroot invocation uses the special command of sh -l to run as a login shell and source /etc/profile:

# chroot $CHROOT/ sh -l

Troubleshooting

If you get a ERROR: ${package_name}.post-install: script exited with error 127 while trying to add the alpine-base, that normally means that qemu binfmt_misc hasn't been setup properly. Check if the qemu-$arch package has been installed and see if the binfmt_misc service is running. Remember to restart the service after adding a new architecture.

# rc-service qemu-binfmt restart

You can also see if there is a qemu-$arch entry in /proc/sys/fs/binfmt_misc/.

Programs that use Netlink (e.g. ip addr) don't seem to work properly since qemu doesn't emulate the Netlink protocol properly.

References