linux动态模块的编译及安装

编译动态的模块加载


为主机编译模块

要编译的模块文件==》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.
else
KERNELDIR ?= /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_install
clean:
    $(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.ko
filename:       /home/wiwa/sourcecodes/kernel/drivers/char/fishing/hello.ko
description:    A Hello, world Module
author:         Shakespeare
license:        GPL
srcversion:     1AE41B6A73B1ED6874F5359
depends:        
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.ko
wiwa@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=arm
CROSS_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.
else
KERNELDIR ?= ~/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_install
clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
endif  


 
   
 

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

wiwa@tech:~/sourcecodes/kernel/drivers/char/fishing$ make 
## fishing为需要编译的模块文件放置的目录,此处依旧编译上述的“Hello.c”文件

在完成上-步后,可以将编译好的模hello.ko块,拷贝到目标板上执行insmod或rmmod指令。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值