Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will discuss how to build kernel modules. Let's start with the basic structure. Every kernel module must have at least an initialization function and an exit function. Can anyone tell me what these functions do?
The initialization function runs when the module is loaded, right?
Exactly! It’s where we register functionalities. And what about the exit function?
That's used to clean up before unloading the module.
Correct! Think of the initialization function as the welcome mat and the exit function as the goodbye wave. This makes it easier to remember their roles.
How do we define these functions in code?
Great question! You define them in C with static int __init for initialization and static void __exit for exit. Let's look at this example of a simple module:
static int __init example_init(void) { printk(KERN_INFO "Hello, Kernel!\n"); return 0; } and the exit function as static void __exit example_exit(void) { printk(KERN_INFO "Goodbye, Kernel!\n"); }. These functions print messages when loaded or unloaded.
Can anyone summarize what we've learned about the module functions?
The init function welcomes the module, and the exit function says goodbye!
Well put! Remember, these functions are essential for the module's lifecycle.
Now that we have our kernel module code, let’s talk about compiling it. Who knows what we need to compile a module?
We need a Makefile to define how to build it!
Exactly! A Makefile specifies how to compile our module. Here’s a simple Makefile example: obj-m += example.o. What do you think this line does?
It tells the compiler that example.o is the object file we want to build.
Great! And then we have the rules for building and cleaning up. The command to build pulls in the kernel build system. Do you remember how we do that?
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules?
That's right! This command invokes the kernel's build system for our module’s directory. Let's focus on the clean rule. Why do we need it?
To remove unnecessary files after compilation!
Exactly! A good practice to keep our workspace tidy. With this understanding, let’s move on to building a kernel module!
Now, let’s go through the actual steps to build our kernel module. What’s our first step?
We need to write our module code and save it.
Yes! We would save it as example.c. What comes next?
Then we create our Makefile!
Correct! After that, we run the make command. What does this command do?
It compiles our module code into a .ko file!
Exactly! Once we have the .ko file, which command do we use to load the module into the kernel?
insmod mymodule.ko?
Right! Now, if we want to unload the module, what command would we use?
rmmod mymodule!
Perfect! You all grasped the build and load process well. Remember these commands as we continue exploring kernel modules.
To build a module, it's easy to say, write the code and then make it play.
Once upon a time, a coder wanted to add fun features to the kernel. With an init function, it welcomed visitors, and an exit function waved goodbye every time it left.
Remember I-EXIT: Initialization first, exit later!
IMMEDIATE: Initialization, Module, Insert, Get, Exit, Done, All Typical Extensions.
Term
What is a kernel module?
Click to flip
Term
What does a Makefile do?
Click to flip
Term
What is the purpose of an initialization function?
Click to flip
Term
What is an exit function used for?
Click to flip
A piece of code that can be loaded into the kernel to extend its functionality.
A file that defines rules for building and managing compilation of kernel modules.
A function that is executed when the module is loaded into the kernel.
A function that is executed when the module is removed from the kernel.
This section provides an overview of how to build kernel modules, detailing the source code structure and necessary steps for compilation.
The section outlines the process involved in creating kernel modules for the Linux kernel, including writing source code, creating a Makefile, and compiling the module. It emphasizes the structure of the source code and the specific commands needed for building the modules successfully.
Building kernel modules involves several key steps that allow developers to create extensions for the Linux kernel. This section covers the necessary source code structure, the use of a Makefile, and detailed commands to compile the modules effectively.
The basic structure of a kernel module is defined in C and typically includes:
static int __init example_init(void) {
printk(KERN_INFO "Hello, Kernel!\n");
return 0;
}static void __exit example_exit(void) {
printk(KERN_INFO "Goodbye, Kernel!\n");
}MODULE_LICENSE, MODULE_AUTHOR, and MODULE_DESCRIPTION.To compile the kernel module, a Makefile is required. This file typically contains:
obj-m += example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleanThis Makefile outlines the object file, build process, and cleaning commands.
example.c for instance).make command to compile the module..ko file is generated, which can be inserted into the kernel using insmod.Understanding these processes is fundamental for developing kernel modules that enhance Linux functionality without needing to modify the core kernel.