Thursday, February 3, 2011

Compacting Virtualbox VDI files

How to compact an Oracle Virtualbox VDI file

As most of these posts do, this one originated in a problem that required me to stop everything and find a solution. I've been a long time Virtualbox user for running virtual machines on my laptop system for testing and demonstrations as well as application isolation. Well, a few days ago one of my Windows XP virtual machines was refusing to update an installed application; it would just fail. I checked the disk space inside the VM and it showed 12GB total space on C with 4GB free. Then, I dropped back to the host OS (Windows 7 Pro if you must know) and checked the size of the VDI file - it was 11.86GB. So, the solution search began. Here's what I was able to come up with to correct the problem. The steps below are for a Windows guest OS - I'll give Linux guest OS steps in another post.

1. Within the virtual machine's OS (in this case Win XP Pro SP3) I went to Start-Computer-Open-C_drive and right clicked - and went to Properties-Tools and ran a Defrag on the disk. So step one is to Defrag the guest OS.

2. Go to the Microsoft site and download the Windows SysInternals tool called SDELETE into the VM's guest OS. Unzip the file and then go to Start-Run and CMD. Once in a command line window change directory to wherever the SDELETE executable file is located. Run the following command: SDELETE -C This will fill all empty areas of the guest OS virtual disk with zeros.

3. Ok, now for step three. Once SDELETE has finished, then log out and close the VM guest OS. In the host OS, you need to know the path to the VDI file that you are wanting to compact (ex: c:\vms\XPtest\XPtest.VDI). Once noted, then go Start-Run-CMD to open a command line windows and navigate to the directory containing the Virtualbox program (ex: c:\program files\Oracle\Virtualbox). Now, execute the following command:

VBoxManage modifyvdi VDINAME compact

The example in my case would be VBoxManage modifyvdi XPtest.VDI compact Now sit back and let the process run to completion.

By performing these steps I was able to shrink my VDI file size in the host OS to 8.2GB, leaving me nearly 4GB of expansion room in the guest OS.

Yeah, so I know the next logical question - can you expand a VDI file? Nope, although it's a heavily requested feature. My solution - create a new, larger VDI file; attach to the VM that you want to enlarge VDI; boot the VM to an ISO of the UBCD4Win file and use SelfImage to do a byte-level copy of the small VDI to the larger VDI. Shut down the VM, edit the settings to remove the small VDI from the VM and leave the larger VDI - power on the VM. If you have an older OS that does not allow you to enlarge a "C" partition, you can boot one more time to the UBCD4Win ISO file and use the partition management utilities to enlarge the volume before re-booting into the guest OS. Let me know if I need to do a more detailed post on this process and I'll add it to my list.


.