Examples To List / Install / Remove / Configure Linux Kernel Module

Linux is a monolithic kernel. A monolithic kernel is a kernel architecture in which all the services of process management, filesystem management, I/O and device management run in the kernel space, i.e. the kernel is responsible for handling these services. So these are loaded into memory with the kernel, at the time of booting up. With this approach, adding new components into the kernel becomes difficult and complex, because the kernel would have to be recompiled with new components and then loaded into the memory. However, the Loadable Kernel Modules (LKMs) supported by Linux (and some other monolithic kernels), can load and remove from kernel dynamically.

The kernel image needs to be as light as possible. Moreover, not all the drivers can be built into the kernel. The modules in linux can be some third party hardware drivers, drivers for some filesystems, or some other components (for example, there is a module for iptables in linux). Although the kernel daemon loads and unloads these modules for you automatically, but if you want, you can do it yourself, or if there is a module that kernel is not able to load by itself, it must be loaded manually (and removed when it is no longer needed).

Modules in Linux

The modules are called Kernel objects in Linux. They are present in the system as .ko files. They are located in the subdirectories of a directory named as your Kernel version number under /lib/modules. The Kernel version can be checked with 'uname -r' command:

[email protected]:~$ uname -r
2.6.38-13-generic

In the example above, the modules are present under the directory named '2.6.13-generic' on my machine.

[email protected]:~$ 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

To see the .ko files, we will need to look in the subdirectories. For example,

[email protected]:~$ 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

List loaded Modules

The modules that are loaded into the kernel can be viewed by "lsmod" command. It will list all modules loaded currently.

[email protected]:~# 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

Loading a new module

Any module that you wish to load into kernel can be loaded using the command ‘modprobe’ or ‘insmod’. However, ‘modprobe’ command is more powerful, so you might want to use ‘modprobe’ instead of ‘insmod’. Moreover, if there is any dependency in the module that we wish to load, ‘modprobe’ takes care of it. So for the purpose of demonstration, we will use ‘modprobe’.

The module for iptables (ip_tables) has not been loaded into memory yet. This can be checked in the output above, or we can search for it using grep.

[email protected]:~# lsmod | grep table
[email protected]:~#

This command returns nothing. So we will load the corresponding module using ‘modprobe’ as:

[email protected]:~# modprobe ip_tables
[email protected]:~# lsmod | grep table
ip_tables 18125 0
x_tables 21907 1 ip_tables

Here, we wished to load ‘ip_tables’ module and ‘x_tables’ was its dependency that was taken care of by ‘modprobe’.

Removing a module

When we no longer need the module, it can be removed using the command 'rmmod' or 'modprobe -r'. Once again, ‘modprobe’ will unload any dependencies as well. Note that a module will not be unloaded unless it is not in use by any other module. If we try to remove ‘x_tables’ module before ‘ip_tables’, we get an error, because ‘x_tables’ is in use by ‘ip_tables’.

[email protected]:~# modprobe -r x_tables
FATAL: Module x_tables is in use.
[email protected]:~# lsmod | grep table
ip_tables 18125 0
x_tables 21907 1 ip_tables
[email protected]:~#

Now if we remove ip_tables module with ‘modprobe’, it will remove ‘x_tables’ as well.

[email protected]:~# modprobe -r ip_tables
[email protected]:~# lsmod | grep table
[email protected]:~#

Getting information about a module

The command 'modinfo' displays information about a module:

[email protected]:~# 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

Bobbin Zachariah 12:21 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.