Upstream linux kernel on a Raspberry Pi
Recently, I signed up for the Eudyptula Challenge. If you have not heard of it, it is a series of programming exercises designed to train you with Linux Kernel Development process. One of the tasks is to install the latest linux kernel on your machine. You have an option of doing it in a Virtual box setting. Hey, but where is the fun in doing that? I had a Raspberry Pi handy and found a couple of good Wiki pages on how to compile your own kernel for Raspberry Pi (here and here). These two pages have the all the info needed but it is kind of scattered. Fastest way to compile the kernel is to do in in Ubuntu (You could technically do it on a MAC, but it is slightly more involved). I tried on my MAC first but ended up installing Ubuntu through Virtualbox to do the compilation. Here are the steps:
- Install Virtualbox on your MAC (skip if you already have a Ubuntu setup)
- Make sure you allocate atleast 16 GB for your Ubuntu partition.
- Create a new directory in your home directory and call it whatever you want (I called it linux).
- Install git using the following command:
- Change your current directory to the new one you just created.
- Run this command to get the compiler:
- Run this command to get the u-boot (default raspberry pi bootloader won’t work for the upstream kernel)
- Get the kernel source from Linus Torvalds
- Create the following exports:
- Time to create a config file which tells what to compile and what not to. (
ARCH=arm & CROSS_COMPILE=${CCPREFIX}
very important).
- Second command creates a .config file in your current directory and the menuconfig command opens up a new window for selecting different modules to compile. Just enabled the option which says “Modules” and save the configuration.
- Finally to do the actual the compilation and install all the needed modules (in a temporary location):
- Once kernel compiles successfully (which it should if you have followed the steps), time to compile the bootloader. Run the following commands:
- Once this is done, go inside the tools directory under u-boot directory:
- Create a file by name boot.scr with the following contents (for Raspberry Pi Model B):
- Or for Model B+:
- And then run the following command (this should generate a boot.scr.img)
- Now, go to your /boot directory on your Raspberry Pi and create a new directory called backup and move everything inside the backup directory. You might have to sudo the last command.
- Fetch
start.elf
andbootcode.bin
from https://github.com/raspberrypi/firmware/tree/master/boot and save it to your$SD
- Finally, move all the required files from your Ubuntu machine to your Raspberry Pi using scp. You might have to move it to a temporary location first (in this case I am moving it to the home directory).
- Go to your Raspberry Pi and use sudo mv to move the files from your home directory to the /boot directory as follows:
- Now your boot directory should have the following files:
- For Model B+, you will have
bcm2835-rpi-b-plus.dtb
instead ofbcm2835-rpi-b.dtb
. - Copy your source code from the Ubuntu machine to your Raspberry Pi:
- Change the permissions just in case
- Copy the modules next (Move to a temporary folder if you can’t write to /lib/modules directly and then sudo mv it).
- Correct the symbolic links next:
- Finally, we need to fix one more problem: During the cross-compilation build, a couple scripts were compiled for the host. We also need them for the Raspberry Pi. Still on the Pi, this can be checked with e.g.
- Reboot the device and see your new kernel load in all its glory.