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
It's started automatically per default, but you might want to confirm it's running properly:

Setup Chroot
Download your desired stage tarball:

Unpack the tarball:

Install the static qemu into the chroot:

Mount the required directories:

Chroot into the environment:

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:

{{FileBox|filename=qemu-wrapper|lang=c|1=
 * 1) include 
 * 2) include 

int main(int argc, char **argv, char **envp) { char *newargv[argc + 3];

newargv[0] = argv[0]; newargv[1] = "-cpu"; newargv[2] = "cortex-a8";

memcpy(&newargv[3], &argv[1], sizeof(*argv) * (argc - 1)); newargv[argc + 2] = NULL; return execve("/usr/bin/qemu-arm", newargv, envp); }}

Compile the wrapper with:

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