如何列出/加载/卸载Linux内核模块

时间:2020-03-05 15:26:16  来源:igfitidea点击:

Linux是单片内核。
单片内核是内核体系结构,其中在内核空间中运行的过程管理,文件系统管理,I/O和设备管理的所有服务,例如:内核负责处理这些服务。
因此,在启动时,这些将与内核加载到内存中。
通过这种方法,将新组件添加到内核中变得困难和复杂,因为内核必须用新组件重新编译,然后加载到内存中。
但是,Linux(以及其他一些单片内核)支持的可加载的内核模块(LKMS)可以动态地从内核加载和删除。

内核图像需要尽可能光。
此外,并非所有驱动程序都可以内置在内核中。
Linux中的模块可以是一些第三方硬件驱动程序,某些文件系统的驱动程序,或者某些其他组件(例如,Linux中有iptables的模块)。
虽然内核守护程序为自动加载并卸载这些模块,但如果我们愿意,可以自己执行此操作,或者如果有一个模块,内核无法自行加载,则必须手动加载(并卸载)不再需要)。

Linux中的模块

模块称为Linux中的内核对象。
它们存在于系统中作为.ko文件。
它们位于名为/lib/modules下的内核版本号的目录的子目录中。

可以使用uname命令检查内核版本。

$uname -r
2.6.38-13-generic

在上面的示例中,模块在我的机器上名为'2.6.13-generic'的目录下存在。

$ls -l /lib/modules/2.6.38-13-generic/
total 4228
lrwxrwxrwx 1 root root 40 2012-02-13 02:13 build -> /usr/src/linux-headers-2.6.38-13-generic
drwxr-xr-x 2 root root 4096 2012-02-13 02:12 initrd
drwxr-xr-x 11 root root 4096 2012-02-13 02:12 kernel
-rw-r--r-- 1 root root 679119 2012-02-13 02:14 modules.alias
-rw-r--r-- 1 root root 656768 2012-02-13 02:14 modules.alias.bin
-rw-r--r-- 1 root root 6097 2012-01-25 00:39 modules.builtin
-rw-r--r-- 1 root root 7438 2012-02-13 02:14 modules.builtin.bin
-rw-r--r-- 1 root root 69 2012-02-13 02:14 modules.ccwmap
-rw-r--r-- 1 root root 309067 2012-02-13 02:14 modules.dep
-rw-r--r-- 1 root root 458980 2012-02-13 02:14 modules.dep.bin
-rw-r--r-- 1 root root 186 2012-02-13 02:14 modules.devname
-rw-r--r-- 1 root root 665 2012-02-13 02:14 modules.ieee1394map
-rw-r--r-- 1 root root 218 2012-02-13 02:14 modules.inputmap
-rw-r--r-- 1 root root 21509 2012-02-13 02:14 modules.isapnpmap
-rw-r--r-- 1 root root 610 2012-02-13 02:14 modules.ofmap
-rw-r--r-- 1 root root 126391 2012-01-25 00:39 modules.order
-rw-r--r-- 1 root root 440266 2012-02-13 02:14 modules.pcimap
-rw-r--r-- 1 root root 1723 2012-02-13 02:14 modules.seriomap
-rw-r--r-- 1 root root 131 2012-02-13 02:14 modules.softdep
-rw-r--r-- 1 root root 245040 2012-02-13 02:14 modules.symbols
-rw-r--r-- 1 root root 313493 2012-02-13 02:14 modules.symbols.bin
-rw-r--r-- 1 root root 1001662 2012-02-13 02:14 modules.usbmap
drwxr-xr-x 3 root root 4096 2012-02-13 02:14 updates

要查看.ko文件,我们需要在子目录中查看。
例如,

$ls -l /lib/modules/2.6.38-13-generic/kernel/arch/x86/kernel/
total 88
-rw-r--r-- 1 root root 44184 2012-01-25 00:39 apm.ko
drwxr-xr-x 4 root root 4096 2012-02-13 02:12 cpu
-rw-r--r-- 1 root root 7308 2012-01-25 00:39 cpuid.ko
-rw-r--r-- 1 root root 20640 2012-01-25 00:39 microcode.ko
-rw-r--r-- 1 root root 7908 2012-01-25 00:39 msr.ko

