I would like to learn how things work regarding the Linux kernel on the Librem 5, how to modify and rebuild the kernel, how to install a modified version and how to choose which kernel gets used in case several versions are installed. Things like that.
Here is what I figured out so far.
We can get the kernel source code from the git repo at https://source.puri.sm/Librem5/linux-next like this:
sudo apt install git
git clone https://source.puri.sm/Librem5/linux-next.git
That takes a while, it’s a big repo.
Then enter the source directory and install dependencies:
cd linux-next/
sudo apt-get build-dep .
Before building, I made a little change to help convince myself that the modified kernel is really used, to recognize “my own” kernel version. When booting there is a log message like this:
sudo journalctl -b | grep Booting
Feb 25 19:45:35 pureos kernel: Booting Linux on physical CPU 0x0000000000 [0x410fd034]
that comes from this line in the source code, in arch/arm64/kernel/setup.c
:
pr_info("Booting Linux on physical CPU 0x%010lx [0x%08x]\n",
which I changed like this:
pr_info("Booting Linux -- modified by Skalman :-) -- on physical CPU 0x%010lx [0x%08x]\n",
So then when the new kernel is running, that log message should look different.
Now build the kernel:
dpkg-buildpackage -us -uc -b
That took 130 minutes, resulted in 4 different deb files:
cd ..
ls -l *.deb
-rw-r--r-- 1 root root 7352076 Feb 25 23:44 linux-headers-5.9.0-1-librem5_5.9.16pureos0~amber0_arm64.deb
-rw-r--r-- 1 root root 15868196 Feb 25 23:46 linux-image-5.9.0-1-librem5_5.9.16pureos0~amber0_arm64.deb
-rw-r--r-- 1 root root 15676 Feb 25 23:46 linux-image-librem5_5.9.16pureos0~amber0_arm64.deb
-rw-r--r-- 1 root root 1113868 Feb 25 23:44 linux-libc-dev_5.9.16pureos0~amber0_arm64.deb
The largest one seems to be the kernel itself, so install that:
sudo dpkg --install linux-image-5.9.0-1-librem5_5.9.16pureos0~amber0_arm64.deb
and then reboot.
Now, after rebooting, check that log message to see if the new kernel is used:
sudo journalctl -b | grep Booting
Feb 25 23:57:06 pureos kernel: Booting Linux -- modified by Skalman :-) -- on physical CPU 0x0000000000 [0x410fd034]
So it worked, I am now a kernel developer!
Some questions about this:
- It took quite a long time, more than two hours to build, and if using this procedure the whole build will be repeated and it will take two hours again if a small change is made. That will slow down the trial-and-error procedure when debugging or testing something. How to make it faster? If we skip the debian packaging stuff then I think we can just do “make” and that can be quick it there was only a small change, but then both configuration and installation needs to be done differently, when the debian packaging is no longer used for that. What is the best way, to allow testing kernel code changes more quickly?
- Things are installed in the /boot/ directory, notably
vmlinuz
andinitrd.img
files, and it seems like several of those for different kernel versions can be installed in /boot/ and at boot time one of them is picked somehow. How does that work, how can we choose which kernel is used in case several kernel versions are installed? I have seen that grub can be used for this, but on the Librem 5 it seems grub is not used, something else is going on. - I think the main reason that the source code package is so large and takes such a long time to build is that it contains drivers for lots and lots of hardware, most of which does not exist on the Librem 5 so that code is built but is not really going to be used, is that right? If so, could we simplify things and get a smaller kernel that is quicker to build by simply removing unused parts?
- Any other useful tips and tricks regarding kernel building and installing and so on?