KVM简介:

KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机)。

是x86架构且硬件支持虚拟化技术(如 intel VT 或 AMD-V)的Linux全虚拟化解决方案。

它包含一个为处理器提供底层虚拟化 可加载的核心模块kvm.ko(kvm-intel.ko或kvm-AMD.ko)。

KVM还需要一个经过修改的QEMU软件(qemu-kvm),作为虚拟机上层控制和界面。

KVM能在不改变linux或windows镜像的情况下同时运行多个虚拟机,(它的意思是多个虚拟机使用同一镜像)并为每一个虚拟机配置个性化硬件环境(网卡、磁盘、图形适配器……)。

在主流的Linux内核,如2.6.20以上的内核均已包含了KVM核心。

安装kvm虚拟机之前准备工作:

1.关闭iptables防火墙和selinux。

[root@taokey ~]# /etc/init.d/iptables stop[root@taokey ~]# chkconfig iptables off[root@taokey ~]# chkconfig --list iptablesiptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off[root@taokey ~]# vim /etc/sysconfig/selinuxSELINUX=disabled[root@taokey ~]# setenforce 0

2.vmware workstation安装kvm的时候,需要开启CPU支持虚拟化。

            下边开始介绍Centos6.5 x86_64系统安装kvm虚拟机

一、安装kvm虚拟机

1.查看CPU是否支持kvm完全虚拟机。

[root@taokey ~]# grep "flags" /proc/cpuinfoflags           : 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 xsave avx hypervisor lahf_lm arat epb xsaveopt pln pts dts tpr_shadow vnmi ept vpid

注释:如果输出的信息中有vmx,说明intel处理器支持完全虚拟化。如果显示svm,说明是AMD的处理器支持虚拟化。

2.安装kvm和其他虚拟化所需管理软件包。

[root@taokey ~]# yum install -y kvm virt-* libvirts bridge-utils qemu-img

软件包名称介绍:

Kvm:软件包中含有KVM内核模块,它在默认linux内核中提供kvm管理程序

Libvirts:安装虚拟机管理工具,使用virsh等命令来管理和控制虚拟机。

Bridge-utils:设置网络网卡桥接。

Virt-*:创建、克隆虚拟机命令,以及图形化管理工具virt-manager

Qemu-img:安装qemu组件,使用qemu命令来创建磁盘等。

3.加载kvm模块,查看kvm模块是否被加载

[root@taokey ~]# modprobe kvm-intel[root@taokey ~]# lsmod | grep kvmkvm_intel              54285  0 kvm                   333172  1 kvm_intel

4.重启确实KVM是否被加载

[root@taokey ~]# reboot          [root@taokey ~]# lsmod | grep kvmkvm_intel              54285  0 kvm                   333172  1 kvm_intel

5.查看已打开虚拟机列表

[root@taokey ~]# virsh list Id    Name                           State----------------------------------------------------

6.配置eth0、br0网卡

[root@taokey ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0     DEVICE=eth0HWADDR=00:0C:29:33:B3:E5TYPE=EthernetUUID=142284ae-3173-4b0a-80d8-437c26a96719ONBOOT=yesNM_CONTROLLED=yesBOOTPROTO=noneBRIDGE=br0[root@taokey ~]# more /etc/sysconfig/network-scripts/ifcfg-br0     DEVICE=br0HWADDR=00:0C:29:33:B3:E5TYPE=BridgeUUID=142284ae-3173-4b0a-80d8-437c26a96719ONBOOT=yesNM_CONTROLLED=yesBOOTPROTO=staticIPADDR=10.0.1.149NETMASK=255.255.255.0GATEWAY=10.0.1.1[root@taokey ~]# /etc/init.d/network restart

7.查看目前所有的网桥接口

[root@taokey ~]# brctl showbridge name     bridge id               STP enabled     interfacesbr0             8000.000c2933b3e5       no              eth0virbr0          8000.525400d969b8       yes             virbr0-nic

8.修改VNC服务端的配置文件

[root@taokey ~]# vim /etc/libvirt/qemu.conf  vnc_listen = "0.0.0.0"   第十二行,把vnc_listen前面的#号去掉。

9.重启libvirtd和messagebus服务

[root@taokey ~]# /etc/init.d/libvirtd restartStopping libvirtd daemon:                                  [  OK  ]Starting libvirtd daemon: libvirtd: initialization failed  [FAILED]解决办法:[root@taokey libvirt]# echo "export LC_ALL=en_US.UTF-8"  >>  /etc/profile[root@taokey libvirt]# source /etc/profile[root@taokey libvirt]# /etc/init.d/libvirtd restartStopping libvirtd daemon:                                  [FAILED]Starting libvirtd daemon: 2014-08-15 16:14:18.595+0000: 2429: info : libvirt version: 0.10.2, package: 29.el6_5.11 (CentOS BuildSystem 
, 2014-07-31-18:52:08, c6b8.bsys.dev.centos.org)2014-08-15 16:14:18.595+0000: 2429: warning : virGetHostname:2294 : getaddrinfo failed for 'taokey': Name or service not known                                                           [  OK  ][root@taokey ~]# /etc/init.d/messagebus restartStopping system message bus:                               [  OK  ]Starting system message bus:                               [  OK  ]

