背景:最近在做一个虚拟机安全的项目,使用KVM来支撑虚拟化,涉及到KVM和QEMU的代码修改。由于KVM可以以模块的方式加载到内核中,可以单独编译KVM模块,而不必每次重新编译内核(鉴于编译内核所耗的时间,如果每次改动KVM都要重新编译一次内核,估计等项目做完,头发也等白了)
参考:http://blog.chinaunix.net/uid-23390992-id-3300514.html
我的改进:
1.清理
make clean CONFIG_KVM=m CONFIG_INTEL_KVM=m -C /home/kvm/linux-3.14.5 M=/home/kvm/linux-3.14.5/arch/x86/kvm
2.编译
make CONFIG_KVM=m CONFIG_INTEL_KVM=m -C /home/kvm/linux-3.14.5 M=/home/kvm/linux-3.14.5/arch/x86/kvm
3.将arch/x86/kvm目录下生成kvm.ko和kvm-intel.ko生成的两个模块文件,拷贝到/lib/modules/$(uname -r)/kernel/arch/x86/kvm目录下
4.运行depmod -a重新配置模块依赖关系
5.modprobe kvm;modprobe kvm_intel加载两个模块
说明:/home/kvm/linux-3.14.5是linux源码的路径
应用场景:KVM开发
附以上命令的shell脚本,欢迎参考批评改进(github:https://github.com/ioilala/kvmrecompile):
(将path修改为你的linux源码目录)
#!/bin/bash path="/home/kvm/src/linux-3.14.10" echo "1.rmmod kvm_intel kvm\n" rmmod kvm-intel rmmod kvm echo "success!\n" echo "2.rm kvm-intel.ko kvm.ko\n" rm /lib/modules/$(uname -r)/kernel/arch/x86/kvm/kvm.ko rm /lib/modules/$(uname -r)/kernel/arch/x86/kvm/kvm-intel.ko echo "success!\n" echo "3.make clean kvm module\n" make clean CONFIG_KVM=m CONFIG_INTEL_KVM=m -C ${path} M=${path}/arch/x86/kvm echo "success!\n" echo "4.make kvm module" make CONFIG_KVM=m CONFIG_INTEL_KVM=m -C ${path} M=${path}/arch/x86/kvm echo "success!\n" echo "5.insmod kvm-intel.ko kvm.ko\n" cp ${path}/arch/x86/kvm/kvm-intel.ko /lib/modules/$(uname -r)/kernel/arch/x86/kvm/ cp ${path}/arch/x86/kvm/kvm.ko /lib/modules/$(uname -r)/kernel/arch/x86/kvm/ echo "success!\n" echo "6.modprobe kvm_intel kvm" modprobe kvm_intel modprobe kvm lsmod|grep kvm ls -l /dev/kvm echo "recompile and insmod kvm success!\n"