本文介绍使用qemu-img创建qcow2格式磁盘文件的预分配(preallocation)策略,及对虚拟磁盘性能的影响。
查看qemu-img手册,可以看到关于qcow2格式磁盘文件和预分配策略的简要介绍:
qcow2
QEMU image format, the most versatile format. Use it to have smaller images (useful if your filesystem does
not supports holes, for example on Windows), optional AES encryption, zlib based compression and support of
multiple VM snapshots.
Supported options:
"backing_file"
File name of a base image (see create subcommand)
"backing_fmt"
Image format of the base image
"encryption"
If this option is set to "on", the image is encrypted.
Encryption uses the AES format which is very secure (128 bit keys). Use a long password (16 characters)
to get maximum protection.
"cluster_size"
Changes the qcow2 cluster size (must be between 512 and 2M). Smaller cluster sizes can improve the
image file size whereas larger cluster sizes generally provide better performance.
"preallocation"
Preallocation mode (allowed values: "off", "metadata", "falloc", "full"). An image with preallocated
metadata is initially larger but can improve performance when the image needs to grow. "falloc" and
"full" preallocations are like the same options of "raw" format, but sets up metadata also.
qcow2是QEMU的虚拟磁盘映像格式,它是一种非常灵活的磁盘格式,支持瘦磁盘(类似稀疏文件)格式,可选的AES加密,zlib压缩及多快照功能。在使用qemu-img创建qcow2虚拟磁盘,可以设置磁盘预分配策略,其支持4种格式:
off模式
缺省预分配策略,即不使用预分配策略
metadata模式
分配qcow2的元数据(metadata),预分配后的虚拟磁盘仍然属于稀疏映像类型(allocates qcow2 metadata, and it’s still a sparse image.)分配元数据所需的空间,但不为数据分配任何空间,这是配置最快的,但客户机写入最慢。
falloc模式
使用posix_fallocate()函数分配文件的块并标示它们的状态为未初始化,相对full模式来说,创建虚拟磁盘的速度要快很多(which uses posix_fallocate() to “allocate blocks and marking them as uninitialized”, and is relatively faster than writing out zeroes to a file)。
为元数据和数据分配空间,但将块标记为未分配, 这将比metadata配置得慢,但比full配置要快。客户机写入性能将比metadata快得多,并且接近full。
full模式
分配所有磁盘空间并置零,预分配后的虚拟磁盘属于非稀疏映像类型(allocates zeroes and makes a non-sparse image)
为元数据和数据分配空间,因此将消耗所有物理空间(而不是稀疏的), 所有空的已分配空间将设置为零,这是最慢的配置,跟falloc客户机写入性能相近。
使用qemu-img创建qcow2格式虚拟磁盘,并设置不同预配置参数:
[root@CentOS65 kvm]# qemu-img create -f qcow2 /home/kvm/test1-offdata.qcow2 5G
Formatting '/home/kvm/test1-offdata.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536
[root@CentOS65 kvm]# qemu-img create -f qcow2 -o preallocation=metadata /home/kvm/test1-metadata.qcow2 5G
Formatting '/home/kvm/test1-metadata.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='metadata'
[root@CentOS65 kvm]# qemu-img create -f qcow2 -o preallocation=full /home/kvm/test1-fulldata.qcow2 5G
Formatting '/home/kvm/test1-fulldata.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='full'
[root@CentOS65 kvm]# qemu-img create -f qcow2 -o preallocation=falloc /home/kvm/test1-fallocatdata.qcow2 5G
Formatting '/home/kvm/test1-fallocatdata.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='falloc'
[root@CentOS65 kvm]# ls -lash /home/kvm/
total 13G
4.0K drwxr-xr-x. 2 root root 4.0K Jan 29 04:45 .
4.0K drwxr-xr-x. 6 root root 4.0K Jan 25 08:48 ..
5.1G -rw-r--r--. 1 root root 5.1G Jan 29 04:45 test1-fallocatdata.qcow2
5.1G -rw-r--r--. 1 root root 5.1G Jan 29 04:44 test1-fulldata.qcow2
912K -rw-r--r--. 1 root root 5.1G Jan 29 04:44 test1-metadata.qcow2
136K -rw-r--r--. 1 root root 193K Jan 29 04:43 test1-offdata.qcow2
从测试结果上看,可得出以下结论:
参考:
http://www.linux-kvm.org/images/9/92/Qcow2-why-not.pdf
https://www.jamescoyle.net/how-to/1810-qcow2-disk-images-and-performance