07 Jul 2011

# Reducing the size of a VirtualBox hard drive

Often times I have run into the situation where I need to share a VM image with a co-worker or store as a base starting point. Case in point today I needed to create a SharePoint 2010 on Windows 2008 R2 image for some classes and development I will be doing in the next few weeks.

After getting done with the base install I was left with a large footprint on my hard disk of the virtual hard drive that I know could be reduced in size. So here are the steps I took to reduce the size of the virtual hard drive.

### 1. Clean Up

The first thing that you need to do is clean up any places on the virtual machine that have temporary or unnecessary data. For instance:

1. Temp directories
2. Internet caches

### 2. Defragmenting

The second thing you want to do is defragment you virtual hard drive. This will reorganize the space to be used in the most efficient manor. It is best to defrag you virtual hard drive at least 3 times.

### 3. Cleaning Free Space

Just because you have deleted a file, doesn’t mean that it is really gone from the hard drive. Or in our case the virtual hard drive, so the next thing we need to do is zero out all the free space on the virtual hard drive. I prefer to use a tool from SysInternals called SDelete. You can find SDelete here. After you have downloaded, extract to your C drive and run the following command:

sdelete -p 3 -c c:

### 4. Compacting Virtual Hard Drive

The next step requires you to shut down your VM and run one final command. Everything above has been pretty generic and can be used for any VM software, however the next part is VirtualBox specific, if you use another client please check the documentation.

We are now going to compact the virtual hard drive by removing all the free space. To do this in VirtualBox we need to open up the command prompt as an administrator and run the following:

cd C:\Program Files\Oracle\VirtualBox

This will bring us the VirtualBox tools directory where we want to run the following command:

vboxmanage modifyhd c:\path\to\vdi\image --compact

For me this command looked like this:

vboxmanage modifyhd "C:\Users\Nick\VirtualBox VMs\SharePoint 2010" --compact

### My Results

For me the above resulted in a 2.5 GB savings, yours will probably vary. From here you can copy the reduced size image to another drive or share it with a co-worker. Personally I reduce the size even farther by compressing the image using 7-zip for easy transportation, but that is up to you.

Tags: ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.

#### 6 Responses to “Reducing the size of a VirtualBox hard drive”

This article was titled incorrectly. It should read “Reducing host storage taken by a VirtualBox hard drive”. While your article is certainly a nice synopsis of reducing the amount of space taken by a VirtualBox hard drive on the host, it doesn’t affect the guest size of the drive at all. You also could have added merging a VM’s snapshots as a way to further reduce its footprint.

So any idea about reducing the amount of space taken by a VirtualBox hard drive on the host?

Thanks for this article. After following the steps you describe, I found out that using the -c option for sdelete is not the right one, because it only cleans free space. To be able to reduce the disk image size you should use the -z option, which nullifies the free space. The sdelete documentation mentions that this option is especially useful for compressing virtual disk images.

Nice article Nick!
I did not use sdelete technique, but will try one day.
I am used to use Paragon Hard Drive Manager Pro iso-cd image to boot into Windows virtual machine. It has a defragmenter, defragmenting is done on the offline hd-image, and thus is producing much better results versus defragmenting system drive of running VM. I can change the size of the partition, if necessary. Then wiping up the free space feature can write zeroes doing multiple passes, again while the guest’s system drive is offline. Then, after shutting down the guest (I run Linux host):
vboxmanage modifyhd “/home/user/VirtualBox VMs/Windows XP Pro.vdi” –compact