星期六, 1月 22, 2011

Hello World of Linux Kernel Module Development

花了好些時間從算搞定。

首先先編譯Kernel,參照下列網址

http://blog.avirtualhome.com/2010/11/06/how-to-compile-a-ubuntu-10-10-maverick-kernel/
http://duopetalflower.blogspot.com/2010/10/ubuntu-maverick-64bit-kernel.html

編譯好裝上新Kernel後,就可以在/lib裡頭看到新的kernel了。

接著在home目錄下編輯hello.c
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
接著再編輯Makefile
obj-m := hello.o // 目標module
KERNELDIR = /lib/modules/2.6.35-24-cacore/build // 2.6.35-24-cacore是編譯過的Kernerl名稱
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
接著執行make,應該會出現hello.ko檔
然後
#sudo insmod hello.ko
可以裝上module

#sudo rmmod
會移除module

完成!

後記:用inmod不會去檢查相依性,所以建議改用modprobe
下列是新增及移除的方法
#modprobe module [parameter]
#modprobe -r module

沒有留言: