AllRounder.ai
Chapters in this course

Enrol to start learning

Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.

Enrol free

5.6. Building Kernel Modules

Interactive Audio Lesson

Session 1: Introduction to Kernel Module Code Structure

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

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?

Noah
Noah

The initialization function runs when the module is loaded, right?

Sarah
SarahInstructor

Exactly! It’s where we register functionalities. And what about the exit function?

Isabella
Isabella

That's used to clean up before unloading the module.

Sarah
SarahInstructor

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.

Akash
Akash

How do we define these functions in code?

Sarah
SarahInstructor

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:

Sarah
SarahInstructor

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.

Sarah
SarahInstructor

Can anyone summarize what we've learned about the module functions?

Ananya
Ananya

The init function welcomes the module, and the exit function says goodbye!

Sarah
SarahInstructor

Well put! Remember, these functions are essential for the module's lifecycle.

Session 2: Creating a Makefile

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Robert
RobertInstructor

Now that we have our kernel module code, let’s talk about compiling it. Who knows what we need to compile a module?

Noah
Noah

We need a Makefile to define how to build it!

Robert
RobertInstructor

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?

Isabella
Isabella

It tells the compiler that example.o is the object file we want to build.

Robert
RobertInstructor

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?

Akash
Akash

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

Robert
RobertInstructor

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?

Ananya
Ananya

To remove unnecessary files after compilation!

Robert
RobertInstructor

Exactly! A good practice to keep our workspace tidy. With this understanding, let’s move on to building a kernel module!

Session 3: Building and Loading the Module

Unlock the classroom podcast

The transcript is free to read. A free account plays the conversation back.

Sarah
SarahInstructor

Now, let’s go through the actual steps to build our kernel module. What’s our first step?

Noah
Noah

We need to write our module code and save it.

Sarah
SarahInstructor

Yes! We would save it as example.c. What comes next?

Isabella
Isabella

Then we create our Makefile!

Sarah
SarahInstructor

Correct! After that, we run the make command. What does this command do?

Akash
Akash

It compiles our module code into a .ko file!

Sarah
SarahInstructor

Exactly! Once we have the .ko file, which command do we use to load the module into the kernel?

Ananya
Ananya

insmod mymodule.ko?

Sarah
SarahInstructor

Right! Now, if we want to unload the module, what command would we use?

Noah
Noah

rmmod mymodule!

Sarah
SarahInstructor

Perfect! You all grasped the build and load process well. Remember these commands as we continue exploring kernel modules.