Compiling Kernel   «Prev  Next»

Lesson 3 Working with kernel modules
ObjectiveList available kernel modules.

Working with Linux Kernel Modules and compiling the Linux Kernel

In Red Hat Linux, the available kernel modules can be listed by accessing the appropriate system directories and using certain commands. Here is a step-by-step process for listing the available kernel modules.
  1. Use the lsmod Command: The lsmod command provides an immediate listing of all loaded kernel modules. To execute it, open the terminal and type:
    lsmod
    

    The output will display three columns:
    1. Module: The name of the kernel module.
    2. Size: The size of the module (in kilobytes).
    3. Used by: The number of instances of the module currently in use and the dependent modules.
    Please note that this command only lists the modules currently loaded by your system.
  2. Use the modprobe Command: The modprobe command with the -l or --list option can be used to display all available modules. However, this has been deprecated in recent versions of modprobe. If your system's modprobe command supports it, use:
    modprobe --list
    

    This command lists all the kernel modules available in the directories specified in
    /etc/modprobe.d/.
    
  3. Directly Browse the /lib/modules/ Directory:
    All available kernel modules are generally stored in the
    /lib/modules/ 
    
    directory. Each subdirectory corresponds to a different kernel version. The kernel version currently in use can be determined with the uname command:
    uname -r
    

    To list all available kernel modules for the currently active kernel, use:
    ls /lib/modules/$(uname -r)
    

    Replace "$(uname -r)" with your specific kernel version if needed. This will list all the directories containing the available kernel modules. The .ko files in these directories represent the individual modules.

Remember that manipulating kernel modules can have significant system-wide effects, so always use these commands with care. Additionally, root or sudo permissions may be needed for some of these operations.
You can load modules, which are simply object files[1] into your modular kernel at any time. You might need to load a module when, for example, you add new hardware or want to support a new kind of filesystem.
Linux stores its modules in /lib/modules/, under a directory named for the running kernel's version. This arrangement allows your system to support modules for multiple kernel versions. To determine the running kernel's version, use
uname -r 	or 
cat /proc/version.
 

Within each version directory there are various subdirectories that separate modules according to their type. For example, all network modules are in the net directory and all SCSI modules are in the scsi directory.

Listing currently loaded modules

The lsmod command lists the modules currently resident in the kernel.
More information on lsmod is discussed below.
# lsmod
pwc              43392 1
videodev         5120 2 [pwc]
usbcore          59072 1 [audio pwc usb-uhci]

Working with kernel modules
The next lesson discusses how to load kernel modules.
The Linux kernel is modular, which means it can extend its capabilities through the use of dynamically-loaded kernel modules. A kernel module can provide:
  1. a device driver which adds support for new hardware; or,
  2. support for a file system such as btrfs or NFS.
Like the kernel itself, modules can take parameters that customize their behavior, though the default parameters work well in most cases.
User-space tools can
  1. list the modules currently loaded into a running kernel,
  2. query all available modules for available parameters and module-specific information, and
  3. load or unload (remove) modules dynamically into or from a running kernel.
Many of these utilities, which are provided by the module-init-tools package, take module dependencies into account when performing operations so that manual dependency-tracking is rarely necessary.
On modern systems, kernel modules are automatically loaded by various mechanisms when the conditions call for it. However, there are occasions when it is necessary to load and/or unload modules manually, such as when a module provides optional functionality, one module should be preferred over another although either could provide basic functionality, or when a module is misbehaving, among other situations.

[1]Object files: Compiled source code (such as C or C++).