Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting kernel version from linux kernel module at runtime

how can I obtain runtime information about which version of kernel is running from inside linux kernel module code (kernel mode)?

like image 887
Bogi Avatar asked Nov 06 '11 22:11

Bogi


People also ask

How do I find my Linux Kernel version?

To check Linux Kernel version, try the following commands: uname -r : Find Linux kernel version. cat /proc/version : Show Linux kernel version with help of a special file. hostnamectl | grep Kernel : For systemd based Linux distro you can use hotnamectl to display hostname and running Linux kernel version.

How do I see what is using my kernel module?

You can try lsmod | grep <module name> to see all loaded kernel modules that are using a module. You can also try dmesg | grep <module name> to see if the kernel logs have any clues as to which processes may be using a module. You may be able to remove the module using rmmod --force <module_name> .

Which of the following is used to display the kernel version?

We will use uname command, which is used to print your Linux system information such as kernel version and release name, network hostname, machine hardware name, processor architecture, hardware platform and the operating system.

How do I list loaded kernel modules?

Under Linux use the file /proc/modules shows what kernel modules (drivers) are currently loaded into memory.


2 Answers

By convention, Linux kernel module loading mechanism doesn't allow loading modules that were not compiled against the running kernel, so the "running kernel" you are referring to is most likely is already known at kernel module compilation time.

For retrieving the version string constant, older versions require you to include <linux/version.h>, others <linux/utsrelease.h>, and newer ones <generated/utsrelease.h>. If you really want to get more information at run-time, then utsname() function from linux/utsname.h is the most standard run-time interface.

The implementation of the virtual /proc/version procfs node uses utsname()->release.

If you want to condition the code based on kernel version in compile time, you can use a preprocessor block such as:

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
...
#else
...
#endif

It allows you to compare against major/minor versions.

like image 61
Dan Aloni Avatar answered Nov 15 '22 04:11

Dan Aloni


You can only safely build a module for any one kernel version at a time. This means that asking from a module at runtime is redundant.

You can find this out at build time, by looking at the value of UTS_RELEASE in recent kernels this is in <generated/utsrelease.h> amongst other ways of doing this.

like image 22
Flexo Avatar answered Nov 15 '22 03:11

Flexo