A concise entry point that demystifies the kernel without getting bogged down in unnecessary complexity. It’s a practical bridge for developers looking to transition from user-space logic to hardware-level interaction.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Coding My First Linux Kernel Driver in C (Beginner)Added:
Hello everybody. In today's video, we're going to code a What a boring intro. We're going to code a kernel driver. That's actually quite interesting. And the funny part is I haven't prepared this video at all. The only thing I have done is I've looked up kernel driver hello world on Google and found this result. I haven't clicked on it yet. Hello world part one the simplest module. That sounds quite promising. And maybe we're going to use that to create our own kernel driver in C.
And just as a disclaimer, I haven't prepared prepared this video. I have never dealt with kernel drivers. I have never programmed anything related to kernel drivers. I haven't even I haven't programmed a kernel driver itself.
So, this is just me trying to learn something new. All right. And maybe it's interesting for you to follow along.
And learn along with me.
And all the kernel driver pros, please put whatever you find deserves correction in the comments as always.
And let's make this a great learning experience.
So, hello world part one the simplest module. This is a hello world kernel module programming guide on the Linux documentation project which is a project that attempts to document a lot of things in Linux.
>> [snorts] >> And we're going to use that.
So, the kernel module is I think the same thing as a kernel driver.
No idea. Is a kernel module a driver?
Maybe a kernel module that's my first um idea. Maybe kernel module is just a generic piece of software that runs in kernel mode. So, it's not um part of the user system.
And a driver is a specific kernel module. So, a piece of software that runs inside the kernel that interacts with hardware. So, that is a specific kind of kernel module.
Oh, and there's even a definition here.
A kernel driver is a program kernel module that is designed to drive a piece of hardware. Yeah. I think that's what I just said. Did I say it? I think so. So, a kernel driver is a kernel module that is designed to drive a piece of hardware.
All right. Since I don't have any hardware any interesting hardware with me right here right now, we're going to just create a simple module and I think this example is exactly what we want.
So, when the first caveman programmer chiseled the first program on the walls of the first cave computer, it was a program to paint the string hello world in antelope pictures.
>> [laughter] >> Uh that Okay, I already like this documentation by the first sentence. Um there's this documentation this kind of documentation that is overly extensive and nobody really understands it and nobody reads it and just skips to the code examples. And then there's this kind of documentation. I can follow this. This is nice.
Roman programming textbooks begin with the salute mundi program.
Uh that's probably hello world. I don't know what happens to people who break with this tradition, but I think it's safer to not not to find out. We'll start with a series of hello world programs that demonstrate the different aspects of the basic basics of writing a kernel module. Okay.
Here is the simplest module. Don't compile it yet. We'll cover module compilation in the next section. Hello world.
And I would say let's recreate this hello world.
And inspect the individual lines of code here. So, what I can already see is we have two includes here module and kernel. Oh, that's that's interesting. Of course, because we want to in We want to create a kernel module. So, of course, we we include kernel and module.
>> [laughter] >> If you want to create a hello world, you of course include hello and world. And if you want to create a a cryptocurrency, you include crypto and currency. That's how programming works, guys. That's what That's what you study for 5 years at university for.
So, include kernel and module. I can do that. Let's try and see. So, include kernel.
No, I already messed up. Linux/kernel.
And include linux/module.h.
Those are probably headers that contain the methods that we need. Maybe it makes sense to open or try to find those headers.
If I switch here to user local lib, I don't know if they are if they are located in local lib or in somewhere else because local lib is it's just local to Is it local to my user?
Or is it user space libraries maybe?
I'm just guessing, guys. If I try to find kernel.h, it's not found. So, I don't think that's what we need. Maybe it's somewhere else. I'm just going to search in user.
Okay, there are a couple of files here that look like matches. I think this is the match. Include user include linux kernel. Okay, so it's not inside user local lib or user local include. It's in user include linux kernel.
And then let's get the real path of this. This returns the absolute path of this result here. So, now we have an absolute path and then we can open that one in them using using tab e.
And there's nothing in here. Oh, great.
It's just a header including something else.
Okay, fine. So, there's not much information here except if we really want to dig deeper then we have to open other header header files.
And the other thing that we wanted to check is module.h.
And there it is. And now I messed up my layout. Great.
So, let me just fix it for a second.
And there we are. Okay.
So, let me just grab that absolute path here and open it here. And this is the module.h file. And again, it's just What is it even doing? It's just defining linux model module.h.
And then it's defining a couple of flags here for the sysf_init_module, whatever that is. And then ending it already. That's the entire header file.
That's I I expected it to be more complex than that. Honestly. But maybe I'm looking at at um at some stops that are not used. I don't know. If you know it, then please put it in the comments, guys.
So, now that we have included the header files that we need, let's have a look at the hello world example again. And this one makes sense. Okay, we want to create a module something that runs as part of the kernel or rather in kernel space.
We have to initialize the module. That makes sense. That is something that I can intuitively understand.
So, if we do int, that's probably a return return type or return value for the success of of this module.
And then init_module init_module takes in no arguments. So, we don't even need to specify void there, I think.
Then there's a function printk which uses a macro kern_info.
And then a string is printed.
Okay.
That's an interesting syntax. I have never seen this combination of a print command with a macro inside the argument.
And then the macro takes as argument this string argument and then probably does something with it. I don't know.
Macros are always magic to me. I have never I never understand how they really work.
Um of course, macros are simple string substitution. So, this kern_info thing right here, this will be looked up by the compiler somewhere.
And there will be some string substitution being made in the code. Of course, this can be arbitrarily complex and that's why it's natural to not have an intuitive grasp of macros because macros escape the normal C syntax and they completely break through it all. They are basically unlimited. You can do whatever you want with macros as far as I know.
So, we just And since the these are things that you can't derive from from your own brain.
Right? This is information that you can't think up naturally yourself. You can't come up with this yourself. This is stuff that you have to look up and that's why you need to follow some guide.
Right? So, it's technically it's impossible to make a tutorial on a kernel driver programming.
Yeah, it's impossible to make a tutorial about kernel driver programming without having looked up code before because there were some people in the comments complaining that probably the code that I'm showing you guys is not made up by me.
Um and of course in such examples here it's impossible to make it up yourself.
Because this is something that some develop- developer has arbitrarily decided and then you just have to follow this pattern.
So, let's just follow the pattern and look things up along the way. So, we're going to print K. I assume it's then for print kernel.
So, print K. There's no auto suggestion currently possible because Vim is not aware of that prototype.
Print K then kern_info kern_info And I would actually like auto completion in Vim. So, what I could do is here in user include I could rip grep for print K.
And let's see if we can find anything.
And there are a couple of results here.
Um I don't know if anything is an exact match. sysctl lp Those looks Those definitely look like some driver file files. atm_eni I don't know.
But there's no method prototype for print K itself.
Maybe [clears throat] the kernel headers are located somewhere differently and we can find this out later.
So, print K kern_info and then we just type hello world.
Ah, that's too boring. Let's make it super exciting and print hello Daniel.
That's how you write an exciting software.
Huh, heart rate is raising.
Okay.
And then a non-zero return means init module failed and that's what I already assumed by the int return value. That's only used to indicate an error and in this example it's just it's always returning zero. So, return zero.
And what's the next thing? We clean up the module.
And that's just another message that is being printed here. So, let's just do that as well, but I think technically we could also leave the cleanup function which is returning void. We could probably leave the function body empty. It's just nice to see um the interaction with module loading and unloading, I think.
So, I think when we load the mon- module, the init module will run and when we unload the module the cleanup function will run. So, let's see. Print K kern_info again kern info and bye-bye.
Love you.
Ah, yeah.
Okay. So, someone's Ah, [gasps and sighs] someone's nice to me.
Intimate relationships with kernel drivers, guys.
That's how you do it.
So, Okay, kernel modules must have at least two functions. Okay, we've already found that out. I probably um found out about about all of these things myself um just by talking right now.
Um typically init module either registers a handler blah blah blah blah blah blah blah blah blah.
The cleanup module function is supposed to undo whatever init module did so the module can be unloaded safely. Aha, okay.
Fine. We don't need that right now.
And then lastly every kernel module needs to include linux/module.h.
We needed to include linux/kernel.h only for macro expansion for the print K log level kern_alert which you learn about in section 2.1.1.
Aha, so this right here is a log level.
Ah, and I put a typo here. There should be no comma as far There should be no comma as far as I understood.
Aha, this is interesting. So, log levels can be provided here as macros.
So, introducing print K. Despite what you might think, print K was not meant to communicate information to the user even though we use it for exactly this purpose in hello in the hello world program it happens to be a logging mechanism for the kernel and is used to log information or give warnings. Therefore, each print K statement comes with a priority which is the one and kern_alert you see. Wait, which one?
Is this one in the string? Is this mandatory?
I thought I can just skip it.
Wait.
With which is the one and kern_alert you see. Where do I see a kern_alert? I just see kern_info.
kern_alert Hm.
There are eight priorities and the kernel has macros for them. So, you don't have to use cryptic numbers and you can view them and their meanings in linux/kernel.h.
I can't view anything in linux/kernel.h, but maybe in sysinfo. Maybe or maybe in const.h. So, let's open the current directory const.h. Maybe there's something in here and if we look for kern kern_info.
There is nothing in here. kern_info.
Okay, there's nothing in here. Maybe let's check the other file. What was it called? sysinfo tab e sysinfo.header >> [gasps] >> And in here kern info is not included either. So, I don't get where these are defined. rip grep kern_info Mhm. Okay, there were some results here.
Yeah, timeshift. No, that's not what I wanted.
Where is Wait. linux/kernel.h location user include linux kernel.h is include is intended for user processes, not kernel modules.
The print K priority macros now live in linux Where is it? priority macros now live in linux/printk.h.
I'm guessing you're reading the original guide which is based on 2.6 kernel series. For modern kernels, you should read the updated guide. There's an updated guide?
Which kernel am am I even using?
Am I using something that is outdated?
What if I search for printk.h as this guy here indicated? printk.h Let's have a look there.
CD user include print f Ah, no. What am I doing?
FD print K There's nothing here.
There's a printk.h. There are several printk.h's.
These are source files.
lib/modules I don't know. This does not look like clean paths that are used that are supposed to be used to me.
Anyways, I'm just going to try to follow the guide here and not question it too much because as soon as I start questioning questioning things things start falling apart.
And I think we should try to compile this first and see if it even works before I try to question things here.
So, introducing printk and printk well, actually should be defined somewhere here.
And there is a lot of stuff referencing printk, but I don't know which one is the correct one and that's always that's always annoying.
user include rip grep printk What's in here?
Does not look like printk is available on my system at all.
Take time to read through the priority macros. Yeah, but how where the hell are they?
I'm going to find them guys. I'm going to not give up. kern info where is it?
Wait, what if I pipe this into less and carefully read it?
So, this looks like we could find something.
This is all On the other hand, all of this looks like these are these source paths starting with source.
So, they don't look like they are what I'm looking for.
But this one is using printk with kern info.
So, if I'm looking up this file, uh where is it?
Uh let's for example this one.
What is it including?
keys trusted type Mhm.
Maybe my Linux works without a kernel.
Maybe my Linux distribution works without a kernel. Yeah, and that could be the case. So, mine is a kernel-less driver. Just like you have uh you sometimes have fruit without kernels. Is Is kernel even the right mhm term in English?
kern English core Yeah, kernel.
Mhm.
Yeah, it's just like the stone in a cherry.
There are cherries without stones inside them.
So, probably my Linux also kernel-less.
I'm really milking the joke.
I've repeated it three times already.
So, a kernel module Wait.
Where do I find kern info?
kern levels.h kern levels.h Is that it?
find kern levels There it is.
But this is also in these source directories. I thought this is What is this even? Linux HWE What does this stand for?
Linux HWE hardware enablement >> [sighs and gasps] >> Server installations will default to the general availability kernel and provide the hardware enablement kernel as optional.
I don't want I just want the regular one.
Linux kernel headers Where?
I don't have them Do I have to install them maybe first?
Linux Linux install kernel headers If I have to install them all and that that would be the explanation. You should be able to install the kernel header files for the currently running kernel by running the following in a terminal.
Okay, sudo apt get install Linux headers.
What is uname -r? I think I I saw that somewhere that is I think operating system info.
Uh What is that 617?
Is that my kernel version?
R is the kernel release. Okay, so that's the kernel release.
And if I do this, and this is an answer from 2011. I hope it's still up to date, guys. Otherwise, I will nuke my system probably.
is already installed.
But where?
kernel headers generic if you are on a desktop installation.
Is that maybe it?
What's the difference between the generic one and the version specific one?
I hear someone complain this won't install the headers for future updates automatically.
Ah, okay.
Mhm.
So, now it's it's building a module. What the hell is going on?
I hear my processor my processor spinning up.
I hope nothing is exploding.
Am I installing something critical down No, I What did I just install? Linux headers generic.
Mhm. So, Linux headers generic. Okay, that should be uh that should just be header files, but it was a 100 megabytes.
>> [snorts] >> Whatever.
So, let's try to find the kern info now.
It's still not there.
kern info But then there are headers for this specific kernel version that I have installed that we just found out using uname -r.
Although I have the -20 generic and this only lists -20. So, is there a difference between those? I don't know if there's a difference.
But I Wait.
Here the kern info is defined.
Is it defined somewhere else? It's also defined here.
And here.
kern levels.h Wait, if I search for kern levels.h now, do I find something new?
I think those are the same results I already had before.
I don't know, guys. So, I know that I'm using this specific kernel release. So, I'm going I'm going to use this kernel header file here.
Okay, let's not overcomplicate it. I am There are so many details to kernels um that I'm that I didn't really ever consider.
For example, that there are different versions of the kernel.
You can have multiple steps from multiple different kernel versions installed on your system.
I don't know. I don't know if it's even included like that.
But I Let's see. Let's just see.
I mean, I don't know if it's included like that because the default include path is user include. So, anything under here will be found by the compiler.
And anything under user local include will also be found.
But the kernel headers were defined here in user source Linux blah blah blah blah blah.
include Linux kernel driver We'll see how this will compile in the end. But so far, now with kern levels open we can at least find out something about the lock levels here.
So we are using kernel info here. It's an informational log. It just is the string six.
But then how the hell does the printk accept this string and this string without a comma in between them? This can only work if printk itself is some magic some black magic thing.
So let's search for black magic printk here. printk and I assume Oh.
I I just thought printk is defined as printf here. printf is just printk. What is that?
void printk maybe maybe there's something about that.
So okay.
I'm just going to accept printk for now.
Let's not question too much. I found that whenever you question things it's an incredible an incredible slowdown.
I think this documentation here said that printk was a macro and therefore you can expect some magic to happen.
>> [sighs] >> If the priority is less than console log level the message is printed on your current terminal. If both blah blah blah. Okay, that's boring. We don't need that. I want some kernel stuff to happen now.
What do we even expect to happen? Do we just expect a message to be printed to the console?
Compiling kernel modules. Oh.
Ah, right. Because if everything is correct now then this is a minimal kernel module.
And now there's a section on compiling kernel modules.
Kernel modules need to be compiled a bit differently from regular user space apps. Okay, so we can't use the the glorious GCC compiler.
We have to use something else. So former kernel versions required us to care much about these settings which are usually stored in makefiles although hierarchically hierarchically organized many redundant settings accumulated in sublevel makefiles and made them large and rather difficult to maintain.
Fortunately there's a new way of doing these things called Kbuild.
Oh.
And the build process for external loadable modules is now fully integrated into the standard kernel build mechanism. So as I said I suspected that there's a dedicated build mechanism for kernel modules. You can't just compile them yourself somehow or maybe probably you can compile them somehow yourselves but it you need to um account for many settings and flags and the likelihood that you mess up is incredibly big.
And maybe if you know exactly why then put it in the comments. If you know why kernel level modules need to be compiled using a kernel build mechanism. So what's the core difference between kernel level modules and user space apps?
That would be interesting to find out about.
But of course on the application level one runs in user space the other one runs in kernel space. Fine. But I mean from a software standpoint.
To me as a programmer both are just code running somewhere. There's no difference.
To learn more on how to compile modules which are not part of the official kernel such as all the examples you find in this guide see Linux documentation Kbuild modules.
So let's look at a simple makefile for compiling a module called hello.c.
This is a makefile right here. I have also never really used makefiles except in absolute emergencies and never without instruction.
I don't get them. So we're going to create a makefile for a basic kernel module. From a technical point of view just the first line is really necessary.
The all and clean targets were added for pure convenience.
Now you can compile the module by issuing the compile command make. You should obtain an output which resembles the following.
Okay.
But I find it interesting that the documentation says that we have to use the new approach Kbuild and then this makefile does not even use Kbuild or does it?
It uses it uses lib modules and the kernel module version probably.
/build. Is that Kbuild? I don't know.
Probably it is.
So now with this makefile copied I'm just I'm not going to go over it term by term word by word as with the previous C source file just because I really cannot contribute any meaningful knowledge about makefiles here. They are horrible to me.
So let's just copy it paste it here.
Maybe here CD into the driver into the kernel driver directory.
I think we should Am I located in the kernel driver directory?
Wait.
Yeah. Okay.
So now we have two files here.
driver.c and makefile and now according to this guide I can just do make.
Oh.
Okay. The kernel was built by GCC. Okay, so it used GCC under the hood.
The compiler differs from the one used to build the kernel.
The kernel was built by GCC.
Okay.
It created a file hello.o which is a compiled file. I don't see any file in the current directory so maybe it was put somewhere else now.
I think probably the file was moved somewhere because here it's leaving the directory.
Oh, then it's encountering an error and another error in line four.
And line 248 wait. That's not my makefile.
So is this now a success or is it an error? The last line indicates error which is not too great.
Note that kernel 2.6 introduces a new file naming convention. Kernel modules now have a.ko extension in place of the old.o extension which easily distinguishes them from conventional object files. Okay, the reason for this is that they contain an additional modinfo section at where it that where additional information about the module is kept.
Okay, but Okay, I assume for me the compilation just didn't work because there's an error here. So what's the error?
The documentation said that the all section is not even necessary but I can't imagine this makefile doing anything meaningful without the all section. And the all section is I think what causes the error because it says makefile colon four which is this line right here which is erroneous.
So what's wrong?
Leaving directory.
Hm.
No rule to make target hello.1.o.
Ah.
Of course the source file has the wrong name. I just copied this makefile blindly and hello.1.o should maybe be driver.o because my source file is called driver.c.
And the error message is complaining about no source to create no rule to make target hello. 1.o needed by Ah, no maybe that's not an error. But it's definitely one thing that would surprise me here. Um I will just retry.
I don't think that's the exact error but it also didn't look too healthy.
Now something else happened.
modpost simvers missing module license in driver.o. Did we create a file? Now something happened. Okay, now something happened guys. We have the C file which we created. Then we have a dot mod file, a dot O file, and a modules.order file. There's still an error.
What's the error?
But did it not compile? Well, because I'm encountering an error here, but I still have a compiled object file here.
And maybe since Mhm. Since I have a kernel that is higher release than 2.6, I have to call them file.ko instead.
>> [snorts] >> I don't trust this thing.
So, let's have a look at the make file in in a bit more detail because it just a couple of minutes ago it proved to be the source of the first issue.
So, we make -C. Okay, we probably provide a file to another make file, which is this build thing.
And we pass the current directory into it.
Ah.
driver.o Or maybe it's this a It would be a good idea to uh >> [groaning] >> to search for an error. So, the first error that we get here is error on modpost missing module license in driver.o.
So, I'm going to search for error modpost.
Error one.
Okay, let's search for this error, guys.
Here.
My Linux kernel driver contains module license, but during compilation following warnings printed blah blah blah. This makefile was incorrect. The module name was equal the source file name.
The following works. mymodule.o before mymodule.o my module why?
So, it has to do with naming, maybe.
>> [sighs and gasps] >> I had to add module license at the end of my module.c file and it worked.
See also this GitHub issue.
That's interesting. So, this seems to be an issue quite a few people seem to have.
Although it's still quite opaque to me what the nature of this of this issue is.
Missing module license in driver.o.
I didn't know that the source file should have the same name specified by object M.
After renaming the file to helloaxso.c, everything worked. Okay, we also had this regarding the make file.
Uh Of course, now the make all requires sudo to install the module. Do we even install here?
What if I compile this with sudo now?
Is that the issue, maybe?
sudo make This one looks different again.
bin shell flex not found No such file or directory.
But indeed it it uses the user source Linux headers blah blah blah directory.
It tries to, at least.
And it doesn't find files. Deleted file here.
Interesting.
It's interesting how opaque problems look if you have no idea what they are.
The error shouldn't matter. Just do this, this, this. The error shouldn't matter.
Okay, wait. The error shouldn't matter.
That's a good indicator. So, I have a a compiled object file. Good. The error shouldn't matter. What if the error just What if we can just ignore them the error?
So, if we just ignore the error, then I have a second to have a sip of water.
And then lots of useful information you see here.
Where Where do we see useful information?
We see useful information if we do make and then modinfo. Ah.
Wait. modinfo driver.o Why does this guy Why is he able to do modinfo on his compiled file and I'm not?
Maybe it's not called driver.ko in the kernel. No.
modinfo modinfo modinfo help. What does this do?
man modinfo Show information about a Linux kernel module. modinfo extracts information from the Linux kernel modules given on the command line.
If the module name is not a file name, then the directory searched.
>> [gasps] >> So, I can just specify a file name.
And the file is available here at driver.o.
So, modinfo driver.o should work.
driver.o not found. Are you stupid? It's in this directory. I just gave you the path.
>> [sighs] >> Now you can compile the module by issuing the command make.
You should obtain an output which resembles the following.
But I don't have a KO file. So, maybe that's maybe that's an effect of the error that I don't get a.ko file.
Why do I get a different output every time I execute this?
>> [laughter] >> So, flex not found flex not found kernel module flex not found kernel kernel module flex not found Open a terminal control alt T sudo I into blah blah blah. Delete its contents.
No.
What is flex?
sudo apt install flex. If it's not found, I'm just going to install it, whatever it is.
And now I can probably install it.
bison not found. What What What are those programs?
Okay, then let's install bison.
>> [laughter] >> What's not Now it's doing something.
Oh, it's What the hell was that? I just installed things that sounded like I need to install them.
So, what do I need to do now?
proxy execution Yes, no?
I have no idea what that is. Restarting configuration. I'm just going to hit yes.
Um And now I get another error. Okay.
So, it's the rule of confidence. As soon as you get confident about anything in programming, you get an error.
And that's because I just sensed a hint, just a shade of joy because it looked like it's compiling here.
The second the universe sensed that I was joyful, it threw me an error.
So now, no rule to make target awk as needed by include generated ASM CPU feature masks. Stop.
I make What's even the point of kernel programming? Well, this is And now I do sudo make and the the error is the log is again different.
CPU feature masks AWK I'm just going to search for this error here.
Do I Wait, I didn't even check if I need to install any dependencies before compiling the kernel. Which dependencies for or which requirements for compiling kernel module in Linux?
Build essential, okay. Kernel headers, okay, I do have them now.
Makefile No, all of this looks okay.
Compile and make to generate the KO file.
And then we insert the module.
Uh You need build essential and kernel headers.
I have them all.
If I do make, it doesn't work. No rule to make target.
Well, then just compile without a rule.
I mean what? I mean it's it's literally just a single printed message from the from the kernel module.
>> [snorts] >> So, let's check here.
In the makefile, just change subdirectories into shell PWD.
Why?
So, here it's trying to access the current directory.
PWD is the current directory that the makefile is executed from, that's my understanding.
>> [gasps] >> Maybe we have to replace it, but then why? Why doesn't it work?
Works like a charm. Okay, I'm just going to replace it with PWD.
So, here shell PWD.
Shell PWD, is that correct?
It is correct, then let's try to sudo make.
Error.
Missing module license in driver.o. So, we're back to the first error that I thought it was solved by compiling with sudo make.
Missing module license.
You need to add a call to module license to your source file hello.c in your case. This is what the error message is talking about. Why does it need this license thing?
Okay, I'm just going to do it.
Not question things. Don't question stuff.
Just do what the kernel people are telling you to do. Be a slave to the kernel people.
No wonder that if you're a kernel programmer, you are so unhappy in your life.
Um Regular programmers are much happier.
Uh So, where do I put it? Where do I put this um module license thing?
Maybe up here.
Module license GPL No idea what that license is. No idea what anything is. What is GPL license?
GPL license Most widely used free software license.
Can I just give it my own license? Let's try and see.
And this worked. Okay, so with a Daniel license solved it. So, guys, this is the official I'm the official kernel compilation authority. Just put module license Daniel in your code. Another string won't work, by the way. It's only Daniel that works.
And then you do Now we have a couple of new files here, and now we can use modinfo to find out what driver.ko means or is. So, aha.
This is a file. It has a license.
I love the license. So, Daniel's magic license. And by the way, I'm putting arbitrary strings in here to show that this this is meaningful meaningless information. It's just some string that someone wanted to be filled, but it's not technically necessary to do anything.
Module description. Ah, I can also put a description there. So, let's also put a description.
Interesting that the license is not mandatory.
This is the most elaborate kernel driver that has ever been devised.
Pure art.
Okay, and now if we compile this most elaborate kernel level driver or kernel driver, we get the file driver.ko, which is the kernel object file. We do a modinfo driver.ko, and now we see the license here.
Daniel's magic license and the another license. Oh, that's because I messed up and put two module license in here. Of course, that's wrong. It needs to be module description.
Description. That is a crazy This one is a crazy episode, by the way, because I'm really I'm only airing forward, and I can already smell the comments again.
Um from from some guys um complaining that I just try out stuff, and um I have no clue what I'm doing, and I'm just creating errors and looking up stuff, and then uh and it's horrible to watch me, but this is how I learn. And I just record videos about this stuff.
This is how I started. The last couple of videos were more um edited.
I tried to make them a bit faster, a bit more um adapted to the algorithm, but it really sucks the joy out of video production for me. For me, I enjoy making videos if I can just hit record and do something here and learn something, because I'm not a teacher. Right, I'm or I never intended this channel like a as a tutorial channel. I just intended it to show the stuff I'm doing and to share my stupidity with the world.
And I really mean it. I just wanted to show how you can do stuff with computers, how I do stuff with computers, and how you can look up things, and maybe along the way learn stuff.
And if it takes some time, fine. If you have no idea what the errors mean, fine.
If something interests you, if if there's a topic that interests you, and that sparks some some curiosity in you, then then you can go and read some theory and read books about that topic. But first, try to put your toes in dip your toes in all the different topics and find out a bit of about of all of them. And today we found out about the about the module API or even ABI here about the specific way kernel modules are compiled, about these licensing things there, these flags that need to be added. So, we kind of already built some basic knowledge, and the next time we we record something about kernel drivers, we're already better prepared. So, there's always value in airing forward.
So, now let's check the module info and now there's this module here. This is the license, Daniel's magic license. This is the description that I just put in. This is the file name.
Source version, okay, some hash probably.
It's a driver.
And this right here is the kernel it was compiled against probably.
And now we can just close all these error investigation tabs and reopen the hello world kernel module guide. Let's skip forward to the compilation which we have now succeeded in. And now I think we can just load this kernel module somewhere. Wait.
How is it loaded?
Init module, no.
Wait.
So, here.
Ah. [sighs] Wait, wait, wait. Let's have a look at what what else needs to be done here.
So, here this guy does some some things. Again, no idea what those are.
Let's skip forward. I want to find out how to how to load these kernel modules and where I can see anything that I did using them. Building modules for a precompiled kernel, blah, blah, blah, blah.
Modules, programs.
So much stuff here going on.
Okay. How to load a kernel module in Linux.
So, [snorts] here.
Blog posts. Blog posts are always the most horrible source for anything.
But sometimes you can find something valuable.
Usually they are not very helpful.
How can I load kernel modules on boot?
No.
A load.ko file.
To load a.ko kernel object file in Linux, use sudo insmod for a specific file path or sudo modprobe to automatically handle dependencies.
Verify loading with lsmod and remove with rmmod.
Okay.
So, now we can check using lsmod the active kernel modules. lsmod.
There are a couple.
And so, these are the kernel level modules that are running currently.
Now, if I do sudo insmod driver.o invalid Ah, driver.ko of course. Invalid module format. Why?
insmod file.ko >> [snorts] >> invalid [clears throat] module format insmod invalid module format. What's the error here?
Kernel from which you built your kernel module and to which you are inserting module should be the same version. If you do not want to take care of this thing, you can use the following makefile.
Well, that's exactly what I did.
Or is it not?
Now you can build and try to insert your module.
You did everything correct, but you did not boot your system with the kernel you compile compiled. So, the first step you is you should boot with it. If you are using Ubuntu, you can hold shift button at the same time at the time of booting and you will be given a list of compiled kernels in your system.
From there, select Linux source, blah, blah, blah, and then try to build your module and install it with your way.
What?
I'm just going to copy this sudo apt install thing here.
Let's see if this helps me. 500 megabytes.
What am I What am I even installing here? Linux headers?
I don't even recall if I really changed my kernel at some point. It's possible that I at some point changed it.
But overall, um I'm surprised it doesn't work.
insmod invalid module format Module and kernel are not the same version. Sometimes you can get rid of this error if you didn't use the same compiler.
Well, What have What have I done done wrong?
In the makefile, I literally use uname -r to find the right version.
And that's what's used to compile. So, what?
Where did I Where did I go wrong?
In your case, you recompiled the kernel and you didn't change rebuild initrd.
Hm.
Invalid module format error.
It means that the file you are trying to load is not a valid kernel module.
Either it never was or it has been corrupted or possibly it is for an architecture other than the one you have.
Yeah, but I'm using this makefile.
And it's using uname -r to find the current kernel version.
uname -r Kernel system information. Let's check here.
Yeah, Linux kernel 6.17 here. Generic.
>> [sighs] [snorts] [snorts] >> Maybe after this refreshing here it works.
Wait.
What is Is this removing my graphics drivers?
Nvidia?
Oh, please don't. Please.
Please, I just set up everything to work so nicely.
Invalid module format when running a cross-compiled module. Yeah, but I'm I haven't cross-compiled.
I have not cross-compiled.
I'm just running. I have created the code on my machine. I've created the make makefile on my machine. I'm using uname -r to find the right kernel version.
>> Yes, this path may be wrong. Let's check if it even exists.
lib modules CT lib modules And there they are.
And there are a couple of kernel kernel versions here.
But why? Why are there a couple of kernel versions and why were they all refreshed right now?
Am I right now changing my kernel version somehow? Building initial module for 617022 now.
>> [laughter] >> What the hell did I just invoke?
Yeah.
Let me try to find out what I invoked.
Here.
apt upgrade apt remove Linux headers auto remove sudo apt auto clean. Okay, apt install Linux headers. It's just headers.
So, it should be fine.
This will reinstall the kernel headers.
I tried to reinstall the headers. It didn't work, but purge is the key. I guess it worked.
auto remove Go.
Fast fast fast, let's go. Be faster. I'm in the middle of a recording and must be interesting at all times. I must serve the algorithm. I must retain everybody.
>> [snorts] >> So, nice weather today, right?
I hope you also have nice weather.
For me, it's quite sunny.
And now it's installed. Perfect.
Um So, we can now list the modules. We can probably sudo make. Now we get another error. Kernel driver modules, no such file or directory.
uname -r returns this.
Maybe I need to create a new tab here.
CD into the kernel driver then sudo make.
Error.
No such file or directory.
But why Okay, so this apparently means that auto remove somehow removed the kernel that I'm not a and some Wait, I I don't even get what's going on here.
So, auto remove removed or purged removed this directory.
>> [gasps] >> And now I don't have it anymore, but which directories do I even have?
lib modules If I list them there it is.
617020 So, this should exist or the file under it doesn't exist.
022 generic No, 20. It was 20 generic.
And there is no built in here. What if we search for built?
68100 10 built So, there's only one built file, but we're searching for other Wait, we're searching for another built file.
Why does Wait.
kernel install built tools Yeah, kernel header header files. Okay, fine.
Linux headers uname -r Okay, install.
And now I have the right built tool.
Now it's here.
I don't know why it only installed this one before.
>> [snorts] >> But my system definitely uses the 617 kernel.
Oh my god, this really drives one crazy.
And now if I do sudo make did it work?
It maybe it worked, yes. And then I do modinfo driver.ko.
This worked.
And now I can do insmod mod driver.ko.
Could not insert module driver.ko.
insmod help kmod version 31 >> [sighs] >> insmod >> [snorts] >> Could not insert module driver.ko.
Could not insert module driver.
module Could not insert module.
You need to disable secure boot.
Run the insmod command via sudo. Okay, I'm doing that.
You need to disable secure boot.
Alternative ways to disable secure boot using mokutil.
>> [sighs] >> This is ridiculous, guys. So, how do I create a signed driver now? Do I need to sign insmod sign kernel module.
Disable secure boot. Everybody says disable secure boot.
Insert kernel module without secure boot.
Generate a machine owner key pair.
>> Oh gosh.
So in any case, I need to reboot.
But I don't get a signature error. I just get this invalid module format. So I don't think Maybe I'm just Maybe I've just looked too far.
So it's the format that is an issue, not the key or a signature.
I can inspect the output of dmesg after a failed insmod.
So let's again try to do this and then dmesg.
API mismatch. The client NVIDIA settings has the version this, but this kernel module has the version this.
This is not related.
Invalid relocation target.
Invalid relocation target.
I'm going to do a restart and see if it works afterward. I have the feeling I just need to do a restart for some reason.
So after I think four or five restarts dealing with some kernel versions, I discovered the following. Well, the first restart I did After the first restart I did, let me just show you what the version of my kernel suddenly was. It was suddenly 6.17.0-22.
So suddenly the version of my kernel changed. That's probably because I installed something some kernel header or I don't know what that changed the kernel version and that's what yielded the results the error that we saw. We compiled with this with the old -20 version, but under the hood somehow my kernel driver was my kernel release was already replaced and that's what caused the error.
So now if I do uh if I do Here I used Claude to help me figure out all the driver issues. Let me close that. I don't need that anymore.
So now we have the driver. It's compiled.
However, it's compiled against an old kernel version. So we have to redo the release like this. Now I entered the wrong password. Let's do it again.
Now this looks good. Perfect.
I don't see any error here.
If I now do sudo ins mod kernel uh driver.ko it works.
Okay. And now what was the remaining procedure? Now the the kernel module is somehow installed.
insmod. Let me just Let me just try to find it. Um What did we search for earlier?
insmod kernel module.
Compile and insert kernel module.
How do I How to load a kernel module? Right, that's what I searched for.
And then in this guide did I find anything useful?
All right, we encountered the ls mod command, which we also used. By the way, the all the restarting and all the figuring out of all the versioning and driver issues and it took forever. So that's why I've already forgot everything again. Uh even OBS, the recording software that I'm using right now didn't work because of NVIDIA driver changes and I had to reinstall the the graphics driver and then my monitor didn't work and then I restarted again and then I installed a different driver and then it didn't work yet again and then I had to again reinstall everything. I It was horrible.
So I'm glad my computer is still running.
So lsmod.
Is my driver now running?
Do I find it anywhere here?
It's just called driver, right? My kernel It could be my module. The size is 12,288 bytes. Does it make sense? Is it that one?
Oh no, I don't know. No, no, no. Maybe it's not it.
Although it is called driver.c, so I would expect it to be that.
What if I use modinfo on the driver?
driver.ko The name is driver. So it might be It might be that.
So it might If I do this lsmod grep driver, then that's the only one.
But how do I now see because the magic thing that we did here was when we initialized the kernel module, we printed something.
But where do I see that log?
modprobe Mm.
No.
etsy modules There needs to be something somewhere.
So see kernel module logs.
How to view Linux kernel logs?
Maybe that's it.
dmesg dmesg And there it is, guys.
>> [laughter] >> Yes.
There it is. Hello Daniel.
Ah, that's So we really loaded our own driver. It doesn't do anything. It's completely useless, but we managed to do it. We managed to load our own driver.
So here really fancy elaborate drivers do their thing, WireGuard, USB drivers, fancy things and then there's the real end boss driver that prints hello Daniel.
>> [laughter] >> This is awesome. Nice. And then probably to trigger the other call. So what we're seeing right now is this init module call right here. And now I want to see the cleanup module call, which prints bye-bye, love you because it's such an empathic, loving driver. So let's see how do we do it? Unload kernel module.
rmmod. Okay. sudo rmmod and then Do I just pass the module name? Okay, driver.
By the way, driver is a really, really risky module name. It can conflict so easily.
Let's try and see.
I removed it and if I look at the kernel messages again, bye-bye, love you. Ah. [sighs and gasps] Guys, this was so awesome. This was a horrible episode um for my stress levels because >> [laughter] >> because of all the restarts and all the versioning conflicts and everything and under the hood or actually it should all be so simple. It's just this piece of C code and we load it as a kernel module, but the compilation and everything was ridiculously hard for me because I'm I like to mess things up.
Um but it was fun. It's It's awesome to see the the module being active and loaded inside the kernel.
That's awesome. That's the first time I've ever done this. So please excuse me for all the difficulties encountered in this episode.
If you want to learn >> [laughter] >> It's funny to make an advertisement now.
Um if you want to learn C programming the way I do it, not this type of C coding, but user space programming, graphics, all the useful programs, um then have a look at the No Nonsense C Programming Course.
It's a course that I created. It's linked in the video description below the video.
It teaches you C from a very practical perspective and teaches you some raylib basics, how to deal with libraries, how to compile user programs, so real programs that you will use in the end.
And then a couple of mental models that will really benefit you in the long run as an engineer.
Uh and they are mental models about how I think as a programmer to solve problems at etc. All right.
Let's not talk too much. I hope you have a good rest of the day.
And I hope you spark the discussion in the comments.
And tell me everything where I went wrong, how I could make things easier, and maybe now because this hurdle is overcome, what's the next thing that I can do?
The next kernel level thing that I can do or driver. Can I do something else here now with this as a basic framework?
Because now I know how to how to write a very very very basic kernel module and compile it and load it.
So all the all the formalities are overcome, but now we can do the interesting stuff. So put put any ideas in the comments.
Thanks, and see you next time. Bye-bye.
Related Videos
U.S. Military Just Flexed The Most Dangerous Aircraft Ever Built The F-47
MaxAfterburnerusa
11K views•2026-05-29
Heating Staying On On The Hottest Day Of The Year
PlumbLikeTom
507 views•2026-05-29
발전 효율을 높이는 태양광 추적 시스템의 기술적 원리 #공학 #공정 #태양광 #알고리즘 #재생에너지
찐현장기술
2K views•2026-05-29
Peterborough to Newark Northgate Driver's Eye View aboard an InterCity 225 - East Coast Main Line
TrainsTrainsTrains
822 views•2026-05-31
AI turbine design: hypersonic cooling leap #shorts #ai #hypersonic
bobbby_rn
671 views•2026-05-31
직관 및 곡관 배관 결합 고정 작업 #worker #process #fabrication #pipework #clamp
월드촌촌
2K views•2026-05-30
How Far Can A Tomahawk Missile Actually Travel?
WarCurious
13K views•2026-05-28
Wire To Wire Connection Trick | Strong And Secure Electrical Joint #shortvideo #wireworks
ElectricianTips-b1h
5K views•2026-06-02











