Raspberry Pi 2 Cross Compile kernel on Ubuntu

Since a step by step guide for cross compiling a kernel and installing it on the pi2 is pretty much non-existent, here is my guide.

Ok, first step, install Ubuntu. You will need to be able to mount the SD card for the Pi, so doing it in HyperV isn't going to work, you can't mount SD Cards as block devices. I didn't try VMWare or VirtualBox, I just used an old laptop, worked great as it had an SD Card slot in it.

Once you have Ubuntu installed and set up, update it to the latest and greatest, not necessary but it's a good thing to do. At a console, run the following commands.

sudo apt-get update
sudo apt-get upgrade

Now that you have updated everything, we need to create some directories to do this. What made it easy for me was creating a folder structure like this in my /home/<username> directory.

raspberry
|-kernel
  |-modules
  |-mounts
  |-scripts
|-raspbian

If you just want to copy the commands to create it, here you go

cd ~
mkdir raspberry
mkdir raspberry/kernel/modules
mkdir raspberry/kernel/mounts
mkdir raspberry/kernel/scripts

The raspberry folder is there as the root for our environmentraspberry/kernel/modules is there for when we compile the kernel, we will need a place to store the built modules, we really badly don't want to install them on the local system....`raspberry/kernel/mounts` is there as a place to mount the sd card when installing the kernel and modules raspberry/kernel/scripts is there as a place to store some scripts for building and installing the kernel and modules raspberry/raspbian is there as a place to store any downloaded raspbian files, the base image, etc. Just made it easy.

Now we need to go and get the git repositories for building the kernel

In a console, cd into your kernel directory

cd ~/raspberry/kernel

Get the build tools from raspberry pi's repository:

git clone https://github.com/raspberrypi/tools.git

Get the kernel from raspberry pi's repository:

git clone https://github.com/raspberrypi/linux.git

If you want to use the scripts for building and installing the kernel, which I recommend,

cd ~/raspberry/kernel/scripts
git init
git remote add origin https://github.com/veccsolutions/RaspberryPi2Scripts.git
git pull origin master

Now that we have the repositories we can get to building a kernel. I will give the examples using the scripts just because of how long the commands are. However, if you want to see the commands, you can look inside of the scripts.

You will want to get the default pi config to get started with. You can use the one from the kernel source by going into the linux directory cd ~/raspberry/kernel/linux and change type make bcm2709_defconfig. Or from the Raspberry P 2i, get a hold of the /proc/config.gz file, decompress it and save it as .config in the linux directory.

Now, from within the scripts directory (cd ~/raspberry/kernel/scripts), you can now run upgradekernel.sh and it will run through the kernel config upgrade process. This will run make oldconfig with all of the ridiculous parameters that are needed for the cross compile.

./upgradekernel.sh

To give you an idea of what the make command is that you would be typing that you really don't want to type

make oldconfig ARCH=arm CROSS_COMPILE=~/raspberry/kernel/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- INSTALL_MOD_PATH=~/raspberry/kernel/modules -j 8

Now you know why I suggest using the scripts. It includes all that stuff in the command automatically for you.

Now that you have upgraded your config file, you will want to configure your kernel options, to do so run:

./makemenuconfig.sh

The raw command that ends up being ran is:

make menuconfig ARCH=arm CROSS_COMPILE=~/raspberry/kernel/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf- INSTALL_MOD_PATH=~/raspberry/kernel/modules -j 8

Now that you have configured the kernel, it's time to build it, the first time will generally take a while, for my laptop was about 5 minutes.

./makekernel.sh

Now the kernel and modules have been built. Lets install it. You will first need to find out where your sd card's device is in /dev. So, do a ls /dev and look for a mmcblk device. Mine showed up as mmcblk0. If your sd card is in, you should see a few entries. mmcblk0, mmcblk0p1 and mmcblk0p2. The p1 and p2 are the partitions, if you did a basic raspbian install, you should see a p1 and p2. If your sd card is mmcblk0, then you are set and you don't need to modify anything. If your sd card is anything else other than mmcblk0, you will need to modify the globalvariables.sh file and set the sd card prefix. This will be just the device name, no /dev or partition numbers. For example, on my laptop it is `mmcblk0`.

Now, just to make sure it's set correctly, you can run ./mountroot.sh and make sure it mounts the root partition p2 of the sdcard int ~/raspberry/kernel/mounts directory.

You can now run installnewkernel.sh to automatically mount the partitions and copy the kernel and modules over to the sd card. Once it's done, takes only a couple of seconds, you can then place the sd card in your raspberry pi, and you should be good to go.

./installnewkernel.sh

So, I hope that helps others build their own custom kernel and get it setup and running on the raspberry pi 2.