Linux depmod Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the depmod command in Linux to analyze the dependency tree of kernel modules. The depmod command is used to generate a dependency file that contains information about the dependencies between kernel modules, which is crucial for ensuring that the required modules are loaded correctly. You will start by understanding the purpose of the depmod command, then explore the dependency tree of kernel modules, and finally, learn how to troubleshoot module dependencies using depmod. This lab will provide you with practical examples and insights into managing kernel modules and their dependencies in a Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/PackagesandSoftwaresGroup -.-> linux/software("`Linux Software`") subgraph Lab Skills linux/software -.-> lab-422631{{"`Linux depmod Command with Practical Examples`"}} end

Understand the Purpose of the depmod Command

In this step, you will learn about the purpose of the depmod command in Linux. The depmod command is used to analyze the module dependencies of the Linux kernel modules.

The depmod command generates a dependency file, typically located at /lib/modules/<kernel_version>/modules.dep, which contains information about the dependencies between kernel modules. This file is used by the modprobe command to automatically load the required kernel modules when a module is requested.

Let's start by running the depmod command:

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 depmod command scans the /lib/modules/<kernel_version> directory and its subdirectories to find all the installed kernel modules. It then generates the modules.dep file, which contains the dependency information for each module.

You can view the contents of the modules.dep file using the following 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 file lists the dependencies for each kernel module, indicating which other modules need to be loaded for a particular module to function correctly.

In the next step, you will explore the dependency tree of kernel modules in more detail.

Explore the Dependency Tree of Kernel Modules

In this step, you will learn how to explore the dependency tree of kernel modules using the depmod command.

First, let's list all the installed kernel modules on the 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 view the dependencies for a specific kernel module, you can use the modinfo command. For example, let's check the dependencies for the acpi_power_meter module:

sudo modinfo -d acpi_power_meter

Example output:

depends: acpi_ipmi

This output shows that the acpi_power_meter module depends on the acpi_ipmi module.

You can also use the depmod command to generate a more detailed dependency tree. The depmod -n -a command will display the dependency tree without actually loading the 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 shows the full dependency tree for each kernel module, indicating which other modules need to be loaded for a particular module to function correctly.

Understanding the dependency tree of kernel modules is important when troubleshooting issues related to kernel modules, as you need to ensure that all the required dependencies are met.

In the next step, you will learn how to troubleshoot module dependencies using the depmod command.

Troubleshoot Module Dependencies with depmod

In this final step, you will learn how to use the depmod command to troubleshoot issues related to kernel module dependencies.

Let's start by creating a custom kernel module that has a dependency on another module. We'll use the hello_world module as an 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

Now, let's create a second module that depends on the hello_world module:

## 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, let's try 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 message indicates that the hello_dependent module is unable to find the hello_world module, which it depends on.

To troubleshoot this issue, we can use the depmod command to update the module dependency information:

sudo depmod -a

Now, let's try loading the hello_dependent module again:

sudo insmod hello_dependent.ko

This time, the module should load successfully, and you should see the "Hello Dependent Module!" message in the kernel logs.

In this step, you learned how to use the depmod command to troubleshoot issues related to kernel module dependencies. By updating the module dependency information, you can ensure that all required modules are loaded correctly, allowing your custom modules to function as expected.

Summary

In this lab, you first learned about the purpose of the depmod command in Linux, which is used to analyze the module dependencies of the kernel modules. The depmod command generates a dependency file, modules.dep, that contains information about the dependencies between kernel modules. This file is used by the modprobe command to automatically load the required kernel modules when a module is requested. You then explored the dependency tree of kernel modules in more detail, learning how to use the depmod command to list all the modules and their dependencies, as well as how to troubleshoot module dependencies using the depmod command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like