Skip to content

Build an RPi4 64bit Kernel on the RPi

Hecatron edited this page Sep 25, 2020 · 4 revisions

This is just a quick guide on building the same kernel sources used for gentoo-on-rpi-64bit

For the rpi4

For the rpi3

Downloading the kernel source from github

First we're going to download the source
At the time of writing gentoo-on-rpi-64bit is using the official rpi sources 5.4 branch for the pi4

Now we could do a git clone and download the git repo, but that could take a while
so as a shortcut lets just download the sources for the version we want instead.

  • First lets go to https://github.com/raspberrypi/linux
  • Next select the rpi-5.4.y from the list of branches on the left hand side
  • Select Commits, and click on the specific commit as a marker for the date time of the source we want to use

At the time of writing the latest commit in use by bcm2711-kernel-bis is 3b41649ff96d
but this appears to be an orphaned commit, for the 5.4 branch this appears to be
https://github.com/raspberrypi/linux/commit/dec0ddc506ab5d93a7de4b8a7c8dc98e0a96f85c
with the same description

Lets use that url we've copied

cd /usr/src
wget https://github.com/raspberrypi/linux/archive/dec0ddc506ab5d93a7de4b8a7c8dc98e0a96f85c.zip
unzip dec0ddc506ab5d93a7de4b8a7c8dc98e0a96f85c.zip
mv linux-dec0ddc506ab5d93a7de4b8a7c8dc98e0a96f85c 5.4.47-v8-p4-bis-gbd1
rm dec0ddc506ab5d93a7de4b8a7c8dc98e0a96f85c.zip

For the version number 5.4.47 we can see what this is by looking at the top of the Makefile.
The name of the directory can be anything you want
In this case I'm making sure to use a version string different from the default one provided with gentoo-on-rpi-64bit.
This copy won't include any of the git history so should be a lot quicker to download.

Patching Sources

There's a custom script within bcm2711-kernel-bis, we can use this to patch the sources
Currently I don't think it does anything, although it looks like it can be set to pull in pull requests from github and patch them in.

cd /usr/src/5.4.47-v8-p4-bis-gbd1
wget https://raw.githubusercontent.com/sakaki-/bcm2711-kernel-bis/master/patch_kernel.sh
chmod +x patch_kernel.sh
./patch_kernel.sh

Configuring Source

There is a script called conform_config.sh we could use to alter the default rpi config
although instead I'm just going to use the kernel config from the bcm2711-kernel-bis repo

# Assume we're already inside the linux source directory
wget https://raw.githubusercontent.com/sakaki-/bcm2711-kernel-bis/master/config
mv config .config

At this point you can change any setting you want.
typically I use

make menuconfig

Setting the Config local version

Next one change that's good to make is to call the new kernel something different from the old one.
This way the modules directory / kernel names won't clash and overwrite one another.
In this case I'm going to try and get it to match the name of the directory.
so edit the .config file and change the setting of CONFIG_LOCALVERSION

# from
-v8-3b41649ff96d-p4-bis
# to something different
-v8-p4-bis-gbd1

Building / Installing the kernel

The rpi4 is quad core, so lets build everything
Note there's no zImage for arm64

make -j4 Image modules dtbs

Lets install the kernel and modules

make modules_install
cp arch/arm64/boot/Image /boot/kernel-5.4.47-v8-p4-bis-gbd1.img

Switch over the kernel

To switch across to the new kernel Edit /boot/config.txt and switch across the kernel option to point to the new file

kernel=kernel-5.4.47-v8-p4-bis-gbd1.img

Finally reboot and check which kernel is in use

uname -r

Creating an initrams with dracut

I've found dracut to be a little easier to use than genkernel for generating a initramfs file

# install dracut
emerge dracut
# lets enable gzip compression
# add the following to a file such as /etc/dracut.conf.d/user
compress="gzip"
# run dracut
dracut

At this point we should have a file within our /boot directory
something like /boot/initramfs-5.4.47-v8-p4-bis-gbd1.img
By default gzip compression will be used inside the cpio image

Lets add that to the config.txt file

initramfs initramfs-5.4.47-v8-p4-bis-gbd1.img followkernel

One of the advantages to an initramfs image is an emergency shell that can be used in the event something goes wrong. Although it may be easier to just switch to the OS on the SD card in the event this happens.

Note to run dracut and overwrite an existing initramfs file already present

dracut --force

To generate an initramfs for a kernel version which is not currently running (one you may have just built)

dracut --kver 5.4.65-v8-p4-bis
Clone this wiki locally