Raspberry Pi/Kernel Compilation

From Gentoo Wiki
Jump to: navigation, search

The Raspberry Pi cannot run a vanilla Linux kernel. A patched version of the kernel is maintained by the Raspberry Pi Foundation and is available from their GitHub page, https://github.com/raspberrypi

Note
The official Raspberry Pi Foundation kernels are built 32 bit, which is appropriate for Raspberry Pi 1, 2 and 3 (running in 32 bit mode; recommended). This guide does not cover building a 64 bit kernel for the Raspberry Pi 3 (issues / unstable / not recommended).

Prerequisites

To compile a kernel, you require dev-vcs/git to download the source code and also (optional) genkernel to manage the build process.

root #emerge --ask git genkernel

Get the Kernel Source

root #cd /opt
root #git clone --depth 1 git://github.com/raspberrypi/linux.git
root #ln -s /opt/linux /usr/src/linux

Compile and Install the Kernel with genkernel

Note
Compiling will take about 6 hours.

Using genkernel you can build a Linux kernel with support for many different features. Follow one of the examples below that has the features that you require.

Default Kernel

In this example the configuration options from the running kernel are used to compile the new kernel.

root #genkernel --kernel-config=/proc/config.gz kernel

After the kernel has compiled it will be installed into the /boot folder.

Kernel with initramfs

This example will run menuconfig before compiling the kernel, allowing you to enable any extra modules you may need. Using a kernel with an initramfs allows you to load modules, decrypt partitions and other more complex task that maybe require early in the boot process.

root #genkernel --kernname=rpi --menuconfig all

To support initramfs the following options need to be enabled in menuconfig.

KERNEL
General setup  --->       
    [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
        ()    Initramfs source file(s) (NEW)
        [*]   Support initial ramdisks compressed using gzip (NEW)
        [ ]   Support initial ramdisks compressed using bzip2 (NEW)

After the kernel has compiled it and the initramfs be installed into the /boot folder, you need to add it to bootloader (skip to Adding New Kernel to Bootloader)

Compile and Install the Kernel without genkernel

First time you get kernel sources, create default .configure file (for Raspberry Pi2 use bcm2709_defconfig) :

root #cd /usr/src/linux
root #make bcm2835_defconfig

After that, you can modify this default configuration (a good idea is to add .config support) :

root #cd /usr/src/linux
root #make menuconfig

Share-> I use this one : https://github.com/modulix/raspggen/blob/master/kernel.conf

And then try to compile/install it :

root #cd /usr/src/linux
root #mount /boot
root #mkdir /boot/overlays
root #make -j4 zImage modules dtbs
root #make modules_install dtbs_install
root #scripts/mkknlimg arch/arm/boot/zImage /boot/kernel7.img
Note
If this kernel is called kernel7.img, you don't need to add it in /boot/config.txt file.

For now, to make work WIFI, you need also to download firmware:

Adding New Kernel to Bootloader

By default the Raspberry Pi looks for a kernel in /boot/kernel.img. This is changed in the configuration file /boot/config.txt to load the new kernel.

FILE /boot/config.txtExample config.txt
kernel=kernel-genkernel-arm-3.2.27+

If using an initramfs you also need to add that to the config.txt.

FILE /boot/config.txtExample config.txt with an initramfs
kernel=kernel-rpi-arm-3.2.27+
initramfs initramfs-rpi-arm-3.2.27+

Now the Raspberry Pi can be rebooted and should make use of the new kernel. If for some reason the new kernel does not load or gives errors, the kernel entry in the /boot/config.txt can be removed. Then on the next reboot the default kernel.img will be loaded.

Detailed step-by-step guide

If you encounter problems building or deploying the kernel, try following the detailed kernel building guide for clues on resolving the problems. Additionally The Raspberry Pi foundation provides these build guides to assist in Kernel compilation.