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.
Let's start with the first step of building a kernel module, which is to write the kernel module code. This code typically resides in a file named example.c. What’s crucial here is defining the initialization and exit functions.
What do these functions actually do?
Great question! The module_init function is executed when the module is loaded, and the module_exit function is executed when it is unloaded. Think of it like a theater where the initialization function sets the stage, and the exit function takes it down.
Can you give us an example of what that code looks like?
"Absolutely! For instance:
Now that we have our kernel module code, the next step involves creating a Makefile. Why do you think a Makefile is needed for building the module?
Is it to compile the code correctly?
"Exactly! The Makefile tells the build system how to compile the module. Here’s a simple Makefile you can use:
Having created our Makefile and written the module code, it's time to compile! Who can tell me how we go about that?
We need to run make in the terminal.
Correct! Let's explore what happens when you run make. It invokes the rules defined in the Makefile, which compiles the kernel module and generates a .ko file. What command do you use to check if your module compiled successfully?
We can look for the .ko file in our directory, right?
Precisely! If the .ko file is present, it indicates a successful compilation. If not, check for error messages in the terminal. Can anyone think of a reason we might want to compile a kernel module?
To add functionality or support for new hardware?
Exactly! Compiling a kernel module allows us to enhance the functionality of the Linux kernel dynamically. Now, let’s summarize our discussion. We covered how to compile the module using the make command and what to do if errors occur during compilation.
After compiling our kernel module, we now need to load it into the kernel. What command do you think we use?
I believe we use the insmod command?
Correct! The insmod command inserts the module into the kernel. For example, you would type sudo insmod example.ko. What do you think happens if there’s an error when loading?
Maybe we will see an error message in the terminal?
Yes! The kernel will provide feedback on what went wrong. Additionally, we can use dmesg to check kernel logs for more detailed information. Always remember that debugging is a key part of working with kernel modules. Let’s quickly recap what we’ve discussed about loading kernel modules.
The source code that defines how a kernel module operates, including initialization and exit functions.
A file that provides instructions for compiling the kernel module and managing build processes.
The process of converting the source code into an executable kernel module, resulting in a .ko file.
The act of inserting the compiled module into the kernel using tools such as insmod.
To load a module, you must compile, Make it right, and it will compile!
John wants to enhance his computer's functionality, so he writes a kernel module. First, he crafts the code, then builds it with a Makefile, and finally loads it with 'insmod' to see his changes take effect!
C-M-L: Code, Makefile, Load - the steps to build a kernel module.
B-C-L: Build, Compile, Load - steps in the kernel module building process.
Term
What is a kernel module?
Click to flip
Term
What is the purpose of a Makefile?
Click to flip
Term
What command loads a kernel module?
Click to flip
Term
What does 'KERN_INFO' signify?
Click to flip
A piece of code that can be loaded into the kernel at runtime, extending its functionality.
A file containing rules and commands for building a kernel module by specifying how to compile and link the code.
A variable used in a Makefile to specify object files that need to be compiled into a kernel module.
A command used to insert a module into the Linux kernel.
A command that displays the kernel ring buffer, useful for examining kernel messages.
A log level used in kernel messages indicating general information.
This section outlines the essential steps required to build a Linux kernel module, focusing on writing code, creating a Makefile, and compiling the module.
In this section, we delve into the specific steps necessary for building a Linux kernel module. Key activities include writing the module’s source code, creating an accompanying Makefile to manage the compilation process, using the make command, and ultimately loading the compiled kernel module for use.
To successfully build a Linux kernel module, developers must follow a systematic approach that includes specific coding and compilation steps. The process begins with writing the kernel module source code, typically in a file named example.c, where important functions like module_init and module_exit are defined to dictate the behavior of the module when it is loaded and unloaded, respectively.
Next, the developer creates a Makefile, which serves as an essential tool for compiling the module. This Makefile includes directives that specify how to compile and link the object files necessary for the module. A standard Makefile for kernel modules commands the build system to utilize the current kernel build path and defines rules for creating the module.
Finally, after writing the code and creating the Makefile, developers execute the make command in the directory containing the source code and Makefile. Upon successful compilation, a .ko file is produced, which represents the kernel module and can be loaded into the kernel using insmod.
This section emphasizes the importance of these steps in the kernel development workflow, as they lay the groundwork for extending the capabilities of the Linux kernel.