I am in a situation where it would be very convenient to find the version of a loaded kernel module by querying the loaded module or .ko file.
Is there a standard way to do this without digging into the source code?
I am in a situation where it would be very convenient to find the version of a loaded kernel module by querying the loaded module or .ko file.
Is there a standard way to do this without digging into the source code?
Runtime method
Tested with this setup on kernel 4.9.6.
version
version
is set by theMODULE_VERSION
macro. The file does not exist ifMODULE_VERSION
is not given.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#L400To enable it, either:
MODULE_VERSION
for the moduleCONFIG_MODULE_SRCVERSION_ALL
.srcversion
then gets generated for all modules, including those withoutMODULE_VERSION
set: modinfo srcversion: How do I generate this from my source?The
srcversion
is only present when is given.You can then check that the built
.ko
matches the insmodded one with: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?