构建KVM环境

硬件系统配置要求

1531909543928.png

1532056981521.png

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。

1531909863030.png

Qemu 架构

1531910341493.png

从本质上看,虚拟出的每个虚拟机对应 host 上的一个 Qemu 进程,而虚拟机的执行线程(如 CPU 线程、I/O 线程等)对应 Qemu 进程的一个线程。

1531910502812.png

区分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驱动

Linux 4.15 kvm源码编译

root@android:~/kernel/linux-4.14.15# make help
Cleaning targets:
  clean		  - Remove most generated files but keep the config and
                    enough build support to build external modules
  mrproper	  - Remove all generated files + config + various backup files
  distclean	  - mrproper + remove editor backup and patch files

Configuration targets:
  config	  - Update current config utilising a line-oriented program
  nconfig         - Update current config utilising a ncurses menu based
                    program
  menuconfig	  - Update current config utilising a menu based program
  xconfig	  - Update current config utilising a Qt based front-end
  gconfig	  - Update current config utilising a GTK+ based front-end
  oldconfig	  - Update current config utilising a provided .config as base
  localmodconfig  - Update current config disabling modules not loaded
  localyesconfig  - Update current config converting local mods to core
  silentoldconfig - Same as oldconfig, but quietly, additionally update deps
  defconfig	  - New config with default from ARCH supplied defconfig
  savedefconfig   - Save current config as ./defconfig (minimal config)
  allnoconfig	  - New config where all options are answered with no
  allyesconfig	  - New config where all options are accepted with yes
  allmodconfig	  - New config selecting modules when possible
  alldefconfig    - New config with all symbols set to default
  randconfig	  - New config with random answer to all options
  listnewconfig   - List new options
  olddefconfig	  - Same as silentoldconfig but sets new symbols to their
                    default value
  kvmconfig	  - Enable additional options for kvm guest kernel support
  xenconfig       - Enable additional options for xen dom0 and guest kernel support
  tinyconfig	  - Configure the tiniest possible kernel

Other generic targets:
  all		  - Build all targets marked with [*]
* vmlinux	  - Build the bare kernel
* modules	  - Build all modules
  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)
  dir/            - Build all files in dir and below
  dir/file.[ois]  - Build specified target only
  dir/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 link
  modules_prepare - Set up for building external modules
  tags/TAGS	  - Generate tags file for editors
  cscope	  - Generate cscope index
  gtags           - Generate GNU GLOBAL index
  kernelrelease	  - 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 hogs
  namespacecheck  - Name space analysis on compiled kernel
  versioncheck    - Sanity check on version.h usage
  includecheck    - Check for duplicate included header files
  export_report   - List the usages of all exported symbols
  headers_check   - Sanity check on exported headers
  headerdep       - Detect inclusion cycles in headers
  coccicheck      - Check with Coccinelle.

Kernel selftest:
  kselftest       - Build and run kernel selftest (run as root)
                    Build, install, and boot kernel before
                    running kselftest on it
  kselftest-clean - Remove all generated kselftest files
  kselftest-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 packages
  binrpm-pkg          - Build only the binary kernel RPM package
  deb-pkg             - Build both source and binary deb kernel packages
  bindeb-pkg          - Build only the binary kernel deb package
  tar-pkg             - Build the kernel as an uncompressed tarball
  targz-pkg           - Build the kernel as a gzip compressed tarball
  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball
  tarxz-pkg           - Build the kernel as a xz compressed tarball
  perf-tar-src-pkg    - Build perf-4.14.15.tar source tarball
  perf-targz-src-pkg  - Build perf-4.14.15.tar.gz source tarball
  perf-tarbz2-src-pkg - Build perf-4.14.15.tar.bz2 source tarball
  perf-tarxz-src-pkg  - Build perf-4.14.15.tar.xz source tarball

Documentation targets:
 Linux kernel internal documentation in different formats from ReST:
  htmldocs        - HTML
  latexdocs       - LaTeX
  pdfdocs         - PDF
  epubdocs        - EPUB
  xmldocs         - XML
  linkcheckdocs   - check for broken external links (will connect to external hosts)
  cleandocs       - clean all generated files

  make SPHINXDIRS="s1 s2" [target] Generate only docs of folder s1, s2
  valid 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-guide

  make SPHINX_CONF={conf-file} [target] use *additional* sphinx-build
  configuration. 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 or
                  install to $(INSTALL_PATH) and run lilo
  fdimage      - 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 kernel
                  FDINITRD=file initrd for the booted kernel

  i386_defconfig           - Build for i386
  x86_64_defconfig         - Build for x86_64

  make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build
  make V=2   [targets] 2 => give reason for rebuild of target
  make O=dir [targets] Locate all output files in "dir", including .config
  make C=1   [targets] Check re-compiled c source with $CHECK (sparse by default)
  make C=2   [targets] Force check of all c source with $CHECK
  make RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sections
  make W=n   [targets] Enable extra gcc checks, n=1,2,3 where
		1: warnings which may be relevant and do not occur too often
		2: warnings which occur quite often but may still be relevant
		3: more obscure warnings, can most likely be ignored
		Multiple levels can be combined with W=12 or W=123

Execute "make" or "make all" to build all targets marked with [*]
For further info see the ./README file

1531913065801.png

1531913129376.png

1531913166954.png

Qemu-kvm编译安装

  1. 获取源码
git clone git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm.git
  1. 配置

1531917279615.png

./configure --enable-kvm  --enable-vnc --disable-werror

必须加 –disable-werror ,不然各种无厘头报错,好烦~

  1. 编译

编译报错解决

1531917944771.png

  1. 安装

1531918964627.png

参考

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