内核模块常用操作命令

1. lsmod 列出当前内核中的所有模块

root@debian:/home/zcx/drivers# lsmod
Module                  Size  Used by
hellomodule            16384  0
g_multi                16384  0
galcore               372736  0
root@debian:/home/zcx/drivers#

2. insmod 将一个模块加载到内核当中,需要使用root权限

zcx@debian:~/drivers$ insmod hellomodule.ko
insmod: ERROR: could not insert module hellomodule.ko: Operation not permitted
zcx@debian:~/drivers$ su
Password:
root@debian:/home/zcx/drivers# insmod hellomodule.ko
[ 2052.653411] hellomodule: module license 'GPL2' taints kernel.
[ 2052.657949] Disabling lock debugging due to kernel taint
[ 2052.663549] [ KERN_EMERG ]  Hello  Module Init

Message from syslog[ 2052.669703] [ default ]  Hello  Module Init
d@debian at Jun  4 15:56:05 ...
 kernel:[ 2052.663549] [ KERN_EMERG ]  Hello  Module Init

3. modprobe 与insmod类似,除此以外modprobe还能检查模块之间的依赖关系, 并且按照顺序加载这些依赖,可以理解为按照顺序多次执行insmod。modprobe之前需要先用depmod -a建立模块之间的依赖关系。

4. depend modprobe是怎么知道一个给定模块所依赖的其他的模块呢?在这个过程中,depend起到了决定性作用,当执行modprobe时, 它会在模块的安装目录下搜索module.dep文件,这是depmod创建的模块依赖关系的文件。

5. rmmod rmmod命令卸载某个内存模块时,内存模块会自动执行*_exit()函数,进行清理操作。rmmod不会卸载一个模块所依赖的模块,需要依次卸载,当然是用modprobe -r 可以一键卸载

6. modinfo modinfo用来显示我们在内核模块中定义的几个宏

root@debian:/home/zcx/drivers# modinfo hellomodule.ko
filename:       /home/zcx/drivers/hellomodule.ko
alias:          test_module
description:    hello world module
author:         embedfire
license:        GPL2
depends:
name:           hellomodule
vermagic:       4.19.94 SMP preempt mod_unload modversions ARMv7 p2v8

7. 如何让内核模块开机自动加载呢?
首先将自动加载的模块统一放在/lib/modules/内核版本目录下,内核版本使用uname -r查询
其次使用depmod -a建立模块之间的依赖关系
在/etc/modules中将自己的模块名写进modules文件中即可

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

hellomodule.ko

8. 内核模块加载时如何像main函数那样传参进去呢,在驱动中先定义变量,使用module_param对参数进行说明,如果其他驱动会去使用该参数,那么将该参数通过EXPORT_SYMBOL的方式导出。在使用insmod安装驱动时,后面像main函数传参那样,跟上参数值即可

static int itype=0;
module_param(itype,int,0);

EXPORT_SYMBOL(itype);


root@debian:/home/zcx/drivers# insmod parametermodule itype=123
insmod: ERROR: could not load module parametermodule: No such file or directory
root@debian:/home/zcx/drivers# insmod parametermodule.ko itype=123
[ 1459.518866] param init!
[ 1459.519856] itype=123
[ 1459.524492] btype=0
[ 1459.525139] ctype=0
[ 1459.527228] stype=(null)
root@debian:/home/zcx/drivers# insmod calculation.ko
[ 1472.638593] calculation  init!
[ 1472.640196] itype+1 = 124, itype-1 = 122
root@debian:/home/zcx/drivers#

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值