Cross compiling Raspbian Kernel

Submitted by sjs205 on Sat, 02/09/2013 - 22:48

I recently needed to build a number of additional kernel modules for the Rasbian distro to support the LTC4151 voltage and current monitor. The project in question required this to monitor a sealed lead acid battery power source and charging voltage.

I found a number of articles online with instruction on how to do this, however, many of them were out of date. The best link I found was on the elinux wiki: RPi Kernel Compilation, however, even this was a little hard to follow. What follows is the recipe that worked for me on an x86_64 funning Fedora 17, mostly based on the elinux page - with some of the rubbish removed.

So, first things first, we need to download the cross-compiler toolchain, the Linux kernel sources and the Raspberry Pi firmware as follows:

Get the toolchain:

One can clone a local git repo if so inclined, however, if you do not intend to start developing the toolchain, downloading a tar of the repo is much quicker and requires less space.

Compile kernel

Before we can compile the kernel we need to get a working configuration file, the easiest way to get this is from a live Raspberry Pi with the following command.

$ zcat /proc/config.gz > config

Transfer this 'config' file to the the head of the kernel sources tree downloaded earlier and rename it '.config'.

We are now ready to start the compilation, change to the kernel sources directory and run make as follows. (NOTE: if on a multicore system one can use the -j flag)

$ cd $SRC
$ make ARCH=arm CROSS_COMPILE=${CCPREFIX}

This can take a while depending on the system used to cross-compile. Once this stage is complete you will find the new kernel image files in the '${SRC}/arch/arm/boot' directory, we now need to install the modules built in the compilation to our '${ROOTFS} directory we created earlier, as follows.

Transfer the build

We now have everything we need to install the new kernel. there are many ways of doing this, basically, we just need to transfer the generated files to the raspbian's file system, either buy mounting this locally or transferring them to a live pi using scp. We can ease this process by populating the '$ROOTFS' we created earlier with the required files and then creating a tar archive:

Simply extract the archive into the Raspbian root file system and you should be ready to go.

Some notes:

You will need to be root to extract the tar;

You may need to remove old modules and kernel manually if you are compiling the same kernel with the same substring;

See the elinux page for the 'config.txt' for details of how to add multiple kernel versions to your pi.

And that is it. I found the hardest part of this was getting the right versions and firmware transferred over to the pi, that is, I was constantly finding there was a mismatch between the firmware and the kernel due to the instructions being unclear/outdated.

Following the above instructions will ensure that any additional modules you chose to compile will work with the installed kernel.