Article title: The simplest Linux2.6 kernel driver module. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
/* Hello. c */
# Include /* Needed by all modules */
# Include /* Needed for KERN_ALERT */
# Include /* Needed for the module-macros */
Static int _ init hello_init (void) // Module entry function specified by module_init ()
{
Printk (KERN_ALERT "Hello, world! \ N ");
Return 0;
}
Static void _ exit hello_exit (void) // Module exit function specified by module_exit ()
{
Printk (KERN_ALERT "Goodbye, cruel world! \ N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
MODULE_LICENSE ("Dual BSD/GPL"); // shocould always exist or you'll get a warning
MODULE_AUTHOR ("BENSON"); // optional
MODULE_DESCRIPTION ("STUDY_MODULE"); // optional
/* Makefile */
# Makefile 2.6
Obj-m + = hello. o
KDIR: =/lib/modules/$ (shell uname-r)/build
# PWD = $ (shell pwd)
All:
Make-C $ (KDIR) M = $ (PWD) modules
Clean:
Make-C $ (KDIR) M = $ (PWD) clean
Obj-m: = hello. o indicates that the hello. o module is generated after compilation.
$ (KDIR) specifies the path of the kernel source code. "M =" indicates that this is an external module, and M = $ (PWD) specifies the path of the module file.
Note: makefile predefines the $ (PWD) variable. you do not need to define it again here.
Run # make
Load Module
# Insmod hello. ko
# Lsmod outputs the kernel loaded module information. you can view the loaded hello module.
......
Module Size Used
Hello 5632 0
You can view the module loading information in the log.
# Vi/var/log/messages
......
Sep 27 13:25:21 localhost kernel: Hello, world!
Uninstall module
# Rmmod hello. ko
# Lsmod found that the hello module has been uninstalled
View log information
# Vi/var/log/messages ......
............
Sep 27 13:26:58 localhost kernel: Goodbye, cruel world!