实验目的:通过一个基础镜像(node.img),里面把各个虚拟机都需要的环境都搭建好,然后基于这个镜像建立起一个个增量镜像,每个增量镜像对应一个虚拟机,虚拟机对镜像中所有的改变都记录在增量镜像里面,基础镜像始终保持不变。
功能:节省磁盘空间,快速复制虚拟机。
环境:
基本镜像文件:node.img 虚拟机ID:node
增量镜像文件:node4.img 虚拟机ID:node4
要求:以基本镜像文件node.omg为基础,创建一个镜像文件node4.img,以此创建一个虚拟机机node4,虚拟机node4的改变将存储于node4.img中。
[root@target kvm_node]#qemu-img create -b node.img -f qcow2 node4.img
[root@target kvm_node]# qemu-img info node4.img
image: node4.img
file format: qcow2
virtual size: 20G (21495808000 bytes)
disk size: 33M
cluster_size: 65536
backing file: node.img (actual path: node.img)
注:该实验只是针对qcow2格式的镜像文件,未测试raw格式的镜像文件是否可行。
[root@target kvm_node]# cp /etc/libvirt/qemu/node.xml /etc/libvirt/qemu/node4.xml
[root@target kvm_node]# vim /etc/libvirt/qemu/node4.xml
<domain>
<name>node4</name> #node4的虚拟机名,须修改,否则与基本虚拟机冲突
<uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fe4</uuid> #node4的UUID,必须修改,否则与基本虚拟机冲突
<memory>524288</memory>
<currentMemory>524288</currentMemory>
<vcpu>2</vcpu>
<os>
<type>hvm</type>
<boot/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk>
<driver/>
<source/> #将原指向/virhost/kvm_node/node.img改为node4.img
<target/>
<address/>
</disk>
<interface>
<mac/> #修改网卡MAC,防止冲突
<source/>
<model/>
<address/>
</interface>
<interface>
<mac/> #修改网卡MAC,防止冲突
<source/>
<model/>
<address/>
</interface>
<serial>
<target/>
</serial>
<console>
<target/>
</console>
<input/>
<graphics>
<listen/>
</graphics>
<video>
<model/>
<address/>
</video>
<memballoon>
<address/>
</memballoon>
</devices>
</domain>
[root@target kvm_node]#virsh define /etc/libvirt/qemu/node4.xml
[root@target kvm_node]#virsh start node4
[root@target kvm_node]# du -h node.img
6.3G node.img
[root@target kvm_node]# du -h node4.img
33M node4.img
[root@node4 ~]# dd if=/dev/zero of=test bs=1M count=200 #在虚拟机node4上增量200M大小文件
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 1.00361 seconds, 209 MB/s
[root@target kvm_node]# du -h node.img #基本镜像文件node.img大小未变
6.3G node.img
[root@target kvm_node]# du -h node.img #增量镜像文件node4.img增加200M了
234M node4.img