Thursday, 28 April 2011

VirtualBox : Compacting / Shrinking a Virtualbox drive file

Using Hyper-V over the past couple of years, I blogged on size optimization of the VHD files.
(They get bloated and consume more space than necessary)

Now I'm using VirtualBox for a personal SQL Server test environment, here is how to do the same for VirtualBox drives.

Compacting a VirtualBox Drive
  1. Delete rubbish from your virtual machine (clear temporary internet files, windows installer files, unwanted data, empty recycle bin etc)

  2. Defragment the drive (Use the OS’s defragmenter or third party FREE tool Defraggler)

  3. Zero out the unused data space in the guest OS

    You have 2 options here -
    i. Use Precompact.exe (details in Hyper-V post) or
    ii. Use Microsoft’s SDelete  e.g. SDELETE –c C:  (a lot simpler)

  4. Shrink the hard drive file from the host OS using VboxManage.exe
    vboxmanage modifyhd filename.vdi compact

    I found that Vboxmanage only runs from it’s install directory (dependency on DLL files) hence run it from there, e.g.

    C:\> cd C:\Program Files\Oracle\VirtualBox

    C:\Program Files\Oracle\VirtualBox> VBoxManage modifyhd c:\!RD\Virtualbox\w2003.vdi compact
This screenshot shows the space i was able to reclaim from one of my VMs...

Resources
http://www.dedoimedo.com/computers/virtualbox-shrink-expand-disks.html
http://www.plinky.it/blog/2009/05/18/virtualbox-compact-the-vdi-hard-disk-file/

No comments: