Welcome! Log In Create A New Profile

Advanced

HowTo: Install chroot and cross compile kernel/packages for armel

Posted by pld 
pld
HowTo: Install chroot and cross compile kernel/packages for armel
March 15, 2016 04:27PM
(still under construction, will continue later)

First remark is that cross compiling is quite east. Thus, native compilation on ARM devices is a waste of time (you need ca. 2.5h on ARM to build openssl vs. 5-10min on modern desktop using single thread!).

Second remark is that compilation always creates a mess. You need to install sources, compiler, libraries, devel packages and so on. Even if you try to delete unneeded things afterwards then there are always some remainings. So, it is always a good idea to separate the host side os from the build environment by creating chroot (a linux inside of another linux). After compilation you may simply delete the chroot directory.
In case of cross compilation you actually create a mixture of packages from different architectures (host and target). I would not even consider doing this without chroot cage.

Third remark is that googling about cross building on debian is a bit confusing. It is because in debian there are few different ways to cross-build a packages which offer different level of automation and separation between host side os and chroot environment. Choose one and stick with it! If you use different tutorials make sure that all are based on the same scenario.
I decided to keep everything in chroot (actually this is the only solution for me since I’m not a debian user).

1. Creating debian chroot.

First step is to create base chroot directory. If you distro has a debootstrap (it does not have to be debian) then it is quite simple. Run the following

# download packages:
debootstrap --arch amd64 --foreign --variant=minbase jessie /the/chroot/directory http://ftp.us.debian.org/debian/
# enter chroot:
chroot /the/chroot/directory /bin/bash -l
# install packages:
DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true LC_ALL=C LANGUAGE=C LANG=C /debootstrap/debootstrap --second-stage
# some packages need following directory:
mkdir /root/tmp
# leave chroot:
exit


Done! You have a minimal jessie debian in the directory /the/chroot/directory. The thing you need to remember is that some programs need the access to pseudo file systems (proc, dev, sys..). To mount it do the following:

cd /the/chroot/directory
for DIR in dev dev/pts proc sys
  do
    mount -o bind /$DIR $DIR
  done


The second step is to install packages needed for cross compiling. Edit the file /the/chroot/directory/etc/apt/sources.list. Make sure that pseudo file system are mounted and enter the chroot. Add armel architecture:

apt-get update
dpkg --add-architecture armel


If you use jessie then some packages need to be loaded from the embedian repository. Thus you need to add appropriate repository and archive key:

echo deb http://emdebian.org/tools/debian/ jessie main >> /etc/apt/sources.list
apt-get install curl
curl http://emdebian.org/tools/debian/emdebian-toolchain-archive.key | apt-key add -
apt-get update


In case of stretch it may be necessary to put "deb [arch=amd64,armel]" at the beginning of the appropriate line in the sources.list.

Now install the packages used for armel cross building:

apt-get install gcc-arm-linux-gnueabi dpkg-dev libc6:armel libncurses5-dev libssl-dev bc debhelper

Done!
Actually when I started to google about debian chroot I expected to find tar balls ready to unpack and use. I was quite disappointed because I did not find any. So, if your distro does not have debootstrap simply download the once prepared by me:
jessie-clean.amd.txz (28MB)
jessie-cross-armel.amd64.txz (102MB)
stretch-clean.amd64.txz (32MB)
stretch-cross-armel.amd64.txz (188MB)


2. Cross build a package

3. Cross compile the linux kernel

I assume that you are inside the chroot. Otherwise some of the commands may delete your running kernel and/or modules.

