lsmod rmmod modprobe modinfo

lsmod, rmmod, modprobe, and modinfo commands

 

What is Linux Kernel?

The kernel is just a piece of code. Ok, not a bit of code 1000’s of lines. For example, Linux kernel has 20 million lines of code. A kernel is a central part of computer operating system which acts as a mediator between system hardware and applications installed in the system. The Kernel converts program’s instructions to machine understandable language and passes them through device drivers.

What are types of Kernel?

We have two types of Kernels are there.

  1. Monolithic kernel
  2. Modular kernel

Before going into difference of kernels we should know what is a kernel module is

What is a Kernel module?

A kernel module is a part of the kernel which can do some work. Say for example Linux Kernel will not support all types of hardware, So programmers will write some kernel modules and distribute them along with their software. Whenever you want to make this hardware to work, we have to load this piece of software into the kernel so that your system understands it. This software is nothing but Kernel module. The modules also referred to as device drivers are specialized pieces of software which power or control access to a peripheral. They are loaded into or unloaded from the running kernel without the need of a reboot.

What is a Monolithic kernel?

A monolithic kernel is where the entire operating system works in kernel space. There is no such space called User space where you can load/remove kernel modules from userspace. In other words, entire device drivers, Filesystem, and IPC will work in kernel space.

What is a Modular kernel?

A modular kernel is where only a particular part of OS is loaded into kernel space. In this way, we will reduce the burden on the system as we can load kernel modules into kernel where and whenever it is required. And the services will run complete isolation from Kernel space.

The /proc/devices file contains a mapping of the device major numbers and device types broadly classified as character and block devices. The kernel gains access to these devices through the corresponding module provided that it has been loaded correctly. Given below is a snippet of the content contained in this file.

[root@linuxnix ~]# head /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc

The number on the left is the primary number of the module which is used internally by the kernel to identify the peripheral type and the names to the right of the number represent the device type.

The /proc/modules file contains a list of currently loaded modules. Here’s a snippet from the file:

[root@linuxnix ~]# head /proc/modules
ebtable_filter 12827 0 - Live 0xffffffffa0657000
ebtables 35009 1 ebtable_filter, Live 0xffffffffa0649000
ip6table_filter 12815 0 - Live 0xffffffffa048a000
ip6_tables 26901 1 ip6table_filter, Live 0xffffffffa0641000
iptable_filter 12810 0 - Live 0xffffffffa048f000
vmw_vsock_vmci_transport 30577 0 - Live 0xffffffffa0481000
vsock 34855 3 vmw_vsock_vmci_transport, Live 0xffffffffa0463000
snd_seq_midi 13565 0 - Live 0xffffffffa05f0000
snd_seq_midi_event 14899 1 snd_seq_midi, Live 0xffffffffa063c000
intel_powerclamp 14419 0 - Live 0xffffffffa0631000

The name in the leftmost/first column in the file represents the module name.

We could go to /sys/module/<module_name> directory to view the files that hold the settings for the driver.

Example 1: To view the corresponding files for the SCSI disk driver represented by sd_mod module, I could head to the /sys/module/sd_mod directory.

[root@linuxnix ~]# cd /sys/module/sd_mod
[root@linuxnix sd_mod]# ls
coresize  holders  initsize  initstate  notes  refcnt  rhelversion  sections  srcversion  taint  uevent

How to see module loaded into the kernel?

Example 2: If we open the initstate and rhelversion files they show that the module is loaded indicated by “live” and the OS for which it’s been loaded is 7.3

[root@linuxnix sd_mod]# cat initstate
live
[root@linuxnix sd_mod]#
[root@linuxnix sd_mod]# cat rhelversion
7.3

To manage kernel modules, we use the lsmod and modprobe commands.

We can add specific options to the modules to customize external access. These options can be configured in the /etc/modprobe.d directory.

Example 3: We use the lsmod command to list currently loaded drivers. This command actually obtains its data from the /proc/modules file.

Output(Clipped):

Last login: Tue Oct 24 21:24:04 2017 from 49.180.168.95
root@linuxnix:~# lsmod
Module                  Size  Used by
ufs                    73728  0
msdos                  20480  0
xfs                   970752  0
ppdev                  20480  0
kvm_intel             172032  0
kvm                   544768  1 kvm_intel
irqbypass              16384  1 kvm
crct10dif_pclmul       16384  0
crc32_pclmul           16384  0
ghash_clmulni_intel    16384  0
joydev                 20480  0
input_leds             16384  0
serio_raw              16384  0
parport_pc             32768  0
pvpanic                16384  0
parport                49152  2 ppdev,parport_pc
ib_iser                49152  0
rdma_cm                49152  1 ib_iser


Example 4:
To filter out for a specific module we can use AWK command as shown below.

[root@linuxnix ~]# lsmod | awk ‘NR==1 {print} /^sd_mod/ {print}’

Module                  Size  Used by

sd_mod                 46322  3

The size column indicates the amount of memory used by the module in bytes and the used by field indicates the number of modules that are dependent on this module is loaded.

