编译 Ubuntu-20.04 驱动

文章目录

  • 1. hello.c
  • 2. test.c
  • 3. Makefile
  • 4. insmod & rmmod
  • 5. 可能的情况

内核信息:

uname -a
Linux ubuntu 5.4.0-37-generic #41-Ubuntu SMP Wed Jun 3 18:57:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

内核源码路径:
/usr/src/linux-headers-5.4.0-37-generic/

Xshell Xftp 下载地址:
https://www.netsarang.com/zh/free-for-home-school/

1. hello.c

 #include 
 #include 
 MODULE_LICENSE("Dual BSD/GPL");
 
 extern void testfunc(void);
 static int hello_init(void)
 {
     printk("Hello,world.\n");
	 testfunc();
     return 0x0;
 }
 static void hello_exit(void)
 {
     printk("Hello,exit.\n");
	 testfunc();
     return;
 }
 module_init(hello_init);
 module_exit(hello_exit);

2. test.c

#include 
#include 
void testfunc(void)
{
    printk("%s\n",__func__);
}

3. Makefile

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

obj-m := xhr.o
xhr-objs += hello.o
xhr-objs += test.o

modules:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

.PHONEY:clean
clean :
	rm -f *.o *.ko *.symvers *.order *.mod.c *.mod .*.cmd

4. insmod & rmmod

sudo insmod xhr.ko
sudo rmmod xhr
dmesg

输出:
[ 6408.823731] Hello,world.
[ 6408.823732] testfunc
[ 6432.889653] Hello,exit.
[ 6432.889655] testfunc

5. 可能的情况

若在Makefile文件中没有CONFIG_MODULE_SIG=n这条语句,那么hello.txt文件中将会显示:

[ 9753.845869] hello: module verification failed: signature and/or required key missing - tainting kernel

即:CONFIG_MODULE_SIG=n 写在 makefile 第一行。

你可能感兴趣的:(编译 Ubuntu-20.04 驱动)