I am in a situation where it wo开发者_如何学Gould 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?
$ 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:
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:
- set
MODULE_VERSIONfor the module - compile with
CONFIG_MODULE_SRCVERSION_ALL.srcversionthen gets generated for all modules, including those withoutMODULE_VERSIONset: modinfo srcversion: How do I generate this from my source?
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?
加载中,请稍侯......
精彩评论