CentOS 6.5下安装KVM


一、检查CPU是否支持虚拟化。(在主板BIOS中开启CPU的VirtualizationTechnology)

# grep -E -o 'vmx|svm' /proc/cpuinfo

vmx


二、基础环境配置

1.安装KVM软件包

# yum -y groupinstall 'Virtualization' 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'


查看模块

# lsmod | grep kvm

kvm_intel              53484  6

kvm                   316506  1 kvm_intel


2.网卡桥接配置


2.1 如果启用了NetworkManager,最好关闭该服务,因为该服务与network有冲突。

chkconfig NetworkManager off

service NetworkManager stop


2.2 关闭 selinux 并重启系统

# vi /etc/sysconfig/selinux

SELINUX=disabled


2.3 编辑网卡


# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

BOOTPROTO=none

BRIDGE=br0


# vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0

TYPE=Bridge

ONBOOT=yes

BOOTPROTO=none

IPADDR=192.168.200.15

NETMASK=255.255.255.0

GATEWAY=192.168.200.1

DNS1=202.106.0.20


2.4 重启服务器

reboot


2.5 查看网络

# brctl show

bridge name bridge idSTP enabled

interfaces

br0 8000.ac4e914d0aa2no

eth0

virbr0 8000.5254004d5befyes

virbr0-nic


2.6 检查ip转发是否开启

# cat /etc/sysctl.conf |grep ip_forward

net.ipv4.ip_forward = 0 


3.创建虚拟机

3.1 创建磁盘镜像文件,文件格式qcow2格式是kvm支持的标准格式,raw格式为虚拟磁盘文件通用格式。

创建文件名win2008r2.qcow2的磁盘镜像文件名,文件格式为qcow2,磁盘大小为100G

# qemu-img create -f qcow2 /kvm/pjgl/win2008r2.qcow2 100G


查看磁盘镜像文件信息 

# qemu-img info /kvm/pjgl/win2008r2.qcow2


3.2 创建虚拟机配置文件


<!--

WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE 

OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:

  virsh edit win2008r2

or other application using the libvirt API.

-->



<domain type='kvm'>

  <name>win2008r2</name>

  <uuid>7b6f172b-7a03-c629-f6cb-4127bdbdfb51</uuid>

  <memory unit='KiB'>2097152</memory>

  <currentMemory unit='KiB'>2097152</currentMemory>

  <vcpu placement='static'>2</vcpu>

  <os>

    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>

    <boot dev='cdrom'/>

    <boot dev='hd'/>



  </os>

  <features>

    <acpi/>

    <apic/>

    <pae/>

  </features>

  <clock offset='localtime'>

    <timer name='rtc' tickpolicy='catchup'/>

  </clock>

  <on_poweroff>destroy</on_poweroff>

  <on_reboot>restart</on_reboot>

  <on_crash>restart</on_crash>

  <devices>

    <emulator>/usr/libexec/qemu-kvm</emulator>

    <disk type='file' device='disk'>

      <driver name='qemu' type='qcow2' cache='writeback'/>

      <source file='/kvm/win2008r2/win2008r2.qcow2'/>

      <target dev='hda' bus='ide'/>

      <address type='drive' controller='0' bus='0' target='0' unit='0'/>

    </disk>

    <disk type='file' device='cdrom'>

      <driver name='qemu' type='raw'/>

      <source file='/iso/Windows_2008R2_64-bit.ISO'/>

      <target dev='hdc' bus='ide'/>

      <readonly/>

      <address type='drive' controller='0' bus='1' target='0' unit='0'/>

    </disk>

    <controller type='usb' index='0' model='ich9-ehci1'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>

    </controller>

    <controller type='usb' index='0' model='ich9-uhci1'>

      <master startport='0'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>

    </controller>

    <controller type='usb' index='0' model='ich9-uhci2'>

      <master startport='2'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>

    </controller>

    <controller type='usb' index='0' model='ich9-uhci3'>

      <master startport='4'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>

    </controller>

    <controller type='ide' index='0'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>

    </controller>

    <interface type='bridge'>

      <mac address='52:54:00:b3:c6:d3'/>

      <source bridge='br0'/>

      <model type='e1000'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

    </interface>

    <serial type='pty'>

      <target port='0'/>

    </serial>

    <console type='pty'>

      <target type='serial' port='0'/>

    </console>

    <input type='tablet' bus='usb'/>

    <input type='mouse' bus='ps2'/>

    <graphics type='vnc' port='5903' autoport='no' listen='0.0.0.0'>

      <listen type='address' address='0.0.0.0'/>

    </graphics>

    <video>

      <model type='vga' vram='9216' heads='1'/>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>

    </video>

    <memballoon model='virtio'>

      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>

    </memballoon>

  </devices>

</domain>



将上面的内容复制到win2008r2.xml文件后,根据情况修改系统名称、UUID、MAC地址、光盘镜像的路径、磁盘镜像的路径等。


3.3 启动虚拟机

# virsh

# define /kvm/pjgl/win2008r2.xml

# list --all

# start win2008r2


3.4 配置防火墙策略

防火墙缺省是不允许连接这些端口的,使用下列步骤配置防火墙,允许VNC客户端连接VNC server。

# vi /etc/sysconfig/iptables  

1) 找到下面的语句:

-A INPUT -j REJECT --reject-with icmp-host-prohibited

在此行之前,加上下面的内容:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5900:5920 -j ACCEPT   #允许其它机器访问本机的5900到5903端口

2) 然后使用root身份重新启动防火墙:

# service iptables restart

3) 设置开机自动启动

# chkconfig iptables on


3.5 使用TightVNC Viewer客户端,进入系统UI界面安装操作系统,并进行配置即可。