- Open Terminal
- Input: mkdir moduletest, create a folder where files are placed
- Go to folder, CD Moduletest
- Create file: Nano hello.c, file contents are as follows:
#include <linux/init.h>#include <linux/module.h>MODULE_LICENSE("Dual BSD/GPL");staticint hello_init(void){ printk(KERN_ALERT"Hello, xiaoming\n"); return0;}staticvoid hello_exit(void){ printk(KERN_ALERT"Goodbye, xiaoming\n");}module_init(hello_init);module_exit(hello_exit);
- Create Makefile file: Nano Makefile, code as follows:
obj-m := hello.oKERNELBUILD :=/usr/src/linux-headers-3.11.0-12-genericdefault: make -C $(KERNELBUILD) M=$(shell pwd) modulesclean: rm -rf *.o *.ko *.mod.c .*.cmd.tmp_versions *.order *.symvers
The path inside is different from the Linux version, you can go to the corresponding path to see
Compile, enter make, and compile the process:
Insert module: sudo insmod Hello.ko
Delete module: sudo rmmod Hello.ko
View Results: DMESG | grep "Xiaoming"
DMESG viewing system kernel module information
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Linux operating system" Linux kernel insert unload module