KVM虚拟化笔记------kvm虚拟机的克隆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KVM虚拟化笔记------kvm虚拟机的克隆相关的知识,希望对你有一定的参考价值。

  kvm虚拟机的克隆分为两种情况:kvm主机本机虚拟机直接克隆和通过复制配置文件与磁盘文件的虚拟机复制克隆。接下来我们一一进行测试:

(一)kvm主机虚拟机的直接克隆

1,查看虚拟机的配置文件和磁盘文件:

[[email protected] qemu]# cat /etc/libvirt/qemu/hadoop1.xml 
<!--
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 hadoop1
or other application using the libvirt API.
-->
<domain type=‘qemu‘>
  <name>hadoop1</name>
  <uuid>919f0921-0736-ad5b-780b-a440de2f35cb</uuid>
  <memory unit=‘KiB‘>524288</memory>
  <currentMemory unit=‘KiB‘>524288</currentMemory>
  <vcpu placement=‘static‘>1</vcpu>
  <os>
    <type arch=‘x86_64‘ machine=‘rhel6.6.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=‘/images/test/hadoop1.img‘/>
      <target dev=‘vda‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x05‘ function=‘0x0‘/>
    </disk>
    <disk type=‘block‘ device=‘cdrom‘>
      <driver name=‘qemu‘ type=‘raw‘/>
      <target dev=‘hdc‘ bus=‘ide‘/>
      <readonly/>
      <address type=‘drive‘ controller=‘0‘ bus=‘1‘ target=‘0‘ unit=‘0‘/>
    </disk>
    <controller type=‘usb‘ index=‘0‘ model=‘ich9-ehci1‘>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x7‘/>
    </controller>
    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci1‘>
      <master startport=‘0‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x0‘ multifunction=‘on‘/>
    </controller>
    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci2‘>
      <master startport=‘2‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x1‘/>
    </controller>
    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci3‘>
      <master startport=‘4‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x2‘/>
    </controller>
    <controller type=‘ide‘ index=‘0‘>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x01‘ function=‘0x1‘/>
    </controller>
    <interface type=‘bridge‘>
      <mac address=‘52:54:00:b6:bf:1f‘/>
      <source bridge=‘br0‘/>
      <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=‘mouse‘ bus=‘ps2‘/>
    <graphics type=‘vnc‘ port=‘5911‘ autoport=‘no‘ listen=‘0.0.0.0‘>
      <listen type=‘address‘ address=‘0.0.0.0‘/>
    </graphics>
    <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=‘0x06‘ function=‘0x0‘/>
    </memballoon>
  </devices>
</domain>

查看磁盘文件

[[email protected] test]# ll  /images/test/
total 2688992
-rwxr-xr-x 1 root root 10737418240 Feb  4 14:38 hadoop1.img
-rwxr-xr-x 1 qemu qemu 10737418240 Feb  4 14:43 hadoop4.img
-rwxr-xr-x 1 qemu qemu  8589934592 Feb  2 15:03 win7.img
[[email protected] test]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 3     hadoop4                        running
 -     hadoop1                        shut off

2,开始克隆

[[email protected] qemu]# virt-clone -o hadoop1 -n hadoop2 -f /images/test/hadoop2.img
Cloning hadoop1.img                                                                    |  10 GB     02:52     
Clone ‘hadoop2‘ created successfully.

技术分享

技术分享

3,然后启动虚拟机,配置主机名,ip地址等等相关的信息,相关详细配置都是经常使用的略。


 (二)复制配置文件与磁盘文件克隆

这里采用以hadoop4作为模板,来进行克隆。

1,关闭hadoop4虚拟机

[[email protected] qemu]# virsh destroy hadoop4
Domain hadoop4 destroyed
[[email protected] qemu]# virsh list --all     
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 6     hadoop2                        running
 -     hadoop1                        shut off
 -     hadoop4                        shut off

2,克隆hadoop4.xml文件