列出已加载的模块

可以通过"lsmod"命令查看加载到内核中的模块。
它将列出当前加载的所有模块。

# lsmod
Module Size Used by
ppp_deflate 12838 0
zlib_deflate 26594 1 ppp_deflate
bsd_comp 12777 0
ppp_async 17308 1
crc_ccitt 12595 1 ppp_async
binfmt_misc 13213 1
parport_pc 32111 0
ppdev 12849 0
snd_hda_codec_idt 60537 1
snd_hda_intel 24113 2
snd_hda_codec 90901 2 snd_hda_codec_idt,snd_hda_intel
snd_hwdep 13274 1 snd_hda_codec
snd_pcm 80042 2 snd_hda_intel,snd_hda_codec
snd_seq_midi 13132 0
i915 451053 3
option 21045 2
lib80211_crypt_tkip 17203 0
snd_rawmidi 25269 1 snd_seq_midi
snd_seq_midi_event 14475 1 snd_seq_midi
snd_seq 51291 2 snd_seq_midi,snd_seq_midi_event
wl 2642531 0
joydev 17322 0
snd_timer 28659 2 snd_pcm,snd_seq
snd_seq_device 14110 3 snd_seq_midi,snd_rawmidi,snd_seq
usb_wwan 19711 1 option
drm_kms_helper 40971 1 i915
snd 55295 13 snd_hda_codec_idt,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_rawmidi,snd_seq,snd_timer,snd_seq_device
drm 184164 4 i915,drm_kms_helper
i2c_algo_bit 13184 1 i915
dell_laptop 13515 0
dcdbas 14054 1 dell_laptop
dell_wmi 12601 0
sparse_keymap 13666 1 dell_wmi
usbserial 37116 7 option,usb_wwan
uvcvideo 66851 0
soundcore 12600 1 snd
videodev 75143 1 uvcvideo
snd_page_alloc 14073 2 snd_hda_intel,snd_pcm
lib80211 14570 2 lib80211_crypt_tkip,wl
video 18951 1 i915
psmouse 73312 0
serio_raw 12990 0
lp 13349 0
parport 36746 3 parport_pc,ppdev,lp
usbhid 41704 0
hid 77084 1 usbhid
r8169 42534 0

加载新模块

可以使用命令'modprobe'或者'insmod'加载我们要加载到内核的任何模块。
但是,'modprobe'命令更强大,所以我们可能想要使用'modprobe'而不是'insmod'。
此外,如果我们希望加载的模块存在任何依赖性,则'modprobe'会负责它。
因此,为了演示的目的,我们将使用'modprobe'。

iptables(ip_tables)的模块尚未加载到内存中。
这可以在上面的输出中检查,或者我们可以使用grep搜索它。

# lsmod | grep table
#

此命令没有返回。
因此,我们将使用"modprobe"加载相应的模块,如:

# modprobe ip_tables
# lsmod | grep table
ip_tables 18125 0
x_tables 21907 1 ip_tables

其中我们希望加载'ip_tables'模块,并且'x_tables'是它由'modprobe'处理的依赖性。

卸载模块

当我们不再需要模块时,可以使用命令'rmmod'或者'modprobe -r'来删除它。
再一次,'modprobe'也会卸载任何依赖项。
请注意,除非任何其他模块未使用,否则不会卸载模块。
如果我们尝试在'ip_tables'之前删除"x_tables"模块,我们会收到错误,因为'x_tables'正在使用'ip_tables'。

# modprobe -r x_tables
FATAL: Module x_tables is in use.
root@igi-Inspiron-1440:~# lsmod | grep table
ip_tables 18125 0
x_tables 21907 1 ip_tables
#

现在,如果我们使用"modprobe"删除IP_Tables模块,它也将删除"X_Tables"。

# modprobe -r ip_tables
# lsmod | grep table

获取模块信息

命令'modInfo'显示有关模块的信息:

# modinfo ip_tables
filename: /lib/modules/2.6.38-13-generic/kernel/net/ipv4/netfilter/ip_tables.ko
description: IPv4 packet filter
author: Netfilter Core Team <[email protected]>
license: GPL
srcversion: 71A366B1B82EB2884B5A127
depends: x_tables
vermagic: 2.6.38-13-generic SMP mod_unload modversions 686