depmod Command in Linux

Introduction

In this practical lab, delve into mastering the depmod command within a Linux environment, a crucial tool for analyzing kernel module dependencies. The depmod command plays a pivotal role in generating dependency files, mapping the intricate relationships between kernel modules. This ensures modules load in the correct order, preventing system instability. Starting with understanding the core function of depmod, you'll progress to examining the dependency trees of modules and conclude with hands-on troubleshooting of dependency-related issues. Equip yourself with the expertise needed to manage kernel modules and their dependencies effectively in your Linux system administration tasks.

Understand the Purpose of the depmod Command

This section focuses on clarifying the main function of the depmod command in Linux. Specifically, depmod examines and catalogs the module dependencies within the Linux kernel.

Executing depmod creates a vital dependency file, typically found at /lib/modules/<kernel_version>/modules.dep. This file details inter-module dependencies, information leveraged by the modprobe command to intelligently load kernel modules on demand.

Initiate this process by executing:

sudo depmod

Example output:

Scanning /lib/modules/5.15.0-1023-aws for modules
Scanning /lib/modules/5.15.0-1023-aws/kernel for modules
Scanning /lib/modules/5.15.0-1023-aws/updates for modules
Scanning /lib/modules/5.15.0-1023-aws/weak-updates for modules

The output illustrates depmod scanning the /lib/modules/<kernel_version> directory structure for kernel modules. It compiles this information into the modules.dep file, which then reflects the dependencies each module requires.

You can examine the contents of this modules.dep file using the command:

cat /lib/modules/5.15.0-1023-aws/modules.dep

Example output:

kernel/drivers/acpi/acpi_power_meter.ko: kernel/drivers/acpi/acpi_ipmi.ko
kernel/drivers/acpi/acpi_thermal_rel.ko: kernel/drivers/acpi/acpi_ipmi.ko
kernel/drivers/acpi/apei/ghes.ko: kernel/drivers/acpi/apei/apei-base.ko
kernel/drivers/acpi/apei/erst-lib.ko: kernel/drivers/acpi/apei/apei-base.ko

The modules.dep output demonstrates how each kernel module's dependencies are listed, clearly showing which other modules must be loaded for the primary module to work as intended. Accurate systemadmin work depends on this.

The next step delves into the dependency trees and their intricacies in greater detail.

Explore the Dependency Tree of Kernel Modules

This section guides you through investigating the kernel module dependency tree. You'll leverage the power of the depmod command for this purpose.

Begin by enumerating all the installed kernel modules on your system:

sudo modprobe -l

Example output:

kernel/drivers/acpi/acpi_power_meter.ko
kernel/drivers/acpi/acpi_thermal_rel.ko
kernel/drivers/acpi/apei/ghes.ko
kernel/drivers/acpi/apei/erst-lib.ko
...

To discover a specific kernel module's dependencies, the modinfo command is invaluable. For instance, to inspect the acpi_power_meter module, use:

sudo modinfo -d acpi_power_meter

Example output:

depends: acpi_ipmi

The output indicates that acpi_power_meter depends on acpi_ipmi. Essential knowledge for systemadmin tasks.

The depmod command can also generate a more comprehensive dependency tree view. Executing depmod -n -a displays the tree without actually loading any modules:

sudo depmod -n -a

Example output:

kernel/drivers/acpi/acpi_power_meter.ko:
 kernel/drivers/acpi/acpi_ipmi.ko

kernel/drivers/acpi/acpi_thermal_rel.ko:
 kernel/drivers/acpi/acpi_ipmi.ko

kernel/drivers/acpi/apei/ghes.ko:
 kernel/drivers/acpi/apei/apei-base.ko

kernel/drivers/acpi/apei/erst-lib.ko:
 kernel/drivers/acpi/apei/apei-base.ko

This output provides a complete dependency tree for each listed kernel module, clarifying the required loading order. This detailed information is crucial for effective kernel module management and resolution of any loading issues.

A thorough grasp of the kernel module dependency tree is vital when addressing kernel module issues. Ensuring all dependencies are met is paramount.

The subsequent section focuses on using depmod to effectively troubleshoot module dependency problems.

Troubleshoot Module Dependencies with depmod

This final step provides hands-on knowledge of using depmod to resolve kernel module dependency-related issues.

Start by creating a custom kernel module that relies on another. The "hello_world" module serves as a good example:

## Create a new directory for the module
mkdir ~/project/hello_world
cd ~/project/hello_world

## Create the module source file
cat << EOF > hello_world.c
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple hello world kernel module");

static int __init hello_world_init(void)
{
    printk(KERN_INFO "Hello, World!\n");
    return 0;
}

static void __exit hello_world_exit(void)
{
    printk(KERN_INFO "Goodbye, World!\n");
}

module_init(hello_world_init);
module_exit(hello_world_exit);
EOF

## Compile the module
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

Next, build a second module dependent on "hello_world":

## Create the dependent module source file
cat << EOF > hello_dependent.c
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A module that depends on the hello_world module");
MODULE_DEPENDS_ON("hello_world");

static int __init hello_dependent_init(void)
{
    printk(KERN_INFO "Hello Dependent Module!\n");
    return 0;
}

static void __exit hello_dependent_exit(void)
{
    printk(KERN_INFO "Goodbye Dependent Module!\n");
}

module_init(hello_dependent_init);
module_exit(hello_dependent_exit);
EOF

## Compile the dependent module
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

Now, attempt to load the "hello_dependent" module:

sudo insmod hello_dependent.ko

Example output:

insmod: ERROR: could not insert module hello_dependent.ko: Unknown symbol in module

The error indicates that "hello_dependent" cannot find "hello_world".

To correct this, use depmod to update the dependency information:

sudo depmod -a

Retry loading "hello_dependent":

sudo insmod hello_dependent.ko

This time, the load should succeed, with the "Hello Dependent Module!" message appearing in the kernel logs. This is an important skill for any systemadmin using Linux.

You've now learned to use depmod to troubleshoot module dependency issues. This ensures that all necessary modules are loaded correctly, enabling your modules to function properly, whether you are operating as root or a normal user.

Summary

This lab began by exploring the purpose of the depmod command in Linux, emphasizing its role in analyzing kernel module dependencies. The depmod command creates the modules.dep dependency file. This file guides the modprobe command to load the correct kernel modules when requested. It further explored the module dependency tree, demonstrating listing modules and their dependencies with depmod and how to troubleshoot dependency-related problems. All of this is vital knowledge for any aspiring systemadmin.

400+ Linux Commands