KVM笔记之增量镜像
增量镜像
增量镜像即保持一个基础的镜像不变,在此基础镜像上安装或修改相应的服务所创建的一个单独的镜像,增量镜像类似GIT,只记录当前KVM的修改记录,这样可以节省磁盘空间,快速复制虚拟机
基础环境准备
-
KVM基础环境
-
基础镜像 ubuntu-base.qcow2
创建增量镜像
首先基于基础镜像 ubuntu-base.qcow2 创建一个增量镜像 ubuntu-test.qcow2
1(宿主机) # qemu-img info /home/kvm/images/ubuntu-base.qcow2 2image: ubuntu-base.qcow2 3file format: qcow2 4virtual size: 4.0G (4294967296 bytes) 5disk size: 2.4G 6cluster_size: 65536 7Format specific information: 8 compat: 1.1 9 lazy refcounts: true 10 refcount bits: 16 11 corrupt: false 12 13(宿主机) #:/home/kvm/images# qemu-img create -b ubuntu-base.qcow2 -f qcow2 ubuntu-test.qcow2 14Formatting 'ubuntu-test.qcow2', fmt=qcow2 size=4294967296 backing_file=ubuntu-base.qcow2 cluster_size=65536 lazy_refcounts=off refcount_bits=16 15 16(宿主机) #:/home/kvm/images# qemu-img info ubuntu-test.qcow2 17image: ubuntu-test.qcow2 18file format: qcow2 19virtual size: 4.0G (4294967296 bytes) 20disk size: 196K 21cluster_size: 65536 22backing file: ubuntu-base.qcow2 23Format specific information: 24 compat: 1.1 25 lazy refcounts: false 26 refcount bits: 16 27 corrupt: false 28 29(宿主机) #:/home/kvm/images# du -sh * 302.4G ubuntu-base.qcow2 31196K ubuntu-test.qcow2
创建KVM虚拟机
手动创建
1# 注: 我基于基础镜像创建的KVM虚拟机名称为*vm-ubuntu-base*, 请根据实际进行修改 2(宿主机) # cp /etc/libvirt/qemu/vm-ubuntu-base.xml /etc/libvirt/qemu/vm-ubuntu-test.xml 3(宿主机) # vim /etc/libvirt/qemu/vm-ubuntu-test.xml
1<domain type='kvm'> 2 <name>vm-ubuntu-base</name> # 虚拟机名,必须修改,否则与基本镜像虚拟机冲突 3 <uuid>xxxx</uuid> # UUID,必须修改,否则与基本虚拟机冲突 4 ... 5 <devices> 6 <emulator>/usr/bin/kvm-spice</emulator> 7 <disk type='file' device='disk'> 8 <driver name='qemu' type='qcow2'/> 9 <source file='/home/kvm/images/ubuntu-test.qcow2'/> # 改成增量镜像文件位置 10 <target dev='vda' bus='virtio'/> 11 <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> 12 </disk> 13 ... 14 <interface type='bridge'> 15 <mac address='52:54:00:83:5f:e6'/> # MAC地址,必须修改 16 <source bridge='br0'/> 17 <model type='virtio'/> 18 <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> 19 </interface> 20 .... 21</domain>
1(宿主机) # virsh define /etc/libvirt/qemu/vm-ubuntu-test.xml 2(宿主机) # virsh start vm-ubuntu-test 3(宿主机) # virsh list
使用virt-install创建
1(宿主机) # qemu-img create -b ubuntu-base.qcow2 -f qcow2 ubuntu-test01.qcow2 2(宿主机) # virt-install --name vm-ubuntu-test01 --memory 8192 --vcpus 4 --disk /home/kvm/images/ubuntu-test.qcow2 --import --virt-type kvm --os-type linux --os-variant ubuntu18.04 --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0,password=foobar,port=5910 --noautoconsole --hvm
测试增量镜像
-
进入 vm-ubuntu-test 虚拟机, 并创建一个200M的文件
1dd if=/dev/zero of=test.img bs=1M count=200 -
在宿主机上查看镜像大小
12.4G ubuntu-base.qcow2 2294M ubuntu-test.qcow2
可以看出基础镜像大小保持不变, 增量镜像大小发生变化(为什么不是增加200M大小?)
-
进入 vm-ubuntu-base 虚拟机, 并创建一个800M的文件
1dd if=/dev/zero of=base.img bs=1M count=800 -
在宿主机上查看镜像大小
13.0G ubuntu-base.qcow2 2294M ubuntu-test.qcow2
可以看出基础镜像大小发生变化, 增量镜像大小不变
