Environment: Ubuntu 8.04
Kernel version: 2.6.32.59-debug
1. Writing Documents HELLO.C
#include <linux/init.h>
#include <linux/kernel.h>//PRINTK
/* Write kernel driver must load this header file, the role is to dynamically load the module into the kernel, commonly used macro definitions such as Module_licesence (), Module_author (), and so on in this file */
#include <linux/module.h>
Module_license ("Dual BSD/GPL"); Is the statement that must be selected, which is the license permission for the module, and if not declared, a warning message appears.
static int hello_init (void)
{
PRINTK ("Hello, world – this is the kernel speaking\n"); PRINTK: The difference from Printfde is that it runs in the kernel state and has a message-level mechanism
return 0;
}
static void Hello_exit (void)
{
PRINTK ("Short are the life of a kernel module\n");
}
Module_init (Hello_init); The entry point is no longer the main () function, but should be the Module_init (function name)
Module_exit (Hello_exit); The unload point of the module is specified as the Hello_exit function pointer
2, write Makefile
obj-m:=hello.o
Kernelbuild: =/lib/modules/2.6.32.59-debug//This makefile relies on the Linux kernel source path, if cross-compiling, take the code path running on the Development Board
All
Make-c $ (kernelbuild) m=$ (shell pwd) modules//to the directory where the Linux source is located executes the main makefile and the current path to the main makefile, telling the main makefile to return to the current directory after execution, Executive Makefile
Echo Insmod./hello.ko to turn it on
Clean
RM-RF *.o *.ko *.mod.c *order *.symvers. tmp_versions
3, in the HELLO.C directory make, so the current directory appears Hello.ko
4, Insmod Hello.ko
5. Dmesg-c View kernel information (generated by the fourth step)
6, Lsmod | grep "Hello" command can see Hello kernel module If fourth step is correct
7. Rmmod Hello//delete kernel module Hello.ko
8. Dmesg-c View the kernel information (generated by the seventh step).