===========================
yum install qemu-kvm qemu-kvm-tools python-virtinst qemu-img bridge-utils libvirt libguestfs-tools-c
/etc/sysconfig/network-scripts/ifcfg-em2
DEVICE=em2
TYPE=Ethernet
BOOTPROTO=static
ONBOOT=yes
BRIDGE=br2
/etc/sysconfig/network-scripts/ifcfg-br2
DEVICE=br2
TYPE=Bridge
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.21.14
GATEWAY=192.168.21.1
NETMASK=255.255.255.0
reboot后,删除 virbr0
virbr0 使用的是 NAT 模式,我们使用桥接,所以删除它。
virsh net-list
virsh net-destroy default
virsh net-undefine default
/etc/init.d/libvirtd restart
===========================
1. 创建磁盘
首先,需要创建一个镜像文件,可以使用 dd 工具,如下的命令行创建了一个 8GB 大小
的镜像文件 rhel6u3.img:
[[email protected] kvm_demo]# dd if=/dev/zero of=rhel6u3.img bs=1 count=0 seek=100G
8192+0 records in
8192+0 records out
8589934592 bytes (8.6 GB) copied, 76.9331 s, 112 MB/s
然后,准备一个 RHEL6.3 安装所需的 ISO 文件,如下所示:
2. 创建虚拟机
virt-install --name test \
--ram=1024 --arch=x86_64 \
--vcpus=2 \
--check-cpu \
--os-type=linux \
--os-variant=rhel6 \
--clock offset=localtime \
--cdrom=/data/CentOS-6.5-x86_64-bin-DVD1.iso \
--disk path=/data/kvmhost/test.img,device=disk,bus=virtio,size=20,sparse=true \
--boot hd,cdrom,menu=on \
--network bridge=br2,model=virtio \
--autostart \
--graphics vnc,password=d6EWe8rEMaBaCe3a,port=5903,listen=0.0.0.0,keymap=en-us
virt-install --name windows2008-ad \
--ram=4096 --arch=x86_64 \
--vcpus=4 \
--check-cpu \
--os-type=windows \
--os-variant=win2k8 \
--cdrom=/data/cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_vl_build_x64_dvd_617396.iso \
--disk path=/data/kvmhost/windows2008-ad.vfd,bus=ide,size=100 \
--boot hd,cdrom,menu=on \
--network bridge=br2,model=virtio \
--autostart \
--graphics vnc,password=d6EWe8rEMaBaCe3a,port=5901,listen=0.0.0.0,keymap=en-us
注意:http://www.linux-kvm.org/page/WindowsGuestDrivers/Download_Drivers windows网卡驱动
然后在windows机器上用 vncviewer64.exe 连接即可。
另:使用虚拟网络:--network network=nor_route,model=virtio \
http://sourceforge.net/projects/tigervnc/files/tigervnc/1.3.1/
3. 虚拟机克隆
virt-clone -o centos6.5-moban -n centos6.5-pxe -f /data/kvmhost/centos6.5-pxe.img
virt-edit -d centos6.5-pxe /etc/udev/rules.d/70-persistent-net.rules
virt-edit -d centos6.5-pxe /etc/sysconfig/network-scripts/ifcfg-eth0
4.导入虚拟机镜像
virt-install ... --disk [path to your existing image]
--import
...
virt-install --name centos6.5-moban \
--ram=1024 --arch=x86_64 \
--vcpus=2 \
--check-cpu \
--os-type=linux \
--os-variant=rhel6 \
--disk path=/data/kvmhost/centos6.5-moban.img,device=disk,bus=virtio,size=20,sparse=true \
--bridge=br2,model=virtio \
--autostart \
--graphics vnc,password=d6EWe8rEMaBaCe3a,port=-1,listen=0.0.0.0,keymap=en-us \
--boot hd,cdrom,menu=on \
--import
5.修改已有配置文件
virsh dumpxml centos6.5-mantis > centos6.5-mantis.xml
修改centos6.5-mantis.xml
修改vnc监听地址和密码
<graphics type=‘vnc‘ port=‘-1‘ autoport=‘yes‘ passwd=‘d6EWe8rEMaBaCe3a‘ keymap=‘en-us‘>
<listen type=‘address‘ address=‘0.0.0.0‘/>
</graphics>
virsh define centos6.5-mantis.xml # 使配置文件生效
6.常用操作
# virsh list --all
Id Name State
----------------------------------------------------
2 centos6.5-monitor running
11 centos6.5-test1 running
12 centos6.5-test2 running
- centos6.5-moban shut off
virsh shutdown centos6.5-mantis #正常关闭
virsh destroy centos6.5-mantis #关闭电源
virsh start centos6.5-mantis # 启动虚拟机
# virsh dominfo centos6.5-test2
Id: 12
Name: centos6.5-test2
UUID: 4d2b6713-98d6-1afc-d3ea-a030052b3d87
OS Type: hvm
State: running
CPU(s): 1
CPU time: 25.5s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c68,c124 (permissive)
# virsh iface-list --all
Name State MAC Address
--------------------------------------------
br2 active f0:1f:af:e5:ae:ad
em1 inactive f0:1f:af:e5:ae:ac
lo active 00:00:00:00:00:00
7.在线加硬盘
dd if=/dev/zero of=centos6.5-test1-sdc.img bs=1 count=0 seek=5G
linux disk:
virsh attach-disk centos6.5-test1 /data/kvmhost/centos6.5-test1-sdc.img vdc --config
windows iso:
virsh attach-disk windows2008-ad /data/qdrs.iso hdc --type cdrom --config
8.在线迁移
参考:http://www.cnblogs.com/fengrenzw/p/3339918.html
在源宿主机上执行,要在目的宿主机上提前创建好硬盘(路径同源主机)
virsh migrate centos6.5-test qemu+ssh://192.168.21.14/system --live --unsafe
192.168.21.14为目的宿主机