Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Kernel-driver

dd8c908ed08c431031fde66aaece33ac

References

https://tldp.org/LDP/lkmpg/2.6/html/index.html
https://youtu.be/2FlFl1h2n0E?si=RWe1SKxzHmzwtVVQ

What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.
kernel mode

You can see what modules are already loaded into the kernel by running lsmod, which gets its information by reading the file /proc/modules.
lsmod

Kernel modules must have at least two functions: a "start" (initialization) function called init_module() which is called when the module is insmoded into the kernel, and an "end" (cleanup) function called cleanup_module() which is called just before it is rmmoded.

Typically, init_module() either registers a handler for something with the kernel, or it replaces one of the kernel functions with its own code (usually code to do something and then call the original function). The cleanup_module() function is supposed to undo whatever init_module() did, so the module can be unloaded safely.

As of Linux 2.4, you can rename the init and cleanup functions of your modules; they no longer have to be called init_module() and cleanup_module() respectively. This is done with the module_init() and module_exit() macros. These macros are defined in linux/init.h. The only caveat is that your init and cleanup functions must be defined before calling the macros, otherwise you'll get compilation errors.

printk() is alogging mechanism for the kernel, and is used to log information or give warnings. Therefore, each printk() statement comes with a priority. There are 8 priorities and the kernel has macros for them. If you don't specify a priority level, the default priority, DEFAULT_MESSAGE_LOGLEVEL, will be used.

syslog

kern levels
##
Compile with Makefile settings
make
This generates .ko : the kernel module.
Load: sudo insmod (module).ko
Unload: sudo rmmod (module)
Check the log: dmesg
##
Congratulations!

About

A minimal Linux Loadable Kernel Module (LKM) written in C, demonstrating module init/cleanup lifecycle, printk logging with kernel log levels, and build/load/unload workflow using insmod/rmmod.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages