构建KVM环境
硬件系统配置要求


- 硬件必须支持Intel-VT或者AMD-V技术,并且BIOS开启虚拟化选项,不然没得玩。
- CPU指令集包括VT-x
KVM 与 Qemu 的前世今生
Qemu 是一个纯软件实现的开源「模拟」软件,它能够模拟整套虚拟机的实现,包括 CPU、内存、各种 IO 设备、鼠标、键盘、USB 、网卡、声卡等等,基本上没有它不能模拟的。有人可能会比较疑惑它跟 KVM 之间到底有何关系,我们可以把它们看成是合作关系,好基友,谁都离不开彼此。
KVM 离不开 Qemu。KVM 实现初期,为了简化开发和代码重用,在 Qemu 的基础上进行了修改,主要是将比较耗性能的 CPU 虚拟化和内存虚拟化部分移到了内核中实现,保留 IO 虚拟化模块在用户空间实现。这样的做法主要是考虑到性能的原因,CPU 和 内存虚拟化是非常复杂的虚拟化模块,而且使用非常频繁,如果实现在用户空间的话,用户态和内核态的频繁切换势必会对性能造成很大的影响。那为什么要单独保留 IO 虚拟化在用户空间呢,这个也是权衡之下的结果,首先 IO 设备太多了,其次 IO 虚拟化相对其他两个模块使用不是很频繁,开销会小一些,所以,为了尽可能保持内核的纯净性,才有了这样的分配。
Qemu 离不开 KVM。上面也说了,Qemu 是一个纯软件的实现,运行在用户空间,性能非常低下,所以,从 Qemu 的角度,可以说是 Qemu 使用了 KVM 的虚拟化功能,为自身虚拟机提供加速。
早期两者还没有区分(没有同居),KVM 修改的模块叫 qemu-kvm,到 Qemu1.3 版本之后,两者就合二为一了(同居啦),如果我们在用 Qemu 创建虚拟机时,要加载 KVM 模块,需要为其指定参数 —enable-kvm。

- KVM 是基于硬件虚拟化(Intel VT 或 AMD-V)实现的一套虚拟化解决方案,通过以上一个与 Qemu 关系的分析,我们基本上知道它在虚拟化领域处在一个什么样的地位。它其实只负责 CPU 和内存的虚拟化,不负责任何设备的模拟,而是提供接口给用户空间的 Qemu 来模拟。这个接口是 /dev/kvm。
- Qemu 通过 /dev/kvm 接口设置一个虚拟机的地址空间,然后向它提供模拟好的 I/O 设备,并将相关的设备回显操作映射到宿主机,完成整个 I/O 设备的虚拟化操作。
- /dev/kvm 接口是 Qemu 和 KVM 交互的“桥梁”,基本的原理是:/dev/kvm 本身是一个设备文件,这就意味着可以通过 ioctl 函数来对该文件进行控制和管理,从而可以完成用户空间与内核空间的数据交互。在 KVM 与 Qemu 的通信过程主要就是一系列针对该设备文件的 ioctl 调用。
- KVM是一种用于Linux内核中的虚拟化基础设施,可以将Linux内核转换成一个Hypervisor。
- KVM诞生初期,以色列开发人员没有打算从头造轮子,而是通过加载一个内核模块让Linux内核编程一个Hypervisor。
- KVM是一个轻量级的虚拟化管理程序模块,该模块主要来自于Linux内核,虽然诞生比Xen晚,但是其性能和易用性,以及后期开源社区的贡献,是的KVM迅速发展。
Qemu 架构

