kvm 主机克隆

 

完整克隆

自动克隆

注:Domain with devices to clone must be paused or shutoff.主机必须终止或者关机。

[root@kafka-1 ~]# virt-clone --auto-clone -o base-clone-bridge-vm -n auto-clone2-bridge-vm
WARNING  Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating 'base-clone-br0-clone-1.qcow2'                                                                              | 5.0 GB  00:00:26

Clone 'auto-clone2-bridge-vm' created successfully.

[root@kafka-1 ~]# ll /opt/kvm/vm
total 6091572
-rw------- 1 root root 2065825792 Aug  8 09:53 base-clone-br0-clone-1.qcow2
-rw------- 1 root root 2065825792 Aug  8 09:47 base-clone-br0-clone.qcow2
-rw------- 1 root root 5369757696 Aug  8 09:46 base-clone-br0.qcow2

[root@kafka-1 ~]# virt-clone --auto-clone -o base-clone-bridge-vm -n auto-clone3-bridge-vm
WARNING  Setting the graphics device port to autoport, in order to avoid conflicting.
Allocating 'base-clone-br0-clone-2.qcow2'                                                                              | 5.0 GB  00:00:25

Clone 'auto-clone3-bridge-vm' created successfully.

[root@kafka-1 ~]# ll /opt/kvm/vm
total 8209208
-rw------- 1 root root 2065825792 Aug  8 09:53 base-clone-br0-clone-1.qcow2
-rw------- 1 root root 2065825792 Aug  8 09:55 base-clone-br0-clone-2.qcow2
-rw------- 1 root root 2065825792 Aug  8 09:47 base-clone-br0-clone.qcow2
-rw------- 1 root root 5369757696 Aug  8 09:46 base-clone-br0.qcow2

查看配置

 主机名称,删除uuid,磁盘路径,删除mac地址不能重复。

auto-clone已经解决了这个问题。

[root@kafka-1 ~]# grep mac /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:      <mac address='52:54:00:3b:67:4e'/>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:      <mac address='52:54:00:ce:ba:c7'/>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:      <mac address='52:54:00:d1:b0:b7'/>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:      <mac address='52:54:00:5b:0f:1a'/>

[root@kafka-1 ~]# grep source /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:      <source file='/opt/kvm/vm/base-clone-br0-clone-1.qcow2'/>
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:      <source bridge='br0'/>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:      <source file='/opt/kvm/vm/base-clone-br0-clone-2.qcow2'/>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:      <source bridge='br0'/>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:      <source file='/opt/kvm/vm/base-clone-br0-clone.qcow2'/>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:      <source bridge='br0'/>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:      <source file='/opt/kvm/vm/base-clone-br0.qcow2'/>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:      <source bridge='br0'/>

[root@kafka-1 ~]# grep '<name>' /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:  <name>auto-clone2-bridge-vm</name>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:  <name>auto-clone3-bridge-vm</name>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:  <name>auto-clone-bridge-vm</name>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:  <name>base-clone-bridge-vm</name>

[root@kafka-1 ~]# grep '<uuid>' /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/auto-clone2-bridge-vm.xml:  <uuid>77a67030-8bf5-4853-ba57-ecbcb6b594ed</uuid>
/etc/libvirt/qemu/auto-clone3-bridge-vm.xml:  <uuid>4067c56a-ff78-4aa0-a859-ed21ab076c4c</uuid>
/etc/libvirt/qemu/auto-clone-bridge-vm.xml:  <uuid>4da67966-17c8-468d-a1b8-4d4dc9d88577</uuid>
/etc/libvirt/qemu/base-clone-bridge-vm.xml:  <uuid>8163fd1d-6f56-402e-94bf-f6a076eadff9</uuid>

开机

