Ubuntu上测试linux内核模块加载、卸载

1. 查看内核版本:


root@ubuntu:/usr/src# uname -a
Linux ubuntu 4.4.0-21-generic #37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux




2. 下载源码:
从https://www.kernel.org/pub/linux/kernel/


root@ubuntu:~/hello_module# cd /usr/src/
root@ubuntu:/usr/src# ls
linux-4.4.1  linux-4.4.1.tar.gz  linux-headers-4.4.0-21  linux-headers-4.4.0-21-generic


解压后进入目录,分别执行
make oldconfig
make prepare
make scripts


其中报错:
scripts/sign-file.c:23:30: fatal error: openssl/opensslv.h: No such file or directory


sudo apt-get install libssl-dev
说明:我这里是没有网络的,替换了阿里源。


3. 建立文件夹、文件:
hello.c
Makefile


4. 编译文件:
make


5. 加载模块、卸载模块:
root@ubuntu:~/hello_module# insmod hello.ko
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# dmesg | tail -1
[ 2143.690605] Hello word
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# rmmod hello
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# 
root@ubuntu:~/hello_module# dmesg | tail -1
[ 2154.473897] Goodbye world
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
 
static int hello_init(void)
{
	printk("Hello word\n");
	return 0;
}
static void hello_exit(void)
{
	printk("Goodbye world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

ifneq ($(KERNELRELEASE),)
	obj-m:=hello.o
else
	PWD:=$(shell pwd)
KDIR:=/lib/modules/$(shell uname -r)/build
all:
	$(MAKE) -C $(KDIR) M=$(PWD)
clean:
	rm -rf *.o *.mod.c *.ko *.symvers *.order *.markers
endif 

猜你喜欢

转载自blog.csdn.net/zzb5233/article/details/80019336