Posts Tagged ‘sdelete’

How to shrink your dynamically expanding VirtualBox image (handle the VBOX_E_FILE_ERROR 0x80BB0004).

Sometimes bigger isn’t always better. If your dynamically expanding virtual machine images are growing out of control, then here’s how to trim them back.
I’m a big fan of VirtualBox and use separate virtual machines (VMs) for the various separate bits and pieces I’ve got on the go (as I invariably end up messing something up, and can just trash the image and start again, without taking down whatever else it is I’m playing with at the time).
All my VMs use a dynamically expanding image for their hard drive, where you set the maximum size of the disk, but the system will only grow to fill that space if required. By setting this nice and high, I can be sure that the hard drive space is there if I need it, without taking space away unnecessarily from the rest of the system.
Unfortunately, whilst VirtualBox will dynamically expand the hard drive as it’s required, it won’t dynamically shrink it again if you free up space in the VM. This can be a problem, especially if, like me, the sum total of all those theoretical maximums exceeds the actual maximum capacity of the hard drive hosting all these VMs.
The good news is that you can shrink those images back down again. The bad news – it can be awkward process. I hope, this note will go through the steps to shrink your Virtualbox image.

1. Free up space in the client machine.
It’s a bit of an obvious first step but you can only shrink down the client VM by the size of the available free space therein, so delete the files and uninstall the programs that you no longer need but are hogging your resources. Don’t forget to empty recycle/trash bins 🙂

2. Perform a hard disk defragmentation.
Again, it’s rather obvious step in Windows guests. For Linux guests – here is a nice try.

3. Clean the garbage.
After defragmentation, the data is nicely aligned but we still got all those unused blocks that contain garbage (the contents of the files that used to live there). Therefore we need a tool that can find these blocks and overwrite them with zeros.
Windows does not come with such a compact tool but it’s (sdelete) available for download from Microsoft.
Open a command prompt and move to the sdelete directory. Type in the following command (this will add zeroes to all the free space in your C drive):

sdelete -z c:

For Linux guests you need to use zerofree but it is (as always) more complicated – here you can find more details.

4. Shrinking the VM.
Now that we are done with clearing up the VM and zero out all the free space, it’s time to shrink the file size using the VboxManage command. First of all you need to shutdown your VM 🙂
Quite a lot of the online guides say that you’ll have to clone the hard drive image to shrink it, as VirtualBox 2.2 and above dropped support for compacting the image. This isn’t true (certainly not for version 4.3.6 – the version I used to wrote this note) and you can shrink the image in-place with the following command:

VBoxManage modifyhd my_image_name.vdi –compact

where you replace my_image_name.vdi with the name of the image drive you’d like to shrink (for more information on this command, see the VirtualBox manual).

And that’s all – you’ll now have plenty of disk space to fill with equally useless shit 🙂

Unfortunately, life is sometimes not so simple and during shrinking your VM image, you can get the VBOX_E_FILE_ERROR 0x80BB0004 error. How to handle it? In my case, I’ve got the following error:

VBoxManage.exe: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component Medium, interface IMedium
VBoxManage.exe: error: Context: "int __cdecl handleModifyHardDisk(struct HandlerArg *)" at line 582 of file VBoxManageDisk.cpp

First, I try to check disk to verify if there are no errors. I used CHKDSK tool in Windows. On Linux you could use FSCK command. There were no disk errors in my case.
Therefore, I decided to use CloneVDI – a nice tool with GUI which solved my problem. Here you can find more details and download it.