The default directory for kernel compilation is /usr/src. First you need to download the kernel sources:
cd /usr/src
wget -O - https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.0.tar.xz | tar xJv
Some source packages expect to find kernel sources in the directory /usr/src/linux, create a link
ln -s linux-4.4.0 linux
You probably compile for Marvell-kirkwood. Thus, you should apply bodhi patch. Download linux-4.4.0-kirkwood-tld-1-bodhi.tar.bz2. This requires a web browser. You have to do it outside of the chroot and then move it inside to /usr/src directory. Return to chroot, unpack and patch the kernel source.
tar xjvf linux-4.4.0-kirkwood-tld-1-bodhi.tar.bz2
cd linux
patch -p1 <../linux-4.4.0-kirkwood-tld-1.patch
The bodhi kernel config is a good point to start:
cp ../config-4.4.0-kirkwood-tld-1 .config
Kernel sources contain Makefile. It is used by the make command to automate all tasks related to configuration and building. To cross compile you should always use it in the following way:
ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- make <target>
Actually, this is the only difference between native and cross compilation. Useful targets are:

menuconfig – view/change configuration
zImage – to compile a kernel
modules – to compile kernel modules
dtbs – to compile dtb files
modules_install - copies modules from the source tree to /lib/modules/
dtbs_install - copies dtbs from the source tree to /boot/
clean – delete all build binaries
mrproper – remove everything except the plain source tree.

Begin with the menuconfig target. This also creates some script files needed during compilation. After that you ready to compile. Invoke make with following: zImage, modules, dtbs. You may add -jN where N is the number of cores in your CPU.
After compilation install modules by: modules_install, dtbs_install. Now you have a kernel in /usr/src/arch/arm/boot/zImage, modules in /lib/modules/4.4.5-tld-1 and dtbs in /boot/dtbs. Copy all to target host.


to be continued...

--

Regards
PLD = Polish(ed) Linux Distribution, www.pld-linux.org



Edited 6 time(s). Last edit at 03/18/2016 03:27AM by pld.
Re: HowTo: Install chroot and cross compile kernel/packages for armel
March 16, 2016 03:49PM
@PLD

Thanks for this, im looking forward to future updates as this will help me A LOT
Re: HowTo: Install chroot and cross compile kernel/packages for armel
March 25, 2016 01:05PM
thanks, will be useful here too, always wanted to recompile Android for my phone with something less clunky than a full VM.
Re: HowTo: Install chroot and cross compile kernel/packages for armel
March 25, 2016 10:06PM
Subscribed, I hope to see more updates about it
Re: HowTo: Install chroot and cross compile kernel/packages for armel
March 31, 2016 07:42AM
im waiting with baited breath for the next installment!!!

build my "jail" last night and will be having a go at the compilation in a short while - actually this will REALLY help me with my "OrangePi One" as the mainline support is still a work in progress.
Re: HowTo: Install chroot and cross compile kernel/packages for armel
April 06, 2016 03:32PM
On a pogo pro, I use this command to compile natively(2 cores):

time make deb-pkg -j`nproc` LOCALVERSION=-$(date "+%Y%m%d") KDEB_PKGVERSION=$(date "+%H%M%S")
The time it took:
real    361m19.714s
user    471m57.180s
sys     35m3.150s

On a macbook air, I use this this command to cross compile in a Debian virtual machine(3 cores):
time ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- make deb-pkg -j`nproc` LOCALVERSION=-$(date "+%Y%m%d") KDEB_PKGVERSION=$(date "+%H%M%S")
The time it took:
real    11m47.630s
user    27m12.674s
sys     2m23.189s

-syong
Re: HowTo: Install chroot and cross compile kernel/packages for armel
April 07, 2016 12:20AM
The most significant difference is actually the RAM.

If you want to compile natively then use distcc with your stack of 5 HP Thin Client T5325s and Pogo Mobiles :) it is not much of a difference comparing to cross compiling on a fast laptop like the MacBook Air.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: HowTo: Install chroot and cross compile kernel/packages for armel
April 07, 2016 11:36PM
bodhi,

Thank you very much for the information about distcc.

Not aiming for the speed, I like to watch my guinea plugs grazing bits on the Debian wonderland.:)

-syong
Re: HowTo: Install chroot and cross compile kernel/packages for armel
April 08, 2016 03:51AM
> Not aiming for the speed, I like to watch my
> guinea plugs grazing bits on the Debian
> wonderland.:)

:) my farm of guinea plugs are grazing at the same leisurely pace :))

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: