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.


Modules are Object files

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


lsmod program's Output

A group of commands for managing kernel modules is available if the module-init-tools package is installed. Use these commands to determine if a module has been loaded successfully or when trying different modules for a piece of new hardware.
The command
/sbin/lsmod 

displays a list of currently loaded modules. For example
Module Size Used by
nfs 218437 1
lockd 63977 2 nfs
parport_pc 24705 1
lp 12077 0
parport 37129 2 parport_pc,lp
autofs4 23237 2
i2c_dev 11329 0
i2c_core 22081 1 i2c_dev
sunrpc 157093 5 nfs,lockd
button 6481 0
battery 8901 0
ac 4805 0
md5 4033 1
ipv6 232833 16
ohci_hcd 21713 0
e100 39493 0
mii 4673 1 e100
floppy 58481 0
sg 33377 0
dm_snapshot 17029 0
dm_zero 2369 0
dm_mirror 22957 2
ext3 116809 2
jbd 71257 1 ext3
dm_mod 54741 6 dm_snapshot,dm_zero,dm_mirror
ips 46173 2
aic7xxx 148121 0
sd_mod 17217 3
scsi_mod 121421 4 sg,ips,aic7xxx,sd_mod

The /sbin/lsmod output is less verbose and easier to read than the output from viewing /proc/modules. To load a kernel module, use the /sbin/modprobe command followed by the kernel module name. By default, modprobe attempts 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, meaning that other modules must be loaded first for it to load. The /sbin/modprobe command checks for these dependencies and loads the module dependencies before loading the specified module
ls mod output
ls mod output

  1. This is a banner describing each field of lsmod's output.
  2. This is the NFS locking functionality, loaded as a module.
  3. This is the RPC functionality, loaded as a module.
  4. Natural language support (NLS) which gives users access to foreign characters, is also a loaded module.
  5. Support for Microsoft filesystems is loaded as a module.
  6. This is the module's size in bytes.
  7. This is the number of time the kernel is using the module. Modules might have more than one instance.
  8. Indicates the modules resources will be cleaned up automatically when it is unloaded.
  9. This shows that the sunrpc module is dependent on lockd.
  10. This shows that the vfat module is dependent on fat.

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.
The next lesson discusses how to load kernel modules.

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

SEMrush Software 3 SEMrush Banner 3