Qemu 是纯软件实现的虚拟化模拟器,几乎可以模拟任何硬件设备,我们最熟悉的就是能够模拟一台能够独立运行操作系统的虚拟机,虚拟机认为自己和硬件打交道,但其实是和 Qemu 模拟出来的硬件打交道,Qemu 将这些指令转译给真正的硬件。
正因为 Qemu 是纯软件实现的,所有的指令都要经 Qemu 过一手,性能非常低,所以,在生产环境中,大多数的做法都是配合 KVM 来完成虚拟化工作,因为 KVM 是硬件辅助的虚拟化技术,主要负责 比较繁琐的 CPU 和内存虚拟化,而 Qemu 则负责 I/O 虚拟化,两者合作各自发挥自身的优势,相得益彰。
Qemu 可以模拟多种非当前CPU架构平台,虽然性能差,但是至少模拟的了。
从本质上看,虚拟出的每个虚拟机对应 host 上的一个 Qemu 进程,而虚拟机的执行线程(如 CPU 线程、I/O 线程等)对应 Qemu 进程的一个线程。
- Qemu 软件虚拟化实现的思路是采用二进制指令翻译技术,主要是提取 guest 代码,然后将其翻译成 TCG 中间代码,最后再将中间代码翻译成 host 指定架构的代码,如 x86 体系就翻译成其支持的代码形式,ARM 架构同理。

