Linux Tactic

Unlocking the Power of Kernel Modules: A Beginner’s Guide

Introduction to the Kernel Module

A kernel module is a file containing code that extends the functionality of the Linux kernel. It is a small software library that can be loaded and unloaded from the kernel without requiring a system reboot.

Modules are used to support features that are not built into the kernel, such as device drivers, filesystems, and security modules. In this article, we will explore the basics of kernel modules and learn how to add a simple module to the kernel.

We will cover the important aspects of kernel modules, such as file extension and naming convention, loading, compatibility, and utility for loading kernel modules.

File Extension and Naming Convention

Kernel modules have a “.ko” file extension, which stands for kernel object. The naming convention for kernel modules follows the linux kernel standard, which requires the module name to be in lowercase letters and separated by underscores.

A typical naming convention for kernel modules is “module_name.ko”, where “module_name” is the name of the module.

Loading of Kernel Module

Kernel modules are loaded into the kernel using the “insmod” command. The “insmod” command inserts a module into the kernel and updates the running system to include the module’s functionality.

The module is then available for use by other parts of the system.

Compatibility

Kernel modules must be compatible with the kernel version running on the system. Compiling a module for an incorrect version of the kernel can lead to module loading failure.

A module compiled for a specific kernel version will not work on a different version of the kernel. To ensure compatibility, it is recommended to compile modules for the same kernel version as the target system.

Utility for Loading Kernel Module

The utility for loading and unloading a kernel module is the “modprobe” command. The “modprobe” command loads a module into the kernel and resolves any dependencies that the module may have on other modules.

It is also used to remove a module from the kernel.

Adding a Kernel Module

Adding a kernel module is a simple process that involves writing a program in C language and compiling it into a kernel module. In this article, we will use a simple “hello world” module as an example.

Example of

Adding a Kernel Module

Create a directory for the module and create a C file named “hello-world.c” in that directory.

In the “hello-world.c” file, we will include the necessary headers and define the module information using the “MODULE_AUTHOR”, “MODULE_LICENSE”, and “MODULE_DESCRIPTION” macros.

“`c

#include

#include

#include

MODULE_AUTHOR(“John Doe”);

MODULE_LICENSE(“GPL”);

MODULE_DESCRIPTION(“A simple hello world module”);

“`

Next, we will define the module initialization and cleanup functions using the “module_init” and “module_exit” macros.

“`c

static int __init hello_init(void)

{

printk(KERN_INFO “Hello Worldn”);

return 0;

}

static void __exit hello_exit(void)

{

printk(KERN_INFO “Goodbye Worldn”);

}

module_init(hello_init);

module_exit(hello_exit);

“`

This module prints “Hello World” during initialization and “Goodbye World” during cleanup.

Compile the module using the following command:

“`bash

make -C /lib/modules/`uname -r`/build M=`pwd` modules

“`

The “-C” option specifies the directory where the kernel source code is located. The “`uname -r`” command expands to the current kernel version.

The “M” option specifies the directory where the module source code is located.

This command will compile the module and generate a “hello-world.ko” file in the same directory.

Load the module using the “insmod” command:

“`bash

insmod hello-world.ko

“`

Checking if the Module is Loaded

To check if the module is loaded, use the “lsmod” command. This command lists all the loaded modules in the kernel.

“`bash

lsmod | grep hello-world

“`

This command should display the “hello-world” module in the output.

Confirming Successful Module Addition

To confirm that the module was successfully added, use the “dmesg” command. This command displays the kernel ring buffer, which contains information about the kernel and its modules.

“`bash

dmesg | tail -n 10

“`

This command should display the “Hello World” message that was printed during module initialization.

Conclusion

In this article, we explored the basics of kernel modules and learned how to add a simple “hello world” module to the kernel. We covered important aspects of kernel modules, including file extension and naming convention, loading, compatibility, and utility for loading kernel modules.

Adding a kernel module is a simple process that involves writing a program in C language and compiling it into a kernel module. By adding kernel modules, we can extend the functionality of the Linux kernel and

make it more versatile and flexible.

Removing/Unloading a Kernel Module

Kernel modules can be removed or unloaded from the kernel using the “rmmod” command. It is used to remove a loaded module and take back the resources used by the module.

Unloading the Kernel Module

To remove a kernel module, use the “rmmod” command followed by the name of the module.

“`bash

rmmod hello-world

“`

This command removes the “hello-world” module from the kernel.

Confirming Module is Unloaded

To confirm that the module has been successfully unloaded, use the “lsmod” command. This command lists all the loaded modules in the kernel.

“`bash

lsmod | grep hello-world

“`

This command should not display the “hello-world” module in the output.

Getting Information on Kernel Module

The “modinfo” command is used to display information about a loaded module. It provides details such as the author, license, and description of the module.

