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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值