[root@kafka-1 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     auto-clone-bridge-vm           shut off
 -     auto-clone2-bridge-vm          shut off
 -     auto-clone3-bridge-vm          shut off
 -     base-clone-bridge-vm           shut off

[root@kafka-1 ~]# virsh start auto-clone2-bridge-vm
Domain auto-clone2-bridge-vm started

[root@kafka-1 ~]# virsh start auto-clone-bridge-vm
Domain auto-clone-bridge-vm started

[root@kafka-1 ~]# virsh start auto-clone3-bridge-vm
Domain auto-clone3-bridge-vm started

[root@kafka-1 ~]# virsh start base-clone-bridge-vm
Domain base-clone-bridge-vm started

[root@kafka-1 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     auto-clone2-bridge-vm          running
 5     auto-clone-bridge-vm           running
 6     auto-clone3-bridge-vm          running
 7     base-clone-bridge-vm           running

测试网络

[root@kafka-3 ~]# while true;do for i in {16..19};do ping -c3 192.168.64.$i;echo " ";done;sleep 5;echo "  ";done
PING 192.168.64.16 (192.168.64.16) 56(84) bytes of data.
64 bytes from 192.168.64.16: icmp_seq=1 ttl=64 time=0.450 ms
64 bytes from 192.168.64.16: icmp_seq=2 ttl=64 time=0.554 ms
64 bytes from 192.168.64.16: icmp_seq=3 ttl=64 time=0.858 ms

--- 192.168.64.16 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2008ms
rtt min/avg/max/mdev = 0.450/0.620/0.858/0.175 ms

PING 192.168.64.17 (192.168.64.17) 56(84) bytes of data.
64 bytes from 192.168.64.17: icmp_seq=1 ttl=64 time=0.493 ms
64 bytes from 192.168.64.17: icmp_seq=2 ttl=64 time=0.568 ms
64 bytes from 192.168.64.17: icmp_seq=3 ttl=64 time=0.543 ms

--- 192.168.64.17 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.493/0.534/0.568/0.041 ms

PING 192.168.64.18 (192.168.64.18) 56(84) bytes of data.
64 bytes from 192.168.64.18: icmp_seq=1 ttl=64 time=0.580 ms
64 bytes from 192.168.64.18: icmp_seq=2 ttl=64 time=1.76 ms
64 bytes from 192.168.64.18: icmp_seq=3 ttl=64 time=2.15 ms

--- 192.168.64.18 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2012ms
rtt min/avg/max/mdev = 0.580/1.501/2.159/0.671 ms

PING 192.168.64.19 (192.168.64.19) 56(84) bytes of data.
64 bytes from 192.168.64.19: icmp_seq=1 ttl=64 time=0.419 ms
64 bytes from 192.168.64.19: icmp_seq=2 ttl=64 time=0.418 ms
64 bytes from 192.168.64.19: icmp_seq=3 ttl=64 time=2.27 ms

--- 192.168.64.19 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 0.418/1.036/2.272/0.874 ms

手动克隆

 复制磁盘与配置文件

cp domain.qcow2  new-domain.qcow2

virsh dumpxml centos7 > /etc/libvirt/qemu/new-domain.xml
or
 cp /etc/libvirt/qemu/base-clone-bridge-vm.xml /etc/libvirt/qemu/auto-clone4-bridge-vm.xml

 修改导出的xml 主机名称,删除uuid,磁盘路径,删除mac地址

[root@kafka-1 vm]# grep mac /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/clone5-bridge-vm.xml:      <mac address='52:54:00:77:13:85'/>

[root@kafka-1 vm]# grep uuid /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/clone5-bridge-vm.xml:  <uuid>b5cd71c0-f694-49d3-bea3-d7bff596320d</uuid>

[root@kafka-1 vm]# grep 'source' /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/clone5-bridge-vm.xml:      <source file='/opt/kvm/vm/base-clone-br0-clone-5.qcow2'/>
/etc/libvirt/qemu/clone5-bridge-vm.xml:      <source bridge='br0'/>

[root@kafka-1 vm]# grep '<name>' /etc/libvirt/qemu/*.xml
/etc/libvirt/qemu/clone5-bridge-vm.xml:  <name>clone5-bridge-vm</name>

 定义主机,可能会报错nvc端口冲突,需要在xml文件配置 autoport='yes',或者直接修改模版机

virsh define new-domain.xml

启动主机

virsh  start new-domain

一条命令直接克隆

virt-clone -o old-comain -n new-domain -f /opt/kvm/vm/new-domain-name.qcow2
virt-clone -o base-clone-bridge-vm -n clone5-bridge-vm -f /opt/kvm/vm/base-clone-br0-clone-5.qcow2

链接克隆

当源主机文件被删除,链接主机失效。

基础主机可以关机,但是不能删除

qemu-img create -f qcow2 -b domain.qcow2 new-domain-link.qcow2
virsh dumpxml centos7 > /etc/libvirt/qemu/new-domain.xml
virsh define new-domain.xml
修改导出的xml 主机名称,删除uuid,磁盘路径,删除mac地址

 

posted @ 2024-08-03 12:21  不会跳舞的胖子  阅读(8)  评论(0编辑  收藏  举报