Current system environment:
CentOS Linux release 7.2.1511 (Core)
Before the kvm module is compiled separately, it is inevitable to design the writing of the linux kernel template, so I will mention it here.
1Linux kernel module environment constructionThere are 2 methods here:
1.1 Upgrade the kernel:
To upgrade the kernel of the current system, the specific compilation steps will not be described in detail here, but briefly stated: If you want to compile the kernel without modifying the configuration file on the current Linux system, copy the /boot/config-*** file to / home/pizhi/linux-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/linux-4.6.4/.config
Note: After executing the above cp command, you still need to use the make menuconfig command and save the configuration.
You need to install the rpmbuild tool before compiling:
make rpm
This step will generate the corresponding kernel rpm package in /root/rpmbuild/RPMS.
renew:
yum install ./*.rpm
1.2 Install the kernel-devel package
No need to upgrade the kernel as long as in 1.1, just install the kernel-devel rpm package.
Install:
yum install kernel-devel
If there is no problem with the source, the basic installation is the same as the version of the kernel rpm package.
test:
hello.c:
#include <linux/init.h>
#include <linux/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
Note: The log file printed by the kernel is under /var/log/messages.
2 Compile the KVM module separately
2.1 Compile the kernel source from the SPEC file
Download from http://vault.centos.org/7.2.1511/os/Source/SPackages/
kernel-3.10.0-327.el7.src.rpm
Screenshot below:
Resolve dependencies:
yum-builddep kernel-3.10.0-327.el7.src.rpm
rpm -ivh kernel-3.10.0-327.el7.src.rpm
yum-builddep /root/rpmbuild/SPECS/kernel.spec
Generate kernel source:
rpmbuild -bp /root/rpmbuild/SPECS/kernel.spec
Generate the path of the kernel source:
/root/rpmbuild/BUILD/kernel-3.10.0-327.el7
2.2 Compile the KVM module separately
Enter the kernel source directory:
/root/rpmbuild/BUILD/kernel-3.10.0-327.el7/linux-3.10.0-327.el7.x86_64
Compile separately:
make -j8 -C `pwd` M=`pwd`/arch/x86/kvm modules
The generated kvm.ko and kvm-intel.ko are in the kernel source//arch/x86/kvm directory.
Check out the kvm module:
[root @ pizhi-kernel ~] # lsmod | grep sqm
kvm_intel 162153 0
sqm 525259 1 sqm_intel
Uninstall the kvm module:
modprobe -r kvm_intel
安装刚刚单独编译的kvm模块:
cd /root/rpmbuild/BUILD/kernel-3.10.0-327.el7/linux-3.10.0-327.el7.x86_64/arch/x86/kvm
insmod kvm.ko
insmod kvm-intel.ko