区分Qemu、Qemu-KVM、Linux kernel KVM
* Qemu1.3之后默认包含KVM源码。编译的时候 ./configure --enable-kvm 即可* Qemu-kvm是修改了Qemu部分源码适配KVM,本质Qemu-kvm就是Qemu* 本质:Qemu-kvm是针对KVM专门做了修改和优化的Qemu分支* Linux 2.6.20之后均包含KVM源码,但不包括Qemu* 本质:KVM = Linux内核2.6.20之后版本,包括Kernel和KVM驱动
- KVM作为Linux内核模块存在,从Linux 2.6.20版本开始被正式加入内核主干开发和正式发布代码中。
- 只需要下载2.6.20之后的任意版本均包含KVM源码。
Linux 4.15 kvm源码编译
root@android:~/kernel/linux-4.14.15# make helpCleaning targets:clean - Remove most generated files but keep the config andenough build support to build external modulesmrproper - Remove all generated files + config + various backup filesdistclean - mrproper + remove editor backup and patch filesConfiguration targets:config - Update current config utilising a line-oriented programnconfig - Update current config utilising a ncurses menu basedprogrammenuconfig - Update current config utilising a menu based programxconfig - Update current config utilising a Qt based front-endgconfig - Update current config utilising a GTK+ based front-endoldconfig - Update current config utilising a provided .config as baselocalmodconfig - Update current config disabling modules not loadedlocalyesconfig - Update current config converting local mods to coresilentoldconfig - Same as oldconfig, but quietly, additionally update depsdefconfig - New config with default from ARCH supplied defconfigsavedefconfig - Save current config as ./defconfig (minimal config)allnoconfig - New config where all options are answered with noallyesconfig - New config where all options are accepted with yesallmodconfig - New config selecting modules when possiblealldefconfig - New config with all symbols set to defaultrandconfig - New config with random answer to all optionslistnewconfig - List new optionsolddefconfig - Same as silentoldconfig but sets new symbols to theirdefault valuekvmconfig - Enable additional options for kvm guest kernel supportxenconfig - Enable additional options for xen dom0 and guest kernel supporttinyconfig - Configure the tiniest possible kernelOther generic targets:all - Build all targets marked with [*]* vmlinux - Build the bare kernel* modules - Build all modulesmodules_install - Install all modules to INSTALL_MOD_PATH (default: /)dir/ - Build all files in dir and belowdir/file.[ois] - Build specified target onlydir/file.ll - Build the LLVM assembly file(requires compiler support for LLVM assembly generation)dir/file.lst - Build specified mixed source/assembly target only(requires a recent binutils and recent build (System.map))dir/file.ko - Build module including final linkmodules_prepare - Set up for building external modulestags/TAGS - Generate tags file for editorscscope - Generate cscope indexgtags - Generate GNU GLOBAL indexkernelrelease - Output the release version string (use with make -s)kernelversion - Output the version stored in Makefile (use with make -s)image_name - Output the image name (use with make -s)headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH(default: ./usr)Static analysers:checkstack - Generate a list of stack hogsnamespacecheck - Name space analysis on compiled kernelversioncheck - Sanity check on version.h usageincludecheck - Check for duplicate included header filesexport_report - List the usages of all exported symbolsheaders_check - Sanity check on exported headersheaderdep - Detect inclusion cycles in headerscoccicheck - Check with Coccinelle.Kernel selftest:kselftest - Build and run kernel selftest (run as root)Build, install, and boot kernel beforerunning kselftest on itkselftest-clean - Remove all generated kselftest fileskselftest-merge - Merge all the config dependencies of kselftest to existing.config.Userspace tools targets:use "make tools/help"or "cd tools; make help"Kernel packaging:rpm-pkg - Build both source and binary RPM kernel packagesbinrpm-pkg - Build only the binary kernel RPM packagedeb-pkg - Build both source and binary deb kernel packagesbindeb-pkg - Build only the binary kernel deb packagetar-pkg - Build the kernel as an uncompressed tarballtargz-pkg - Build the kernel as a gzip compressed tarballtarbz2-pkg - Build the kernel as a bzip2 compressed tarballtarxz-pkg - Build the kernel as a xz compressed tarballperf-tar-src-pkg - Build perf-4.14.15.tar source tarballperf-targz-src-pkg - Build perf-4.14.15.tar.gz source tarballperf-tarbz2-src-pkg - Build perf-4.14.15.tar.bz2 source tarballperf-tarxz-src-pkg - Build perf-4.14.15.tar.xz source tarballDocumentation targets:Linux kernel internal documentation in different formats from ReST:htmldocs - HTMLlatexdocs - LaTeXpdfdocs - PDFepubdocs - EPUBxmldocs - XMLlinkcheckdocs - check for broken external links (will connect to external hosts)cleandocs - clean all generated filesmake SPHINXDIRS="s1 s2" [target] Generate only docs of folder s1, s2valid values for SPHINXDIRS are: driver-api core-api networking input sound media userspace-api admin-guide filesystems dev-tools process crypto gpu sh kernel-hacking doc-guidemake SPHINX_CONF={conf-file} [target] use *additional* sphinx-buildconfiguration. This is e.g. useful to build with nit-picking config.Architecture specific targets (x86):* bzImage - Compressed kernel image (arch/x86/boot/bzImage)install - Install kernel using(your) ~/bin/installkernel or(distribution) /sbin/installkernel orinstall to $(INSTALL_PATH) and run lilofdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)bzdisk/fdimage*/isoimage also accept:FDARGS="..." arguments for the booted kernelFDINITRD=file initrd for the booted kerneli386_defconfig - Build for i386x86_64_defconfig - Build for x86_64make V=0|1 [targets] 0 => quiet build (default), 1 => verbose buildmake V=2 [targets] 2 => give reason for rebuild of targetmake O=dir [targets] Locate all output files in "dir", including .configmake C=1 [targets] Check re-compiled c source with $CHECK (sparse by default)make C=2 [targets] Force check of all c source with $CHECKmake RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sectionsmake W=n [targets] Enable extra gcc checks, n=1,2,3 where1: warnings which may be relevant and do not occur too often2: warnings which occur quite often but may still be relevant3: more obscure warnings, can most likely be ignoredMultiple levels can be combined with W=12 or W=123Execute "make" or "make all" to build all targets marked with [*]For further info see the ./README file



Qemu-kvm编译安装
- 获取源码
git clone git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm.git
- 配置

./configure --enable-kvm --enable-vnc --disable-werror
必须加 —disable-werror ,不然各种无厘头报错,好烦~
- 编译
编译报错解决

- 安装

参考
aCloudDeveloper:http://www.cnblogs.com/bakari/tag/%E8%99%9A%E6%8B%9F%E5%8C%96/
