kvm跨物理机迁移和克隆过程

来源:互联网 发布:c语言的头文件 编辑:程序博客网 时间:2024/06/08 11:14

最近需要将一批虚拟机从一台物理机A克隆到另一台物理机B,记录一下整个过程。
1.首先说一下跨物理机的迁移
这个比较简单,只要拷贝其磁盘文件和xml配置文件,再根据xml来创建域即可
①将磁盘文件和xml文件,利用scp命令即可。比如有虚拟机名为a.mongo.001,磁盘文件为/data/test/a.mongo.001.qcow2
scp a.mongo.001.qcow2 192.168.119.128:/data/test/
scp /etc/libvirt/qemu/a.mongo.001.xml 192.168.119.128:/data/test/
②根据xml文件创建域
virsh define a.mongo.001.xml
如果顺利这样就完成迁移,还是挺简单的。但是有个特殊的报错情况
迁移过程碰到一个特殊情况:
其他虚拟机均迁移顺利,但是唯独有一台,迁移后无法启动,总是报文件系统出错,启动时出现如下错误信息:

这里写图片描述

然后用qemu-img check这个磁盘,发现确实有问题,命令为:
[root@oss-iaas-kvm-local-002 backup]# qemu-img check a.mongo.001.qcow2
Warning: cluster offset=0x214920000 is after the end of the image file, can’t properly check refcounts.
Warning: cluster offset=0x214930000 is after the end of the image file, can’t properly check refcounts.
Warning: cluster offset=0x214940000 is after the end of the image file, can’t properly check refcounts.
Warning: cluster offset=0x214950000 is after the end of the image file, can’t properly check refcounts.
4 internal errors have occurred during the check.
Image end offset: 8935047168

参考这篇:https://forum.proxmox.com/threads/kvm-backup-error-since-migrate-to-2-3.13122/
将有问题的磁盘文件用convert进行复制,再check时发现错误不复存在,命令如下:
[root@oss-iaas-kvm-local-002 backup]# qemu-img convert -O qcow2 /data/backup/a.mongo.001.qcow2 /data/backup/a.mongo.0001.qcow2
[root@oss-iaas-kvm-local-002 backup]# qemu-img check a.mongo.0001.qcow2
No errors were found on the image.
Image end offset: 7886012416

2.跨物理机的克隆
我需要将虚拟机a.mongo.001从物理机A克隆到物理机B,名字为b.mongo.001,我碰到两种情况,一种是磁盘文件只有一个,另一种是磁盘文件有两个的情况。
1) 针对磁盘文件只有一个的情况
这种情况可以先按照上述迁移的过程在物理机B上创建一个a.mongo.001,然后利用克隆工具即可,命令为:
virt-clone -o a.mongo.001 -n b.mongo.001 -f b.mongo.001.qcow2
这样就创建完成了
2)而对于磁盘文件有多个的情况
假设a.mongo.001对应两个磁盘文件,分别是a.mongo.001.qcow2和a.mongo.001.add001.qcow2
如果按照上述的方法进行克隆,会报错
[root@oss-iaas-kvm-local-002 qemu]# virt-clone -o a.mongo.001 -n a.mongo.002 -f a.mongo.002.qcow2
ERROR A disk path must be specified to clone ‘/data/test/a.mongo.001.add001.qcow2’.
你可以参照如下方法进行解决:
http://www.360doc.com/content/14/0615/16/9075092_386817623.shtml

但是我尝试用了另一种方法,更改xml配置,然后创建
①先将磁盘文件a.mongo.001.qcow2和a.mongo.001.add001.qcow2和xml文件拷贝物理机B上,并重名为b.mongo.001.qcow2, b.mongo.001.add001.qcow2
②更改xml文件配置,需要更改的标签有:
1)name标签,kvm的名字
2)disk中source file配置,更改为重命名的磁盘文件,有几个磁盘文件就得改多少次。
3)uuid,mac地址,利用如下命令

uuidgen #生成uuidecho $RANDOM | md5sum | sed 's/\(..\)/&:/g' | cut -c1-17 #mac地址

用此方法生成mac地址在创建域的时候,有可能会报如下错误:
error: XML error: expected unicast mac address, found multicast ‘2f:5e:65:72:08:0e’
该错误没仔细研究,大概意思是需用的是单播地址,确生成了一个多播地址的mac,重新生成一个即可,看人品
③根据xml文件创建域

virsh define b.mongo.001.xml

如果一切顺利,就成功定义了一个名为b.mongo.001的kvm,但是定义后,需要更改此kvm的如下信息,你可以先启动,然后进系统手动修改,也可以利用virt-copy-in工具,将更改好的文件拷贝进去,需要在关机的情况使用此工具,这个可利用脚本实现。
需要更改的文件信息有:
1)主机名,文件/etc/sysconfig/network
2)IP地址,文件/etc/sysconfig/network-scripts/ifcfg-eth0
3)hosts,文件/etc/hosts
4)ssh自动验证
5)可能还有其他的一些应用程序,这个视情况而定

关于virt-copy-in
对于有多个磁盘文件kvm,利用该工具时需要注意的是不能用-a选项,需改为-d,对域进行操作。如下命令会报错
[root@oss-002 backup]# virt-copy-in hosts -a a.mongo.001.qcow2 /root/
guestfish: no operating system was found on this disk

If using guestfish ‘-i’ option, remove this option and instead
use the commands ‘run’ followed by ‘list-filesystems’.
You can then mount filesystems you want by hand using the
‘mount’ or ‘mount-ro’ command.

If using guestmount ‘-i’, remove this option and choose the
filesystem(s) you want to see by manually adding ‘-m’ option(s).
Use ‘virt-filesystems’ to see what filesystems are available.

If using other virt tools, this disk image won’t work
with these tools. Use the guestfish equivalent commands
(see the virt tool manual page).

3.远程克隆
远程克隆,意思是在远程登录机器,然后在远程机器上执行克隆的过程,并不是将某KVM从物理机A克隆到物理机B的过程,该过程叫kvm的迁移。详情参考:http://liuzhijun.iteye.com/blog/1744236
命令使用如下:
[root@os test]# virt-clone –connect=qemu+ssh://root@192.168.119.128/system -o oss.test.001 -n oss.test.002 -f /data/test/vmdisk/oss.test.002.img
root@192.168.119.128’s password:
Allocating ‘oss.test.002.img’ | 60 GB 00:04

Clone ‘oss.test.002’ created successfully.

0 0
原创粉丝点击