如何构build一个Linux内核模块,以便与所有内核版本兼容?

如何构build一个Linux内核模块,以便与所有内核版本兼容?,第1张

概述如何构build一个Linux内核模块,以便与所有内核版本兼容

我想构build一个与所有内核版本兼容的内核模块。 例如,如果我在内核3.2.0-29上构build一个内核模块并尝试将其加载到3.2.0-86上,则会出现以下错误:

modprobe my_driver

致命错误:插入my_driver时出错(/lib/modules/3.2.0-86-generic/kernel/fs/my_drv/my_drv.ko):模块格式无效

[在日志消息中:my_drv:不同意符号module_layout的版本]

从jenkins背景启动一个shell脚本的清洁方法

JVM信号链接SIGPIPE

如何在拖动窗口时覆盖默认的linux Alt +鼠标行为?

如何输出到当前可见的terminal

我可以在linux进程的地址空间中写保护每一页吗?

我如何在3.2.0-29上构build一个适合所有3.2.0版本的内核模块。

accept()返回“0”零(stdin fd)在C编程编程

JVM进程如何分配内存?

打开和写入/ dev / null的特殊处理?

在Ubuntu 16中缺lessPHP_soap.dll

R 3.0.2上的install.packages(“devtools”)在Ubuntu 14.04中失败

简而言之 :你几乎不可能编写有用的内核模块,它可以加载到相对较宽版本的内核中。

当你使用CONfig_MODVERSIONS编译的内核构建模块时(就像你的情况一样),对于从内核导出的每个符号,这个符号的CRC存储在模块的文件中。 CRC是某种控制总和 ,其中考虑了用于功能参数的类型布局等。 例如,如果假设的struct A布局在两个内核中不同,那么这些内核中的函数f(struct A *a) CRC也不同。

当一个模块被加载到正在运行的内核中时,模块中的所有功能的CRC与内核的CRC相比较。 如果不同,内核拒绝加载模块。 要了解更多关于这个机制的信息,请参阅内核的文档( documentation / kbuild / modules.txt )。

因此,为了使一个模块可以加载到两个不同的内核中,只能使用两个内核中参数具有相同布局的函数来限制。 特别是,如果类型struct module布局不同,那么两个内核都不能加载单个模块。

有几种方法来提供适合于几个内核的驱动程序。 最简单的方法是提供驱动程序的来源 ,并将其添加到dkms 。 这样一来,如果运行的内核没有构建驱动程序,驱动程序将自动使用其源代码进行编译。

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

但是一个重要的说明是:

The kernel to userspace interface is the one that application programs use,the syscall interface. That interface is **very** stable over time,and will not break. I have old programs that were built on a pre 0.9something kernel that still work just fine on the latest 2.6 kernel release. That interface is the one that users and application programmers can count on being stable.

但是不要担心! 我们友好的linux开发人员在同一文档中解释了这个解决方案:

What to do ---------- So,if you have a linux kernel driver that is not in the main kernel tree,what are you,a developer,supposed to do? Releasing a binary driver for every different kernel version for every distribution is a nightmare,and trying to keep up with an ever changing kernel interface is also a rough job. Simple,get your kernel driver into the main kernel tree (remember we are talking about GPL released drivers here,if your code doesn't fall under this category,good luck,you are on your own here,you leech <insert link to leech comment from Andrew and linus here>.) If your driver is in the tree,and a kernel interface changes,it will be fixed up by the person who dID the kernel change in the first place. This ensures that your driver is always buildable,and works over time,with very little effort on your part.

认真的,尝试使用#if liNUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)如上所述是否有宏定义来检查linux内核版本?

添加一个环境变量到Makefile即。 'K_VERSION'。

通过将版本作为环境变量传递来针对已安装的内核构建模块

例如。

make K_VERSION=`uname -r`

针对当前运行的内核进行构建。

一旦建成,可以使用modprobe工具。

总结

以上是内存溢出为你收集整理的如何构build一个Linux内核模块,以便与所有内核版本兼容?全部内容,希望文章能够帮你解决如何构build一个Linux内核模块,以便与所有内核版本兼容?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1278880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存