编译,加载并卸载一个内核模块

本文详细介绍了如何在Linux内核中启用模块支持,通过make menuconfig配置加载和卸载模块,修改Kconfig和Makefile将驱动设为模块方式,以及编译、加载和卸载内核模块的步骤。关键操作包括启用模块卸载功能,使用adb推送模块到目标系统,并通过insmod和rmmod命令进行加载和卸载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一。 make menuconfig 打开 Enable Enable loadable module support
 --------------------------------------------------------------------------------------------
  +----------------------------- Linux Kernel Configuration ------------------------------+
  |  Arrow keys navigate the menu.  <Enter> selects submenus --->.  Highlighted letters   |
  |  are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes features.  Press  |
  |  <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*] built-in  [ ]         |
  |  excluded  <M> module  < > module capable                                             |
  | +-----------------------------------------------------------------------------------+ |
  | |          General setup  --->                                                      | |
  | |      [*] Enable loadable module support  --->                                     | |
  | |      -*- Enable the block layer  --->                                             | |
  | |          Linux Kernel Study & Practice  --->                                      | |

   同时在下级菜单使能模块卸载,否则卸载的时候会报错 rmmod: delete_module 'hello' failed (errno 38)
  
   |      --- Enable loadable module support                                           | |
  | |      [ ]   Forced module loading                                                  | |
  | |      [*]   Module unloading                                                       | |
  | |      [ ]     Forced module unloading (NEW)                                        | |
  | |      [ ]   Module versioning support                                              | |
  | |      [ ]   Source checksum for all modules 
 
 二。把对应源码的config配置修改为模块方式
  Kconfig文件:
  config HELLO
     tristate "First Android Driver"
     default n
     help
     This is the first android driver.
 
   menuconfig设为M ,模块方式
 +-----------------------------------------------------------------------------------+ |
  | |      --- kernel study start support                                               | |
  | |      <M>   First Android Driver    
 
   Makefile 文件
   obj-$(CONFIG_HELLO) += hello.o


 三 编译模块
  先 make
  再 make modules
   如果最后连接报错,有些系统接口找不到,是应该依赖模块的obj文件还没有生成, 那就先make 一下再make modules
  
  四 加载模块
   使用adb工具 push 到 目标系统
   # adb remount                  /*解决只读问题*/
   # adb push hello.ko /system/   /*推送模块到目标板*/
   # insmod /system/hello.ko      /*加载模块到内核*/
   # lsmod                        /*显示已加载的模块*/ 
   # root@android:/ # lsmod
   # hello 4964 0 - Live 0xbf000000

   
 五 卸载模块
  
    root@android:/ # rmmod hello /*卸载模块*/
 root@android:/
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值