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

前端之家收集整理的这篇文章主要介绍了KVM――复制xml文件与磁盘文件克隆虚拟机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

kvm虚拟机克隆有如下两种方式:

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

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


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


  1. 简易版原理图:
  2. A(模板)B(克隆)
  3. diskdisk
  4. xmlxml
  5. 需要更改内容会变的内容
  6.  
  7. udev-macuuid
  8. yumname
  9. console=ttyS0mac
  10. 网卡配置里的macdisk

注释:

A为模板,为了不和B(克隆机)起冲突,主要是硬盘和网卡冲突,需要把A的udev注释,顺手配置好yum,console=ttyS0支持命令行操作,把网卡的uuid,mac信息删掉。

B为克隆机,为了区分出来,不和A起冲突,需要把uuid和mac删掉,把name和disk路径改成B的名字。

  1. 1.开机,进入模板主机,打开udev文件,注释里面内容,防止网卡复制为eth1
  2. 2.配置/etc/grub.conf里加console=ttyS0支持命令行console模式开启虚拟机
  3. 3.cd/var/lib/libvirt/images/
  4. 4.ls一下查看是否为qcow2模式
  5. 5.yum可配可不配
  6. 6.关机
  7. 7.qemu-imgconvert-fraw-Oqcow2sourcename.rawcentos.qcow2
  8. virsheditcentos6(把raw修改qcow2)
  9. 注:如果是硬盘类型是raw类型的,不支持克隆,需要转化,如果是qcow2类型,此步则忽略。


实验步骤:

1.复制配置文件与磁盘文件克隆

  1. [root@localhostimages]#qemu-imgcreate-fqcow2-brhel6.qcow2GHOST
  2. Formatting'GHOST',fmt=qcow2size=8589934592backing_file='rhel6.qcow2'encryption=offcluster_size=65536
  3. [root@localhostimages]#qemu-imgcreate-fqcow2-brhel6.qcow2ClientA
  4. Formatting'ClientA',fmt=qcow2size=8589934592backing_file='rhel6.qcow2'encryption=offcluster_size=65536
  5. [root@localhostimages]#qemu-imgcreate-fqcow2-brhel6.qcow2ClientB
  6. Formatting'ClientB',fmt=qcow2size=8589934592backing_file='rhel6.qcow2'encryption=offcluster_size=65536

2.对比文件,备份模板虚拟机的配置文件

  1. [root@localhostimages]#du-mrhel6.qcow2
  2. 223rhel6.qcow2
  3. [root@localhostimages]#du-mGHOST
  4. 1GHOST
  5. [root@localhostimages]#virshlist--all
  6. Id名称状态
  7. ----------------------------------------------------
  8. -rhel6关闭
  9. 克隆三个虚拟机,数量自己定
  10. [root@localhostimages]#virshdumpxmlrhel6>/etc/libvirt/qemu/ghost.xml
  11. [root@localhostimages]#virshdumpxmlrhel6>/etc/libvirt/qemu/clienta.xml
  12. [root@localhostimages]#virshdumpxmlrhel6>/etc/libvirt/qemu/clientb.xml
  13. [root@localhostimages]#

3.编辑克隆虚拟机的配置文件

  1. (更改name,删除UUID,修改source路径名,删除mac)
  2. [root@localhostimages]#vi/etc/libvirt/qemu/ghost.xml
  3. [root@localhostimages]#vi/etc/libvirt/qemu/clienta.xml
  4. [root@localhostimages]#vi/etc/libvirt/qemu/clientb.xml
  5. [root@localhostimages]#
  6. 注:由于都需要进配置文件,就不贴出来了,反正就是更改这四个地方。

4.定义新虚拟机的配置文件

  1. [root@localhostimages]#virshdefine/etc/libvirt/qemu/ghost.xml
  2. 定义域dhcp(从/etc/libvirt/qemu/ghost.xml
  3.  
  4. [root@localhostimages]#virshdefine/etc/libvirt/qemu/clienta.xml
  5. 定义域clienta(从/etc/libvirt/qemu/clienta.xml
  6.  
  7. [root@localhostimages]#virshdefine/etc/libvirt/qemu/clientb.xml
  8. 定义域clientb(从/etc/libvirt/qemu/clientb.xml

5.使用虚拟机

  1. virshlist--all
  2. virshstartghost
  3. virshconsoleghost

结束语:

  1. 本次实验适用于在没有图形化的条件下如何实现克隆虚拟机,做技术就是需要拥有一颗永不磨灭的好奇心。

猜你在找的XML相关文章