您的位置:首页 > 其它

KVM——复制xml文件与磁盘文件克隆虚拟机

2016-04-10 03:10 405 查看
kvm虚拟机克隆有如下两种方式:

1.KVM本机虚拟机直接克隆

2.通过复制xml文件与磁盘文件复制克隆 (适用于异机的静态迁移和状态保存便于以后使用)。

本次主要介绍如何复制xml文件与磁盘文件克隆虚拟机:

简易版原理图:
A(模板)            B(克隆)
disk                disk
xml                 xml
需要更改内容          会变的内容

udev-mac            uuid
yum                 name
console=ttyS0        mac
网卡配置里的mac       disk
注释:
A为模板,为了不和B(克隆机)起冲突,主要是硬盘和网卡冲突,需要把A的udev注释,顺手配置好yum,console=ttyS0支持命令行操作,把网卡的uuid,mac信息删掉。
B为克隆机,为了区分出来,不和A起冲突,需要把uuid和mac删掉,把name和disk路径改成B的名字。
1.开机,进入模板主机,打开udev文件,注释里面内容,防止网卡复制为eth1
2.配置/etc/grub.conf里加console=ttyS0  支持命令行console模式开启虚拟机
3.cd /var/lib/libvirt/images/
4.ls一下查看是否为qcow2模式
5.yum可配可不配
6.关机
7.qemu-img convert -f raw -O qcow2 sourcename.raw centos.qcow2
virsh edit centos6(把raw修改qcow2)
注:如果是硬盘类型是raw类型的,不支持克隆,需要转化,如果是qcow2类型,此步则忽略。


实验步骤:
1.复制配置文件与磁盘文件克隆
[root@localhost images]# qemu-img create -f qcow2 -b rhel6.qcow2 GHOST
Formatting 'GHOST', fmt=qcow2 size=8589934592 backing_file='rhel6.qcow2' encryption=off cluster_size=65536
[root@localhost images]# qemu-img create -f qcow2 -b rhel6.qcow2 ClientA
Formatting 'ClientA', fmt=qcow2 size=8589934592 backing_file='rhel6.qcow2' encryption=off cluster_size=65536
[root@localhost images]# qemu-img create -f qcow2 -b rhel6.qcow2 ClientB
Formatting 'ClientB', fmt=qcow2 size=8589934592 backing_file='rhel6.qcow2' encryption=off cluster_size=65536
2.对比文件,备份模板虚拟机的配置文件
[root@localhost images]# du -m rhel6.qcow2
223    rhel6.qcow2
[root@localhost images]# du -m GHOST
1    GHOST
[root@localhost images]# virsh list --all
Id    名称                         状态
----------------------------------------------------
-     rhel6                        关闭
克隆三个虚拟机,数量自己定
[root@localhost images]# virsh dumpxml rhel6 > /etc/libvirt/qemu/ghost.xml
[root@localhost images]# virsh dumpxml rhel6 > /etc/libvirt/qemu/clienta.xml
[root@localhost images]# virsh dumpxml rhel6 > /etc/libvirt/qemu/clientb.xml
[root@localhost images]#
3.编辑克隆虚拟机的配置文件
(更改name,删除UUID,修改source路径名,删除mac)
[root@localhost images]# vi /etc/libvirt/qemu/ghost.xml
[root@localhost images]# vi /etc/libvirt/qemu/clienta.xml
[root@localhost images]# vi /etc/libvirt/qemu/clientb.xml
[root@localhost images]#
注:由于都需要进配置文件,就不贴出来了,反正就是更改这四个地方。
4.定义新虚拟机的配置文件
[root@localhost images]# virsh define /etc/libvirt/qemu/ghost.xml
定义域 dhcp(从 /etc/libvirt/qemu/ghost.xml)

[root@localhost images]# virsh define /etc/libvirt/qemu/clienta.xml
定义域 clienta(从 /etc/libvirt/qemu/clienta.xml)

[root@localhost images]# virsh define /etc/libvirt/qemu/clientb.xml
定义域 clientb(从 /etc/libvirt/qemu/clientb.xml)
5.使用虚拟机
virsh list --all
virsh start ghost
virsh console ghost
结束语:
本次实验适用于在没有图形化的条件下如何实现克隆虚拟机,做技术就是需要拥有一颗永不磨灭的好奇心。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  images 虚拟机 克隆