Today I suddenly got errors due to low disk space in my VirtualBox virtual machine. It appeared that I only had a few bytes of free space left on my dynamically expanding hard disk of 60 GB, so I was getting into trouble!
To solve this, I closed all programs, removed unneeded data and applications on the virtual hard disk (by deleting or uninstalling them manually and then using the disk cleanup utility):
After that I was able to free up 3 Gb, but of course this was not enough. It appears you have to do a number of additional things in order to really shrink the hard disk – and maybe at the same time make it a little bigger.
The next thing to do was to run defragmenter. In my case however the disk was not fragmented, so this step was not really necessary.
Then I downloaded a tool called sdelete and executed it in the virtual machine in an elevated command prompt (run as administrator), as follows:
This tool zeroes out unused bytes, which is needed before actually compacting the disk. After this, I shut down the virtual machine.
Now because the format of my virtual disk was VMDK, I had to download a tool called CloneVDI, which enables you to clone a virtual hard disk and at the same time compact and expand it. Cloning is also a safer method because if something goes wrong, you don’t screw up your original hard disk and so you can still go back and try again:
Note that I also checked the ‘Keep old UUID’, as I want the exact same machine (otherwise windows would have to be reactivated). If the new size is bigger, make sure to also check the ‘Increase partition size’ (alternatively you can extend the partition later in disk management of windows 7).
The clone process may take a while to complete:
The end result however is a virtual disk in native VDI format. Now it was just a matter of creating a new virtual machine and selecting the new VDI hard disk for it. But before that you can do this, you have to remove the original hard disk from the Virtual Media Manager, otherwise it will complain that a disk with the same UUID already exists:
After this, starting the new virtual machine resulted in a blue screen. The reason is that the hard disk was added under SATA controller, so I removed it and added it again under IDE controller:
After that it worked as expected.