As you may aware, VirtualBox has 2 types of VDI file; static and dynamic. While static VDI is good to minimize potential VDI file fragmentation, it requires a lot of unnecessary disk space. Assuming you allocate 55GB of disk space for VDI (logical hard disk space in VirtualBox machine), static type will need the whole 55 GB for the VDI although your host OS just consume 4 GB (typical Windows XP installation). Thus, using Dynamic type is more practical and sensible (allocate as much as your guest OS uses, generally).
However, dynamic VDI is not 100% efficient. Due to various factors such as empty space, overhead and block reading method, VDI files seem to use significantly more space. For example, if your guest OS consume 20 GB, there is big chance that your VDI file requires up to 25 GB of actual hard disk space.
To solve this problem, we basically need to make the VDI file more compact and efficient thru
- Defragmentation (Consolidate free space) @ guest OS
- Nullify free space (convert free space to become null value) @ guest OS
- Ask VirtualBox to perform compact command @ host OS.
Let get started.
This is my WinXP SP3 + Visual Studio SP1 + SQL Server 2008 R2 virtual machine. The size of VDI files (main + 1 snapshot) is 43.8 GB. Let see how much I could save by compacting this VDI file.
In guest OS (Windows XP), defrag it thoroughly. I use MyDefrag (was known as JKDefrag previously). The idea is to have the biggest possible free space area. Choose consolidate free space option. Note: you can use built in windows defragmenter as well.
Nullify (Zero) the free space using sdelete utility.
Download sdelete at : http://technet.microsoft.com/en-us/sysinternals/bb897443
Copy sdelete.exe and paste it at your C drive.
At guest OS (Windows XP), open command prompt (Run -> Cmd).
Type sdelete –c C:\
Click Agree button.
Nullifying is in progress…
Once completed, shut down Guest OS (WinXP).
At host OS (Windows 7), type
VBoxManage modifyhd YourDisk.vdi –compact
Errr, clueless? Here is my example…
- My VirtualBox was installed at -> C:\Program Files\Oracles\VirtualBox
- My VDI image’s name: XP .NET.vdi.vdi (yeah, I accidently put .vdi as part of its name, resulting double .vdi there)
- and it is located at G:\VirtualBox HDD\XP (.NET)
Therefore, the correct syntax for my case is:
C:\Program Files\Oracle\VirtualBox\vboxmanage modifyhd “G:\VirtualBox HDD\XP (.NET)\XP .NET.vdi.vdi” – compact
From 43.8 GB become 21.1 GB. Wow. That is huge saving don’t you think.
Alternatively, you may use this handy tool (CloneVDI) to achieve same result as above method + you can clone your VDI as well.
- Download CloneVDI here : http://allmyapps.com/apps/clonevdi-virtualbox