KVM 安装

KVM介绍

Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中。它使用Linux自身的调度器进行管理,所以相对于Xen,其核心源码很少。KVM目前已成为学术界的主流VMM之一。
KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术)。是基于硬件的完全虚拟化。而Xen早期则是基于软件模拟的Para-Virtualization,新版本则是基于硬件支持的完全虚拟化。但Xen本身有自己的进程调度器,存储管理模块等,所以代码较为庞大。广为流传的商业系统虚拟化软件VMware ESX系列是基于软件模拟的Full-Virtualization。

1、环境准备

2、规划说明

开启之后修改IP地址:
本次规划IP地址
IP地址 硬盘大小 内存大小
10.0.0.88 8G 2G

3、虚拟化概念

1.软件模拟

优点:能够模拟任何硬件,包括不存在的
缺点:功能非常低效,一般用于研究,生产环境不同。
代表:QEM

2.虚拟化层翻译

2.1 软件全虚拟化,通过虚拟化层翻译 ===> 你和美国人说话 ---需要通过翻译=VMware
2.2 半虚拟化  改动虚拟机的内核(linux)加上虚拟化层翻译
======>学了英语在和美国人交流   xen(被淘汰)
2.3 硬件支持的全虚拟化   ===>你重生到美国和美国人交流  KVM
3.容器虚拟化 docker

安装

KVM已经集成了,我们需要安装管理软件
  1. [root@kvm /]# egrep '(vmx|svm)' /proc/cpuinfo 
  2. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
  3. [root@kvm /]# yum -y install qemu-kvm qemu-kvm-tools virt-manager libvirt python-virtinst libvirt-python
  4. [root@kvm yum.repos.d]# rpm -qa qemu-kvm qemu-kvm-tools virt-manager libvirt python-virtinst libvirt-python
  5. libvirt-python-0.10.2-60.el6.x86_64
  6. python-virtinst-0.600.0-29.el6.noarch
  7. virt-manager-0.9.0-31.el6.x86_64
  8. qemu-kvm-0.12.1.2-2.491.el6_8.1.x86_64
  9. libvirt-0.10.2-60.el6.x86_64
  10. qemu-kvm-tools-0.12.1.2-2.491.el6_8.1.x86_64
  11. [root@kvm yum.repos.d]# lsmod|grep kvm
  12. kvm_intel              55624  0 
  13. kvm                   341551  1 kvm_intel
