Shrink Your Virtualbox Dynamic Expanding Storage

Thanks to Michael Cole’s Blog on Slimming down a Linux VirtualBox machine for this important nugget of knowledge.

I wrote a bunch of files to a dynamically expanding virtual disk in VirtualBox.  After I deleted the files, the dynamic virtual drive on the host was still huge.

This is because the files on the virtual disk are not really gone, they are just unlinked from the file system, waiting to be overwritten with new data.
To get rid of this unwanted unlinked data (Linux), write over them with zeros like so:

sudo dd if=/dev/zero of=/zerofile; sudo rm /zerofile

sudo dd if=/dev/zero of=/zerofile; sudo rm /zerofile

Then shutdown your virtual OS and tell VirtualBox on the HOST machine (Windows in my example) to compress the virtual drive.


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\djh>cd .VirtualBox
C:\Documents and Settings\djh\.VirtualBox>"C:\Program Files\Sun\VirtualBox\VBoxM
anage.exe" modifyvdi CentOS-cf7.vdi compact
VirtualBox Command Line Management Interface Version 3.1.0
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
C:\Documents and Settings\djh\.VirtualBox

That freed up some space, nice!

Leave a Reply