Shrinking VirtualBox VDI Files
December 20, 2008 – 2:24 pm
If you’re like me and have several VirtualBox Windows virtual machines, you’ll probably find they gradually take up more and more space as time goes on. This is at least partly because the filesystem gets fragmented and old deleted files still take up space in the VirtualBox VDI file. If you’ve created the VDI as a dynamically expanding image, it will only take up as much space in your filesystem as the files inside it take up – but when those files are deleted it won’t shrink again.
A VirtualBox VDI can be made to shrink, however, but first you need to do something about what’s inside it. A useful application called ‘VBoxManage’ can shrink those virtual disk images – but only if the empty space is zeroed first. I run VirtualBox on a Linux system and i use the method i’ve described here – but i can’t guarantee that all of what i’ve written applies to other operating systems.
The first step in reducing the amount of hard disk space a VDI takes up is to delete as much junk from inside it as you can. You can start by deleting the hidden folder of backup files which is created when you install a service pack. This is called \Windows\$NTServicePackUninstall. If you do this, you won’t be able to roll back that service pack upgrade. Personally, i’m not bothered by this because i keep a backup copy of the VDI which i can use to roll back and start again, in the unlikely event that i need to.
Depending on how the installation was done, there may also be a folder C:\Windows\SoftwareDistribution\Download. You can delete that too.
The service pack installation creates a temporary folder as well. This doesn’t always get properly removed. It will be on the drive you downloaded the setup files to, which is probably C:, and it’ll have a name made up of random characters – e.g., C:\be35a76f21ed. If it’s still there, you can delete it.
Don’t delete C:\Windows\ServicePackFiles though. This will be used in future. However, if you want, you can compress it and save a fair bit of space. Right click on it, select “Properties”, click on the “Advanced” button, and select “Compress”.
The next step is to defragment the Windows file system. I always do this two or three times in succession, as it seems to keep improving the way the files are scattered around a bit. That probably doesn’t matter much (or at all), but it seems tidier to me!
After that, you need to zero all the empty space in the filesystem. Download ‘sdelete’ from http://www.microsoft.com/technet/sysinternals/Security/SDelete.mspx to do that job. You should probably download sdelete before you defrag, as when you unzip it a small amount of extra fragmentation is likely to occur.
Open up a DOS window and run sdelete with
sdelete -c c:
(assuming C: is your filesystem). That will zero all the free space.
Then shut down Windows and you’re ready to shrink the VDI file by running the following command on it:
VBoxManage modifyvdi WinXP.vdi compact
(replace “WinXP.vdi” with the name of your vdi file).
One of my VDI files had grown to 5.2GB recently – from a couple of months of Windows updates, etc. Using the method above, i managed to shrink it back to 2.7GB.
I tried this tip but there was no change in the size of the .vdi, still 4.9GB. 🙁
Did you defragment the Windows file system and run ‘sdelete -c’ first?
If so, how much space does Windows tell you is used in that filesystem?
I think one key point is that the clean up actions, especially the sdelete, need to be run in the guest OS in the virtual machine, not in the host.
Thanks Hsiang! I guess i didn’t make that clear in the article.
Ooops 🙁
D:\vBox>VBoxManage modifyvdi d:/vBox/Virtual_Machines/Win2K3srv.vdi compact
VirtualBox Command Line Management Interface Version 2.1.2
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
Error: Shrink hard disk operation is not implemented!
D:\vBox>
Have you any idea?
I suspect it may not work on the filesystem you’re running VirtualBox on. The filesystem that the VDI file resides on must support the method they use to make that file smaller.
I’ve only used VirtualBox on Linux (ext3 filesystem) and i’ve got no idea how this stuff works with any of the Windows filesystems. I’d suggest posting your question in the VirtualBox Windows hosts forum.
It is as the message says, the ‘compact’ option to shrink disks is not available (in 2.1.2, 2.1.4), although it was in an earlier version.
There is a simple workaround you can use to do the shrinking: clone the hard disk. The command would be something like:
VBoxManage clonehd d:/vBox/Virtual_Machines/Win2K3srv.vdi d:/vBox/Virtual_Machines/Win2K3srvCLONE.vdi
If you performed the steps described above (must run sdelete!), this will give you a shrinked new disk (with new UUID).
Have fun!
Dominik
I’m running Ubuntu 8.10 and I’m getting “Shrink hard disk operation is not implemented!” Is this something you can only do with a dynamic disk or is it supposed to work on a fixed disk as well? I made the mistake of making mine fixed and now I’m looking to shrink it.
I’m sure you can’t shrink a fixed-size VDI – that wouldn’t make any sense at all.
All i can suggest is to create a new blank dynamic VDI, install Linux in a virtual box virtual machine (probably with a “live” CD/DVD ISO as the boot disk), attach you current fixed VDI and the new dynamic VDI to that Linux VM, and copy one to the other, using ‘dd’.
You’ll need to do it from the command line, and the command you’ll use will be something like:
dd if=/dev/hda1 of=/dev/hdb1
(assuming your existing Win VDI is /dev/hda and your new dynamic VDI is /dev/hdb).
Before you do that, though, you’ll need to use ‘fdisk’ (in the running Linux virtual machine) to create a partition on the new variable disk.
Then you should be able to remove the old fixed-size VDI from you Win VM and attach the new dynamic one – and, hopefully, if everything went right, it will just work.
After upgrading to VirtualBox 2.1, i got the “Shrink hard disk operation is not implemented!” problem too. Reading the comments in this bug report explained why.
The solution is to uninstall VirtualBox and then install version 2.0.6 (which is the last build in the 2.0 series). You can download it from the old builds section of the VirtualBox download page. Shrinking a dynamic VDI with 2.0.6 works as outlined above.
So long as there are no features of the 2.1 version that you need, 2.0.6 should solve your problem. However, as 2.0 can’t handle virtual machines created by 2.1, you’ll have to set up your virtual machines again – this doesn’t affect the VDIs, though, they work fine.
Thanks for the hint on clonehd for doing a vdi compact.
For those getting an error on ‘shrink not implemented’ .. you can also shrink by doing this:
vboxmanage clonehd
Essentially this will create a copy, but at the same time, shrink. Follow all the steps on defrag/sdelete above
missing params because of the angle brackets, should be:
vboxmanage clonehd *sourcevdi* *destvdi*
Thanks Doug!
Will Kemp, thanks for this solution , its very good and helpful
now the .vdi shrunk to its ~1.8 GB real size , difference about ~7 GB
no need to clone or any other special commands
am using VBox v 3.0.2 (XP host, XP guest), just followed the steps as it is
1- Deleted unwanted files .
2- used sdelete (on guest’s OS c: partition) to zero free space .
3- modified the .vdi file from host command line >> VBoxManage modifyvdi WinXP.vdi compact .
thanks again 🙂
Thanks for the feedback Neo. I’m glad it was helpful.
Ok, I’m trying the steps above and no matter if i use the clonehd or compact… i get the error:
ERROR: Could not find file for the hard disk ‘/home/Keith01/.VirtualBox/VDI/WINXP2.vdi’ (VERR_FILE_NOT_FOUND)
Details: code VBOX_E_FILE_ERROR (0x80bb0004), component HardDisk, interface IHardDisk, callee nsISupports
Context: “OpenHardDisk(Bstr(szFilenameAbs), AccessMode_ReadWrite, false, Bstr(“”), false, Bstr(“”), srcDisk.asOutParam())” at line 624 of file VBoxManageDisk.cpp
I even went in the folder and still same thing…. i’m confused… any help would be appreciated..thanks
Also, I’m running Ubuntu 9.04 x86 and VirtualBox is 3.04. The guest OS are WinXp…
Does /home/Keith01/.VirtualBox/VDI/WINXP2.vdi exist? What happens if you do
ls -l /home/Keith01/.VirtualBox/VDI/WINXP2.vdi
?
Thanks for the useful article. FYI I tried your steps without doing the defrag (VirtualBox 3.0.6) and it shrunk my VDI from 16GB to 8.5GB
Despite the cred of Sysinternals, sdelete appears to be badly broken. I’ve come across numerous postings about it hanging up before completion. Right now, it has hit a brick wall at 33% in my Win2K VM.
Alternatives suggested like Eraser and BleachBit DO NOT set unused file system blocks to zeros – they offer alternative algorithms to overwrite previous data with random bits (as designed).
Any alternatives to sdelete, anyone? Or is there a secret switch to make it complete?
It might be interesting to disable service like sqlserver etc. before starting to defragment you’re hard disk.
Thanks a lot for this hint! Helped me a lot!
Thanks!!! Worked for me on Linux Mint 8 (32 bit) reiserFS 3.6 and Windows XP on VirtualBox. From 7.5 GB to 4.6 GB.
Thanks, I really appreciate your information and will be making your site one of my regular visits.
It’s a combination of BleachBit (executed on the Linux guest) and a VM clone executed on the host, in case you are trying to compact a Linux VM running within VirtualBox. See also http://www.ontoport.de/?q=blog/wbartussek/how-compress-virtual-disk-linux-virtualbox.
Didn’t work for me with sdelete … but making the partition inside the VM smaller and doing a VDI compact after that worked.