How to find the version of a compiled kernel module? How to find the version of a compiled kernel module? linux linux

How to find the version of a compiled kernel module?


$ apropos modinfomodinfo              (8)  - display information about a kernel module$ modinfo cpuid.kofilename:       cpuid.koauthor:         H. Peter Anvin <hpa@zytor.com>description:    x86 generic CPUID driverlicense:        GPLvermagic:       2.6.37 SMP preempt mod_unload PENTIUM4 gcc-4.4.5depends:


Runtime method

insmod /module_version.kocat /sys/modules/module_version/version# => 1.0cat /sys/module/module_version/srcversion# => AB0F06618BC3A36B687CDC5modinfo /module_version.ko | grep -E '^(src|)version'# => version:        1.0# => srcversion:     AB0F06618BC3A36B687CDC5

Tested with this setup on kernel 4.9.6.

/sys/modules/module_version/version

version is set by the MODULE_VERSION macro.

The file does not exist if the MODULE_VERSION macro is not used in the module.

/sys/module/module_version/srcversion

srcversion is an MD4 hash of the source code used to compile the kernel module. It is calculated automatically at build time from https://github.com/torvalds/linux/blob/v4.9/scripts/mod/modpost.c#L1978 using https://github.com/torvalds/linux/blob/v4.9/scripts/mod/sumversion.c#L400

To enable it, either:

The srcversion file is only present when if one of the above holds.

You can then check that the built .ko matches the insmodded one with:

modinfo mymod.ko

This is a very useful sanity check when you are developing your own kernel modules and copying modules between machines.

From inside module code itself with THIS_MODULE

You can use THIS_MODULE->version, here is an example: What is the significance of THIS_MODULE in Linux kernel module drivers?