1、kvm克隆需要将源主机处于暂停或关机状态,常用命令:

virsh>list --all  列出所有主机

virsh>start  domain1  开启domain1虚机

virsh>shutdown  domain1  关闭domain1虚机

virsh>destroy  domain1  断电domain1虚机

virsh>autostart  domain1  自动启动domain1虚机

virsh>undefine domain1  删除domain1虚机

virsh>define domain1.xml  添加domain1虚机

virsh>suspend domain1  暂停domain1虚机

virsh>resume domain1  恢复暂停domain1虚机

 

2、克隆方法:

(1)、 Clone the guest called "demo" on the default connection, auto generating a new name and disk clone path.

         # virt-clone \
              --original demo \
              --auto-clone

(2)、 Clone the guest called "demo" which has a single disk to copy 

         # virt-clone \
              --original demo \
              --name newdemo \
              --file /var/lib/xen/p_w_picpaths/newdemo.img

(3)、Clone a QEMU guest with multiple disks

         # virt-clone \
              --connect qemu:///system \
              --original demo \
              --name newdemo \
              --file /var/lib/xen/p_w_picpaths/newdemo.img \
              --file /var/lib/xen/p_w_picpaths/newdata.img

(4)、Clone a guest to a physical device which is at least as big as the original guests disks. If the destination device is bigger, the new guest can do a filesystem resize when it boots.

         # virt-clone \
              --connect qemu:///system \
              --name demo \
              --file /dev/HostVG/DemoVM \
              --mac 52:54:00:34:11:54

(5)、个人常用的克隆方法:

#virt-clone -o rhel5.4_32_2 -n rhel5.4_32_3 -f /dev/libvirt_lvm/rhel5.4-3  -m 52:54:00:31:15:40

 

3、克隆完成后,需要修改 /etc/libvirt/qemu/下,新生成虚拟机的.xml文件。

将vnc端口修改成未用的。    <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'  passwd='zhao'

 

4、编辑虚机镜像文件

 #virt-edit /dev/libvirt_lvm/rhel5.4-3 /etc/sysconfig/network-scripts/ifcfg-eth0

   # Virtio Network Device
   DEVICE=eth0
   BOOTPROTO=static
   HWADDR=52:54:00:56:71:FG (和-m 52:54:00:31:15:40 一致)
   IPADDR=192.168.199.117
   NETMASK=255.255.255.0
   ONBOOT=yes

5、遗留问题:

启动机器后ifcfg-eth0变成ifcfg-eth0.bak,重命名后,重启网卡即可正常。

解决方法:

在被克隆主机添加以下命令

#vim  /etc/rc.local

if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0.bak ];then
    mv /etc/sysconfig/network-scripts/ifcfg-eth0.bak /etc/sysconfig/network-scripts/ifcfg-eth0
    service network restart
fi