How to load Kernel modules in Linux?

Now I’ll demonstrate how we could load and unload modules using modprobe. For this demonstration, I’ll be using the sr_mod module which is used to access the cd drive.

[root@linuxnix ~]# lsmod | awk 'NR==1 {print} /^sr_mod/ {print}'
Module                  Size  Used by
sr_mod                 22416  0

I won’t be working with the sd_mod module here because whatever happens we don’t unload sd_mod and probably lose access to the disks attached to the system.

Example 5: We use modprobe with the -r option to unload a module. We can add the -v option for verbose output.

[root@linuxnix ~]# modprobe -rv sr_mod 
rmmod sr_modrmmod 
rmmod cdrom

Now if I search for this module in lsmod output or /proc/modules I won’t find it because we’ve just unloaded it.

[root@linuxnix ~]# lsmod | awk 'NR==1 {print} /^sr_mod/ {print}'
Module                  Size  Used by
[root@linuxnix ~]# grep sr_mod /proc/modules

At this point, we’ve lost access to our cdrom peripheral.

Example 6: To load the module type modporbe followed by the module name. You may add the -v flag for verbose output.

[root@linuxnix ~]# modprobe -v sr_mod
insmod /lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/cdrom/cdrom.ko
insmod /lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/scsi/sr_mod.ko
[root@linuxnix ~]#

With that, the module has been loaded, and we have regained access to the systems’ cdrom.

[root@linuxnix ~]# lsmod | awk 'NR==1 {print} /^sr_mod/ {print}'
Module                  Size  Used by
sr_mod                 22416  0
[root@linuxnix ~]# grep sr_mod /proc/modules
sr_mod 22416 0 - Live 0xffffffffa0209000
cdrom 42556 1 sr_mod, Live 0xffffffffa065c000
[root@linuxnix ~]#

You may have observed from the above Example 5 demonstrated modprobe command is calling rmmod to unload a module and insmod to load a module. You may use these commands directly as well.

Example 7: We can use the modinfo command to obtain some descriptive information about the module. Given below is the output obtained by running the modinfo command for the sd_mod module.

[root@linuxnix ~]# modinfo sd_mod
filename:       /lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/scsi/sd_mod.ko
alias:          scsi:t-0x0e*
alias:          scsi:t-0x07*
alias:          scsi:t-0x00*
alias:          block-major-135-*
alias:          block-major-134-*
alias:          block-major-133-*
alias:          block-major-132-*
alias:          block-major-131-*
alias:          block-major-130-*
alias:          block-major-129-*
alias:          block-major-128-*
alias:          block-major-71-*
alias:          block-major-70-*
alias:          block-major-69-*
alias:          block-major-68-*
alias:          block-major-67-*
alias:          block-major-66-*
alias:          block-major-65-*
alias:          block-major-8-*
License:        GPL
description:    SCSI disk (sd) driver
author:         Eric Youngdale
rhelversion:    7.3
srcversion:     3BCC1FE5C7B281E00A15BEF
depends:        crc-t10dif
intree:         Y
vermagic:       3.10.0-514.el7.x86_64 SMP mod_unload modversions
signer:         CentOS Linux kernel signing key
sig_key:        D4:88:63:A7:C1:6F:CC:27:41:23:E6:29:8F:74:F0:57:AF:19:FC:54
sig_hashalgo:   sha256

Example 8: Here is another example of using modinfo this time using the dm_mod module which is the device mapper module.

[root@linuxnix ~]# modinfo dm_mod
filename:       /lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/md/dm-mod.ko
license:        GPL
author:         Joe Thornber <dm-devel@redhat.com>
description:    device-mapper driver
alias:          devname:mapper/control
alias:          char-major-10-236
rhelversion:    7.3
srcversion:     F452D86551786DA41676AC1
depends:
intree:         Y
vermagic:       3.10.0-514.el7.x86_64 SMP mod_unload modversions
signer:         CentOS Linux kernel signing key
sig_key:        D4:88:63:A7:C1:6F:CC:27:41:23:E6:29:8F:74:F0:57:AF:19:FC:54
sig_hashalgo:   sha256
parm:           reserved_rq_based_ios:Reserved IOs in request-based mempools (uint)
parm:           use_blk_mq:Use block multiqueue for request-based DM devices (bool)
parm:           dm_mq_nr_hw_queues:Number of hardware queues for request-based dm-mq devices (uint)
parm:           dm_mq_queue_depth:Queue depth for request-based dm-mq devices (uint)
parm:           stats_current_allocated_bytes:Memory currently used by statistics (ulong)
parm:           major:The major number of the device mapper (uint)
parm:           reserved_bio_based_ios:Reserved IOs in bio-based mempools (uint)
parm:           dm_numa_node:NUMA node for DM device memory allocations (int)

The fields that are present in the above output are actually the parameters which can be tweaked while loading the modules.

This concludes our exploration of kernel modules in Linux. I hope this has been an interesting read for you.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.