Skip to content

alwint3r/crosscompile-raspberrypi-kernel-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Cross-compiling Raspberry Pi Kernel on a Docker Container

An Ubuntu 16.04-based environment for cross-compiling Raspberry Pi kernel.

Why?

Isolation. I need it so bad because I screwed up my host environment.

Pros & Cons

Pros

  • Faster build time, assuming your machine is far more powerful than a Raspberry Pi.

Cons

  • Installing a newly built kernel isn't that simple, especially if you're targetting a Raspberry Pi Compute Module board.

It's a lot simpler if you build the kernel on the Pi itself, but the whole process can take hours to finish.

Starting the Container

Clone the repository

git clone https://github.com/alwint3r/raspberrypi-kernel-crosscompile

Move to the cloned repository, then build the docker image.

docker build -t rpi-cross-compile:latest .

Don't forget to set up a directory to store the compilation result on your host machine. Mount that directory as one of the volumes of the container using /root/raspberrypi as the mount point. For example, I'm going to use the raspberrypi-linux directory in my home directory.

docker run --name rpi-cross-compile-kernel -it \
 -v $HOME/raspberrypi-linux:/root/raspberrypi \
 rpi-cross-compile:latest \
 /bin/bash

A new container named rpi-cross-compile-kernel will be created and a new bash session will be opened.

Getting the Kernel Source Code

You can get the source code by cloning the whole repository at https://github.com/raspberrypi/linux.

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

If you prefer to get the source code at a specific branch only, then run the following command:

git clone --depth=1 --branch rpi-4.18.y https://github.com/raspberrypi/linux

In the example above, the branch that we'll get is rpi-4.18.y which means we will get the source code for kernel version 4.18. If you prefer another version, then just change the branch name to match your preferred kernel version.

Building the Kernel

You can follow the guide on the official documentation.

The steps are pretty much the same as the guide on the official documentation.

Here's what I did

  1. Configuring kernel for target, in this case, I'm targetting a Compute Module 3
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
  1. Run menuconfig to configure the kernel, you can skip this step if you think it's not necessary. It is necessary for me because I need to enable a staging driver.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
  1. Start building the kernel image, modules, and device trees
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs

Yes, you can use j N flagh with N is the number of jobs that will be run at the same time.

  1. As for installing the modules, I need to copy the entire kernel source code to my Raspberry Pi and then run the installation of the modules there. It is a lot easier than copying the pre-installed modules on your container to the Raspberry Pi. Trust me, you don't want to deal with symlinks.

In the Raspberry Pi terminal, inside the linux kernel directory

sudo make install_modules
  1. If you need to compile a driver source code based on the kernel that you just compiled yourself, see this guide. We need to compile the scripts in the Raspberry Pi instead of in our host machine.

About

Cross-compile Raspberry Pi kernel on a docker container.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published