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

来源:互联网 发布:流体仿真分析软件 编辑:程序博客网 时间:2024/05/29 08:32
  • 源码
  • 编译
  • 使模块的有效
  • 加入内核
  • -
[root@localhost helloworld]# pwd/root/helloworld[root@localhost helloworld]# lshello.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.oelse    KERNELDIR=/lib/modules/$(shell uname -r)/build    PWD :=$(shell pwd)modules:    $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendifclean:    rm -rf *.o [root@localhost helloworld]# ----------[root@localhost helloworld]# lshello.c  Makefile[root@localhost helloworld]# makemake -C /lib/modules/3.10.0-123.el7.x86_64/build M=/root/helloworld modulesmake[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.komake[1]: 离开目录“/usr/src/kernels/3.10.0-123.el7.x86_64”[root@localhost helloworld]# lshello.c   hello.mod.c  hello.o   modules.orderhello.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.kosrcversion:     3A617DEB3E8A78C4AEEE6A6depends:        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 helloMessage from syslogd@localhost at Dec 12 09:35:31 ... kernel:hello world!//##查看模块[root@localhost helloworld]# lsmod | grep hellohello                  12425  0 //##卸载模块[root@localhost helloworld]# modprobe -r helloMessage 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/(shellunamer)/buildLinuxmodulesC(KERNELDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。

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

参考【1】

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

参考【3】 [关于模块驱动]:《linux设备驱动》最经典

0 0