设为首页 收藏本站
查看: 2718|回复: 0

[经验分享] kvm虚拟机的克隆

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-15 14:33:08 | 显示全部楼层 |阅读模式
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
[iyunv@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
[iyunv@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
[iyunv@KVM test]# virsh list --all
Id    Name                           State
----------------------------------------------------
1     win7                           running
3     hadoop4                        running
-     hadoop1                        shut off



2,开始克隆

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



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

(二)复制配置文件与磁盘文件克隆
这里采用以hadoop4作为模板,来进行克隆。
1,关闭hadoop4虚拟机
1
2
3
4
5
6
7
8
9
[iyunv@KVM qemu]# virsh destroy hadoop4
Domain hadoop4 destroyed
[iyunv@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
[iyunv@KVM qemu]# virsh dumpxml hadoop4 > /etc/libvirt/qemu/hadoop3.xml
[iyunv@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



QQ截图20160215143207.png 3,复制kvm虚拟机磁盘文件,该磁盘文件的路径也可以通过配置文件xml, <source file='/images/test/hadoop4.img'/>路径进行查看
1
2
3
[iyunv@KVM test]# cp hadoop4.img hadoop3.img
[iyunv@KVM test]# ls
hadoop1.img  hadoop2.img  hadoop3.img  hadoop4.img  win7.img



QQ截图20160215143219.png
4,直接编辑修改配置文件(修改name,uuid,source file,vnc端口号等等)
[iyunv@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
[iyunv@KVM qemu]# virsh define /etc/libvirt/qemu/hadoop3.xml
Domain hadoop3 defined from /etc/libvirt/qemu/hadoop3.xml
[iyunv@KVM qemu]# virsh list --all
Id    Name                           State
----------------------------------------------------
1     win7                           running
6     hadoop2                        running
-     hadoop1                        shut off
-     hadoop3                        shut off
-     hadoop4                        shut off



QQ截图20160215143235.png

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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-178321-1-1.html 上篇帖子: kvm虚拟机控制台登录配置 下篇帖子: kvm虚拟机VNC的配置 虚拟机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表