知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。

检查是否开启CPU虚拟化
cat /proc/cpuinfo | egrep "(vmx|svm)" --color=always
禁用selinux
vim /etc/selinux/config
SELINUX=disabled

setenforce 0
设置转发
sed -i 's/^\(net.ipv4.ip_forward =\).*/\1 1/' /etc/sysctl.conf; sysctl -p 
安装
yum -y install @virt* dejavu-lgc-* xorg-x11-xauth tigervnc libguestfs-tools policycoreutils-python
设置libvirt自动开启并重启
chkconfig libvirtd on;reboot
验证是否加载KVM模块
lsmod | grep kvm
创建虚拟机做为镜像
  • 查看可选用的操作系统

    virt-install --os-variant=list | more
    
  • 安装CentOS 6.5

    • 下载

      wget http://mirrors.aliyun.com/centos/6.5/isos/x86_64/CentOS-6.5-x86_64-minimal.iso
      
    • 创建磁盘文件

      qemu-img create -f qcow2 -o preallocation=metadata /data/centos65.qcow2 8G
      
    • 安装

      virt-install --os-variant=virtio26 --network model=virtio,bridge=br0 --disk /data/centos65.qcow2,sparse=false,bus=virtio,size=8 --cdrom=/data/CentOS-6.5-x86_64-minimal.iso --graphics vnc,listen=0.0.0.0,port=5930,password=123123 --vcpus=1 --ram=512 --name=vm01
      
    • VNC 连接

      打开finder -> connect to server
      vnc://10.211.55.50:5930
      
    • 启动

      virsh start vm01
      
创建虚拟机
  • 生成派生镜像

    qemu-img create -f qcow2 /data/centos3.qcow2 -o backing_file=/data/centos65.qcow2 8G
    
  • 编辑配置文件 centos3.xml

    vim /data/centos3.xml
    
    <domain type='kvm'>
        <name>vm03</name>
        <memory unit='KiB'>524288</memory>
        <currentMemory unit='KiB'>524288</currentMemory>
        <vcpu placement='static'>2</vcpu>
        <os>
            <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
            <boot dev='hd'/>
        </os>
        <features>
            <acpi/>
            <apic/>
            <pae/>
        </features>
        <clock offset='utc'/>
        <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='none'/>
                <source file='/data/centos3.qcow2'/>
                <target dev='vda' bus='virtio'/>
                <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
            </disk>
            <interface type='bridge'>
                <mac address='52:54:00:fd:77:3c'/>
                <source bridge='br0'/>
                <virtualport type='openvswitch'/>
                <model type='virtio'/>
                <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='mouse' bus='ps2'/>
                <graphics type='vnc' port='5931' autoport='no' listen='0.0.0.0' passwd='123123'>
                <listen type='address' address='0.0.0.0'/>
            </graphics>
            <video>
                <model type='cirrus' 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='0x06' function='0x0'/>
            </memballoon>
        </devices>
    </domain>
    
自定义虚拟机
virsh define /data/centos3.xml
启动
virsh start vm03
VNC 连接
打开finder -> connect to server
vnc://10.211.55.50:5931