在运行时从 Linux 内核模块获取内核版本

发布于 2024-12-13 21:12:03 字数 50 浏览 0 评论 0原文

如何从 Linux 内核模块代码(内核模式)中获取有关正在运行的内核版本的运行时信息?

how can I obtain runtime information about which version of kernel is running from inside linux kernel module code (kernel mode)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-12-20 21:12:03

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

为了检索版本字符串常量,旧版本要求您包含 ,其他版本要求 ,较新版本则 <代码><生成/utsrelease.h>。如果您确实想在运行时获取更多信息,那么 linux/utsname.h 中的 utsname() 函数是最标准的运行时接口。

虚拟/proc/version procfs节点的实现使用utsname()->release

如果您想在编译时根据内核版本调整代码,您可以使用预处理器块,例如:

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

它允许您与主要/次要版本进行比较。

By convention, Linux kernel module loading mechanism doesn't allow loading modules that were not compiled against the running kernel, so the "running kernel" you are referring to is most likely is already known at kernel module compilation time.

For retrieving the version string constant, older versions require you to include <linux/version.h>, others <linux/utsrelease.h>, and newer ones <generated/utsrelease.h>. If you really want to get more information at run-time, then utsname() function from linux/utsname.h is the most standard run-time interface.

The implementation of the virtual /proc/version procfs node uses utsname()->release.

If you want to condition the code based on kernel version in compile time, you can use a preprocessor block such as:

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

It allows you to compare against major/minor versions.

£冰雨忧蓝° 2024-12-20 21:12:03

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

您可以在构建时通过查看最近内核中 UTS_RELEASE 的值来找到这一点,该值位于 以及其他执行此操作的方法中。

You can only safely build a module for any one kernel version at a time. This means that asking from a module at runtime is redundant.

You can find this out at build time, by looking at the value of UTS_RELEASE in recent kernels this is in <generated/utsrelease.h> amongst other ways of doing this.

小梨窩很甜 2024-12-20 21:12:03

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

因为内核模块 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.

另请参阅:如何构建Linux 内核模块,以便它与所有内核版本兼容?

在编译时询问如何做到这一点:是否有宏定义来检查Linux内核版本?

Why can't I build a kernel module for any version?

Because the kernel module API is unstable by design as explained in the kernel tree at: Documentation/stable_api_nonsense.txt. The summary reads:

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.

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

How to do it at compile time was asked at: Is there a macro definition to check the Linux kernel version?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文