Using virtualization has its benefits, but also has its shortfalls. For example, creating and deleting files in a virtual disk could make disk usage larger than what it is supposed to be. For example, disk usage of filesystem /dev/sdb1 in the VM is 45 GB. But the virtualbox disk image (VDI) size is 57 GB, as shown below :
Reading various posts, the cure seems consist of two steps :
- run zerofree to fill unused spaces with zeros
- run vboxmanage with option --compact
The reference could be read in several blog posts (for Vmware : here, here , for VirtualBox : here and here) .
But the caveat is, there is no zerofree rpm for RHEL 6. One of the blog posts hinted that we could create one by recompiling zerofree Source RPM (srpm) for Centos 5.
First, obtain the source rpm :
Using RPM search at (RPM search zerofree) , we have these urls :
Total files number: 7
|ftp.muug.mb.ca||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|mirror.switch.ch||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|ftp.gwdg.de||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|ftp.icm.edu.pl||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|ftp.pbone.net||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|ftp.sunet.se||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
|ftp.is.co.za||zerofree-1.0.1-5.el5.src.rpm||RedHat EL 5||17490|
Download one file of the list, and build it :
E2fsprogs-devel need to be installed first, I suppose.
Reexecuting rebuild :
After successfull build, install the RPM :
Afterwards, stop the services using the /dfs, unmount the partition, then run zerofree on /dev/sdb1
The final step is shutting down the VM and run vboxmanage with modifyhd command, using --compact option and also pointing to the VDI image :
After all complete, we have this :
57 GB have turned into 45 GB, yielding 12 GB of additional space.
By running zerofree first and then compacting the VDI, we could free the wasted space in the VirtualBox VM.