Cross Container Support Project

From Gentoo Wiki
Jump to: navigation, search

This page is aimed to introduce the GSoC 2012 project: Cross Container Support.

I will document the progress of my project here.

Running Qemu-user on Chroot

This section updates the qemu-user and describes how to build a chroot with qemu-user on x86 machine.

Host Configuration

Qemu-user Installation

In order to take advantage of qemu-user mode we need to do a few things. First we need to merge the main package we are going to need. Note the use of the static use flag. Python is needed during qemu-user compiling.

root #echo "=app-emulation/qemu-user-1.1.0-r1 ~x86" >> /etc/portage/package.keywords
root #eselect python set python2.7
root #USE="static" emerge -b1 app-emulation/qemu-user

Binfmt_misc Configuration

First you need to build the kernel module binfmt_misc. Add this to your kernel .config: CONFIG_BINFMT_MISC=m or CONFIG_BINFMT_MISC=y. If this module is not built already, then the devel host will require a reboot after the kernel update and modules_install. The complete list of relevant kernel options (tested on 3.2.1-gentoo-r2) is as follows.

KERNEL 3.2.1-gentoo-r2
Executable file formats / Emulations  --->
 [*] Kernel support for MISC binaries

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

root #[ -d /proc/sys/fs/binfmt_misc ] || modprobe binfmt_misc
root #[ -f /proc/sys/fs/binfmt_misc/register ] || mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc

Next, register qemu-user-arch to the binfmt_misc module. You don't need to add them one by one and Luca has provided a initscript to get the bin formats registered. You can use it:

root #/etc/init.d/qemu-binfmt start

or start the service on bootstrap:

root #rc-update add qemu-binfmt default

You could learn more detail of registering bin formats through /usr/portage/app-emulation/qemu-user/files/qemu-binfmt.initd.

Enter/Exit the Chroot

Firstly, you should chose and download a stage3 tarball from installation media. We take arm arch as an example to show how to enter/exit the chroot.

  • Download and unpack arm stage tarball
root #mkdir arm-chroot && cd arm-chroot
root #wget http:// stage3-armv7a-date.tar.bz2
root #tar -xvf stage3-armv7a-date.tar.bz2
  • Install the static qemu-user into the chroot
root # ROOT=$PWD/ emerge -K qemu-user
  • Mount the required directories
root #mkdir -p usr/portage
root # mount --bind /usr/portage usr/portage
root # mount --bind /proc proc
root # mount --bind /sys sys
  • Chroot into the environment
root #chroot . /bin/busybox mdev -s
root # chroot . /bin/bash --login
  • Unmount stuff when not in use
root #umount usr/portage
root # umount sys
root # umount proc

Setup Crossdev

The first thing that is necessary is the creation of an overlay. If you have one, emerge the script:

root #emerge --ask sys-devel/crossdev

This will provide you with the crossdev script. This script automates the steps necessary to build a toolchain. These steps are, in short:

  1. binutils: Build a cross-binutils, which links and processes for the target architecture.
  2. linux-headers: Install a set of C library and kernel headers for the target architecture.
  3. libc-headers: Additional header files
  4. gcc-stage-1: Build a basic (stage 1) gcc cross-compiler. This will be used to compile the C library. It will be unable to build anything almost else (because it can't link against the C library it doesn't have).
  5. libc: Build the cross-compiled C library (using the stage 1 cross compiler).
  6. gcc-stage-2: Build a full (stage 2) C cross-compiler.

All cross toolchains will be kept locally in the overlay, separate from native tools.

FILE /etc/portage/make.conf
source /var/lib/layman/make.conf

The script is used like:

root #crossdev -t powerpc-unknown-linux-gnu

This will build a cross-compiling toolchain for PowerPC machines.

By default, the newest stable version of the binutils, libraries, and C compiler will be used. It is quite often the case they will not compile themselves through the entire build process. Less bleeding edge versions can be specified with additional flags:

--b 2.22      # specifies the version of binutils
--g 4.6.3     # specifies the version of gcc
--l 2.15-r2   # specifies the version of the tuple-specified libc
--k 3.5       # specifies the version of the kernel headers

It is recommended trying older versions, particularly of gcc, if the script fails.

If you want to remove a toolchain, use the clean flag:

root #crossdev -C powerpc-unknown-linux-gnu

This will unmerge the packages created by crossdev.

If you got the errors about fortran, use the fellow command:

root #USE="-fortran nossp" crossdev -t powerpc-unknown-linux-gnu

Cross Container

The tool can download, configure and create a multi-arch Gentoo guest. Using this tool, the user build a native gcc in chroot. You can download it here:

Next, we will take armv7a_hardfloat as a example to build the native compiler in chroot.

Install a cross compiler

You must install the cross compiler manually:

root #USE="-fortran nossp" crossdev -t armv7a-hardfloat-linux-gnueabi

Create the chroot

You can create an arm Gentoo guest:

root #./ create -i ip_address -g gateway -n guest_name -r rootfs -a arm
What is the subarch of arm? armv7a

You can also start and destroy the arm Gentoo guest:

root #./ start -n guest_name
root #./ destroy -n guest_name -r rootfs

Additional developers, bug fixes, comments, etc. are welcome.

Switch to native compiler

In chroot, you can switch to native compiler:

root #cd /root
root #source native

Then, you can get the native armv7a-hardfloat-linux-gnueabi-gcc. To use it, just specify the CC=armv7a-hardfloat-linux-gnueabi-gcc in the Makefile.

You also could switch to emulated gcc as well:

root #source emu