二、在宿主机上创建、安装kvm虚拟机

  1. 创建镜像文件目录和虚拟磁盘存放目录

[root@taokey ~]# mkdir /iso[root@taokey ~]# mkdir -p /data/kvmdisk

2.把Centos6.5系统的镜像文件拷贝到iso目录下

[root@taokey ~]# dd if=/dev/cdrom of=/iso/Centos6.iso8726528+0 records in8726528+0 records out4467982336 bytes (4.5 GB) copied, 298.268 s, 15.0 MB/s

3.创建kvm虚拟机的磁盘文件

[root@taokey ~]# cd /etc/libvirt/[root@taokey libvirt]# qemu-img create -f qcow2 -o preallocation=metadata kvm_mode.img 10G        Formatting 'kvm_mode.img', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 preallocation='metadata'

4.安装虚拟机

[root@taokey libvirt]# virt-install --name=kvm_mode_5566 --ram 1024 --vcpus=1 -f /etc/libvirt/kvm_mode.img --cdrom=/iso/Centos6.iso --graphics vnc,listen=0.0.0.0,port=7788 --force --autostartStarting install...ERROR    internal error Process exited while reading console log output: char device redirected to /dev/pts/2qemu-kvm: -drive file=/etc/libvirt/kvm_mode.img,if=none,id=drive-ide0-0-0,format=raw,cache=none: could not open disk p_w_picpath /etc/libvirt/kvm_mode.img: Permission deniedDomain installation does not appear to have been successful.If it was, you can restart your domain by running:  virsh --connect qemu:///system start kvm_mode_5566otherwise, please restart your installation.解决办法:[root@taokey libvirt]# chmod -R 777 /etc/libvirt[root@taokey libvirt]# virt-install --name=kvm_mode_5566 --ram 1024 --vcpus=1 -f /etc/libvirt/kvm_mode.img --cdrom=/iso/Centos6.iso --graphics vnc,listen=0.0.0.0,port=7788 --force --autostartStarting install...Creating domain...                                                                                           |    0 B     00:00     Cannot open display: Run 'virt-viewer --help' to see a full list of available command line optionsDomain installation still in progress. You can reconnect to the console to complete the installation process.

三、VNC客户端连接KVM虚拟机,设置IP地址。

  1. 用VNC连接,进行创建kvm虚拟机(VNC连上之后,跟安装linux Centos 6.5系统一样,重新装一次)

2.在宿主机上列出创建的kvm虚拟机,手动开启新创建的虚拟机

[root@taokey ~]# virsh Welcome to virsh, the virtualization interactive terminal.Type:  'help' for help with commands       'quit' to quitvirsh # list --all Id    Name                           State---------------------------------------------------- -     kvm_mode_5566                  shut off  virsh # start kvm_mode_5566Domain kvm_mode_5566 started

3.设置静态IP地址

重启网卡会报错。

解决办法:

eth0网卡配置文件中的

HWADDR=00:0C:29:33:B3:E5修改成MACADDR=00:0C:29:33:B3:E5

rm -rf /etc/udev/rules.d/70-persistent-net.rules

如图所示:

reboot新创建的kvm_mode_5566虚拟机,重启network服务就可以配置上IP地址了。

[root@kvm-mode-7788 ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0     

DEVICE=eth0

MACADDR=52:54:00:80:7E:A5

TYPE=Ethernet

UUID=7d042162-53e9-4ef1-ba2f-bf3b892bb353

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=10.0.1.228

NETMASK=255.255.255.0

GATEWAY=10.0.1.1

[root@kvm-mode-7788 ~]# ping www.baidu.com

PING www.a.shifen.com (119.75.218.77) 56(84) bytes of data.

64 bytes from www.baidu.com (119.75.218.77): icmp_seq=1 ttl=54 time=3.05 ms

64 bytes from www.baidu.com (119.75.218.77): icmp_seq=2 ttl=54 time=4.82 ms

64 bytes from www.baidu.com (119.75.218.77): icmp_seq=3 ttl=54 time=3.72 ms

^C

--- www.a.shifen.com ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 2252ms

rtt min/avg/max/mdev = 3.055/3.866/4.822/0.730 ms  

4.查看下宿主机上的虚拟机。

[root@taokey ~]# virsh Welcome to virsh, the virtualization interactive terminal.Type:  'help' for help with commands       'quit' to quitvirsh # list --all Id    Name                           State---------------------------------------------------- 1     kvm_mode_5566                  runningvirsh #

到此为止,试验完毕。

下一篇会介绍通过命令克隆虚拟机和修改配置文件创建虚拟机。