Software & AppsOperating SystemLinux

Building a Single In-Tree Kernel Module on Ubuntu and Debian

Ubuntu 18

In the world of Linux, kernel modules are a crucial component that enable the kernel to load and unload functionalities dynamically. In this article, we’ll dive deep into the process of building a single in-tree kernel module on Ubuntu and Debian systems.

Quick Answer

Building a single in-tree kernel module on Ubuntu and Debian involves navigating to the kernel source directory and using the make command with either the M= argument, the path to the specific file, or the SUBDIRS argument. It is important to ensure that the kernel has been compiled at least once before building the module.

What is an In-Tree Kernel Module?

In-tree kernel modules are those that are included in the kernel source tree. They are part of the kernel source code and are maintained along with it. This is in contrast to out-of-tree modules, which are developed and maintained separately from the kernel source code.

Prerequisites

Before we begin, make sure you have the following:

  • A system running Ubuntu or Debian.
  • The kernel source code. You can obtain it from the official kernel website.
  • Basic knowledge of Linux commands and the terminal.

Building the Kernel Module

Step 1: Navigate to the Kernel Source Directory

Open your terminal and navigate to the top-level source directory of the kernel using the cd command. For example:

cd /usr/src/linux

Step 2: Build the Module

There are three common methods to build a single in-tree kernel module.

Method 1: Using the make Command with the M= Argument

Use the make command followed by the M= argument and the path to the module directory. For example:

make M=drivers/net/can/usb/peak_usb/

In this command, M= tells the make command to only build the part of the kernel tree specified.

Method 2: Using the make Command with the Path to the Specific File

You can also use the make command with the path to the specific file in the kernel tree. For example:

make drivers/usb/serial/visor.ko

In this command, drivers/usb/serial/visor.ko is the path to the specific module file. The .ko extension stands for ‘kernel object’, which is the format for loadable kernel modules.

Method 3: Using the make Command with the SUBDIRS Argument

Another method is to use the make command with the SUBDIRS argument followed by the path to the module directory. For example:

make SUBDIRS=drivers/staging/ft1000/ft1000-usb modules

In this command, SUBDIRS tells the make command to only build the part of the kernel tree specified, and ‘modules’ instructs it to build all modules in the specified directory.

Important Notes

Before building the module, ensure that the kernel has been compiled at least once. If not, you may encounter errors like no symbol version for module_layout.

After building the module, running depmod -a before using modprobe can help resolve any module dependency issues.

Conclusion

Building a single in-tree kernel module on Ubuntu and Debian systems is a straightforward process if you understand the basic commands and principles involved. This article has provided you with the knowledge and tools to do so. Happy coding!

What is the purpose of building a single in-tree kernel module?

The purpose of building a single in-tree kernel module is to add or modify functionalities in the Linux kernel dynamically without the need to recompile the entire kernel.

How do I obtain the kernel source code?

You can obtain the kernel source code from the official kernel website at [https://www.kernel.org/]. Choose the appropriate version and download the source code package.

Can I build a single in-tree kernel module on other Linux distributions?

Yes, you can build a single in-tree kernel module on other Linux distributions as long as you have the kernel source code and the necessary tools installed. However, the specific commands and paths may vary.

What is the purpose of the `make` command with the `M=` argument?

The make command with the M= argument is used to build a specific part of the kernel tree. It allows you to build only the module or directory specified, saving time and resources compared to building the entire kernel.

What is the purpose of the `.ko` file extension?

The .ko file extension stands for ‘kernel object’ and is the format for loadable kernel modules. It contains the compiled code and data for the module, which can be loaded and unloaded dynamically into the running kernel.

Leave a Comment

Your email address will not be published. Required fields are marked *