Linux核心源碼樹建立載入hello模組,linuxhello

來源:互聯網
上載者:User

Linux核心源碼樹建立載入hello模組,linuxhello

  在載入模組之前,書中說要先建立核心源碼樹,那麼,如何建立核心源碼樹呢?

  首先,要Crowdsourced Security Testing道你的OS的核心版本,用uname -r可以查得到

   在該目錄ls一下發現已經存在了這個目錄= =!也就是原來系統已經存在核心源碼樹了。

  

 

  既然核心源碼樹已經存在,下面我們要做的就是將hello模組載入到核心中。

  下面是hello的原始碼

  

 1 #include <linux/init.h>  2 #include <linux/module.h>  3 MODULE_LICENSE("Dual BSD/GPL");  4 static int hello_init(void)  5 {  6     printk(KERN_ALERT "Hello, world\n");  7     return 0;  8 }  9 static void hello_exit(void) 10 { 11     printk(KERN_ALERT"Goodbye, cruel world\n"); 12 } 13 module_init(hello_init); 14 module_exit(hello_exit); 

 

  給代碼編寫Makefile檔案

1 obj-m := hello.o 2 KERNELDIR := /lib/modules/3.16.0-30/build 3 PWD := $(shell pwd) 4 modules: 5     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 6 modules_install: 7     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install 

  運行make

  

  產生相應的hello.ko檔案

  

  然後載入hello.ko模組,注意要在root模式下載入,然後lsmod查看載入的模組(書中說會在控制台輸出hello world ,但是我的控制台沒有輸出)

  

  卸載模組

  

  雖然控制台沒有進行輸出,但是可以在/var/log/syslog下查看hello模組的輸出

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.