您的位置:首页 > 运维架构 > Linux

linux内核驱动模块-再回首helloworld

2015-12-12 09:18 477 查看
源码

编译

使模块的有效

加入内核

-

[root@localhost helloworld]# pwd
/root/helloworld
[root@localhost helloworld]# ls
hello.c  Makefile
[root@localhost helloworld]# cat hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int __init hello_init(void)
{
printk(KERN_EMERG "hello world!\n");
return 0;
}

static void __exit hello_exit(void)
{
printk(KERN_EMERG "hello world, see you!\n");
}

module_init(hello_init);
module_exit(hello_exit);

----------

[root@localhost helloworld]# cat Makefile

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KERNELDIR=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)

modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

clean:
rm -rf *.o
[root@localhost helloworld]#

----------

[root@localhost helloworld]# ls
hello.c  Makefile
[root@localhost helloworld]# make
make -C /lib/modules/3.10.0-123.el7.x86_64/build M=/root/helloworld modules
make[1]: 进入目录“/usr/src/kernels/3.10.0-123.el7.x86_64”
CC [M]  /root/helloworld/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC      /root/helloworld/hello.mod.o
LD [M]  /root/helloworld/hello.ko
make[1]: 离开目录“/usr/src/kernels/3.10.0-123.el7.x86_64”
[root@localhost helloworld]# ls
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers
[root@localhost helloworld]#

----------
[root@localhost helloworld]# mv hello.ko  /lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/md/
mv:是否覆盖"/lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/md/hello.ko"? y
[root@localhost helloworld]# depmod  #重新收集模块信息

//##查看模块信息
[root@localhost helloworld]# modinfo hello
filename:       /lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/md/hello.ko
srcversion:     3A617DEB3E8A78C4AEEE6A6
depends:
vermagic:       3.10.0-123.el7.x86_64 SMP mod_unload modversions
[root@localhost helloworld]#

//##插入模块,不建议使用insmod/rmmod
[root@localhost helloworld]# lsmod | grep hello
[root@localhost helloworld]# modprobe hello
Message from syslogd@localhost at Dec 12 09:35:31 ...
kernel:hello world!

//##查看模块
[root@localhost helloworld]# lsmod | grep hello
hello                  12425  0

//##卸载模块
[root@localhost helloworld]# modprobe -r hello
Message from syslogd@localhost at Dec 12 09:35:36 ...
kernel:hello world, see you!
[root@localhost helloworld]# lsmod | grep hello
[root@localhost helloworld]#

【注】:linux内核打印优先级,数字越小,优先级越高;
#defineKERN_EMERG"<0>"
#defineKERN_ALERT"<1>"
#defineKERN_CRIT"<2>"
#defineKERN_ERR"<3>"
#defineKERN_WARNING"<4>"
#defineKERN_NOTICE"<5>"
#defineKERN_INFO"<6>"
#defineKERN_DEBUG"<7>"


分析上述Makefile文件,KERNELRELEASE时内核源代码顶层所定义的一个变量,当Makefile第一次执行时,这个变量并没有定义,因此直接执行else中的内容,uname-r命令输出当前系统所使用的版本号,/lib/modules/(shelluname−r)/build这个就是Linux源码所在的目录,当执行到modules时,−C(shell uname -r)/build这个就是Linux源码所在的目录,当执行到modules时,-C (KERNELDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。

当从内核源码目录返回时,KERNELRELEASE已被被定义,kbuild也被启动去解析kbuild语法的语句,make将继续读取else之前的内容。else之前的内容为kbuild语法的语句, 指明模块源码中各文件的依赖关系,以及要生成的目标模块名

参考【1】

参考【2】 [内核文档]:/Documentation/kbuild 。 关于内核的东西。最原始,最权威的内容都在这里。

参考【3】 [关于模块驱动]:《linux设备驱动》最经典
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: