Linux kernal develop --1

1. install linux kernal source code
   yum install kernel-headers kernel-devel

   after install

   /usr/src/kernels/kernalversion is not null

2. check your kernal version
   uname -r

3.
#include <linux/module.h>   
#include <linux/init.h>   
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); // 必须!!


4. Makefile

#KVER = /usr/src/linux-source-2.6.38  
KVER = /lib/modules/`uname -r`/build  
CURDIR = $(shell pwd)  
# Kernel modules  
obj-m := hello.o  
build: kernel_modules  
kernel_modules:  
    $(MAKE) -C $(KVER) M=$(CURDIR) modules  
clean:  
    $(MAKE) -C $(KVER) M=$(CURDIR) clean 


5.

insmod hello.ko

dmesg | tail查看,或者查看/var/logs/messages日志

sudo rmmod hello

猜你喜欢

转载自ssh-2009-126-com.iteye.com/blog/1706430