KVM Virtlization: RECLAIMING EMPTY DISK SPACE
In KVM Hosts, we mostly used Sparse files, this could be qcow2 format or raw file. These files had certain features, most importantly thin thin-provisioning,so it grow as data stored in Guest Disk from inside Guest platform. .In this post we would see how to reclaim Disk space of KVM Guest machine. We will use qemu-img command for same.
We just need to take care of final Disk size required for Guest, before Guest installation. Further Disk size will automatically grow when Guest grow.
But sometime when we free some space from Guest machine,it will not show in Host machine. In host machine Guest disk will continue grow. This is because free space from Guest is filled with binary zeros.
So it’s annoying sometime that we are using less Disk space in Guest machine but still showing more Disk space in Host machine. It should occupy same space on host disk as used by Guest machine. For this we need to re-claim disk space.
For this setup we are using Ubuntu-16.04 Host machine and CentOS6 Guest machine. Actually this method doesn’t effect any distribution of Guest OS. It only work on Disk file in shutdown state of Guest machine.
Hosts machine #lsb_release -d Description: Ubuntu 16.04.3 LTS qemu-img command installed through package and its version #dpkg -S /usr/bin/qemu-img qemu-utils: /usr/bin/qemu-img #dpkg -l qemu-utils | awk '$2=="qemu-utils"' ii qemu-utils 1:2.5+dfsg-5ubuntu10.14 amd64 QEMU utilities
We will work on CentOS6 Image. Below are details for same.
Disk path of Guest #virsh domblklist srv6 Target Source ------------------------------------------------ vda /data/vmware_images/CentOS6/system.img #cd /data/vmware_images/CentOS6 Disk path file status #qemu-img info system.img image: system.img file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 8.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
So we have Disk file of srv6 Guest of about 8 GB. Let’s try to see how much space we could reclaim from this Disk.
First we should shutdown Guest machine.
#virsh domstate srv6 shut off
Just for precautionary, i am taking backup of Disk before doing re-claim.
Total size of Disk path #cd /data/vmware_images/CentOS6 #ll total 8.1G -rw-rwxr--+ 1 root root 5.1K Apr 15 08:15 srv6.xml* -rw-r--r-- 1 root root 8.1G Aug 9 13:17 system.img let's take backup and copy to work on #mv system.img system.img.org #cp -rvfp system.img.org system.img.bak 'system.img.org' -> 'system.img.bak' #ll total 21G -rw-rwxr--+ 1 root root 5.1K Apr 15 08:15 srv6.xml* -rw-r--r-- 1 root root 8.1G Aug 9 13:17 system.img.bak -rw-r--r-- 1 root root 8.1G Aug 9 13:17 system.img.org Use qemu-img commmand to reduce file size #qemu-img convert -f raw -O qcow2 system.img.bak system.img Hence it's reduced #ll total 21G -rw-rwxr--+ 1 root root 5.1K Apr 15 08:15 srv6.xml* -rw-r--r-- 1 root root 4.1G Aug 9 13:23 system.img -rw-r--r-- 1 root root 8.1G Aug 9 13:17 system.img.bak -rw-r--r-- 1 root root 8.1G Aug 9 13:17 system.img.org
Although we use qemu-img convert, but it was already qcow2 file and we are just re-converting it which only copy valid space into new file name
Now its reduced to 4.1 GB of Disk for same Guest Machine.
#qemu-img info /data/vmware_images/CentOS6/system.img image: /data/vmware_images/CentOS6/system.img file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 4.1G Reduced Size cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
Let’s start Guest Machine with same Disk.
#virsh start srv6 Domain srv6 started #virsh console srv6 Connected to domain srv6 Escape character is ^] CentOS release 6.9 (Final) Kernel 2.6.32-696.6.3.el6.x86_64 on an x86_64 srv6 login: root Password: Last login: Wed Aug 9 14:42:55 on ttyS0 [root@srv6 ~]# uptime 15:53:04 up 0 min, 1 user, load average: 0.00, 0.00, 0.00
Now machine is up and running with same Disk file with reduced file size. Now we can delete extra Disk used for backup while re-claim.
Small video on same