Home‎ > ‎VirtualBox‎ > ‎

Shrink virtual hard drive Ubuntu as Host OS and Windows 7 Guest OS

In VirtualBox, expanding hard drive will gradually use up more and more space and user will end up with a big file that consists of mostly unused data, emptying trash is not good enough, one must wipe free space with zeros and use special tools to shrink virtual hard drive. Assuming you use VirtualBox, check whether you use virtual disk image .vdi. According to manual, it is the native VirtualBox format and only this one supports the --compact option to this date.

To wipe free unused space inside Windows as Guest OS use SDelete.exe

Download and extract .zip file with SDelete by Mike Russinovich from Microsoft's website to your virtual machine.

In Guest virtual machine enter Windows command line ex. from Start > Run > and type in: cmd

Make sure you know where your sdelete.exe is, in my case I am logged in as admin and sdelete.exe is downloaded on my Desktop, so my command is:

C:\Users\Admin> cd Desktop
C:\Users\Admin\Desktop>sdelete -z c:

This will zero out free space on your virtual machine's hard disk and then you can use vboxmanage and it's --compact option on Host OS to shrink the volume. It takes quite a lot of time, in my case on 500 GB .vdi with 4GB dedicated RAM to virtual machine and i7 processor, it took about 45 minutes, .vdi contained about 60 GB of data and physical hard disk is a pretty fast notebook 7200 RPM hard disk with 4 GB SSD cache.

From Ubuntu Host OS, compact the wiped .vdi

By default, all your virtual machines are in /home/username/Virtual Box VMs/... do not forget to use "" around paths when entering commands in the command line, as long as they contain spaces.

$ vboxmanage modifyhd "/home/VirtualBox VMs/Windows.7/Windows.7.64.Pro.vdi" compact