hello驅動程序開發實戰

有必要查詢下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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章