So guys, once you have downloaded the git repository inside the Udemy course on Netlink repository,
you will find the following folders.
And since we are about to write our Hello World Linux kernel module, just go inside that directory
and you will find the Hello World Linux kernel module file which is the following file.
Hello world lcmc followed by the makefile which is used to compile this linux kernel module.
The rest of the files you can ignore.
So now let us see how to write our first Hello World Linux kernel module.
So guys, now I'm opening our first Hello World Linux kernel module and you can see that this module
consists of only 42 lines of code.
So now let us understand that what a typical Linux kernel module consists of.
So first of all, you need to have include this header file.
This is the header file that you must include for writing any LCM.
Following that, you have to write a function with the following prototype.
The name of the function could be anything.
This function shall be invoked as soon as this is loaded.
Right?
As the name suggests, this function is a kind of initialization function.
You must do all the type of initialization that your Linux kernel module is supposed to do in this function.
Right?
And after that you need to write a compensatory function, which is called a cleanup function, right?
Again, the name of the function could be anything but note the signature or prototype of the function.
Right?
In this function you need to clean up all the memory that your Linux kernel module has ever created
or logged.
Right.
Now, having written these two functions, that is initialization function and cleanup function, you
need to tell the Linux kernel that which function is an initialization function and which function is
a cleanup function.
Remember in your Linux kernel module there could be hundreds of functions.
So we do this by using module init and module exit functions which is provided by the Linux kernel.
The argument to this function is a pointer to your initialization function as well as the cleanup function.
So tell the kernel which are in it and cleanup functions for this Linux kernel module.
If you do not do this registration kernel would try to use init module and cleanup module instead.
Now init module and cleanup module are default functions which Linux kernel will invoke if you do not
do this registration?
So remember, every Linux kernel module has to do this registration of initialization function and cleanup
function and rest of the things are just customary.
You need to tell the Linux kernel that who is the author of this module?
So for that you can use the macro module author.
Then you can also tell the Linux kernel that what is the description of this module?
That is what this module is supposed to do.
So this could be just a one liner string and then give the license to this module as GPL, which stands
for public license.
Right.
So you can see that writing a Linux kernel module is fairly simple and easy.
Now just save this file and just compile this file so you can see that the compilation of this file
is successful.
Now let us see how to write a make file.
Writing a make file for Linux kernel module is fairly very simple because you just need to write a make
file in some predefined format.
So you can see that.
Just specify the object file corresponding to the source file which represents your Linux kernel module.
Right.
And you can see that I'm compiling the Linux kernel module using this path.
This is the path in which we installed the Linux kernel headers.
Those headers are being used to compile your Linux kernel module.
So now let us try to run this module and see our module in action.
So remember in our Linux kernel module, we inserted some print statements so that we would know what
is going on.
So all those print statements would go in the file, var log dot log that is in this file.
So you can actually run this file using tail minus F command so that whatever is printed in this file
will actually displayed on the screen instantly.
Now in the left window I will insert the module into the Linux kernel.
Now the name of the module is Hello World AE.
Right.
So you can see that it will ask for a password.
Just provide the password and you can see on the right hand side that hello world module loads successfully.
That is, our module has been loaded successfully in the Linux kernel.
To verify it, just run the command lsmod.
And in this and in the output of this command you can see in the top.
The name of your module that is this.
Hello world.
Right.
So this module has been successfully inserted in the Linux kernel.
In fact, now it has become a part of the Linux kernel.
To unload the module from the Linux kernel.
You just use the command.
Mode, followed by the name of the Linux kernel module.
There is no need to specify.co.
Okay.
You have to run this command using sudo privileges again.
And you can see on the right hand side that the module has been ejected out of the Linux kernel.
So our module is printing.
Goodbye.
Hello world.
Right?
So guys, that was a very quick short demo on how to write Linux kernel module, compile load run and
unload the Linux kernel module.