Chapter 24. Kernel Modules

The Linux kernel has a modular design. At boot time, only a minimal resident kernel is loaded into memory. Thereafter, whenever a user requests a feature that is not present in the resident kernel, a kernel module is dynamically loaded into memory. After a specified period of inactivity, the module may be removed from memory.

The mechanism that supports dynamic loading of modules is a kernel thread called kmod. Modules are not loaded unless they are needed. When the kernel requests a module, the module is loaded along with all its module dependencies.

Red Hat Linux also includes a cron task that removes all unused modules every ten minutes. The cron task is located in the file /etc/cron.d/kmod. Refer to the Section called Cron in Chapter 22 for more information on cron tasks.

When you install Red Hat Linux, the hardware on your system is probed and you provide information about how the system will be typically used and which programs should be loaded. Based on this probing and the information you provide, the installation program decides which modules need to be loaded at boot time. The installation program sets up the dynamic loading mechanism to work transparently. If you build your own custom kernel, you can make all of these decisions for yourself.

If you add new hardware after installation and the hardware requires a kernel module, you need to set up the dynamic loading mechanism. Kudzu usually detects new hardware. You can also add the new driver by editing the module configuration file, /etc/modules.conf.

For example, if your system included a model SMC EtherPower 10 PCI network adapter at the time of installation, the module configuration file will contain the following line:

alias eth0 tulip

After installation, if you install a second identical network adapter to your system, add the following line to /etc/modules.conf:

alias eth1 tulip

See the Official Red Hat Linux Reference Guide for an alphabetical list of kernel modules and the hardware supported by the modules.

Kernel Module Utilities

You can also use a group of commands to list, load, or unload kernel modules. These commands are useful if you want to try different modules or see if a module has been loaded successfully.

The command /sbin/lsmod displays a list of currently loaded modules.

Example 24-1. Example lsmod output

Module                  Size  Used by
sr_mod                 15264   0 (autoclean)
mga                    95984   1
agpgart                23392   3
nfs                    79008   1 (autoclean)
lockd                  52464   1 (autoclean) [nfs]
sunrpc                 61328   1 (autoclean) [nfs lockd]
autofs                 11264   4 (autoclean)
3c59x                  25344   1 (autoclean)
ipchains               38976   0 (unused)
ide-scsi                8352   0
scsi_mod               95104   2 [sr_mod ide-scsi]
ide-cd                 26848   0
cdrom                  27232   0 [sr_mod ide-cd]
usb-uhci               20720   0 (unused)
usbcore                49664   1 [usb-uhci]

As you can see in Example 24-1, lsmod displays the size, use count, and referring modules for each module currently loaded.

To load a kernel module, you can use the command /sbin/insmod followed by the kernel module name. By default, insmod tries to load the module from the /lib/modules/<kernel-version>/kernel/drivers subdirectories. There is a subdirectory for each type of module, such as the net subdirectory for network interface drivers. Some kernel modules have module dependencies — other modules must be loaded first for it to load. To resolve these dependencies, you can either load the module dependencies and then load the module you want, or you can use the command /sbin/modprobe followed by the module name to load the module along with its dependencies.

For example, the command

/sbin/modprobe tulip

loads the tulip network interface module.

To unload kernel modules, use the command /sbin/rmmod followed by the module name. The rmmod utility will only unload modules that are not in use and that are not a dependency of other modules in use.

For example, the command

/sbin/rmmod tulip

unloads the tulip network interface module.

Another useful kernel module utility is modinfo. You can use the command /sbin/modinfo to display information about a kernel module. The general syntax is:

/sbin/modinfo [options] <module>

Options include -d that displays a brief description of the module and -p that lists the parameters the module supports. For a complete list of options, refer to the modinfo man page (man modinfo).