Compacting Virtual Machines (VirtualBox and VMWare)

Google has never linked me directly to this information, just theories. One day time permitted me to run careful tests so I am sure these techniques are correct/ efficient:

Simple tricks to reduce size VM’s Disk Needs:
These will wipe GBs from your vmdk/ vdi.

  • Disable Windows hibernation (hiberfil.sys is the size of installed memory, you don’t need it)
  • Disable the memory paging file (paging file in a VM makes little sense to me)

Compact a VMDK (VMWare including VMWare Player):

  • Ensure you have no snapshots (as of writing compacting does not work with snapshots)
  • Launch the VM
  • Inside the VM defragment its disk (defraggler works great, Windows degfrag is ok)
  • Inside the VM run “sdelete.exe -z” from DOS (as admin). This zeros out the free space and is an essential step
  • Shut down the VM
  • From VMPlayer: Edit Machine Settings -> Hard Disk -> Utilities -> Defragment (optional step, sometimes helps – official documentation is poor)
  • From VMPlayer: Edit Machine Settings -> Hard Disk -> Utilities -> Compact

At the final step you should see a huge reduction VMDK size.

Below is a screenshot showing the features in VMPlayer. Remember this is next to useless unless you run Mark Russinovich’s “sdelete.exe -z” to mark free space with zeros. Compacting VMs has been this way for years, it’s April 2013 now and surely soon ‘detect and zero free space’ functionality will be built into their compact options.

Compact_VMWAre_Player_VMDK

The image above shows a VM that reached 20Gb once, before being compacted back down to 10.7GB. These are typical results. Once compressed my two work VMs zipped down to ~4GB each; fine for archiving working databases, dev environments etc. One customer’s backup procedures left me concerned so weekly the VMs were AES encrypted and copied to a USB key chain flash drive.

Compact a VDI (VirtualBox):

Until very recently I have used VirtualBox since about 2008. Here are the steps to compact it.

  • Ensure you have no snapshots (as of writing compacting does not work with snapshots)
  • Launch the VM; inside the VM defragment its disk (defraggler works great, Windows degfrag is ok)
  • Inside the VM run “sdelete.exe -z“. This zeros out the free space and is an essential step
  • Shut down the VM
  • From DOS (as admin):
    • cd <location of your VDI>
    • “C:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifyhd <your disk’s name>.vdi –compact

Hope this helps folks. Any issues/errors please post in the comments and I’ll update the post.

Advertisements
This entry was posted in Technology. Bookmark the permalink.

6 Responses to Compacting Virtual Machines (VirtualBox and VMWare)

  1. FireSword says:

    Thank you! It worked with me too. When a new version of the vmware player is available, I make a new virtual machine (it has newer virtual hardware), and attach to it the old VMDK file. When the machine boots it finds some new hardware, and after that works fine again.

  2. sellenoff says:

    Thanks so much for this post. You should modify it to mention that the machine must not have any snapshots or the process will fail to work. I discovered this the hard way.

  3. twalp says:

    Does using “sdelete -z” end up enlarging the virtual drive to its maximum size?
    Does the “Clean Up Disks” feature in Workstation 10 eliminate the need for the process you describe?

    • Qu 1: I have not noticed that. Host s/w is presumably smart enough

      Qu 2: One day hopefully. Am not sure about V10. At home I use free versions, and at work I only use vShpere – other manage

      Sorry for the late reply. A lot of spam came in during June/July and your post was buried in there

  4. about “sdelete -z”: pay attention that -z has changed meaning in the latest releases this may explain the size going to max intead of shrinking:
    SDelete – Secure Delete v1.61
    -z Zero free space (good for virtual disk optimization)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s