Skip to content
Search

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

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.

$ export KERN_DIR=/usr/src/kernels/`uname -r`
$ mkdir ~/module_01
$ cd module_01

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.)

obj-m := module_01.o
KDIR := $(KERN_DIR)

all:
    make -C $(KDIR) M=$(PWD) modules

Then build as follows.

$ make

If a permission-related error occurs, run make with root privileges.

Once the module_01.ko file is finally generated,

  • add the module using insmod,
  • check the added module using lsmod,
  • and remove the module using rmmod.

Log messages printed with printk can be checked using dmesg.

See also

Favorite site

References


  1. Joinc-Linux_kernel_module_programming-2.4.pdf ↩

  2. KLDPWiki-Kernel_Module_Programming_Guide.pdf ↩

  3. Module_programming_for_linux_kernel.7z ↩

  4. The_Linux_Kernel_Module_Programming_Guide.pdf ↩

author Zer0 sitemap