If you use KVM for managing your virtual machines, you can sometimes run out of space in the /var/lib/libvirt/images directory, the default location for the RHEL/CentOS distributions.
Because KVM knows the concept of thin provisioning through the qcow2 image format, the one used by the virt-install command by default, it would be crazy not to use it.
This can be done through the virt-sparsify command coming with the libguestfs-tools rpm package. So, the virt-sparsify command copies a VM image, only keeping the necessary data.
Let’s try an example.
Install the libguestfs-tools package:
# yum install libguestfs-tools
Go to the /var/lib/libvirt/images directory (or the one that you are using for storing your VM images):
# cd /var/lib/libvirt/images
Choose a VM and stop it (here vm.example.com) (use the virsh destroy command if necessary):
# virsh shutdown vm.example.com
Execute the virt-sparsify command:
# virt-sparsify -q vm.example.com.img vm.example.com.img2
Note: The -q option removes any display (quiet). Don’t ask me why this is not the default!
Get the size of the VM before and after:
# du -k vm.* 8388660 vm.example.com.img 1370168 vm.example.com.img2
Note: The ls command doesn’t display the correct information.
Replace the old image with the new one:
# mv -f vm.example.com.img2 vm.example.com.img
Restart the VM:
# virsh start vm.example.com
As you can see, it’s quite simple! I’m sure you will love it!
Recent Comments