简介
本文介绍使用qemu-img创建qcow2格式磁盘文件的预分配(preallocation)策略,及对虚拟磁盘性能的影响。
qcow2磁盘及预分配策略介绍
查看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.)
- full模式:分配所有磁盘空间并置零,预分配后的虚拟磁盘属于非稀疏映像类型(allocates zeroes and makes a non-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)
测试与验证
使用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
从测试结果上看,可得出以下结论:
1. 缺省策略和metadata策略创建出来的磁盘属于稀疏磁盘格式,而falloc和full策略创建出来的磁盘属于非稀疏磁盘格式。
2. 由于falloc策略,无需对磁盘进行置零操作,因此其创建虚拟磁盘时的速度要比full模式快的多
在查看磁盘文件信息时,也可以使用"qemu-img info"和stat命令:
[root@CentOS65 kvm]# qemu-img info /home/kvm/test1-offdata.qcow2
image: /home/kvm/test1-offdata.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 136K
cluster_size: 65536
[root@CentOS65 kvm]# stat /home/kvm/test1-offdata.qcow2
File: `/home/kvm/test1-offdata.qcow2'
Size: 197120 Blocks: 272 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 2359300 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-01-29 05:07:24.747787726 -0500
Modify: 2016-01-29 04:43:52.047787980 -0500
Change: 2016-01-29 04:43:52.047787980 -0500
性能影响
通过falloc和full策略创建的磁盘的性能,要比另外两种策略创建的磁盘高的多,具体数据可以参考资料3.
参考资料
1. Little more disk I/O perf. improvement with ‘fallocate’ing a qcow2 disk
2. Creating a Qcow2 virtual machine
3. qcow2-why not?