We're hiring!
*

Quick hack: Raspberry Pi meets Linux kernel mainline

Helen Koike avatar

Helen Koike
March 12, 2019

Share this post:

Reading time:

With just a few simple steps, you can compile and boot a Raspberry Pi using the Linux kernel mainline source code. Here's a quick tutorial on how to do just that.

This post uses Raspberry Pi model B rev2 as a base, however you can easily adapt it to your board.

Install Raspbian on a SD card:

Download any Raspbian image from https://www.raspberrypi.org/downloads/raspbian/

unzip 2018-11-13-raspbian-stretch-lite.zip
# Check with lsblk what is the device path of your sdcard, using /dev/mmcblk0 just for this example
sudo dd if=2018-11-13-raspbian-stretch-lite.img of=/dev/mmcblk0 bs=512M

Insert your SD card into the Raspberry Pi and turn it on. Raspbian will finish its installation.

Once the boot process is complete, turn it off and insert the SD card into your computer again.

Install the tool chain

sudo apt install gcc-arm-linux-gnueabihf

Cross compiling the kernel

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2835_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8 zImage modules dtbs

Installing the kernel on the SD card

# Mount the sdcard partitions
mkdir -p ../rpi-boot
mkdir -p ../rpi-rootfs
sudo mount /dev/mmcblk0p1 ../rpi-boot
sudo mount /dev/mmcblk0p2 ../rpi-rootfs

# Install the modules to the rootfs
sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../rpi-rootfs modules_install

# Copy the kernel and the device tree files to the sdcard
sudo cp arch/arm/boot/zImage ../rpi-boot
sudo cp arch/arm/boot/dts/*.dtb ../rpi-boot

# Inform to the firmware which kernel image and device tree to use
# You might need to change the device tree according to you board model
echo "kernel=zImage" >> ../rpi-boot/config.txt
echo "device_tree=bcm2835-rpi-b-rev2.dtb" >> ../rpi-boot/config.txt

# Unmount
sudo umount ../rpi-boot
sudo umount ../rpi-rootfs

That's it! Remove the SD card, plug it in your Raspberry Pi and boot!

Automating

Now, just put the previous commands in a bash script (just remember to remove the lines that modifies config.txt as this is only required once), and execute it whenever you want to re-build and re-install the kernel on your sdcard.

Booting the original kernel

If you need to revert to the original kernel, you can always do so by inserting the SD card into your computer, mounting the boot partition and commenting the kernel and device_tree lines from your config.txt

# Uncomment these lines to boot custom kernel
#kernel=zImage
#device_tree=bcm2835-rpi-b-rev2.dtb

Happy hacking!


Visit Helen's blog.

Comments (5)

  1. wb666greene:
    Mar 14, 2019 at 03:29 PM

    What is the point? What is wrong with the kernel that comes with Raspbian? How long does it take to compile? Took over five hours to compile openCV on my Pi2.

    Reply to this comment

    Reply to this comment

    1. Helen Koike:
      Mar 14, 2019 at 06:01 PM

      There are several reasons someone may want to use mainline kernel:
      * You want to contribute back to the community by fixing a bug, helping with testing and reporting the bugs you find, helping to develop a new feature or optimization.
      * You need a feature that was recently added to the kernel but it is not in Raspbian kernel yet
      * You want compile the kernel with some optimizations, or leave out some parts of the kernel that you don't use to make a smaller footprint.

      Regarding the compilation time, it depends on your machine, I have an XPS15 (i7-6700HQ CPU @ 2.60GHz) and compiling with 8 threads (the -j8 option) took me 4 minutes:

      $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- clean
      ...
      $ time make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8 zImage modules dtbs
      ...
      real 3m6.684s
      user 19m0.466s
      sys 1m35.912s

      Reply to this comment

      Reply to this comment

      1. Alex:
        Dec 29, 2019 at 03:36 PM

        I think you're missing the point of mainlining the kernel; yes, the kernel coming with raspbian works fine, but the goal is to get the latest features (there are rare programs that require specific kernel version), performance patches etc

        Reply to this comment

        Reply to this comment

  2. chaitanya:
    Oct 16, 2020 at 10:01 AM

    I tried this procedure on raspberry pi, but the modules(USB, ETHERNET, WIFI, BLUETOOTH ) modules are not working.

    Reply to this comment

    Reply to this comment

    1. Helen Koike:
      Oct 16, 2020 at 07:18 PM

      Hi chaitanya,

      I'm sure there are several things that aren't well supported on mainline yet, I hope that this post would give a starting point for those who want to start contributing.

      You can find all the drivers in the Raspbery Pi downstream kernel on github[1], and if you are interested in starting upstreaming those, it would be even better ;)

      [1] https://github.com/raspberrypi/linux

      Reply to this comment

      Reply to this comment


Add a Comment






Allowed tags: <b><i><br>Add a new comment:


Search the newsroom

Latest Blog Posts

Persian Rug, Part 2 - Other ways to make object soups in Rust

27/09/2023

Why is creating object graphs hard in Rust? In part 1, we looked at a basic pattern, where two types of objects refer to one another. In…

WhisperSpeech: Exploring New Horizons in Text-to-Speech Technology

13/09/2023

Text-to-speech (TTS) models are playing a transformative role, from enriching audiobooks to enhancing podcasts and even improving interactions…

Bridging IIO and Input in Linux

21/08/2023

In Linux, the Industrial Input/Output subsystem manages devices like Analog to Digital Converters, Light sensors, accelerometers, etc. On…

Pushing testing laboratory performance limits by benchmarking LAVA - Part 1

10/08/2023

Collabora's main testing laboratory has grown to automate testing on over 150 devices of about 30 different types. The lab receives job…

Persian Rug - It really ties the Rust room together

09/08/2023

Rust is a modern language known for its memory safety, efficiency, and wide range of high-level features. But many beginners also run into…

Triple Threat: The Power of Transcription, Summary, and Translation

03/08/2023

At Collabora, we're committed to bringing people together. That's why we're pushing state-of-the-art machine-learning techniques like Large…

Open Since 2005 logo

We use cookies on this website to ensure that you get the best experience. By continuing to use this website you are consenting to the use of these cookies. To find out more please follow this link.

Collabora Ltd © 2005-2023. All rights reserved. Privacy Notice. Sitemap.