1 hurryliu_hello.c的编写
[liuyong@centos6 hello]$ cat hurryliu_hello.c
#include<linux/init.h>
#include<linux/module.h>
static int hello_init(void)
{
printk(KERN_INFO "my first module ,hello world \n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO "hello world exit \n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("hurryliu<>");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("My first module ");
MODULE_ALIAS("A sample module");
2 编写makefile
[liuyong@centos6 hello]$ cat Makefile
obj-m += hurryliu_hello.o
modules:
@make -C /home/liuyong/embededproject/platform/s3c24x0/kernel/linux-3.0 M=`pwd` modules
@make clear
clear:
rm *.order *.symvers *.mod.c *.o
clean:
rm *.ko
当我们有这两个文件之后,我们可以执行make命令,直接生成我们需要的hurryliu_hello.ko文件。
注意:在linux中,xxx.ko文件是内核模块文件,即内核加载的某个模块,一般为驱动程序。
[liuyong@centos6 hello]$
[liuyong@centos6 hello]$ ls
hurryliu_hello.c Makefile
[liuyong@centos6 hello]$
[liuyong@centos6 hello]$ make
make[1]: Entering directory `/home/liuyong/embededproject/platform/s3c24x0/kernel/linux-3.0'
CC [M] /home/liuyong/mysource/hello/hurryliu_hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/liuyong/mysource/hello/hurryliu_hello.mod.o
LD [M] /home/liuyong/mysource/hello/hurryliu_hello.ko
make[1]: Leaving directory `/home/liuyong/embededproject/platform/s3c24x0/kernel/linux-3.0'
make[1]: Entering directory `/home/liuyong/mysource/hello'
rm *.order *.symvers *.mod.c *.o
make[1]: Leaving directory `/home/liuyong/mysource/hello'
[liuyong@centos6 hello]$ ls
hurryliu_hello.c hurryliu_hello.ko Makefile
这样,我们的hurryliu_hello.ko模块就生成了。现在,只需把这个文件放到TFTP服务器上,通过
TFTP服务器下载到自己的开发板上,运行就行。(使用TFTP这个不累述)
使用insmod +模块文件名 加载模块到内核
使用rmmod +模块文件名 卸载模块
注意:在卸载内核模块的时候,我们应该知道,在linux 3.0版本以前(如2.6版本),卸载时都需要加上模块的后缀名的,(如上面的模块,卸载的时候需要加.KO),之后,卸载的时候都不能加模块的后缀名。
这样,这个简单的hello 模块就写好了。(虽然看起来很简单,但是对于我这种水平的来说,还是很吃力的,花了很长的时间才搞明白是怎么回事。)
关于这个模块的讲解,以及内核配置和编译,在以下的文章中会一一道来。。。。。。