Linux內核模塊編程之Helloworld(初級)

注意printk那裏,KERN_ALERT和打印消息之間是沒有逗號的,搞得勞資查了半天才發現一直沒有提示信息的緣由javascript

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");//MODULE_LICENSE()指明認證方式,如今支持的有:「GPL」 「GPL v2" "GPL and additional rights" "Dual BSD/GPL" "Dual MIT/GPL" "Dual MPL/GPL" "Proprietary",這是內核2.6裏新添加的,實驗發現它不是必需的。
static int hello_init(void)
{
     printk(KERN_ALERT "Hello, World\n");//printk是內核級別的打印函數,KERN_ALERT是指該條信息是警告信息
     return 0;
}
static int hello_exit(void)
{
     printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);//模塊入口
module_exit(hello_exit);//模塊出口

下面咱們來寫Makefile(命名爲makefile個人會提示錯誤),-C 去內核源碼目錄下讀取Makefile,m=返回當前路徑執行當前目錄下的Makefile
。我以爲Makefile這個文件挺6的,該文件會根據xx.o查找相應的xx.c文件java

TARGET=hello
KDIR=/usr/src/kernels/3.10.0-514.el7.x86_64 //找到內核文件所在路徑,系統不一樣路徑也不一樣,能夠使用find / -name kernel查找
PWD=$(shell pwd)   //這個是指執行shell命令pwd,即用PWD記錄當前路徑
obj-m=$(TARGET).o
default:
    make -C $(KDIR) M=$(PWD) modules

那麼如何運行呢,首先makelinux

[04:21:42] make
make -C /usr/src/kernels/3.10.0-514.el7.x86_64 M=/root/kernel modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
  CC [M]  /root/kernel/hello.o
/root/kernel/hello.c: In function ‘hello_exit’:
/root/kernel/hello.c:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
In file included from /root/kernel/hello.c:1:0:
/root/kernel/hello.c: In function ‘__exittest’:
include/linux/init.h:305:4: warning: return from incompatible pointer type [enabled by default]
  { return exitfn; }     \
    ^
/root/kernel/hello.c:17:1: note: in expansion of macro ‘module_exit’
 module_exit(hello_exit);
 ^
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/kernel/hello.mod.o
  LD [M]  /root/kernel/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[04:21:45] ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers

如今進行加載模塊,首先在當前終端shell

[04:21:47] tail -f /var/log/messages
May 22 04:01:01 bogon systemd: Started Session 727 of user root.
May 22 04:01:01 bogon systemd: Starting Session 727 of user root.
May 22 04:10:01 bogon systemd: Started Session 728 of user root.
May 22 04:10:01 bogon systemd: Starting Session 728 of user root.
May 22 04:18:35 bogon kernel: Hello, World//在另外一個終端執行insmod ./hello.ko纔會出現這個
May 22 04:18:49 bogon kernel: Goodbye, cruel world//在另外一個終端執行rmmod hello纔會出現這個
May 22 04:20:01 bogon systemd: Started Session 729 of user root.
May 22 04:20:01 bogon systemd: Starting Session 729 of user root.
May 22 04:20:01 bogon kernel: Hello, World
May 22 04:20:11 bogon kernel: Goodbye, cruel world
May 22 04:22:06 bogon kernel: Hello, World

我在另外一個終端的執行過程就是進入該目錄,而後執行insmod ./hello.ko 而後第一個終端就會顯示Hello,world。在執行rmmod hello 就會顯示Goodbye, cruel worldcentos

可是!根據系統版本的不一樣仍是什麼其餘緣由,有時候信息在/var/log/messages 裏看到,使用dmesg能夠看到markdown

[root@bogon kernel]# dmesg|tail -5
[  123.690748] test: module verification failed: signature and/or required key missing - tainting kernel
[  876.865300] Hello, World
[  908.638904] Goodbye, cruel world
[ 1235.101965] e1000: ens33 NIC Link is Down
[ 1241.118672] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[root@bogon kernel]# 

或者是在加載或者移除模塊這裏看到
這裏寫圖片描述ssh

也能夠像下面這麼寫,注意,uname -r那裏是使用的反引號就是tab鍵上面的那個鍵,$(PWD)當前工做目錄,clean清除,打印信息是顯示在另外一個終端的,例如我就是在物理機上使用ssh連接虛擬機centos7,而後執行下面的命令,而在虛擬機的終端上則會顯示打印的那幾條信息函數

[root@bogon modules]# cat first.c
#include<linux/kernel.h>
#include<linux/module.h>
int init_module(void){ printk(KERN_ALERT   "hi,this is bp\n");
 return 0;
}
void cleanup_module(void){ printk(KERN_ALERT   "goobye bp\n");
}
[root@bogon modules]# cat Makefile 
obj-m=first.o
default:
 make -C /usr/src/kernels/`uname -r` M=$(PWD) modules
clean:
 make -C /usr/src/kernels/`uname -r` M=$(PWD) clean
[root@bogon modules]# make
make -C /usr/src/kernels/`uname -r` M=/root/modules modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
 Building modules, stage 2.
 MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[root@bogon modules]# modinfo first.ko//查看模塊信息
filename:       /root/modules/first.ko
rhelversion:    7.3
srcversion:     2523BB278E7311D9141E7F4
depends:        
vermagic:       3.10.0-514.el7.x86_64 SMP mod_unload modversions 
[root@bogon modules]# insmod first.ko
[root@bogon modules]# rmmod first
[root@bogon modules]# ls
a.c      a.mod.o  first.ko     first.o   hello.mod.c  Makefile
a.ko     a.o      first.mod.c  hello.c   hello.mod.o  modules.order
a.mod.c  first.c  first.mod.o  hello.ko  hello.o      Module.symvers
[root@bogon modules]# make clean
make -C /usr/src/kernels/`uname -r` M=/root/modules clean
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
 CLEAN /root/modules/.tmp_versions
 CLEAN /root/modules/Module.symvers
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
[root@bogon modules]# ls
a.c  first.c  hello.c  Makefile
[root@bogon modules]#
相關文章
相關標籤/搜索