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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
[root@KVM 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>

查看磁盘文件

1
2
3
4
5
6
7
8
9
10
11
[root@KVM 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
[root@KVM test]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 3     hadoop4                        running
 -     hadoop1                        shut off

2,开始克隆

1
2
3
[root@KVM qemu]# virt-clone -o hadoop1 -n hadoop2 -f /images/test/hadoop2.img
Cloning hadoop1.img                                                                    |  10 GB     02:52     
Clone 'hadoop2' created successfully.

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

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

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


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

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

1,关闭hadoop4虚拟机

1
2
3
4
5
6
7
8
9
[root@KVM qemu]# virsh destroy hadoop4
Domain hadoop4 destroyed
[root@KVM qemu]# virsh list --all     
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 6     hadoop2                        running
 -     hadoop1                        shut off
 -     hadoop4                        shut off

2,克隆hadoop4.xml文件

1
2
3
4
5
6
7
8
9
10
11
[root@KVM qemu]# virsh dumpxml hadoop4 > /etc/libvirt/qemu/hadoop3.xml
[root@KVM 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

KVM虚拟化笔记(六)------kvm虚拟机的克隆3,复制kvm虚拟机磁盘文件,该磁盘文件的路径也可以通过配置文件xml, <source file='/images/test/hadoop4.img'/>路径进行查看

1
2
3
[root@KVM test]# cp hadoop4.img hadoop3.img
[root@KVM test]# ls
hadoop1.img  hadoop2.img  hadoop3.img  hadoop4.img  win7.img

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

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

[root@KVM 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,重新定义新虚拟机的配置文件

1
2
3
4
5
6
7
8
9
10
[root@KVM qemu]# virsh define /etc/libvirt/qemu/hadoop3.xml 
Domain hadoop3 defined from /etc/libvirt/qemu/hadoop3.xml
[root@KVM qemu]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     win7                           running
 6     hadoop2                        running
 -     hadoop1                        shut off
 -     hadoop3                        shut off
 -     hadoop4                        shut off

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


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

上一篇:《Linux/UNIX系统编程手册(上、下册)》——第1章 历史和标准 1.1UNIX和C语言简史


下一篇:MySQL、Oracle分页查询语句