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

前端之家收集整理的这篇文章主要介绍了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
  1. #include<linux/module.h>
  2. #include<linux/kernel.h>
  3. #include<linux/init.h>
  4. static int hello_init(void)
  5. {
  6. printk("Hello word\n");
  7. return 0;
  8. }
  9. static void hello_exit(void)
  10. {
  11. printk("Goodbye world\n");
  12. }
  13. module_init(hello_init);
  14. module_exit(hello_exit);
  15.  
  16. MODULE_LICENSE("GPL");

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

猜你在找的Ubuntu相关文章