PreallocateQCow2Images

16th April 2015 at 11:36am
TechnicalNotes

You probably do not want to create sparse VM disk images. Discovering in the middle of the night that you have an over-subscribed host filesystem and than your hypervisor and guest VMs are all running like dogs with an insanely high load because of IO-wait isn't a nice way to be woken up!

Alas, despite some man pages online claiming that qcow2 format images do support full preallocation, in reality I have found this not to be true:

nicolaw@localhost:/home/nicolaw $ qemu-img create -f qcow2 -o size=5G,preallocation=full disk.img
Formatting 'disk.img', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='full' lazy_refcounts=off 
qemu-img: disk.img: Invalid preallocation mode: 'full'

Using dd would probably take a while too, so instead we can use the fallocate command instead:

qemu-img create -f qcow2 -o size=30G,preallocation=metadata disk.img && fallocate -l 30G disk.img

If you want to see the difference for yourself, try this:

qemu-img create -f qcow2 -o size=30G,preallocation=metadata disk.img

ls -lash disk.img
ls -las disk.img
qemu-img info disk.img

fallocate -l 30G disk.img

ls -lash disk.img
ls -las disk.img
qemu-img info disk.img