Linux:Kernel:Module
lastmod 2025-12-16created 2015-02-1416 edits
What exactly is a kernel module? A module is code that can be loaded into or unloaded from the kernel on demand. It makes it possible to extend the kernel's functionality without rebooting 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 rebuild a monolithic kernel and add new functionality directly into the kernel image. With a large kernel, this has the drawback that we must rebuild and reboot the kernel every time we want to add new functionality.
Categories
Simple example
- Learning device drivers with a target board! (2) - Building a basic module
- Writing device drivers - Module programming
- Writing device drivers - printk and kernel version information
The example below was tested on CentOS7 with kernel version 3.10.0-123.20.1.el7.x86_64. First, proceed as follows. If a permission problem occurs with KERN_DIR, adjust it with chmod, chown, etc.
Create the module_01.c file.
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
int __init init_hello(void)
{
printk(KERN_ALERT "Hello world.\n");
return 0;
}
void __exit exit_hello(void)
{
printk(KERN_ALERT "Good-bye\n");
}
module_init(init_hello);
module_exit(exit_hello);
MODULE_LICENSE("GPL");
Create the Makefile file. (The clean target is omitted.)
Then build as follows.
If a permission-related error occurs, run make with root privileges.
Once the module_01.ko file is finally generated,
Log messages printed with printk can be checked using dmesg.
See also
Favorite site
- Wikipedia (en) Loadable kernel module (LKM)
- Linux Kernel Module Programming - 2.4 1
- Kernel Module Programming Guide 2
- Analyzing the Linux kernel with module programs - 01 3
- Writing device drivers - Makefile caveats
- The Linux Kernel Module Programming Guide 4
- obj-y ? obj-m ? obj-$(CONFIG_TEST) ?
- Mr.Rootable :: Linux basics every hacker should know-11 : Loading, removing, etc. kernel modules (Kernel Module)
- [Recommended] Simple Linux kernel module example and how to build it
- [Recommended] Linux Kernel Module Programming: Hello World Program