编写、编译、安装、移除一个内核模块的基本过程,不能再简单了的
1.gcc、make、kernel-devel的安装
??? 可以执行rpm查看它们是否已经安装,如下:
[root@localhost ~]# rpm -q gcc
gcc-4.1.0-3
[root@localhost ~]# rpm -q make
make-3.80-10.2
[root@localhost ~]# rpm -q kernel-devel
kernel-devel-2.6.15-1.2054_FC5
[root@localhost ~]#
??? 如上,如果显示版本号,就说明安装了。如果是下面的显示就没有安装:
package kernel-devel is not installed
??? 安装也非常简单,以kernel-devel为例(gcc,make也一样的):
??? 先看一下内核版本号:
[root@localhost ~]# uname -r
2.6.15-1.2054_FC5
[root@localhost ~]#
??? 用yum安装,因yum要下载文件,要保持网络连通:
[root@localhost ~]# yum -install kernel-devel*
(注意:*代表内核版本号,也就是上面用uname -r看到,我这为2.6.15-1.2054_FC5)
???
??? 之后有询问y/N的就都选y就可以了。
///////////////////////////////////////////////////////////////
2.编写一个c文件,以下有个我找的例子可以使用。
??? 将它保存为.c文件就可以了(假设我们用hello.c)。
#include <linux/init.h>
#include <linux/sched.h>??? /*为了引用current而加入的头文件*/
#include <linux/module.h>
MODULE_LICENSE("GPL");?? /*这行用于告诉内核该模块拥有free license,在2.6中这是必须的*/
/*执行真正的初始化工作*/
static int hello_init(void) {
?? unsigned int cr3;
?? __asm__ ("movl %%cr3, %0":"=a"(cr3));
?? printk(KERN_ALERT "Hello, world\n");
?? printk(KERN_ALERT "The process is "%s" (pid %i)\n", current->comm, current->pid);
?? printk(KERN_ALERT "The cr3 register is "0x%08X"\n", cr3);
?? return 0;
}
/*执行真正的析构工作*/
static void hello_exit(void) {
?? printk(KERN_ALERT "Goodbye, cruel world\n");
}
/*该函数注册模块的构造函数*/
module_init(hello_init);
/*该函数注册模块的析构函数*/
module_exit(hello_exit);
///////////////////////////////////////////////////////////////
3.编写Makefile文件
??? Makefile文件内容如下:
??????? TARGET := hello
??????? obj-m := $(TARGET).o
??????? KERNELDIR := /lib/modules/$(shell uname -r)/build
??????? PWD:=$(shell pwd)
default:
??????? $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
i:
??????? insmod $(TARGET).ko
r:
??????? rmmod $(TARGET).ko
c:
??????? rm -rf *.o *.mod.c *.ko
???
??? 注意,内容里的缩进是<Tab>而不是几个空格,我这本写的是<Tab>,但浏览器可能把它转换为空格了,
要将它转换回来。之后将它保存为Makefile,要和hell.c在同一个目录下。
///////////////////////////////////////////////////////////////
4.编译
??? 如果你前面没有出错,那么就很简单了,只要在终端下转到hello.c和Makefile所在的目录,运行make就可以了,如下:
[root@localhost ~/shdx/module]# make
make -C /lib/modules/2.6.15-1.2054_FC5/build M=/root/shdx/module modules
make[1]: Entering directory `/usr/src/kernels/2.6.15-1.2054_FC5-i686'
? Building modules, stage 2.
? MODPOST
make[1]: Leaving directory `/usr/src/kernels/2.6.15-1.2054_FC5-i686'
[root@localhost ~/shdx/module]# make
make -C /lib/modules/2.6.15-1.2054_FC5/build M=/root/shdx/module modules
make[1]: Entering directory `/usr/src/kernels/2.6.15-1.2054_FC5-i686'
? Building modules, stage 2.的
? MODPOST
make[1]: Leaving directory `/usr/src/kernels/2.6.15-1.2054_FC5-i686'
[root@localhost ~/shdx/module]#
??? 类似上面的信息说明编译成功。若有类似“没有规则可以创建目标”或“No rule to make target”的错误,
一般是你的Makefile没有写正确,如果确实没有写错。那就要据提示的错误具体解决了。还有一点要注意:确定目录
/lib/modules/2.6.15-1.2054_FC5(版本号可能不同,下同) 下有一个叫build的文件,是一个符号连接,指向/usr/src/kernels
/2.6.15-1.2054_FC5-i686 如果没有,就创建一个,方法如下:
[root@localhost /lib/modules/2.6.15-1.2054_FC5]# ln -s /usr/src/kernels/2.6.15-1.2054_FC5-i686 build
[root@localhost /lib/modules/2.6.15-1.2054_FC5]#
??? 编译成功后,可以看到有hello.ko的文件。
///////////////////////////////////////////////////////////////
5.安装与移除
??? 可以用insmod hello.ko或make i来安装,其实make i也是调用insmod的,在Makefile中有定义。
移除类似安装如下:
[root@localhost ~/shdx/module]# insmod hello.ko
[root@localhost ~/shdx/module]# rmmod hello.ko
[root@localhost ~/shdx/module]# make i
insmod hello.ko
[root@localhost ~/shdx/module]# make r
rmmod hello.ko
[root@localhost ~/shdx/module]#
??? 安装之后,你可以看到printk()输出的信息,但不在终端下,到一个/var/log/messages文件的最后。
如果你看到了你输出的信息,那么就成功了。