查看我们的管理工具
  1. [root@kvm yum.repos.d]# rpm -ql qemu-kvm
  2. /etc/ksmtuned.conf
  3. /etc/modprobe.d/blacklist-kvm.conf
  4. /etc/rc.d/init.d/ksm
  5. /etc/rc.d/init.d/ksmtuned
  6. /etc/sasl2/qemu-kvm.conf
  7. /etc/sysconfig/ksm
  8. /etc/sysconfig/modules/kvm.modules
  9. /etc/udev/rules.d/80-kvm.rules
  10. /usr/libexec/qemu-kvm
  11. /usr/sbin/ksmtuned
  12. /usr/share/doc/qemu-kvm-0.12.1.2/COPYING
  13. /usr/share/doc/qemu-kvm-0.12.1.2/COPYING.LIB
  14. /usr/share/doc/qemu-kvm-0.12.1.2/Changelog
  15. /usr/share/doc/qemu-kvm-0.12.1.2/LICENSE
  16. /usr/share/doc/qemu-kvm-0.12.1.2/README
  17. /usr/share/doc/qemu-kvm-0.12.1.2/README.systemtap
  18. /usr/share/doc/qemu-kvm-0.12.1.2/TODO
  19. /usr/share/doc/qemu-kvm-0.12.1.2/qemu-doc.html
  20. /usr/share/doc/qemu-kvm-0.12.1.2/qemu-tech.html
  21. /usr/share/man/man1/qemu-kvm.1.gz
  22. /usr/share/qemu-kvm
  23. /usr/share/qemu-kvm/bios.bin
  24. /usr/share/qemu-kvm/dump-guest-memory.py
  25. /usr/share/qemu-kvm/dump-guest-memory.pyc
  26. /usr/share/qemu-kvm/dump-guest-memory.pyo
  27. /usr/share/qemu-kvm/extboot.bin
  28. /usr/share/qemu-kvm/keymaps
  29. /usr/share/qemu-kvm/keymaps/ar
  30. /usr/share/qemu-kvm/keymaps/common
  31. /usr/share/qemu-kvm/keymaps/da
  32. /usr/share/qemu-kvm/keymaps/de
  33. /usr/share/qemu-kvm/keymaps/de-ch
  34. /usr/share/qemu-kvm/keymaps/en-gb
  35. /usr/share/qemu-kvm/keymaps/en-us
  36. /usr/share/qemu-kvm/keymaps/es
  37. /usr/share/qemu-kvm/keymaps/et
  38. /usr/share/qemu-kvm/keymaps/fi
  39. /usr/share/qemu-kvm/keymaps/fo
  40. /usr/share/qemu-kvm/keymaps/fr
  41. /usr/share/qemu-kvm/keymaps/fr-be
  42. /usr/share/qemu-kvm/keymaps/fr-ca
  43. /usr/share/qemu-kvm/keymaps/fr-ch
  44. /usr/share/qemu-kvm/keymaps/hr
  45. /usr/share/qemu-kvm/keymaps/hu
  46. /usr/share/qemu-kvm/keymaps/is
  47. /usr/share/qemu-kvm/keymaps/it
  48. /usr/share/qemu-kvm/keymaps/ja
  49. /usr/share/qemu-kvm/keymaps/lt
  50. /usr/share/qemu-kvm/keymaps/lv
  51. /usr/share/qemu-kvm/keymaps/mk
  52. /usr/share/qemu-kvm/keymaps/modifiers
  53. /usr/share/qemu-kvm/keymaps/nl
  54. /usr/share/qemu-kvm/keymaps/nl-be
  55. /usr/share/qemu-kvm/keymaps/no
  56. /usr/share/qemu-kvm/keymaps/pl
  57. /usr/share/qemu-kvm/keymaps/pt
  58. /usr/share/qemu-kvm/keymaps/pt-br
  59. /usr/share/qemu-kvm/keymaps/ru
  60. /usr/share/qemu-kvm/keymaps/sl
  61. /usr/share/qemu-kvm/keymaps/sv
  62. /usr/share/qemu-kvm/keymaps/th
  63. /usr/share/qemu-kvm/keymaps/tr
  64. /usr/share/qemu-kvm/linuxboot.bin
  65. /usr/share/qemu-kvm/multiboot.bin
  66. /usr/share/qemu-kvm/pxe-e1000.bin
  67. /usr/share/qemu-kvm/pxe-ne2k_pci.bin
  68. /usr/share/qemu-kvm/pxe-pcnet.bin
  69. /usr/share/qemu-kvm/pxe-rtl8139.bin
  70. /usr/share/qemu-kvm/pxe-virtio.bin
  71. /usr/share/qemu-kvm/sgabios.bin
  72. /usr/share/qemu-kvm/simpletrace.py
  73. /usr/share/qemu-kvm/simpletrace.pyc
  74. /usr/share/qemu-kvm/simpletrace.pyo
  75. /usr/share/qemu-kvm/systemtap/conf.d/qemu_kvm.conf
  76. /usr/share/qemu-kvm/systemtap/script.d/qemu_kvm.stp
  77. /usr/share/qemu-kvm/trace-events
  78. /usr/share/qemu-kvm/vapic.bin
  79. /usr/share/qemu-kvm/vgabios-cirrus.bin
  80. /usr/share/qemu-kvm/vgabios-qxl.bin
  81. /usr/share/qemu-kvm/vgabios-stdvga.bin
  82. /usr/share/qemu-kvm/vgabios-vmware.bin
  83. /usr/share/qemu-kvm/vgabios.bin
  84. /usr/share/systemtap/tapset/qemu-kvm-simpletrace.stp
  85. /usr/share/systemtap/tapset/qemu-kvm.stp
