linux动态模块的编译及安装

来源:互联网 发布:美工培训班学费多少 编辑:程序博客网 时间:2024/05/29 09:34

编译动态的模块加载


为主机编译模块

要编译的模块文件==》hello.c文件:「摘自linux kernel development」

/* * hello.c - The Hello, World! Kernel Module */#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>/* * hello-init - the init function, called when the module is loaded. * Return zero if successfully loaded, nonzero otherwise. */ static int hello_init(void) { printk(KERN_ALERT "I bear a charmed life.\n"); return 0; }  /*  * hello_exit - the exit function, called when the module is removed.  */   static void hello_exit (void) {  printk(KERN_ALERT "Out, out, brief candle!\n"); }    module_init(hello_init);  module_exit(hello_exit);    MODULE_LICENSE("GPL");  MODULE_AUTHOR("Shakespeare");  MODULE_DESCRIPTION("A Hello, world Module");

Makefile文件:「摘自linux device driver」

# If KERNELRELEASE is defined, we've been invoked from the# kernel build system and can use its language.ifneq ($(KERNELRELEASE),)     obj-m := hello.o# Otherwise we were called directly from the command# line; invoke the kernel build system.elseKERNELDIR ?= /lib/modules/$(shell uname -r)/build  #第一中找内核路径方法#KERNELDIR ?= /usr/src/"linux-headers-$(shell uname -r)" #第二种找内核路径的方法,实质也是前一种方法的链接PWD := $(shell pwd)default:    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules      #此处巧妙的运用了 “-C” 实现了二次调用Makefile,注意make前要有”tab“按键modules_install:    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean endif
调试命令:
 1238  make  1239  sudo insmod hello.ko  #加载模块 1240  dmesg |grep charm     #查看模块输出信息 1241  sudo rmmod hello.ko   #卸载模块 1243  dmesg | tail -n 3     #查看卸载模块信息
查看模块信息:
wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ modinfo hello.kofilename:       /home/wiwa/sourcecodes/kernel/drivers/char/fishing/hello.kodescription:    A Hello, world Moduleauthor:         Shakespearelicense:        GPLsrcversion:     1AE41B6A73B1ED6874F5359depends:        vermagic:       3.13.0-74-generic SMP mod_unload modversions 

进阶模块安装:

wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ sudo make modules_install  #模块安装wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ sudo depmod                #更新模块依赖表wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ cat /lib/modules/$(uname -r)/modules.dep | tail -n 3 #查看模块依赖表                                                                                                          #多了hello.ko模块kernel/lib/percpu_test.ko:extra/hello.ko:extra/fishing.ko:wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ ls -l /lib/modules/$(uname -r)/extra  #查看 多了hell.ko文件total 8-rw-r--r-- 1 root root 3816 1月   9 18:06 fishing.ko-rw-r--r-- 1 root root 3812 1月  10 16:21 hello.kowiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ sudo modprobe hello        #用modprobe加载hello模块wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ sudo modprobe -r hello     #移除hello模块 

交叉模块编译:

在嵌入式中,宿主机可以为目标板编译模块

编译交叉模块修改Makefile文件为:「摘自linux device driver」

ARCH=armCROSS_COMPILE=arm-linux-gnueabihf-INSTALL_MOD_PATH=/media/wiwa/sys   #此处为目标板的系统文件根目录# If KERNELRELEASE is defined, we've been invoked from the# kernel build system and can use its language.ifneq ($(KERNELRELEASE),)        obj-m := hello.o# Otherwise we were called directly from the command# line; invoke the kernel build system.elseKERNELDIR ?= ~/sourcecodes/kernel # ~/sourcecodes/kernel 为目标板linux内核文件夹位置PWD := $(shell pwd)default:    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules      #此处巧妙的运用了 “-C” 实现了二次调用Makefile,注意make前要有”tab“按键modules_install:    #$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:    $(MAKE) -C $(KERNELDIR) M=$(PWD) cleanendif  
 
 

切换到模块所在目录下,执行以下操作:

wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ make ## fishing为需要编译的模块文件放置的目录,此处依旧编译上述的“Hello.c”文件
在完成上-步后,可以将编译好的模hello.ko块,拷贝到目标板上执行insmod或rmmod指令。




0 0
原创粉丝点击