While using Virtualbox, one thing that I have found is that the size of the VM (a file with .VDI extension) expands over time. If you have selected the “Dynamically Expanding Storage” option when first creating the VM, you will find that the file size expands whenever you install new applications in the VM. That is perfectly normal. However, when you uninstall or delete applications in the VM, the file size does not shrink or return to its previous size. That is to say, if you are constantly installing and uninstalling applications in your VM, the VDI file could be taking up more space than it is actually using.
If you have a great deal of hard disk space to spare, you can simply ignore this. However, if you find yourself always running short of hard disk space, you might want to consider shrinking the VM and free up unused disk space.
This tutorial will go through the steps to shrink your Virtualbox VM.
It doesn’t matter if you are using Windows/Linux host. The instructions for a Windows and Linux guest are slightly different though. (I have not tried this on a Mac host yet. Theoretically it should work, but do it at your own risk.)
For Windows guest
If you are using Windows guest, do the following:
- Uninstall all unnecessary applications
- Perform a hard disk defragmentation. If you are using WinXP, you You can access the inbuilt disk fragmenter from Start -> All Programs -> Accessories -> System Tools -> Disk Defragmenter.
Download sdelete and extract it to your desktop.
Open a command prompt and cd to the sdelete directory. Type in the following command:
This will add zeroes to all the free space in your C: drive.
Once it is done, shutdown the VM.
For Linux guest
If you are using Linux guest (this tutorial assumes you are using Ubuntu and is running on ext2/ext3 filesystem. It does not work on ext4), here is what you need to do:
- Remove any unused applications.
- Clean up your Linux VM
Restart and boot your Linux VM into Recovery mode. (This is generally the second option in the Grub boot up screen.)
In the root shell prompt, type
This will display all the available filesystem in your OS. Record down the filesystem where the main directory lies. (it should be in /dev/sdaX or /dev/hdaX format)
Mount the filesystem in read-only mode with the following command:
replacing the /dev/sda1 with the filesytem info that you have recorded just now.
Next, type the following command to zero out all the free space.
Once again, replace the /dev/sda1 with your own filesystem info.
Once you are done, shutdown the Linux VM:
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.
If you are still using the older Virtualbox 2.0.x version, you can easily shrink the hard disk with the following command:
If you have upgraded your Virtualbox to the 2.1.x version, you will find that the above command will no longer work. There is a bug in the software that disable the use of the modifyhd command. An alternative solution is to use the clonehd command to creat a smaller clone copy of the original VM.
In your terminal (or command prompt), type the following:
This will create a clone of the original VM with the name that you have specified in the command above.
After shrinking my VM, I manage to shave 45% off the size of the oirginal VM and free up a whopping 4.5GB of hard disk space to store my other stuff.
The original VDI: 10GB
The cloned VDI: 5.5GB
Importing the clone VM into Virtualbox
The whole process is not finished yet. You still need to import the clone VM into Virtualbox and remove/delete the original VM from your system.
In your Virtualbox console, click on the New button.
Follow the instructions to create a new VM. When it reaches the screen to ask you to choose the hard disk, select Existing.
In the Virtual media manager window, click Add to add the newly cloned VM.
Highlight the newly added VM and click Select.
Back to the Hard disk selection window, you should see your new VM in the dropdown selection. Click on Next to finish up the import.
Once everything is in place, run the newly added VM to make sure that it is working fine.
On the main Virtualbox screen, highlight the original VM and go to Machine -> Delete. This will remove the entry from the console.
Go to File -> Virtual Media Manager. Highlight the original VM and click the Remove button. When prompt if you want to keep the file, select Delete. This will delete the vdi file from your system.