English / Deutsch | Print version Plop Linux  
Twitter
twitter

<< Previous
Cross compile Linux Kernel

Table of Contents

Next >>
Hardware

Cross build with chroot


You can Cross Compile from a running system (ex. x86) with chroot to an ARM environment. The advantages are a stable host system in the background, easy data sharing from the host system to the chroot environment, faster compilation when you have a powerful host machine.

You can chroot to the ARM system

  • when it's stored in a directory.
  • when it's on a hard disk partition.
  • when it's on an image of a partition (as loop device).
  • when it's on a partition in a hard disk image (as loop device).

You have to work as root to have enough permissions.

I prefer to chroot to a directory. There are no space limits of an image file.



1. Requirements
2. Prepare chroot
2.1. Chroot directory
2.2. Chroot partition of a hard disk image
3. How to chroot
4. Additional resources

1. Requirements


Download the ploplinux-src-24.2.tar.gz file to get the ARM chroot tools and extract it. The chroot tools are in the 99-ARM/cross-compile/chroot-tools/ directory. You can also download each individual file from here.

The archive:

arm/                         <- Chroot to this directory

arm-chroot.sh                <- Use this script to chroot

.bashrc                      <- This adds the red CHRT info to the command
			        shell when you chroot to the ARM system

mount.sh                     <- Mount a partition in a hard disk image without
                                chroot environment setup (as loop device)

qemu-static/                 <- Static QEMU to emulate the ARM CPU

qemu-wrapper/                <- Wrapper to run QEMU with the required parameters

setup-arm-emu-hdimage.sh     <- Script to setup the chroot environment for a 
                                partition in a hard disk image (as loop device)

setup-arm-emu-local.sh       <- Script to setup the chroot environment for a 
                                directory

Compile the qemu-wrapper: Change to the qemu-wrapper/ directory. Run sh make-qemu-wrapper

Change to the qemu-static/ directory and extract the QEMU program for the system you want to emulate. The QEMU program for ARM qemu-arm-static is already extracted.


2. Prepare chroot


2.1. Chroot directory


When you want to use the chroot just with a directory and no image or hard disk, then extract the ARM system to the arm/ directory.

Plop Linux for ARM: ploplinux-desktop-24.2-arm.tar.gz

Copy the file qemu-static/qemu-arm-static and qemu-wrapper/qemu-wrapper to the directory arm/usr/bin/.

Preparations done, see below how to chroot.


2.2. Chroot to a partition of a hard disk image


When you want to chroot to a partition in a hard disk image then you have to mount the partition as loop device with an offset.

Download the (compressed) hard disk image: ploplinux-24.2-arm.img.xz

Uncompress the file (warning, the file became 10GB): xz -d ploplinux-24.2-arm.img.xz

You have to figure out the start of the system partition. Run fdisk ploplinux-24.2-arm.img
Use the command p to print the partition layout

Welcome to fdisk (util-linux 2.27).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk ploplinux-24.2-arm.img: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf1ec283e

Device                              Boot Start      End  Sectors Size Id Type
ploplinux-24.2-arm.img1        *     2048 20971519 20969472  10G 83 Linux

Command (m for help): 

There is one partition. You see the start value of 2048. Edit the mount.sh and setup-arm-emu-hdimage.sh scripts and set the PARTITION_START_OFFSET variable.

File: mount.sh

#!/bin/sh

ROOT_DIR=arm

IMAGE_FILE=ploplinux-24.2-arm.img
PARTITION_START_OFFSET=2048
IMAGE_BLOCKSIZE=512

mount -o loop,offset=$[$PARTITION_START_OFFSET*$IMAGE_BLOCKSIZE] $IMAGE_FILE $ROOT_DIR/

File: setup-arm-emu-hdimage.sh

#!/bin/sh

ROOT_DIR=arm

IMAGE_FILE=ploplinux-24.2-arm.img

PARTITION_START_OFFSET=2048
IMAGE_BLOCKSIZE=512

SHARE=/root


mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
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-wrapper:' > /proc/sys/fs/binfmt_misc/register

mount -o loop,offset=$[$PARTITION_START_OFFSET*$IMAGE_BLOCKSIZE] $IMAGE_FILE $ROOT_DIR/


mount --bind /proc $ROOT_DIR/proc..
mount --bind /tmp $ROOT_DIR/tmp..
mount --bind /sys $ROOT_DIR/sys..
mount --bind /dev $ROOT_DIR/dev..
mount --bind /dev/pts $ROOT_DIR/dev/pts..

#if required, bind the system /root directory to the chroot environment
#mount --bind $SHARE $ROOT_DIR/mnt

Mount the partition to the arm/ directory with starting the script mount.sh.

Copy the file qemu-static/qemu-arm-static and qemu-wrapper/qemu-wrapper to the directory arm/usr/bin/.

Preparations done, see next section how to chroot.


3. How to chroot


To enable the chroot environment you have to start the script

  • setup-arm-emu-local.sh - when the ARM system is extracted in the arm/ directory.
  • or setup-arm-emu-hdimage.sh - when the ARM system is on a partition in a hard disk image.

Then run arm-chroot.sh to do the chroot. Do a lscpu to see the emulated CPU information.

Now you can start programs on the ARM system. Start the Midnight Commander mc to speed up your navigation on the ARM system. You can compile programs in the same way, as the emulated hardware would be real.

You can run arm-chroot.sh parallel in other terminals to work parallel on the ARM system.


4. Additional resources


http://community.arm.com/groups/embedded/blog/2013/11/21/cross-compilation-for-arm
https://wiki.debian.org/QemuUserEmulation



<< Previous
Cross compile Linux Kernel

Table of Contents

Next >>
Hardware


© 2024 by Elmar Hanlhofer