软件包解释
  1. [root@m01 kvm]# rpm -qa|grep -E 'qemu|libvirt|virt'
  2. libvirt-python-0.10.2-60.el6.x86_64      #libvirt的图形化虚拟机管理软件,需要图形界面操作系统
  3. virt-what-1.11-1.2.el6.x86_64            #基于Libvirt的图像化虚拟机管理软件,需要图形界面操作系统
  4. qemu-img-0.12.1.2-2.491.el6_8.1.x86_64   #用于操作虚拟机硬盘镜像的创建、查看和格式化转化
  5. gpxe-roms-qemu-0.9.7-6.15.el6.noarch     #虚拟机IPXE的启动固件,支持虚拟机从网络启动
  6. libvirt-client-0.10.2-60.el6.x86_64      #Libvirt的客户端,最重要的功能之一就是在宿主机关机时可以通过虚拟机也关机,使虚拟机系统正常关机,而不是被强制关机,造成数据丢失
  7. python-virtinst-0.600.0-29.el6.noarch    #一套Python的虚拟机安装工具
  8. virt-manager-0.9.0-31.el6.x86_64         #基于Libvirt的图像化虚拟机管理软件,需要图形界面操作系统
  9. qemu-kvm-0.12.1.2-2.491.el6_8.1.x86_64   #KVM在用户运行的程序
  10. libvirt-0.10.2-60.el6.x86_64             #用于管理虚拟机,它提供了一套虚拟机操作API
  11. qemu-kvm-tools-0.12.1.2-2.491.el6_8.1.x86_64
加入host 解析
  1. vim /etc/host
  2. 10..0.88 kmv
启动服务(libvirt)管理kvm
  1. [root@kvm yum.repos.d]# /etc/init.d/libvirtd start
  2. Starting libvirtd daemon:                                  [  OK  ]
生成了几个网卡
  1. [root@kvm yum.repos.d]# ip add
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
  3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4.     inet 127.0.0.1/8 scope host lo
  5.     inet6 ::1/128 scope host 
  6.        valid_lft forever preferred_lft forever
  7. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  8.     link/ether 00:0c:29:30:e9:bc brd ff:ff:ff:ff:ff:ff
  9.     inet 10.0.0.88/24 brd 10.0.0.255 scope global eth0
  10.     inet6 fe80::20c:29ff:fe30:e9bc/64 scope link 
  11.        valid_lft forever preferred_lft forever
  12. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  13.     link/ether 00:0c:29:30:e9:c6 brd ff:ff:ff:ff:ff:ff
  14.     inet 172.16.1.88/24 brd 172.16.1.255 scope global eth1
  15.     inet6 fe80::20c:29ff:fe30:e9c6/64 scope link 
  16.        valid_lft forever preferred_lft forever

  17. 4: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
  18.     link/ether 52:54:00:57:95:13 brd ff:ff:ff:ff:ff:ff
  19.     inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
  20. 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
  21.     link/ether 52:54:00:57:95:13 brd ff:ff:ff:ff:ff:ff
DNS小工具
  1. [root@kvm yum.repos.d]# ps -ef|grep dns
  2. nobody     2735      1  0 11:31 ?        00:00:00 /usr/sbin/dnsmasq --strict-order --pid-file=/var/run/libvirt/network/default.pid --conf-file= --except-interface lo --bind-interfaces --listen-address 192.168.122.1 --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases --dhcp-lease-max=253 --dhcp-no-override --dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile --addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
  3. root       2793   2272  0 11:32 pts/1    00:00:00 grep dns
管理dhcp的一些功能。
1.创建虚拟磁盘
  1. [root@kvm yum.repos.d]# qemu-img create -f raw /opt/kvm.raw 10G
  2. Formatting '/opt/kvm.raw', fmt=raw size=10737418240 
  3. ================================================================
  4. [root@kvm yum.repos.d]# df -h                      
  5. Filesystem      Size  Used Avail Use% Mounted on
  6. /dev/sda3       6.6G  1.9G  4.4G  31% /
  7. tmpfs           932M     0  932M   0% /dev/shm
  8. /dev/sda1       190M   36M  145M  20% /boot
2.挂载镜像光盘