如果两台服务器之间没有共享存储(因此无法进行标准迁移),将虚拟机从一台服务器克隆到另一台服务器的最简单方法是什么?
我在一台服务器上安装了生产准备好的VM,我想将它克隆到另一个系统上。 我没有两个主机之间的共享存储,但我已经复制了两台主机之间的磁盘映像,并添加了一个configuration(virsh定义它)。 当我尝试启动它,但它不需要:
# virsh create /etc/libvirt/qemu/cloned-vm.xml error: Failed to create domain from /etc/libvirt/qemu/cloned-vm.xml error: Unable to read from monitor: Connection reset by peer
我在RHEL6上使用KVM。 这是重复的configuration
<!-- 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 or other application using the libvirt API. --> <domain type='kvm'> <name>cloned-vm</name> <uuid>NEW_UUID_HERE</uuid> <memory>15360000</memory> <currentMemory>15360000</currentMemory> <vcpu>7</vcpu> <os> <type arch='x86_64' machine='rhel6.2.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='raw' cache='none'/> <source file='/local/vm/cloned-vm.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </disk> <interface type='bridge'> <mac address='NE:W_:MA:C_:AD:DR'/> <source bridge='br2'/> <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='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport='yes'/> <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='0x05' function='0x0'/> </memballoon> </devices> </domain>
这就是我所做的,其中$ VM是VM名称,DEST是目标pipe理程序主机名。 它使用dd和快照LVM磁盘(假设LVM组名为HypGroup00)运行虚拟机。
我只是把它扔在一起,所以它不一定是最漂亮的,但是做这个工作,我用它把CentOS 5.9pipe理程序中的一些虚拟机迁移到CentOS 6上,停机时间最less。
这是CentOS 5.9,在CentOS 5和6作为目的地进行testing。
VM=webserver DEST=hyp5
找出要复制的磁盘,因为我们的大多数VM磁盘都是通过/ dev / mapper /path而不是/ dev / volgroup / volnamepath引用的。 所以我们需要在两者之间进行翻译。
DISKS=`cat /etc/libvirt/qemu/$VM.xml | grep HypGroup00 | sed "s|.*/\(HypGroup00-.*\)'/>|\1|"` LVS="" for disk in $DISKS; do echo VM Disk $disk LV=`lvdisplay /dev/mapper/$disk | grep "LV Name" | awk '{print $3}'` LVS="$LVS $LV" done
复制VM定义并注册
virsh dumpxml $VM > /tmp/$VM.xml scp /tmp/$VM.xml $DEST:/tmp/ ssh $DEST virsh undefine $VM > /dev/null 2>&1 ssh $DEST virsh define /tmp/$VM.xml
现在在远程服务器上创buildLV
for lv in $LVS; do ssh $DEST lvremove --force $lv SIZE=`lvdisplay $lv | grep "LV Size" | awk '{print $3}'` SHORTNAME=`basename $lv` ssh $DEST lvcreate -L"$SIZE"G -n$SHORTNAME HypGroup00 done
现在创build快照LV并开始复制数据
virsh suspend $VM for lv in $LVS; do lvcreate -L10G -s -n $lv-snapshot $lv done virsh resume $VM
复制磁盘
for lv in $LVS; do echo Copying LV $lv, this will take a while... time dd bs=1M if=$lv-snapshot | gzip --fast | ssh $DEST "gzip -d | dd of=$lv" done
上述更复杂的版本,如果需要从dd显示进度,由于/ tmp / pid不适用于多个副本,但是如果您愿意,可以更改为包含$$。
(dd bs=1M if=$lv-snapshot & echo $! >&3 ) 3>/tmp/pid 2> >(grep 'copied' 1>&2) | ssh $DEST "dd bs=1M of=$lv" & # Need this sleep to give the above time to run sleep 1 PID=$(</tmp/pid) while kill -0 $PID; do kill -USR1 $PID sleep 5 done
清理
for lv in $LVS; do lvremove --force $lv-snapshot done
好吧,我这样做的方式实际上工作得很好。 问题是我没有足够的资源来运行该虚拟机。 所以只是为了回答我自己的问题…以下是我如何在不使用共享磁盘的情况下在不同的服务器上执行VM复制的详细信息。
由于您没有共享磁盘,因此无法进行典型的“克隆”,然后进行“迁移”。 相反,你做一个典型的克隆
下面是执行克隆的命令( / local / vm /是VM镜像的path,通常是/ var / something / ):
virt-clone –original = vm-to-clone –name = cloned -vm -f /local/vm/cloned-vm.img –mac = xx:xx:xx:xx:xx:xx
现在将这个img文件从一台服务器复制到另一台服务器上…我的服务器不能直接与对方通话,所以我使用这个小小的SSHredirect来实现:
ssh -n server1'(cd / local / vm /; cat clone-vm.img)'| ssh server2'(cd / local / vm /; cat> cloned-vm.img)'
然后将该VM的configuration复制到:
ssh -n server1'(cd / etc / libvirt / qemu /; cat cloned-vm.xml)'| ssh server2'(cd / etc / libvirt / qemu /; cat> cloned-vm.xml)'
用任何新的更改更新configuration。 在我的情况(这是什么原因导致我的问题),我需要降低“内存”和“currentMemory”属性。
添加新的虚拟机到libvirt:
virsh定义/etc/libvirt/qemu/cloned-vm.xml
运行:
virsh创build/etc/libvirt/qemu/cloned-vm.xml