When we learn module programming, the first small program must be hello, kernel!. For a novice, how do we avoid some mistakes and how to fix the bugs we have when writing the first module program? Is there any example we can refer to? Here is one example.
1. Write the hello.c
01 |
#include <linux/init.h> |
02 |
#include <linux/module.h> |
03 |
#include <linux/kernel.h> |
06 |
MODULE_LICENSE( "GPL" ); |
08 |
static int hello_init( void ) |
10 |
printk(KERN_ALERT "hello,I am edsionte\n" ); |
14 |
static void hello_exit( void ) |
16 |
printk(KERN_ALERT "goodbye,kernel\n" ); |
19 |
module_init(hello_init); |
20 |
module_exit(hello_exit); |
22 |
MODULE_AUTHOR( "edsionte Wu" ); |
23 |
MODULE_DESCRIPTION( "This is a simple example!\n" ); |
24 |
MODULE_ALIAS( "A simplest example" ); |
Usually in a module program, module load, module unload and module license declaration are necessary. But the module description, author or alias are optional.
After we write the module load function, we still need to use module_init(mode_name) to register the this function. Because when we use insmod to load the module, the kernel will automatically search and execute the kernel load function and do some initialization. Similarly when we use rmmod command, the kernel will automatically search and execute kernel unload function.
Please note the printk() function here, it can be simply understood as the kernel's printf(). You may make type it wrongly the first time when you use it.
2. Edit makefile
Below is the makefile:
03 |
CURRENT_PATH:=$(shell pwd) |
04 |
#the current kernel version number |
05 |
LINUX_KERNEL:=$(shell uname -r) |
07 |
LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL) |
10 |
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules |
13 |
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean |
The first line specifies the file to be compiled. In fact it's enough to just have this line in a makefile. But if we just have this line, we then need to type some other commands manually every time, so we'd better add some commands we want to execute every time.
We first get the current relative path(you can type pwd in the terminal to test this command), then we need to get the version number of the kernel, this will help us get the absolute path of the current kernel. You can type the path of the current kernel manually as wee, but this is not portable. If the current kernel version is different from the version number in the file, then you have to modify it.
Here you may see some $(variable name), this is a reference to the variable in the bracket. For the detailed syntax, you can refer makefile syntax.
3. make
After creating above two files, you can run make command in the current directory. It will generate a new hello.ko file which is the module target file.
4. insmod,rmmod and dmesg
insmod adds the module we write into the kernel, usually we need to run it with sudo. rmmod will remove the module. Sometimes we may want to display some notifications when load and unload the module, we can use dmesg to check.
This program is tested under Ubuntu. In other Linux system, you may need to modify the LINUX_KERNEL_PATH in the makefile accordingly.
It's simple, right? Go and have a try.
Source : http://edsionte.com/techblog/archives/1336