How to find the version of a compiled kernel modul

2019-06-16 10:58发布

问题:

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?

回答1:

$ 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:


回答2:

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.

version

version is set by the MODULE_VERSION macro. The file does not exist if MODULE_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#L400

To enable it, either:

  • set MODULE_VERSION for the module
  • compile with CONFIG_MODULE_SRCVERSION_ALL. srcversion then gets generated for all modules, including those without MODULE_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:

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?