averainy's Blog

averainy

24 Apr 2022

How to Convert Free Space in Qcow2 Image to Kvm Host

How do you convert free space within the QCOW2 disk image to free space on the kvm host? I use Virt-sparsify to do this.

qcow2 image is a very common file format for kvm virtual machine.It can automatically increase space with the increase of files in the virtual machine,but It cannot decrease space with the decrease of files in the virtual machine.So,here we can use Virt-sparsify to do that.

DESCRIPTION

Virt-sparsify is a tool which can make a virtual machine disk (or any disk image) sparse a.k.a. thin-provisioned. This means that free space within the disk image can be converted back to free space on the host.

Virt-sparsify can locate and sparsify free space in most filesystems (eg. ext2/3/4, btrfs, NTFS, etc.), and also in LVM physical volumes.

Operation steps

  1. shutdown virtual
sudo virsh shutdown win10_test
  1. use Virt-sparsify,I recommend you to set parameter ‘–tmp’,because virt-sparsify nedd enough tmp space to restore middle files,and the defualt “/tmp” usually does not hava enough space.
sudo virt-sparsify win10_test-sda win10_test-sda_new --tmp tmp
  1. back up old qcow2 image file and rename new qcow2 image file
mv win10_test-sda win10_test-sda_old
mv win10_test-sda_new win10_test-sda
  1. now you can start your virtual machine
sudo virsh start win10_test
  1. Be caefully check the new virtual machine can boot and work correctly,If ok,delete the old image file .
rm win10_test-sda_old

Categories