SARPi Project - Slackware ARM on a Raspberry Pi
Building your own Linux kernel on the Raspberry Pi
Configuring and compiling a Linux kernel and modules is no easy task for most people, even on Slackware. However, on the Raspberry Pi, it can be somewhat easier and less time consuming than on a lot of other platforms. Depending on how much time and effort is required and/or how much you're willing to invest.
This project will take you through the kernel building process the quick and easy way. You will need to have 'bison', 'gawk', 'git' and 'flex' installed on your system. Due to the comprehensive nature of Slackware ARM, everything you need is included, except for the RPi Linux kernel source. It might be prudent to use a spare SD card for this project, installed with Slackware ARM. If things go very wrong than at least you're not trashing a perfectly good working system which you may depend on.
Check that you have the above packages installed using the 'whereis' command.
If any of these packages are missing, install them before proceeding. All packages are available from the Slackware.uk mirror service for Slackware Linux based projects. Or any other Slackware ARM mirror.
This project guide came as a result of many people over many years asking how to build kernels on the Raspberry Pi.
Download the Raspberri Pi Linux kernel source
To download the Linux kernel source code you will use the 'git' command. You're not going to download the entire source-tree. You'll only be downloading a shallow clone with a history that's truncated to the last commit.
git clone --depth=1 git://github.com/raspberrypi/linux
This will download the latest commit of the RPi Linux kernel branch into a directory named 'linux' inside your '/tmp/build-dir' directory.
Configure the Linux kernel source
The simplest method to configure the kernel source for your Raspberry Pi is to use a pre-defined set of values called a 'defconfig'. This basically means it's a 'default configuration' file and a lot of them exist for many different types of hardware and devices. Obviously, the Raspberry Pi Linux kernel is optimised for the device and therefore has a 'defconfig' file to cover all model revisions. Invoking a 'defconfig' will create a kernel '.config' file, which is the blueprint set of values and settings from which the gcc compiler will build a kernel and modules.
First you should make the new 'linux' directory your current directory, in order to create the kernel '.config' file for your Raspberry Pi. Read the options below and use the one best suited to you.
If you are using a Raspberry Pi (1) or Zero type the following command:
If you are using a Raspberry Pi 2 or 3 type the following command:
If you are using a Raspberry Pi 4 type the following command:
What this process has done is to create a file named '.config' in your 'linux' source directory. This file is required in order to move on to the next step.
Compiling the Linux kernel source
Now you're all set to build the kernel, kernel modules, and device-tree blobs. This process is going to take some time. If you're working on a Raspberry Pi 4 (4GB) this time could be around 2-3 hours. If you're working on a Raspberry Pi (1) or Zero then, after the build process has been started, come back tomorrow. [NB: that is not a joke!]
BE AWARE! It can take around 24 hours [i.e. a whole day] to compile a full 'bcmrpi_defconfig' kernel and modules on the Raspberry Pi (1) [512Mb RAM].
NB: On a Raspberry Pi 2, 3 or 4, you can use 'multijobs' by specifying the gcc make '-j4' option. This splits the work between all [4x] CPU cores, significantly decreasing compilation time. You can simply add this option into the 'make' command.
For example: make -j4 Image && make -j4 dtbs && make -j4 modules
To start the kernel build process type the following command [and don't forget about the '-j4' option if you can use it]:
The build process will start and carry out each 'make' command seperated by '&&' [i.e. one after the other, in order] until the last one has completed.
Installing the kernel modules
When the above has completed type the following command to install the newly built modules:
The above command will install the kernel modules into the '/lib/modules/' directory. Depending on the version of kernel you're compiling will determine the directory name. For example, if you're building kernel 4.19.60 then the module directory will be '/lib/modules/4.19.60' and may have a filename suffix too.
Installing the kernel and device-tree blobs
Installing is the appropriate phraseology, but "copying" is more accurate. All you need to do here is copy over the /boot files from the compiled binaries you've created from the Linux source code, which includes the kernel, System.map, and device-tree blobs [dtbs]. NB: If you have any existing files that you'll be over-writing, be sure to back them up BEFORE copying any files over!
IMPORTANT! There is a different filename for the kernel depending on which Raspberry Pi version you are building for. These kernel filenames are;
• Raspberry Pi (1): kernel.img
• Raspberry Pi 2/3: kernel7.img
• Raspberry Pi 4: kernel7l.img
So, when you copy the kernel image make sure to name it according to your Raspberry Pi version. The "l" in 'kernel7l.img' is a small "L", as in large. It's actually an indicator for "Large Physical Address Extension (LPAE)".
So, [for example] if you were using a Raspberry Pi 4 you would use the following commands to copy the files:
cp System.map /boot/System.map
cp arch/arm/boot/dts/*.dtb /boot/
Now your system should have the new kernel and modules, and device-tree blobs, in place for the next (re)boot. That's basically all you need to do.
The final test is to boot the Raspberry Pi. If you've done everything correctly then the device should boot into Slackware ARM using your newly built kernel, modules and blobs.
If you have any problems (re)booting the Raspberry Pi it may be because you have forgotten something in the build procedure. Or it could be down to the firmware you have installed. Updating the firmware and keeping up-to-date with it is a prudent step.
The SARPi Project hopes the information in this mini-project was helpful or useful. If you have any questions, or need assistance please use the Linux Questions Slackware ARM Forum.
Thanks for your interest in our efforts!
SARPi Project Team