The question is, how do I install the correct version of kernel-headers for whatever
uname -r kernel would be running on a random Raspberry Pi?
Long story is, I want to compile some loadable kernel module for the currently running version of the Linux kernel. To install the Linux headers, there is an apt package in the Raspberry Pi repositories, named raspberrypi-kernel-headers.
However, raspberrypi-kernel-headers is for an older kernel version (from a year ago) than the running kernel. Yet, the package wrongly installs as if it were for the currently running kernel version.
As a result, all kind of weird errors might appear during a compilation. Even worst, for some modules the compilation might succeed without errors, yet the loading and running of the compiled driver will throw all kind of strange errors at runtime.
Took me a day to figure out that sudo apt install raspberrypi-kernel-headers installed a wrong version of headers for the currently running kernel.
Currently running Raspberry OS (based on Debian 12 Bookworm) is from a fresh SD card imaged with '2024-03-15-raspios-bookworm-armhf-lite.img.xz'. uname -a
Linux rpi 6.6.20+rpt-rpi-v6 #1 Raspbian 1:6.6.20-1+rpt1 (2024-03-07) armv6l GNU/Linux
uname -r
6.6.20+rpt-rpi-v6
apt policy raspberrypi-kernel-headers
raspberrypi-kernel-headers:
Installed: (none)
Candidate: 1:1.20230405-1
Version table:
1:1.20230405-1 500
500 http://archive.raspberrypi.com/debian bookworm/main armhf Packages
Kernel and headers from the apt repository 1.20230405-1 corresponds to kernel version 6.1.21+ from a year ago, while the current running version is 6.6.20+rpt-rpi-v6.
I've learned that for Raspberry Pi the kernel is seen as firmware update, and there is a dedicated tool for that, named rpi-update. When run without arguments, with sudo rpi-update, it downloads and installs the latest kernel for Raspberry Pi (from the github repository), and at the next boot the newest kernel will be in effect. When an argument is given, it will download and install from the location specified as an argument. The argument can be a git commit ID, or the URL of a git tag asset from github. For example:sudo rpi-update https://github.com/raspberrypi/firmware/archive/refs/tags/1.20230405.tar.gz
sudo reboot
will downgrade the currently running kernel 6.6.20+rpt-rpi-v6 to the 6.1.21+ kernel from last year.
So far (as a workaround) I've used
rpi-update to downgrade the current kernel
6.6.20+rpt-rpi-v6 to
6.1.21+ from last year, because the today kernel-headers from the apt repos are also for
6.1.21+. So, I've downgraded the kernel to a version from a year ago, then install the current 'sudo apt install raspberrypi-kernel-headers' from today apt repositories, which for some reason I don't understand, happens to contain the kernel-header for the
6.1.21+ from a year ago.
Now, my own loadable module compiles and runs without errors, but with the last year kernel. This is only a hack. I would like to use a more recent kernel and be able to compile for that one. Preferably the one that came with the current Raspberry OS Bookworm, kernel
6.6.20+rpt-rpi-v6.[/s]
How do I install the correct version of kernel-headers for whatever
uname -r kernel would be running on a random Raspberry Pi?[/s]
Later edit:
-------------
After digging for a few more days, turns out many of the above was because of my misunderstanding. Too hard to correct, so will just strikeout the text, my bad, sorry. Also, for either Bullseye or Bookworm Raspberry OS lite I've tried, the kernel headers are already included when flashing the stock image on the SD card, no additional headers installs needed. For Bookworm, the kernel sources, too, (not only the headers) seems to be included in the stock SD card image.