在说kvm模块单独编译之前,难免设计到lin内核模板的编写,所以今天小编教大家如何搭建lin内核模块环境
1.升级内核
升级当前系统的kernel,具体编译步骤这里不再详细说明,简单表述一下:如果想在当前的lin系统上面,不用修改配置文件来编译内核,就将/boot/config-***文件拷贝至/home/pizhi/lin-4.6.4/.config
-
[root@pizhi-kernel boot]# pwd
-
/boot
-
[root@pizhi-kernel boot]# cp config-3.10.0-229.el7.x86_64 /home/pizhi/lin-4.6.4/.config
注意:执行完上面的cp命令以后,仍然需要使用make menuconfig命令并保存配置。
编译之前需要安装rpmbuild工具:
make rpm
这一步会在/root/rpmbuild/RPMS生成对应的kernel rpm包。
更新:
yum install ./*.rpm
2.安装kernel-devel包
不需要像1.1中的花很长时间升级内核,只需要安装kernel-devel rpm包即可。
安装:
yum install kernel-devel
如果源没啥问题,基本安装的和kernel rpm包的版本一致即可。
测试:
hello.c:
#include <lin/init.h>
#include <lin/module.h>
static int hello_init(void) {
printk(KERN_WARNING"Hello, pikachu kernel!\n");
return 0;
}
static void hello_exit(void) {
printk(KERN_INFO"Goodbye, pikachu kernel!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
Makefile:
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /lib/modules/`uname -r`/build
all :
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
endif
make过程遇到的问题:
-
make -C /lib/modules/`uname -r`/build M=/root/code_kernel/hello modules
-
make: *** /lib/modules/3.10.0-327.el7.x86_64/build: No such file or directory. Stop.
-
make: *** [all] Error 2
解决方法:
-
/lib/modules/3.10.0-327.el7.x86_64/build没有指向正确的kernel source。建立软连接即可。
-
[root@localhost 3.10.0-327.el7.x86_64]# pwd
-
/lib/modules/3.10.0-327.el7.x86_64
-
[root@localhost 3.10.0-327.el7.x86_64]# rm build
-
[root@localhost 3.10.0-327.el7.x86_64]# ln -sv /usr/src/kernels/3.10.0-514.2.2.el7.x86_64/ build
注:kernel打印的日志文件在/var/log/messages下。