Utility for Getting Module Info

To view the information for a loaded module, use the “modinfo” command followed by the name of the module.

“`bash

modinfo hello-world

“`

This command displays information about the “hello-world” module, such as the author, license, description, and version.

Details Provided by modinfo

The “modinfo” command provides some important details, such as “srcversion” and “vermagic”.

The “srcversion” field contains the unique identifier for the source code that was used to compile the module.

This identifier is used to ensure that the module is compatible with the running kernel.

The “vermagic” field contains the version and other build details of the running kernel.

It is used to ensure that the module is compatible with the running kernel.

Conclusion

In this article, we covered the topics of removing/unloading a kernel module and getting information on a kernel module. By using the “rmmod” command, we can remove a loaded module and take back the resources used by the module.

To confirm that the module has been successfully unloaded, we can use the “lsmod” command. The “modinfo” command provides information about a loaded module, such as the author, license, description, and version.

It also provides important details such as “srcversion” and “vermagic” to ensure that the module is compatible with the running kernel. By understanding these important aspects of kernel modules, we can better manage and control the functionality of the Linux kernel.

Example Code

In this article, we discussed the basics of kernel modules, including file extension and naming convention, loading, compatibility, utility for loading/unloading kernel modules, and getting information on a kernel module. To illustrate these concepts, we will provide a sample code that implements a simple kernel module that prints a message during initialization and cleanup.

The following C code implements the “hello-world” module that we used as an example in this article.

“`c

#include

#include

#include

MODULE_AUTHOR(“John Doe”);

MODULE_LICENSE(“GPL”);

MODULE_DESCRIPTION(“A simple hello world module”);

static int __init hello_init(void)

{

printk(KERN_INFO “Hello Worldn”);

return 0;

}

static void __exit hello_exit(void)

{

printk(KERN_INFO “Goodbye Worldn”);

}

module_init(hello_init);

module_exit(hello_exit);

“`

This code imports the necessary kernel headers and defines the module information using the “MODULE_AUTHOR”, “MODULE_LICENSE”, and “MODULE_DESCRIPTION” macros.

The module initialization function “hello_init” is defined using the “__init” macro, which indicates that this function is called during module initialization.

This function uses the “printk” function to print the “Hello World” message to the system log. The “__init” macro also informs the kernel that this code will only be used during module initialization.

The module cleanup function “hello_exit” is defined using the “__exit” macro, which indicates that this function is called during module cleanup. This function uses the “printk” function to print the “Goodbye World” message to the system log.

The “__exit” macro also informs the kernel that this code will only be used during module cleanup.

Finally, the “module_init” and “module_exit” macros are used to specify the module initialization and cleanup functions respectively.

The kernel calls these functions during module insertion and removal.

To compile this module, we need to create a Makefile that specifies the target, source, and module.

Here’s an example Makefile.

“`Makefile

obj-m += hello-world.o

all:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

“`

This Makefile defines the target object file “hello-world.o”, which is the output of the compilation process.

The “all” target is used to compile the module using the kernel source code located at “/lib/modules/$(shell uname -r)/build”. The “clean” target is used to clean up the compilation artifacts in the current directory.

To compile the module, run the following command.

“`bash

make

“`

This command will compile the module and generate a “hello-world.ko” file in the same directory.

To insert the module into the kernel, run the following command.

“`bash

sudo insmod hello-world.ko

“`

This command will insert the module into the kernel and print the “Hello World” message to the system log.

To remove the module from the kernel, run the following command.

“`bash

sudo

rmmod hello-world

“`

This command will remove the module from the kernel and print the “Goodbye World” message to the system log.

To view the system log, run the following command.

“`bash

dmesg | tail

“`

This command will display the last few lines of the system log, which should include the messages printed by the module.

Conclusion

In this article, we provided a sample code that implements a simple kernel module that prints a message during initialization and cleanup. We explained the important aspects of kernel modules, including file extension and naming convention, loading, compatibility, utility for loading/unloading kernel modules, and getting information on a kernel module.

By understanding these concepts and using this sample code as a reference, developers can create and manage kernel modules and extend the functionality of the Linux kernel. In this article, we discussed the basics of kernel modules, including file extension and naming convention, loading, compatibility, utility for loading/unloading kernel modules, and getting information on a kernel module.

We provided an example code that implements a simple kernel module that prints a message during initialization and cleanup. By understanding these concepts and using the sample code as a reference, developers can create and manage kernel modules to extend the functionality of the Linux kernel.

Kernel modules are an essential aspect of the Linux operating system and allow developers to add support for new hardware and features to the kernel without the need of a system reboot. By utilizing kernel modules, developers can

make the Linux kernel more versatile and flexible for various use cases.

Popular Posts