KVM image format convertion: raw thin provisioning to raw preallocated

disk-imagekvm-virtualizationqemu

I'd like to convert a KVM virtual machine disk image in raw file from a thin privisioning format to preallocated.

In the first place I've got a preallocated 20Gb raw image file:

image: /var/lib/libvirt/images/ArchLinux.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 20G

I used the following command to make the backup disk file:

sudo qemu-img -O qcow2 -cp ArchLinux.img BackupArchlinux.qcow2

After that I tried to get the first image file back using:

sudo qemu-img -O raw -p BackupArchlinux.qcow2 Archlinux.img

but I get a thin privisioned raw format as you can see here:

image: .../ArchLinux.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 1.6G

How can I convert this raw file to preallocated format back?

Note: qemu-img version 2.11.0

Best Answer

To fully preallocate a RAW image, rather than copying/converting the whole image with qemu-img, you can simply issue fallocate <image_file> -l <size_to_preallocate>

In your case, you can issue fallocate ArchLinux.img -l 20G.

Of course, do a backup before messing with the disk's image file.