Block Device in QEMU
Preallocate filter / preallocation=full
QEMU
In QEMU, the preallocate filter is used to allocate the full size of a virtual disk image's backing storage when the image is created, rather than allowing the storage to be allocated dynamically as data is written to the image. This can be particularly useful for ensuring that sufficient disk space is available for the virtual machine and can help avoid runtime errors due to insufficient disk space.
Example command using qemu-img to create a preallocated image:
qemu-img create -f qcow2 -o preallocation=full disk_image.qcow2 10G
-
full
: Allocates the entire space. Both the data blocks and the metadata are fully allocated on the physical storage. -
metadata
: Allocates only the metadata required to manage, The actual blocks are allocated dynamically as data is written to the disk. Might still lead to some performance overhead during runtime due to lack of zero-initialization. -
falloc
: Uses theposix_fallocate()
function to preallocate, This reserves the disk space while avoiding the overhead of writing zeros to the allocated space.
prealloc-align
/ prealloc-size
QEMU
这两个这样用:
qemu-img create -f qcow2 -o preallocation=full,prealloc-align=1M disk_image.qcow2 10G
qemu-img create -f qcow2 -o preallocation=full,prealloc-size=64K disk_image.qcow2 10G
#define PREALLOCATE_OPT_PREALLOC_ALIGN "prealloc-align"
#define PREALLOCATE_OPT_PREALLOC_SIZE "prealloc-size"
static QemuOptsList runtime_opts = {
.name = "preallocate",
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
.desc = {
{
.name = PREALLOCATE_OPT_PREALLOC_ALIGN,
.type = QEMU_OPT_SIZE,
.help = "on preallocation, align file length to this number, "
"default 1M",
},
{
.name = PREALLOCATE_OPT_PREALLOC_SIZE,
.type = QEMU_OPT_SIZE,
.help = "how much to preallocate, default 128M",
},
{ /* end of list */ }
},
};
prealloc-size
Allows for more granular control over disk space allocation, which can help balance between performance and storage efficiency. This can be useful for specific workloads that benefit from particular chunk sizes. In above example, prealloc-size=64K
specifies that each preallocated chunk should be 64 KB in size.