close

請下載: Linux DeviceDriver\VM\Debian6.rar
VMWare-player-12安裝

安裝完VMWare-player-12之後,
再解開Debian6.rar的virtual machine
用VM開啟,登入lINUX的帳號密碼:
root/root
安裝過程會需要移除舊的VMware,
請移除舊的VM,輸入email即可

 

 

#vi hello.c

 

#include <linux/kernel.h>
#include <linux/module.h>

int init_module(void)    //進入點
{
    printk("Hello\n");
    return 0;
}
void cleanup_module(void)
   //離開點
{
    printk("bye\n");
}

:qw 儲存離開

#vi Makefile
obj-m +=hello.o    //記得是點o檔

qw儲存離開

#ls /lib/modules/2.6.32/build

#make -C /lib/modules/2.6.32/build M=`pwd` modules 
 

#ls

#lsmod ./hello.ko
 

#tail /var/log/kern.log

 



#rmmod hello     //則可以移除
#tail /var/log/kern.log


 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hello-1.c

step1:

 

#vi hello-1.c  //寫一支hello-1.c的程式

#include <linux/kernel.h>  //要使用已經有人寫好的kernel函式
#include <linux/module.h>  //module函式
#include <linux/init.h>

int hello_nikiinit(void)  //隨意取名進入點的函式
{
    printk("Hello Niki!\n");
 //在kernel下的印出是使用 printk
    return 0;
}

void byebye_nikicleanup(void)
 //隨意取名離開點的函式
{
    printk("bye bye Niki!!\n");
  //當程式離開會印出 bye bye Niki!!
}

module_init(hello_nikiinit);  //要記得告訴 module_init 進入點的函式
module_exit(byebye_nikicleanup);  //要記得告訴 module_cleanup 離開的函式

 

step2:  寫一個 Makefile
#obj-m += hello-1.o
這裡的m 是指module ,  += 不可以分開寫  ,產產生 .o 檔

 

 

step3:

#make -C /lib/modules/2.6.32/build M=`pwd` modules

-C Change 改變資料夾在讀 Makefiles之前
 M=`pwd` 目前的path

 

step4: insert module
#insmod ./hello-1.ko
因為我的printk裡面沒有打 <1> 權限不夠大,就沒有顯示


#tail /var/log/kern.log  
這時候使用=>tail 查看 /var/log/kern.log

可以看到我程式印出的 Hello Niki

 

step5: list all the modules loaded in kernel

#cat /proc/modules

 

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

arrow
arrow
    文章標籤
    Linux device drive
    全站熱搜

    布拉怡 發表在 痞客邦 留言(0) 人氣()