From: http://hi.baidu.com/liu_bin0101/blog/item/02ff1afc043443f5fd037f95.html
2.6.xx kernel is very different from 2.4.xx kernel programming. The following text briefly describes how to implement the kernel module (Hello World) in 2.6.xx.
Environment: Ubuntu 704
Kernel: 2.6.20-15-generic, which can be viewed through uname-R
GCC version: 4.1.2
I. Preparations
Required development library for installing Kernel
# Sudo apt-Get install linux-kernel-devel
Install the kernel header file
# Sudo apt-Get install linux-headers-'uname-R'
Of course, tools such as gcc/make are inherently needed.
2. Write the code hello. C.
As follows:
// ------------------ Hello. c -------------------//
# Ifndef _ KERNEL __
# DEFINE _ KERNEL __
# Endif
# Ifndef Module
# Define Module
# Endif
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/kernel. h>
Static int hello_init (void)
{
Printk (kern_alert "Hello world! /N ");
Return 0;
}
Static void hello_exit (void)
{
Printk (kern_alert "bye world! /N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Module_license ("GPL ");
Module_author ("wfj ");
// ------------------- End of Hello. c -------------------//
3. Compile the MAKEFILE file and put it in the same directory as hello. C.
OBJ-M: = Hello. o
Kernelbuild: =/lib/modules/'uname-R'/build
Default:
Make-C $ (kernelbuild) M = $ (shell PWD) Modules
Clean:
Rm-RF *. O *. Ko *. Mod. C. *. cmd. tmp_versions
(Note the required tab in makefile)
Iv. Compilation Module
# Sudo make
Compilation Module
At this time, there will be hello. KO in the folder where hello. C is located. This is the kernel module we need, haha!
# Sudo make clean
Clean up the compilation garbage, and hello. Ko will also be cleared.
4. Insert a module to make it work
# Sudo insmod./Hello. Ko
We can use dmesg to see the generated kernel information, hello World!
# Sudo rmmod./Hello
Use dmesg to view bye world!
This is the simplest module compilation process in 2.6.xx.