3

Does anyone know how to determine the kernel release of a Raspbian kernel on disk (e.g. 4.19.93-v7+ or something similar)?

I know uname -r will tell me the release of the running kernel, but if maintenance has been applied and a reboot has not yet been done, the values may differ.

Before someone asks "why?", I wanted to write a quick C program to validate that the kernel on disk, the modules (/lib/modules), the initramfs statement in /boot/config.txt, and the initramfs file in /boot/ are all in agreement.

PS. It's not a string in the kernel that I can see with the strings command, strings /boot/kernel7.img | grep "4.19"

3

if you are running a Raspbian Buster image it is easy to find the version in the image of the new 64 bit /boot/kernel8.img. You can just use:

rpi ~$ strings /boot/kernel8.img | grep 'Linux version'
Linux version 4.19.93-v8+ (dom@buildbot) (gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9)) #1290 SMP PREEMPT Fri Jan 10 16:51:04 GMT 2020

But it only works because this is the only kernel image that isn't compressed by default. That's the reason why you don't find the version simply with strings in other kernel images. You have to decompress it but that cannot simply be done because the compressed part is anywhere contained in the kernel image and you have to know how it is compressed. I have checked that it is compressed in gzip format but you have to find the start of the compressed part. The header of the gzip part starts with a magic string \x1f\x8b\x08 so you can search for it. Here in short how I have tested it. For details look at the references. To get /proc/config.gz to look for the compressed format CONFIG_KERNEL_* you have to load sudo modprobe configs.

rpi ~$ pos=$(LC_ALL=C grep -P -a -b -m 1 --only-matching '\x1f\x8b\x08' /boot/kernel7l.img | cut -f 1 -d :)
rpi ~$ dd if=/boot/kernel7l.img of=kernel7l.gz bs=1 skip=$pos
rpi ~$ gzip --decompress --stdout kernel7l.gz | strings | grep 'Linux version'
Linux version 4.19.93-v7l+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1290 SMP Fri Jan 10 16:45:11 GMT 2020

gzip: kernel7l.gz: decompression OK, trailing garbage ignored

Please note that LC_ALL=C preceeding grep is important because it is using UTF-8 by default and will not find the hex value \x8b due to its most significant bit is set. Grep will not find the offset then.


References:
Getting kernel version from the compressed kernel image
Find gzip start and end?
Bytewise grep fails for some byte values

| improve this answer | |
  • Thank you Ingo! That's a lot of digging! All my Pi's are 32-bit (Zero W's and 3B+'s - all others are retired). An assumption (and probably a poor one, as all assumptions are) is that the the 64-bit "8" kernel is the same release as all of the other Raspbian kernels supplied in an update. Is that a fair statement? Thanks again. – tommylovell Feb 6 at 16:41
  • @tommylovell I see the new kernel8.img more as a preview because it isn't very useful these days without a 64 bit operating system (Raspbian is still only 32 bit). I think it will also come compressed later. – Ingo Feb 6 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.