[[email protected] qemu]# virsh dumpxml hadoop4 > /etc/libvirt/qemu/hadoop3.xml
[[email protected] qemu]# ll /etc/libvirt/qemu/
total 32
drwxr-xr-x 2 root root 4096 Feb  2 17:18 autostart
-rw-r--r-- 1 root root    1 Feb  2 17:13 hadoop1.bak.xml
-rw------- 1 root root 2998 Feb  2 13:54 hadoop1.xml
-rw------- 1 root root 2998 Feb  4 14:54 hadoop2.xml
-rw-r--r-- 1 root root 2740 Feb  4 15:12 hadoop3.xml
-rw------- 1 root root 2965 Feb  2 17:25 hadoop4.xml
drwx------ 3 root root 4096 Jan 26 16:47 networks
-rw------- 1 root root 3036 Feb  2 15:52 win7.xml

技术分享3,复制kvm虚拟机磁盘文件,该磁盘文件的路径也可以通过配置文件xml, <source file=‘/images/test/hadoop4.img‘/>路径进行查看

[[email protected] test]# cp hadoop4.img hadoop3.img
[[email protected] test]# ls
hadoop1.img  hadoop2.img  hadoop3.img  hadoop4.img  win7.img

技术分享

4,直接编辑修改配置文件(修改name,uuid,source file,vnc端口号等等)

[[email protected] qemu]# vi /etc/libvirt/qemu/hadoop3.xml 


<domain type=‘qemu‘>

  <name>hadoop3</name>

  <uuid>586b3cae-943f-d283-d8e7-ed62b01bfa38</uuid>

  <memory unit=‘KiB‘>1048576</memory>

  <currentMemory unit=‘KiB‘>1048576</currentMemory>

  <vcpu placement=‘static‘>1</vcpu>

  <os>

    <type arch=‘x86_64‘ machine=‘rhel6.6.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=‘/images/test/hadoop4.img‘/>

      <target dev=‘hda‘ bus=‘ide‘/>

      <address type=‘drive‘ controller=‘0‘ bus=‘0‘ target=‘0‘ unit=‘0‘/>

    </disk>

    <disk type=‘block‘ device=‘cdrom‘>

      <driver name=‘qemu‘ type=‘raw‘/>

      <target dev=‘hdc‘ bus=‘ide‘/>

      <readonly/>

      <address type=‘drive‘ controller=‘0‘ bus=‘1‘ target=‘0‘ unit=‘0‘/>

    </disk>

    <controller type=‘usb‘ index=‘0‘ model=‘ich9-ehci1‘>

      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x7‘/>

    </controller>

    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci1‘>

      <master startport=‘0‘/>

      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x0‘ multifunction=‘on‘/>

    </controller>

    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci2‘>

      <master startport=‘2‘/>

      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x1‘/>

    </controller>

    <controller type=‘usb‘ index=‘0‘ model=‘ich9-uhci3‘>

      <master startport=‘4‘/>

      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x04‘ function=‘0x2‘/>

    </controller>

    <controller type=‘ide‘ index=‘0‘>

      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x01‘ function=‘0x1‘/>

    </controller>

    <interface type=‘network‘>

      <mac address=‘52:54:00:fe:f5:a3‘/>

      <source network=‘default‘/>

      <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=‘mouse‘ bus=‘ps2‘/>

    <graphics type=‘vnc‘ port=‘5913‘ autoport=‘no‘ listen=‘0.0.0.0‘>

      <listen type=‘address‘ address=‘0.0.0.0‘/>

    </graphics>

    <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>


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

[[email protected] qemu]# virsh define /etc/libvirt/qemu/hadoop3.xml 
Domain hadoop3 defined from /etc/libvirt/qemu/hadoop3.xml
[[email protected] qemu]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 6     hadoop2                        running
 -     hadoop1                        shut off
 -     hadoop3                        shut off
 -     hadoop4                        shut off

技术分享


5,最后启动该虚拟机,登录进行相关的配置等等。

本文出自 “清风明月” 博客,请务必保留此出处http://liqingbiao.blog.51cto.com/3044896/1741093

以上是关于KVM虚拟化笔记------kvm虚拟机的克隆的主要内容,如果未能解决你的问题,请参考以下文章

KVM 克隆系统

转:KVM 虚拟机的克隆

KVM虚拟机的克隆

KVM虚拟机克隆方式

如何给运行的kvm虚拟机做快照

6kvm克隆虚拟机