Embedded Handbook/General/Compiling with qemu user chroot

Compiling with qemu user chroot How to compile with QEMU user chroot.

Usage
In order to take advantage of QEMU user mode we need to do a few things. First we need to emerge the package with the right settings. That means building it with  and setting QEMU_USER_TARGETS to include the targets we want to utilize.

See for other ways of doing this:

Tweak the list here to include the target(s) you care about. See the output of for the full list:

Then install the package:

and create a binary package for it

Next we need to build the kernel module binfmt_misc. Add this to your kernel file:   or. If this module is not built already, then the devel host will require a reboot after the kernel update and modules_install.

Mount the binfmt_misc handler if it's not already, then we need to register our format with the kernel via the procfs:

Do not register a handler that matches the host machine:

After this, make sure the binfmt service is (re-)started:

OpenRC
You might want to add to the services started by default on boot:

systemd
For the service, add files containing the desired handler registration strings under. For example:

With this done, restart the service:

It's started automatically per default, but you might want to confirm it's running properly:

Setup chroot
Download the desired stage tarball:

Unpack the tarball:

Install the static qemu into the chroot:

Mount the required directories:

Chroot into the environment:

Keep QEMU from being altered within the chroot (if deleted or reinstalled within the chroot, breakage will occur):

Unmount stuff when not in use:

Sometimes we'll need to pass additional args to QEMU (CPU model), so we'll create a wrapper script (in C) that'll call QEMU with it:

Compile the wrapper with:

Then copy into the chroot. Notice the first example ARM entry in the binfmt_misc section uses this method.

Caveat
Currently, qemu doesn't support pid-sandbox and network-sandbox.

Execute

Or, insert the following line in /etc/portage/make.conf on chroot.