Reclaiming Free Space in Centos 6 / RHEL 6 VirtualBox Disk Images

Background


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 :

The VDI (cdh-node1_secondary_hdd.vdi) is about 57 GBs, yet the usage in /dev/sdb1 is 45 GB. So there is about 12 GB of wasted space.

The cure

Reading various posts, the cure seems consist of two steps :
  1. run zerofree to fill unused spaces with zeros
  2. 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.

Running zerofree

First, obtain the source rpm :
Using RPM search at (RPM search zerofree) , we have these urls  :
Total files number: 7
Site  Filename  Distribution  File size
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

Running VBoxmanage


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.

Conclusion

By running zerofree first and then compacting the VDI, we could free the wasted space in the VirtualBox VM.

Comments

Popular posts from this blog

Long running process in Linux using PHP

Reverse Engineering Reptile Kernel module to Extract Authentication code

SAP System Copy Lessons Learned