2011-11-06 233 views

回答

16

按照惯例,Linux内核模块加载机制不允许加载未针对正在运行的内核编译的模块,因此您所指的“正在运行的内核”很可能已在内核模块编译时已知。

为了检索版本字符串常量,旧版本要求您包含<linux/version.h>,其他<linux/utsrelease.h>和更新的<generated/utsrelease.h>。如果您真的想在运行时获得更多信息,那么函数linux/utsname.h是最标准的运行时接口。

执行虚拟/proc/version procfs节点使用utsname()->release

如果你想调节基于在编译时内核版本的代码,你可以使用一个预处理器块,如:

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16) 
... 
#else 
... 
#endif 

它可以让你比较兑主要/次要版本。

+0

中的utsname()函数的伎俩。谢谢。 – Bogi

3

您一次只能安全地为任何一个内核版本构建模块。这意味着在运行时从模块询问是多余的。

通过查看最近的内核中的UTS_RELEASE的值,您可以在构建时找到此值,其中包括<generated/utsrelease.h>以及其他这样做的方式。

0

为什么我不能为任何版本构建内核模块?

由于内核模块API的设计不稳定,如内核树中所述:Documentation/stable_api_nonsense.txt。摘要如下:

Executive Summary 
----------------- 
You think you want a stable kernel interface, but you really do not, and 
you don't even know it. What you want is a stable running driver, and 
you get that only if your driver is in the main kernel tree. You also 
get lots of other good benefits if your driver is in the main kernel 
tree, all of which has made Linux into such a strong, stable, and mature 
operating system which is the reason you are using it in the first 
place. 

参见:How to build a Linux kernel module so that it is compatible with all kernel releases?

如何做到这一点在编译时被要求在:Is there a macro definition to check the Linux kernel version?