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're going to learn about the basic structure of a kernel module using a simple example. Let's start by looking at the code we will be using.
What exactly is a kernel module?
Great question! A kernel module is a piece of code that can extend the functionality of the kernel. It allows us to add features without rebooting the system, which is crucial for maintaining uptime in production environments.
What does the initialization function do?
The initialization function, like example_init in our code, is executed when the module is loaded into the kernel. It typically registers the module's functionalities or resources.
So what does the code printk(KERN_INFO "Hello, Kernel!\n"); do?
This line logs a message to the kernel log, which is crucial for debugging and understanding what happens when the module is loaded. Remember, printk is similar to printf but used for logging in the kernel space.
Is there an exit function too?
Yes! The exit function, like example_exit, is called when the module is removed. It can be used to clean up resources that were allocated when the module was loaded.
To summarize, the initialization function handles setup, while the exit function manages cleanup. Together, they form the backbone of a kernel module.
Now, let’s look at the metadata within our module. This includes important details such as license, author, and description information.
What are those macros you mentioned?
The macros MODULE_LICENSE, MODULE_AUTHOR, and MODULE_DESCRIPTION provide essential information about the module. They ensure compatibility with the kernel and inform users about the module's author and purpose.
Why is the license important?
The license is crucial for legal reasons. It defines the terms under which code can be shared and used. In this case, using GPL indicates that the code is released under the GNU General Public License.
How do we compile this code?
To compile the module, you need to create a Makefile that directs the build process. The command make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules will compile your module into a .ko file.
So, once compiled, I can load it into the kernel?
Exactly! After compiling, you can use insmod to load your module into the kernel and rmmod to remove it when you're done. Always check kernel logs with dmesg for any messages from your module!
Overall, we've covered the basic structure, compiled it, and discussed the importance of metadata in kernel modules.
Core logic allowing dynamic expansion of kernel functionality.
The entry point for logic executed when the module loads.
A crucial cleanup routine executed when the information is unloaded.
Essential module details including license, authorship, and descriptions.
When a module’s in and you need a hand, / Initialize to make it grand!
Imagine a teacher (initialization) who welcomes students (resources) at the start of the day, and at the end of the day, the teacher helps everyone pack up (exit function) before they leave.
I.E. - 'Init' and 'Exit' to remember module functions.
M.E.D. - Metadata, Entry, Destruction to recall key module aspects.
Term
What is a kernel module?
Click to flip
Term
What does 'printk' do?
Click to flip
A piece of code that extends the functionality of the Linux kernel at runtime.
Function executed when the kernel module is loaded, typically used for registering functionalities.
Function executed when the kernel module is unloaded, used for cleanup operations.
Macro that defines the licensing terms of the module.
Macro that defines the author of the module.
Macro that provides a brief description of the module.
A logging function used within kernel modules to write messages to the kernel log.
This section introduces the basic code structure for a Linux kernel module, demonstrating its initialization and exit functions.
The section provides a foundational understanding of writing basic kernel modules in Linux by showcasing a sample code in C. It explains the purpose of module initialization and exit functions and highlights key metadata associated with kernel modules.
Kernel modules are essential for enhancing the functionality of the Linux kernel without requiring a system reboot. This section specifically focuses on writing a basic kernel module in C, showcasing the necessary components to set up a functioning module.
example_init is where the module logs a message to the kernel log stating that it has loaded.example_exit function logs a message indicating the module has been removed.MODULE_LICENSE, MODULE_AUTHOR, and MODULE_DESCRIPTION, one can define important information regarding the module's license, authorship, and a brief description, which is critical for proper handling of kernel modules by the system.The example code serves as a starting point for developing kernel modules, providing a basic structure for future expansions.