有必要查询下Linux内核
# uname -r
2.6.28-11-generic
# ls /usr/src/
linux-headers-2.6.28-11 linux-headers-2.6.28-11-generic
由此可见内核版本和内核头文件版本是一致的,都是2.6.28-11。(如果不一致的话在insmod一步必定出错:
Error inserting './hello.ko': -1 Invalid module format
编写hello.c文件
linhui@ubuntu:~/workspace/driver_test$ cat hello.c
//Begin---hello.c
#include</usr/src/linux-headers-4.4.0-31/include/linux/init.h>
#include</usr/src/linux-headers-4.4.0-31/include/linux/module.h>
MODULE_LICENSE("GPL");
//printk(KERN_ALERT "Begin\n");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Good bye, ubuntu\n");
// return 0;
}
module_init(hello_init);
module_exit(hello_exit);
//End---hello.c
编写makefile文件:
linhui@ubuntu:~/workspace/driver_test$ cat Makefile
#Begin---Makefile
KERNELDIR=/lib/modules/4.4.0-31-generic/build
PWD:=$(shell pwd)
INSTALLDIR=/home/linhui/workspace/driver_test/install
obj-m:= hello.o
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
cp hello.ko $(INSTALLDIR)
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
#End---Makefile
执行make编译:
linhui@ubuntu:~/workspace/driver_test$ make
make -C /lib/modules/4.4.0-31-generic/build M=/home/linhui/workspace/driver_test modules
make[1]: Entering directory `/usr/src/linux-headers-4.4.0-31-generic'
CC [M] /home/linhui/workspace/driver_test/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/linhui/workspace/driver_test/hello.mod.o
LD [M] /home/linhui/workspace/driver_test/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-4.4.0-31-generic'
编译后文件目录如下:
-rw-rw-r-- 1 linhui linhui 433 Jun 5 15:31 hello.c
-rw-rw-r-- 1 linhui linhui 3983 Jun 5 15:35 hello.ko
-rw-rw-r-- 1 linhui linhui 251 Jun 5 15:35 .hello.ko.cmd
-rw-rw-r-- 1 linhui linhui 763 Jun 5 15:35 hello.mod.c
-rw-rw-r-- 1 linhui linhui 2872 Jun 5 15:35 hello.mod.o
-rw-rw-r-- 1 linhui linhui 27034 Jun 5 15:35 .hello.mod.o.cmd
-rw-rw-r-- 1 linhui linhui 2792 Jun 5 15:35 hello.o
-rw-rw-r-- 1 linhui linhui 27020 Jun 5 15:35 .hello.o.cmd
-rw-rw-r-- 1 linhui linhui 370 Jun 5 15:35 Makefile
-rw-rw-r-- 1 linhui linhui 51 Jun 5 15:35 modules.order
-rw-rw-r-- 1 linhui linhui 0 Jun 5 15:35 Module.symvers
drwxrwxr-x 2 linhui linhui 4096 Jun 5 15:35 .tmp_versions/
安装驱动:
linhui@ubuntu:~/workspace/driver_test$ sudo insmod hello.ko
不加sudo会提示没有权限!!!
在执行一遍,提示错误:
insmod: error inserting 'hello.ko': -1 File exists
说明hello模块是安装成功的。
查看模块:
linhui@ubuntu:~/workspace/driver_test$ lsmod |head -5
Module Size Used by
hello 16384 0
nls_utf8 16384 1
isofs 40960 1
snd_ens1371 28672 2
卸载模块
# sudo rmmod hello.ko
再用
# lsmod | head -5
发现hello模块已经不存在了。
查看log:
linhui@ubuntu:~/workspace/driver_test$ dmesg | tail -8
[ 464.990762] Hello World!
[ 1133.339257] Good bye, ubuntu
linhui@ubuntu:~/workspace/driver_test$ cat /var/log/kern.log|tail -10
Jun 5 15:28:11 ubuntu kernel: [ 37.278449] ISO 9660 Extensions: Microsoft Joliet Level 3
Jun 5 15:28:11 ubuntu kernel: [ 37.294413] ISOFS: changing to secondary root
Jun 5 15:28:16 ubuntu kernel: [ 42.317511] audit_printk_skb: 153 callbacks suppressed
Jun 5 15:28:16 ubuntu kernel: [ 42.317517] audit: type=1400 audit(1496647696.864:63): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/lib/cups/backend/cups-pdf" pid=2559 comm="apparmor_parser"
Jun 5 15:28:16 ubuntu kernel: [ 42.317526] audit: type=1400 audit(1496647696.864:64): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/cupsd" pid=2559 comm="apparmor_parser"
Jun 5 15:28:16 ubuntu kernel: [ 42.318199] audit: type=1400 audit(1496647696.864:65): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/cupsd" pid=2559 comm="apparmor_parser"
Jun 5 15:28:18 ubuntu kernel: [ 43.932946] audit: type=1400 audit(1496647698.480:66): apparmor="DENIED" operation="capable" profile="/usr/sbin/cupsd" pid=2622 comm="serial" capability=21 capname="sys_admin"
Jun 5 15:35:42 ubuntu kernel: [ 464.972715] hello: module verification failed: signature and/or required key missing - tainting kernel
Jun 5 15:35:42 ubuntu kernel: [ 464.990762] Hello World!
Jun 5 15:46:50 ubuntu kernel: [ 1133.339257] Good bye, ubuntu