Wednesday, October 7, 2015

Determine kernel version of the Linux kernel just built


You just successfully built Linux kernel (but have not installed yet) and wish to find out (and may be want to use) the version number of the newly generated kernel, then running following command in the kernel source root directory:
make kernelversion
will print something like:
4.3.0-rc4

However, this is not the complete version if you have enabled CONFIG_LOCALVERSION_AUTO in the your configuration (.config)

After some search within the kernel tree, complete kernel version was found placed in the file utsrelease.h (by the build process). Further research suggested that the location of the file utsrelease.h has moved within the kernel source tree since some year around 2011. So here is a series of chain command, keeping that in mind, that would output the complete Linux kernel version (and just that) that you could use:

$ grep UTS_RELEASE $(find -name utsrelease.h) | cut -d \" -f 2
4.3.0-rc4-12566-gf670268

 I found this useful to make a copy of .config of the just built kernel in the /boot directory, like so:
$ export newkernelver=$(grep UTS_RELEASE $(find -name utsrelease.h) | cut -d \" -f 2)
$ echo $newkernelver
4.3.0-rc4-12566-gf670268
$ sudo cp .config /boot/config-$newkernelver
$ ls /boot/config-4.3*
/boot/config-4.3.0-rc4-12566-gf670268

Do leave me a comment, if you know a better way to do the same.

UPDATE:
Found discussed elsewhere that following command gives out the complete kernel version too:
$ file arch/x86/boot/bzImage
arch/x86/boot/bzImage: Linux kernel x86 boot executable bzImage, version 4.3.0-rc4-12566-gf670268 (someuser@somehost) #3 S, RO-rootFS, swap_dev 0x5, Normal VGA

Following chain command will extract just the version part that could be used elsewhere:
$ file arch/x86/boot/bzImage | awk -F "version " '{print $2}' | cut -d \  -f 1
4.3.0-rc4-12566-gf670268
Any better solutions ? Leave me a comment.

Listing 'make targets' related to Linux Kernel

The README file in the root of linux kernel source tree lists and explains only most commonly used 'make targets' supported by the kernel Makefile, but does not list them all.

So after playing a little with grep I concluded following would be useful for listing all possible 'make targets' (ignoring some false positives) in the Linux kernel Makefile.

grep  ^.*: Makefile | grep -v = | grep  -v ^#
Snipped output:
help:
prepare: ;
scripts: ;
clean: $(clean-dirs)
tags TAGS cscope gtags: FORCE
includecheck:
versioncheck:
coccicheck:
namespacecheck:
export_report:
checkstack:
kernelrelease:
kernelversion:
image_name:
Note: I have a feeling that better solution(s) than this exists to list the same, so please leave in comments.