12

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?

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78

2 Answers2

10
$ apropos modinfo
modinfo              (8)  - display information about a kernel module
$ modinfo cpuid.ko
filename:       cpuid.ko
author:         H. Peter Anvin <hpa@zytor.com>
description:    x86 generic CPUID driver
license:        GPL
vermagic:       2.6.37 SMP preempt mod_unload PENTIUM4 gcc-4.4.5
depends:
Sdaz MacSkibbons
  • 27,668
  • 7
  • 32
  • 34
  • 2
    While this answer solved my problem, I believe that the vermagic value reflects the kernel version that the module was compiled against, not the version of the module. To test this, I tried "modprobe nvidia" because nvidia is not included with the kernel, and so must have a different version number. "modprobe nvidia" returned the kernel version for the vermagic value. – Jacob Marble Jan 29 '11 at 22:56
  • 1
    Well, judging by reskimming [LDD3](http://lwn.net/Kernel/LDD3/), that is the only *standard* version information included. The driver author is free to include another version stamp, but the kernel doesn't care, if it's not in the info structure. If they do include it as a string, you may have some luck with `strings foo.ko`, but if they construct it from integers with `kprintf` (look for `%d.%d.%d` or something along those lines), you might be out of luck. P.S. [superuser.com](superuser.com) may be a better place to post this, and you may get other answers if you post there. – Sdaz MacSkibbons Jan 30 '11 at 03:46
4

Runtime method

insmod /module_version.ko

cat /sys/modules/module_version/version
# => 1.0

cat /sys/module/module_version/srcversion
# => AB0F06618BC3A36B687CDC5

modinfo /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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985