The entire process of making the hello. Ko kernel module in Linux
1. Linux system is used redflag 6.0 SP1: ftp://ftp.redflag-linux.com/pub/redflag/dt6sp1/SP1/redflag-6-sp1.iso, the system installation is very easy, the installation prompt is good.
Used kernel source code directory tree: Kernel.
Mount method: Mount-T iso9660 redflag-6-tool-sp1-src1.iso/mnt/-o loop
Kernel directory tree Installation Method: CD/mnt/redflag/srmps/
Rpm-I kernel-2.6.23.1-4.src.rpm
3. Write the hello module code. The source code is as follows:
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("GPL ");
Static int hello_init (void)
{
Printk (kern_alert "Hello, world \ n ");
Return 0;
}
Static void hello_exit (void)
{
Printk (kern_alert "Goodbye, cruel world \ n ");
}
Module_init (hello_init );
Module_exit (hello_exit );
4. Compile the MAKEFILE file of the hello module. The makefile content is as follows:
# Makefile 2.6
OBJ-M: = Hello. o
Kernel: =/usr/src/kernels/2.6.23.1-4-i686/
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kernel) M = $ (PWD) Modules
. Phoney: clean
Clean:
Rm-f *. O *. Ko
5. Compilation Module
Run the make command in the "Hello. c" folder to compile the hello module. Use the LS command to view the hello. Ko file, which is our custom kernel module.
6. Install the hello Module
Run the following command on the command line: insmod hello. Ko. Run CAT/var/log/messages
The following message is displayed: "Aug 6 13:37:59 localhost kernel: Hello, world", indicating that the module has been loaded successfully.
7. Compile makefile of another module
# If kernelrelease is defined, we 've been invoked from
# Kernel build system and can use its language.
Ifneq ($ (kernelrelease ),)
OBJ-M: = Hello. o
# Otherwise we were called directly from the command
# Line; invoke the kernel build system.
Else
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
Default:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules
Endif
7. Uninstall the hello Module
Run rmmod hello. Ko on the command line. Run CAT/var/log/messages.
The following message is displayed: "Aug 6 13:40:36 localhost kernel: Goodbye, cruel world", indicating that the module is successfully uninstalled.
8. View module information
Run the following command on the command line: modinfo hello