写一个linux简单的内核

操作系统centos6.5
yum install kernel-devel

cd /usr/src/kernels

ln -s 2.6.32-754.29.2.el6.i686/ 2.6.32-431.el6.i686
这里是因为uname -r与下载的kernel名字不一样所以要创建软连接,注意要根据具体编号修改你可以用uname -r查看名字前面是你的kernel后面名字是uname -r的名字

回到主目录在任意地方mkdir一个目录
文件一
vim hello.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
//必选
//模块许可声明
MODULE_LICENSE(“GPL”);
//模块加载函数
static int hello_init(void)
{
printk(KERN_ALERT “hello,I am kernel module\n”);
return 0;
}
//模块卸载函数
static void hello_exit(void)
{
printk(KERN_ALERT “goodbye, kernel module\n”);
}
//模块注册
module_init(hello_init);
module_exit(hello_exit);
//可选
MODULE_AUTHOR(“zhiqiang Wu”);//可以修改你的名字
MODULE_DESCRIPTION(“This is a simple example!\n”);
MODULE_ALIAS(“A simplest example”);

用:wq保存
文件二
vim Makefile

obj-m += hello.o
#generate the path
PWD:=KaTeX parse error: Expected 'EOF', got '#' at position 13: (shell pwd) #̲the current ker…(shell uname -r)
#the absolute path
KDIR:=/usr/src/kernels/$(shell uname -r)
#complie object
all:
make -C (KDIR)M=(KDIR) M=(PWD) modules
#clean
clean:
make -C (KDIR)M=(KDIR) M=(PWD) clean

用:wq保存

写好后make

insmod hello.ko

dmesg|grep hello
写一个linux简单的内核