http://kernel.lupaworld.com/newbie/lkmpg/#AEN150
=============================================
.
|– Makefile
|– hello
| |– Makefile
| -- hello-3.c<br />
– winnt_mod
|– Makefile
-- winnt_mod.c<br /><br />./Makefile:<br /> # Don't set $(PWD) to
pwd, will occur error!!!<br /># PWD = '
pwd'<br /><br />SUB_DIRS:= winnt_mod/ hello/<br />MODULE := mod<br />MKFILES := $(addprefix $(PWD)/, $(addsuffix Makefile, $(SUB_DIRS)))<br /><br />obj-m := $(MODULE).o<br />include $(MKFILES)<br /><br />all:<br /> make -C /lib/modules/
uname -r/build M=
pwdmodules <br /><br />clean:<br /> make -C /lib/modules/
uname -r/build M=
pwdclean<br /> <br />winnt_mod/Makefile:<br /> WINNT_MOD_OBJS += winnt_mod.o<br /> <br /> $(MODULE)-objs += $(addprefix winnt_mod/, $(WINNT_MOD_OBJS))<br /> <br />hello/Makefile:<br /> HELLO_OBJS += hello-3.o<br /> <br /> $(MODULE)-objs += $(addprefix hello/, $(HELLO_OBJS))<br /> <br /> <br />winnt_mod/winnt_mod.c:<br /> #include <linux/module.h><br /> #include <linux/kernel.h><br /> #include <linux/init.h><br /> #include <linux/kthread.h><br /> #include <linux/workqueue.h><br /> <br /> MODULE_LICENSE("GPL");<br /> MODULE_AUTHOR("jfo");<br /> <br /> static struct workqueue_struct *WINNT_wq;<br /> static struct task_struct *WINNT_task = NULL;<br /> <br /> static void func(void *msg)<br /> {<br /> printk("jfo-kernel: %s", (char*)msg);<br /> }<br /> <br /> static int WINNT_proxy(void *parm)<br /> {<br /> DECLARE_WORK(work, func, "queued work func called.n");<br /> <br /> parm = parm;<br /> printk("jfo-kernel: WINNT_proxy is running.n");<br /> <br /> queue_delayed_work(WINNT_wq, &work, HZ * 3);<br /> <br /> <br /> while(!kthread_should_stop()) {<br /> set_current_state(TASK_INTERRUPTIBLE);<br /> schedule();<br /> }<br /> <br /> printk("jfo-kernel: WINNT_proxy returned.n");<br /> return 0;<br /> }<br /> <br /> static int __init WINNT_init(void)<br /> {<br /> struct task_struct *p;<br /> <br /> /* create WINNT_wq and WINNT workqueue thread */<br /> WINNT_wq = create_singlethread_workqueue("WINNT_workqueue");<br /> BUG_ON(!WINNT_wq);<br /> <br /> /* create WINNT_proxy thread */<br /> p = kthread_create(WINNT_proxy, NULL, "WINNT_proxy");<br /> if(IS_ERR(p)) {<br /> printk("jfo-kernel: WINNT_proxy created failed.n");<br /> return -1;<br /> }<br /> WINNT_task = p;<br /> wake_up_process(p);<br /> <br /> printk(KERN_INFO "jfo-kernel: WINNT module loaded.n");<br /> return 0;<br /> }<br /> <br /> static void __exit WINNT_exit(void)<br /> {<br /> if(WINNT_task)<br /> kthread_stop(WINNT_task);<br /> // cancel_rearming_delayed_workqueue(WINNT_wq, work);<br /> destroy_workqueue(WINNT_wq);<br /> <br /> printk(KERN_INFO "jfo-kernel: WINNT module unloaded.n");<br /> }<br /> <br /> module_init(WINNT_init);<br /> module_exit(WINNT_exit);<br /> <br /> <br /><br />=============================================<br /><br /><br />Chapter 2. Hello World<a name="AEN150">2.1. Hello, World (part 1): 最简单的内核模块</a><p>当第一个洞穴程序员在第一台洞穴计算机的墙上上凿写第一个程序时, 这是一个在羚羊皮上输出
Hello, world’的字符串。罗马的编程书籍上是以 Salut, Mundi'这样的程序开始的。 我不明白人们为什么要破坏这个传统, 但我认为还是不明白为好。我们将从编写一系列的
Hello, world’模块开始, 一步步展示编写内核模块的基础的方方面面。
这可能是一个最简单的模块了。先别急着编译它。我们将在下章模块编译的章节介绍相关内容。
Example 2-1. hello-1.c
/hello-1.c - The simplest kernel module.
/
#include <linux/module.h> / Needed by all modules /
#include <linux/kernel.h> / Needed for KERN_ALERT /
int init_module(void)
{
printk(KERN_INFO "Hello world 1.n");
/
A non 0 return means init_module failed; module can’t be loaded.
/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.n");
}
一个内核模块应该至少包含两个函数。一个“开始”(初始化)的函数被称为init_module()
还有一个“结束” (干一些收尾清理的工作)的函数被称为cleanup_module()
,当内核模块被rmmod卸载时被执行。实际上,从内核版本2.3.13开始这种情况有些改变。 你可以为你的开始和结束函数起任意的名字。 你将在以后学习如何实现这一点Section 2.3。 实际上,这个新方法时推荐的实现方法。但是,许多人仍然使init_module()
和 cleanup_module()
作为他们的开始和结束函数。
一般,init_module()
要么向内核注册它可以处理的事物,要么用自己的代码 替代某个内核函数(代码通常这样做然后再去调用原先的函数代码)。函数 cleanup_module()
应该撤消任何init_module()
做的事,从而 内核模块可以被安全的卸载。
最后,任一个内核模块需要包含linux/module.h。 我们仅仅需要包含 linux/kernel.h当需要使用 printk()
记录级别的宏扩展时KERN_ALERT,相关内容将在Section 2.1.1中介绍。
2.1.1. 介绍
printk()
不管你可能怎么想,printk()
并不是设计用来同用户交互的,虽然我们在 hello-1就是出于这样的目的使用它!它实际上是为内核提供日志功能, 记录内核信息或用来给出警告。因此,每个printk()
声明都会带一个优先级,就像你看到的<1>和KERN_ALERT 那样。内核总共定义了八个优先级的宏, 所以你不必使用晦涩的数字代码,并且你可以从文件 linux/kernel.h查看这些宏和它们的意义。如果你 不指明优先级,默认的优先级DEFAULT_MESSAGE_LOGLEVEL将被采用。
阅读一下这些优先级的宏。头文件同时也描述了每个优先级的意义。在实际中, 使用宏而不要使用数字,就像<4>。总是使用宏,就像 KERN_WARNING。
当优先级低于int console_loglevel,信息将直接打印在你的终端上。如果同时 syslogd和klogd都在运行,信息也同时添加在文件 /var/log/messages,而不管是否显示在控制台上与否。我们使用像 KERN_ALERT这样的高优先级,来确保printk()
将信息输出到 控制台而不是只是添加到日志文件中。 当你编写真正的实用的模块时,你应该针对可能遇到的情况使用合 适的优先级。
2.2. 编译内核模块
内核模块在用gcc编译时需要使用特定的参数。另外,一些宏同样需要定义。 这是因为在编译成可执行文件和内核模块时, 内核头文件起的作用是不同的。 以往的内核版本需要我们去在Makefile中手动设置这些设定。尽管这些Makefile是按目录分层次 安排的,但是这其中有许多多余的重复并导致代码树大而难以维护。 幸运的是,一种称为kbuild的新方法被引入,现在外部的可加载内核模块的编译的方法已经同内核编译统一起来。想了解更多的编 译非内核代码树中的模块(就像我们将要编写的)请参考帮助文件linux/Documentation/kbuild/modules.txt。
现在让我们看一个编译名为hello-1.c的模块的简单的Makefile:
Example 2-2. 一个基本内核模块的Makefile
obj-m += hello-1.oall:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
现在你可以通过执行命令 make编译模块。 你应该得到同下面类似的屏幕输出:
hostname:~/lkmpg-examples/02-HelloWorld# makemake -C /lib/modules/2.6.11/build M=/root/lkmpg-examples/02-HelloWorld modules
make[1]: Entering directory
/usr/src/linux-2.6.11'<br /> CC [M] /root/lkmpg-examples/02-HelloWorld/hello-1.o<br /> Building modules, stage 2.<br /> MODPOST<br /> CC /root/lkmpg-examples/02-HelloWorld/hello-1.mod.o<br /> LD [M] /root/lkmpg-examples/02-HelloWorld/hello-1.ko<br />make[1]: Leaving directory
/usr/src/linux-2.6.11’hostname:~/lkmpg-examples/02-HelloWorld#
请注意2.6的内核现在引入一种新的内核模块命名规范:内核模块现在使用.ko的文件后缀(代替 以往的.o后缀),这样内核模块就可以同常规的目标文件区别开。这样做的理由是它们包含一个附加的.modinfo段, 那里存放着关于模块的附加信息。我们将马上看到这些信息的好处。
使用modinfo hello-.ko来看看它是什么样的信息。
hostname:~/lkmpg-examples/02-HelloWorld# modinfo hello-1.kofilename: hello-1.ko
vermagic: 2.6.11 preempt PENTIUMII 4KSTACKS gcc-3.3
depends:
到目前为止,没什么惊人的。一旦我们对后面的一个例子,hello-5.ko,使用modinfo,那将会改变。 hostname:~/lkmpg-examples/02-HelloWorld# modinfo hello-5.ko
filename: hello-5.ko
license: GPL
author: Peter Jay Salzman
vermagic: 2.6.11 preempt PENTIUMII 4KSTACKS gcc-3.3
depends:
parm: myintArray:An array of integers (array of int)
parm: mystring:A character string (charp)
parm: mylong:A long integer (long)
parm: myint:An integer (int)
parm: myshort:A short integer (short)
hostname:~/lkmpg-examples/02-HelloWorld#
这里有很多有用的信息去看。报告错误的作者信息,许可证信息,甚至对它接受参数的简短描述。
更详细的文档请参考 linux/Documentation/kbuild/makefiles.txt。在研究Makefile之前请确认你已经参考了这些文档。它很可能会节省你很多工作。
现在是使用insmod ./hello-1.ko命令加载该模块的时候了(忽略任何你看到的关于内核污染的输出 显示,我们将在以后介绍相关内容)。
所有已经被加载的内核模块都罗列在文件/proc/modules中。cat一下这个文件看一下你的模块是否真的 成为内核的一部分了。如果是,祝贺你!你现在已经是内核模块的作者了。当你的新鲜劲过去后,使用命令 rmmod hello-1.卸载模块。再看一下/var/log/messages文件的内容是否有相关的日志内容。
这儿是另一个练习。看到了在声明 init_module()
上的注释吗? 改变返回值非零,重新编译再加载,发生了什么?
2.3. Hello World (part 2)
在内核Linux 2.4中,你可以为你的模块的“开始”和“结束”函数起任意的名字。它们不再必须使用 init_module()
和cleanup_module()
的名字。这可以通过宏 module_init()
和module_exit()
实现。这些宏在头文件linux/init.h定义。唯一需要注意的地方是函数必须在宏的使用前定义,否则会有编译 错误。下面就是一个例子。
Example 2-3. hello-2.c
/hello-2.c - Demonstrating the module_init() and module_exit() macros.
This is preferred over using init_module() and cleanup_module().
/
#include <linux/module.h> / Needed by all modules /
#include <linux/kernel.h> / Needed for KERN_ALERT /
#include <linux/init.h> / Needed for the macros /
static int init hello_2_init(void)
{
printk(KERN_INFO "Hello, world 2n");
return 0;
}
static void exit hello_2_exit(void)
{
printk(KERN_INFO "Goodbye, world 2n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);
现在我们已经写过两个真正的模块了。添加编译另一个模块的选项十分简单,如下:
Example 2-4. 两个内核模块使用的Makefile
obj-m += hello-1.oobj-m += hello-2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
现在让我们来研究一下linux/drivers/char/Makefile这个实际中的例子。就如同你看到的, 一些被编译进内核 (obj-y),但是这些obj-m哪里去了呢?对于熟悉shell脚本的人这不难理解。这些在Makefile中随处可见 的obj-$(CONFIG_FOO)的指令将会在CONFIG_FOO被设置后扩展为你熟悉的obj-y或obj-m。这其实就是你在使用 make menuconfig编译内核时生成的linux/.config中设置的东西。
2.4. Hello World (part 3): 关于init和exit宏
这里展示了内核2.2以后引入的一个新特性。注意在负责“初始化”和“清理收尾”的函数定义处的变化。宏 init
的使用会在初始化完成后丢弃该函数并收回所占内存,如果该模块被编译进内核,而不是动态加载。
也有一个宏initdata
同init
类似,只不过对变量有效。
宏exit
将忽略“清理收尾”的函数如果该模块被编译进内核。同宏 __exit
一样,对动态加载模块是无效的。这很容易理解。编译进内核的模块 是没有清理收尾工作的, 而动态加载的却需要自己完成这些工作。
这些宏在头文件linux/init.h定义,用来释放内核占用的内存。 当你在启动时看到这样的Freeing unused kernel memory: 236k freed内核输出,上面的 那些正是内核所释放的。
Example 2-5. hello-3.c
/hello-3.c - Illustrating the init, initdata and __exit macros.
/
#include <linux/module.h> / Needed by all modules /
#include <linux/kernel.h> / Needed for KERN_ALERT /
#include <linux/init.h> / Needed for the macros /
static int hello3_data initdata = 3;
static int init hello_3_init(void)
{
printk(KERN_INFO "Hello, world %dn", hello3_data);
return 0;
}
static void exit hello_3_exit(void)
{
printk(KERN_INFO "Goodbye, world 3n");
}
module_init(hello_3_init);
module_exit(hello_3_exit);
2.5. Hello World (part 4): 内核模块许可证和内核模块文档说明
如果你在使用2.4或更新的内核,当你加载你的模块时,你也许注意到了这些输出信息:
# insmod xxxxxx.oWarning: loading xxxxxx.o will taint the kernel: no license
See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Hello, world 3
Module xxxxxx loaded, with warnings
在2.4或更新的内核中,一种识别代码是否在GPL许可下发布的机制被引入, 因此人们可以在使用非公开的源代码产品时得到警告。这通过在下一章展示的宏 MODULE_LICENSE()
当你设置在GPL证书下发布你的代码时, 你可以取消这些警告。这种证书机制在头文件linux/module.h 实现,同时还有一些相关文档信息。 /
The following license idents are currently accepted as indicating free
software modules
"GPL" [GNU Public License v2 or later]
"GPL v2" [GNU Public License v2]
"GPL and additional rights" [GNU Public License v2 rights and more]
"Dual BSD/GPL" [GNU Public License v2
or BSD license choice]
"Dual MPL/GPL" [GNU Public License v2
or Mozilla license choice]
The following other idents are available
"Proprietary" [Non free products]
There are dual licensed components, but when running with Linux it is the
GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
is a GPL combined work.
This exists for several reasons
1. So modinfo can show license info for users wanting to vet their setup
is free
2. So the community can ignore bug reports including proprietary modules
3. So vendors can do likewise based on their own policies
/
类似的,宏MODULE_DESCRIPTION()
用来描述模块的用途。 宏MODULE_AUTHOR()
用来声明模块的作者。宏MODULE_SUPPORTED_DEVICE()
声明模块支持的设备。
这些宏都在头文件linux/module.h定义, 并且内核本身并不使用这些宏。它们只是用来提供识别信息,可用工具程序像objdump查看。 作为一个练习,使用grep从目录linux/drivers看一看这些模块的作者是如何 为他们的模块提供识别信息和档案的。
我推荐在/usr/src/linux-2.6.x/目录下使用类似grep -inr MODULE_AUTHOR 的命令。不熟悉命令行工具的人可能喜欢网上那样的方法, 搜索提供LXR做索引的内核源代码树的网站(或在自己的本地机器上安装它)。
使用像emacs或vi那样传统的Unix编辑器的用户将会发现tag文件很有用。它们能够在/usr/src/linux-2.6.x/ 下用make tags或make TAGS生成。 一旦你在内核目录树中得到了这种tag文件,你就能把鼠标放到某个函数调用上使用一些组合键直接跳 到函数的定义处。
Example 2-6. hello-4.c
/hello-4.c - Demonstrates module documentation.
/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#define DRIVER_AUTHOR "Peter Jay Salzman <p@dirac.org>"
#define DRIVER_DESC "A sample driver"
static int init init_hello_4(void)
{
printk(KERN_INFO "Hello, world 4n");
return 0;
}
static void exit cleanup_hello_4(void)
{
printk(KERN_INFO "Goodbye, world 4n");
}
module_init(init_hello_4);
module_exit(cleanup_hello_4);
/
You can use strings, like this:
/
/
Get rid of taint message by declaring code as GPL.
/
MODULE_LICENSE("GPL");
/
Or with defines, like this:
/
MODULE_AUTHOR(DRIVER_AUTHOR); / Who wrote this module? /
MODULE_DESCRIPTION(DRIVER_DESC); / What does this module do /
/
This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might
be used in the future to help automatic configuration of modules, but is
currently unused other than for documentation purposes.
/
MODULE_SUPPORTED_DEVICE("testdevice");
2.6. 从命令行传递参数给内核模块
模块也可以从命令行获取参数。但不是通过以前你习惯的argc/argv。
要传递参数给模块,首先将获取参数值的变量声明为全局变量。然后使用宏MODULE_PARM()
(在头文件linux/module.h)。运行时,insmod将给变量赋予命令行的参数,如同 ./insmod mymodule.ko myvariable=5。为使代码清晰,变量的声明和宏都应该放在 模块代码的开始部分。以下的代码范例也许将比我公认差劲的解说更好。
宏module_param()
需要三个参数,变量的名字,其类型和在sysfs中关联文件的权限。 整数型既可为通常的signed也可为unsigned。 如果你想使用整数数组或者字符串,请看module_param_array()和module_param_string()。
module_param(myint, int, 0);
数组同样被支持。但是情况和2.4时代有点不一样了。为了追踪参数的个数,你需要传递一个指向数目变量的指针作为第三个参数。 在你自己,你也可以忽略数目并传递NULL。我们把两种可能性都列出来:
int myintarray[2];module_param_array(myintarray, int, NULL, 0); / not interested in count /
int myshortarray[4];
int count;
module_parm_array(myshortarray, short, & count, 0); / put count into "count" variable /
将初始值设为缺省使用的IO端口或IO寻址是一个不错的作法。如果这些变量有缺省值,则可以进行自动设备检测, 否则保持当前设置的值。我们将在后续章节解释清楚相关内容。在这里我只是演示如何向一个模块传递参数。
最后,还有这样一个宏,MODULE_PARM_DESC()
被用来注解该模块可以接收的参数。该宏 两个参数:变量名和一个格式自由的对该变量的描述。
Example 2-7. hello-5.c
/hello-5.c - Demonstrates command line argument passing to a module.
/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char mystring = "blah";
static int myintArray[2] = { -1, -1 };
static int arr_argc = 0;
/
module_param(foo, int, 0000)
The first param is the parameters name
The second param is it’s data type
The final argument is the permissions bits,
for exposing parameters in sysfs (if non-zero) at a later stage.
/
module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "A short integer");
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "An integer");
module_param(mylong, long, S_IRUSR);
MODULE_PARM_DESC(mylong, "A long integer");
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");
/
module_param_array(name, type, num, perm);
The first param is the parameter’s (in this case the array’s) name
The second param is the data type of the elements of the array
The third argument is a pointer to the variable that will store the number
of elements of the array initialized by the user at module loading time
The fourth argument is the permission bits
*/
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");
static int init hello_5_init(void)
{
int i;
printk(KERN_INFO "Hello, world 5n=============n");
printk(KERN_INFO "myshort is a short integer: %hdn", myshort);
printk(KERN_INFO "myint is an integer: %dn", myint);
printk(KERN_INFO "mylong is a long integer: %ldn", mylong);
printk(KERN_INFO "mystring is a string: %sn", mystring);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
{
printk(KERN_INFO "myintArray[%d] = %dn", i, myintArray[i]);
}
printk(KERN_INFO "got %d arguments for myintArray.n", arr_argc);
return 0;
}
static void __exit hello_5_exit(void)
{
printk(KERN_INFO "Goodbye, world 5n");
}
module_init(hello_5_init);
module_exit(hello_5_exit);
我建议用下面的方法实验你的模块:
satan# insmod hello-5.ko mystring="bebop" mybyte=255 myintArray=-1mybyte is an 8 bit integer: 255
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: bebop
myintArray is -1 and 420
satan# rmmod hello-5
Goodbye, world 5
satan# insmod hello-5.ko mystring="supercalifragilisticexpialidocious"
> mybyte=256 myintArray=-1,-1
mybyte is an 8 bit integer: 0
myshort is a short integer: 1
myint is an integer: 20
mylong is a long integer: 9999
mystring is a string: supercalifragilisticexpialidocious
myintArray is -1 and -1
satan# rmmod hello-5
Goodbye, world 5
satan# insmod hello-5.ko mylong=hello
hello-5.o: invalid argument syntax for mylong: ‘h’
2.7. 由多个文件构成的内核模块
有时将模块的源代码分为几个文件是一个明智的选择。
这里是这样的一个模块范例。
Example 2-8. start.c
/start.c - Illustration of multi filed modules
/
#include <linux/kernel.h> / We’re doing kernel work /
#include <linux/module.h> / Specifically, a module /
int init_module(void)
{
printk(KERN_INFO "Hello, world - this is the kernel speakingn");
return 0;
}
另一个文件:
Example 2-9. stop.c
/stop.c - Illustration of multi filed modules
/
#include <linux/kernel.h> / We’re doing kernel work /
#include <linux/module.h> / Specifically, a module /
void cleanup_module()
{
printk(KERN_INFO "Short is the life of a kernel modulen");
}
最后是该模块的Makefile:
Example 2-10. Makefile
obj-m += hello-1.oobj-m += hello-2.o
obj-m += hello-3.o
obj-m += hello-4.o
obj-m += hello-5.o
obj-m += startstop.o
startstop-objs := start.o stop.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
这是目前为止所有例子的完整的Makefile。前五行没有什么特别之处,但是最后一个例子需要两行。 首先,我们为联合的目标文件构造一个名字,其次,我们告诉make什么目标文件是模块的一部分。
2.8. 为已编译的内核编译模块很显然,我们强烈推荐你编译一个新的内核,这样你就可以打开内核中一些有用的排错功能,像强制卸载模块(MODULE_FORCE_UNLOAD): 当该选项被打开时,你可以rmmod -f module强制内核卸载一个模块,即使内核认为这是不安全的。该选项可以为你节省不少开发时间。
但是,你仍然有许多使用一个正在运行中的已编译的内核的理由。例如,你没有编译和安装新内核的权限,或者你不希望重启你的机器来运行新内核。 如果你可以毫无阻碍的编译和使用一个新的内核,你可以跳过剩下的内容,权当是一个脚注。
如果你仅仅是安装了一个新的内核代码树并用它来编译你的模块,当你加载你的模块时,你很可能会得到下面的错误提示:
insmod: error inserting ‘poet_atkm.ko’: -1 Invalid module format一些不那么神秘的信息被纪录在文件/var/log/messages中;
Jun 4 22:07:54 localhost kernel: poet_atkm: version magic ‘2.6.5-1.358custom 686REGPARM 4KSTACKS gcc-3.3’ should be ‘2.6.5-1.358 686 REGPARM 4KSTACKS gcc-3.3’
换句话说,内核拒绝加载你的模块因为记载版本号的字符串不符(更确切的说是版本印戳)。版本印戳作为一个静态的字符串存在于内核模块中,以 vermagic:。 版本信息是在连接阶段从文件init/vermagic.o中获得的。 查看版本印戳和其它在模块中的一些字符信息,可以使用下面的命令 modinfo module.ko:
[root@pcsenonsrv 02-HelloWorld]# modinfo hello-4.kolicense: GPL
author: Peter Jay Salzman <p@dirac.org>
description: A sample driver
vermagic: 2.6.5-1.358 686 REGPARM 4KSTACKS gcc-3.3
depends:
我们可以借助选项–force-vermagic解决该问题,但这种方法有潜在的危险,所以在成熟的模块中也是不可接受的。 解决方法是我们构建一个同我们预先编译好的内核完全相同的编译环境。如何具体实现将是该章后面的内容。
首先,准备同你目前的内核版本完全一致的内核代码树。然后,找到你的当前内核的编译配置文件。通常它可以在路径 /boot下找到,使用像config-2.6.x的文件名。你可以直接将它拷贝到内核代码树的路径下: cp /boot/config-uname -r
/usr/src/linux-uname -r
/.config。
让我们再次注意一下先前的错误信息:仔细看的话你会发现,即使使用完全相同的配置文件,版本印戳还是有细小的差异的,但这足以导致 模块加载的失败。这其中的差异就是在模块中出现却不在内核中出现的custom字符串,是由某些发行版提供的修改过的 makefile导致的。检查/usr/src/linux/Makefile,确保下面这些特定的版本信息同你使用的内核完全一致:
VERSION = 2PATCHLEVEL = 6
SUBLEVEL = 5
EXTRAVERSION = -1.358custom
…
像上面的情况你就需要将EXTRAVERSION一项改为-1.358。我们的建议是将原始的makefile备份在 /lib/modules/2.6.5-1.358/build下。 一个简单的命令cp /lib/modules/uname -r
/build/Makefile /usr/src/linux-uname -r
即可。 另外,如果你已经在运行一个由上面的错误的Makefile编译的内核,你应该重新执行 make,或直接对应/lib/modules/2.6.x/build/include/linux/version.h从文件 /usr/src/linux-2.6.x/include/linux/version.h修改UTS_RELEASE,或用前者覆盖后者的。
现在,请执行make来更新设置和版本相关的头文件,目标文件:
[root@pcsenonsrv linux-2.6.x]# makeCHK include/linux/version.h
UPD include/linux/version.h
SYMLINK include/asm -> include/asm-i386
SPLIT include/linux/autoconf.h -> include/config/*
HOSTCC scripts/basic/fixdep
HOSTCC scripts/basic/split-include
HOSTCC scripts/basic/docproc
HOSTCC scripts/conmakehash
HOSTCC scripts/kallsyms
CC scripts/empty.o
…
如果你不是确实想编译一个内核,你可以在SPLIT后通过按下CTRL-C中止编译过程。因为此时你需要的文件 已经就绪了。现在你可以返回你的模块目录然后编译加载它:此时模块将完全针对你的当前内核编译,加载时也不会由任何错误提示。