虚拟机的克隆
作者:互联网
当需要批量部署虚拟机的时候,可以选择克隆虚拟机的方式。
使用图形界面克隆虚拟机
进行虚拟机克隆之前必须要将虚拟机关闭或暂停!
1、选择需要克隆的虚拟机,右击,选择clone命令。
等待克隆完成即可。
[root@localhost ~]# virsh dominfo rhel7-vm01-clone
Id: -
Name: rhel7-vm01-clone
UUID: 3e3320dc-4169-41b4-85d6-d4f13cba129c
OS Type: hvm
State: shut off
CPU(s): 1
Max memory: 1048576 KiB
Used memory: 1048576 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
[root@localhost ~]# virsh domiflist rhel7-vm01-clone
Interface Type Source Model MAC
-------------------------------------------------------
- network default virtio 52:54:00:ff:13:77
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
- rhel7-vm01 shut off
- rhel7-vm01-clone shut off
- vm01 shut off
[root@localhost ~]# virsh start rhel7-vm01-clone
Domain rhel7-vm01-clone started
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 rhel7-vm01-clone running
使用virt-clone工具克隆虚拟机
使用virt-clone工具先要安装virt-install软件包。
[root@localhost ~]# yum install -y virt-install
安装好virt-clone工具后,可以执行virt-clone命令。其中有三个选项,-o 为克隆前虚拟机的名称,-n为克隆后新的虚拟机名称,-f指明克隆后生成新的虚拟机的镜像文件。命令输入后,会出现克隆进度提示。
[root@localhost ~]# virt-clone -o rhel7-vm01 -n vm02 -f /var/lib/libvirt/images/vm02.qcow2
Allocating 'vm02.qcow2' | 9.0 GB 01:15
Clone 'vm02' created successfully.
同样使用virsh命令查看、开启、关闭新虚拟机。
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 rhel7-vm01-clone running
[root@localhost ~]# virsh list --all
Id Name State
----------------------------------------------------
3 rhel7-vm01-clone running
- rhel7-vm01 shut off
- vm01 shut off
- vm02 shut off
[root@localhost ~]# virsh start vm02
Domain vm02 started
[root@localhost ~]# virsh list
Id Name State
----------------------------------------------------
3 rhel7-vm01-clone running
4 vm02 running
使用虚拟机的配置文件克隆虚拟机
通过修改虚拟机的配置文件进行克隆虚拟机。
1、使用uuidgen命令为克隆后的新的虚拟机准备一个UUID号,每个虚拟机都有一个唯一的UUID号码。
[root@localhost ~]# uuidgen
1c5ba030-ec09-4c5e-8d2d-fc9be0121845
2、复制要克隆的虚拟机的配置文件。
[root@localhost ~]# cd /etc/libvirt/qemu/
[root@localhost qemu]# ls
autostart networks rhel7-vm01-clone.xml rhel7-vm01.xml vm01.xml vm02.xml
[root@localhost qemu]# cp rhel7-vm01.xml vm03.xml
[root@localhost qemu]# ls
autostart rhel7-vm01-clone.xml vm01.xml vm03.xml
networks rhel7-vm01.xml vm02.xml
3、编辑新复制的xml配置文件,将虚拟机名称、UUID号码进行更改,还要修改虚拟机的镜像文件名称,MAC地址。
[root@localhost qemu]# vi vm03.xml
[root@localhost qemu]# cat vm03.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 rhel7-vm01
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>vm03</name>
<uuid>1c5ba030-ec09-4c5e-8d2d-fc9be0121845</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact'>
<model fallback='allow'>Broadwell-noTSX</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm03.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:b2:de:cc'/>
<source network='default'/>
<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>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='keyboard' bus='usb'>
<address type='usb' bus='0' port='2'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' keymap='en-us'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='4'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
</devices>
</domain>
4、生成新虚拟机的镜像文件,通过cp命令,复制虚拟机vm01的镜像文件,生成新的虚拟机镜像文件。
[root@localhost qemu]# cd /var/lib/libvirt/images/
[root@localhost images]# ls
rhel7-vm01_1.img rhel7-vm01.qcow2 vm01_2.img vm01.dsk
rhel7-vm01-clone.qcow2 vm01_1.img vm01_3.img vm02.qcow2
[root@localhost images]# cp vm02.qcow2 vm03.qcow2
[root@localhost images]# ll
total 7179848
-rw-r--r--. 1 root root 104857600 Feb 26 10:47 rhel7-vm01_1.img
-rw-------. 1 qemu qemu 1410203648 Mar 25 16:26 rhel7-vm01-clone.qcow2
-rw-------. 1 root root 9665380352 Mar 25 11:50 rhel7-vm01.qcow2
-rw-r--r--. 1 root root 104857600 Mar 24 16:06 vm01_1.img
-rw-r--r--. 1 root root 197120 Mar 24 16:27 vm01_2.img
-rw-r--r--. 1 root root 105185280 Mar 24 16:46 vm01_3.img
-rw-------. 1 root root 8591507456 Mar 25 06:22 vm01.dsk
-rw-------. 1 qemu qemu 1410203648 Mar 25 16:22 vm02.qcow2
-rw-------. 1 root root 1410203648 Mar 25 16:30 vm03.qcow2
[root@localhost images]# virsh define /etc/libvirt/qemu/vm03.xml
Domain vm03 defined from /etc/libvirt/qemu/vm03.xml
[root@localhost images]# virsh start vm03
Domain vm03 started
标签:rhel7,vm01,克隆,虚拟机,root,clone,localhost 来源: https://blog.csdn.net/qq_26350199/article/details/115192281