Chapter 7. Kernel Modules

Chapter 7. Kernel Modules

The linux kernel make an extensive use of modules to control several things from peripherals, network, or inner computer hardware.

OBJECTIVES

  • List the advantanges of kernel modules
  • Use insmod rmmod modprobe to load and unload kernel modules
  • Know how to use modinfo to find information about kernel modules

ADVANTAGES OF KERNEL MODULES

Pack different functionalities of the kernel in modules offers flexibility to the operative system, as the usable modules can be plugged in or out as needed. For example lets consider a wearable device which is based on linux, certainly such device won't use all the modules that a fully personal computer may use.

Note: the linux kernel uses a monolithic kernel architecture.

MODULE UTILITIES 

Command Description
lsmod List loaded modules
insmod Directly load modules
rmmod Directly remove modules
modprobe Load or unload modules, using a pre-built module database with dependency information
depmod Rebuild the module dependency database; needed by modprobe and modinfo
modinfo Display information about a module

 

MODULE LOADING AND UNLOADING

Module's location is often at this directory

/lib/modules/<kernel-version>

​Also all kernel modules are files with this extension

.ko

​And they are kernel version specific, therefore they must be compatible with the running kernel otherwise they won't load 

insmod

This commad is used to load kernel modules

$ sudo /sbin/insmod <pathto>/module_name.ko

lsmod

This command list the loaded modules

$ lsmod

rmmod

This command is used to remove kernel modules

$ rmmod module_name

modprobe

There is another way to load or unload modules, such way is accomplished with modprobe command

To load a module

$ sudo /sbin/modprobe module_name

To unload a module 

​$ sudo /sbin/modprobe -r module_name

Note: For this command to work the modules must be loaded at

/lib/modules/$(uname -r)

​SOME CONSIDERATIONS

  • It is impossible to unload a module if it is being used by other modules
  • It is impossible to unload a module that is being used by a process
  • When a module is loaded through modprobe, the command will load all the necessary modules required for the initial module to work
  • When a module is being unloaded through modprobe the system will unload all the dependency-modules if such dependency module is not being used elsewhere

MODULE INFORMATION

In order to get module information we must use the modinfo command

$ /sbin/modinfo my_module
$ /sbin/modinfo <path_to_module>/my_module.ko

There is also another way to get information about the module and even to modify its parameters, it will be using the /sys folder, for example

/sys/module/e1000/parameters

MODULE PARAMETERS

Many modules can be loaded while specifying parameter values, such as in:

$ sudo /sbin/insmod <pathto>/e1000e.ko debug=2 copybreak=256

Or for a module already in the proper system location it is easier with:

$ sudo /sbin/modprobe e1000e debug=2 copybreak=256

MODULE CONFIGURATION

Module configurations can be controlled in the files located at

/etc/modprobe.d/my_module.conf

LAB

  • List all currently loaded kernel modules on your system.
$ /sbin/lsmod
  • Load a currently unloaded module on your system.
$ /sbin/modprobe zaurus
  • Re-list all loaded kernel modules and see if your module was indeed loaded.
$ /sbin/lsmod
  • Remove the loaded module from your system.
$ /sbin/modprobe -r zaurus
  • Re-list again and see if your module was properly removed.
$ /sbin/lsmod