[英]How to find the version of a compiled kernel module?
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.我的情况是,通过查询加载的模块或 .ko 文件来查找加载的内核模块的版本会非常方便。
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.在内核 4.9.6 上使用此设置进行了测试。
/sys/modules/module_version/version
version
is set by the MODULE_VERSION
macro. version
由MODULE_VERSION
宏设置。
The file does not exist if the MODULE_VERSION
macro is not used in the module.如果模块中未使用
MODULE_VERSION
宏,则该文件不存在。
/sys/module/module_version/srcversion
srcversion
is an MD4 hash of the source code used to compile the kernel module. srcversion
是用于编译内核模块的源代码的MD4哈希值。 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它是在构建时从https://github.com/torvalds/linux/blob/v4.9/scripts/mod/modpost.c#L1978使用https://github.com/torvalds/linux/blob/自动计算的v4.9/scripts/mod/sumversion.c#L400
To enable it, either:要启用它,请执行以下任一操作:
MODULE_VERSION
for the moduleMODULE_VERSION
CONFIG_MODULE_SRCVERSION_ALL
.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?srcversion
,包括那些没有设置MODULE_VERSION
模块: modinfo srcversion:我如何从我的源生成这个? The srcversion
file is only present when if one of the above holds. srcversion
文件仅在srcversion
条件成立时才存在。
You can then check that the built .ko
matches the insmodded one with:然后,您可以检查构建的
.ko
与 insmodded 匹配:
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
从内部模块代码本身与
THIS_MODULE
You can use THIS_MODULE->version
, here is an example: What is the significance of THIS_MODULE in Linux kernel module drivers?您可以使用
THIS_MODULE->version
,这里是一个例子: THIS_MODULE 在 Linux 内核模块驱动程序中的意义是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.