Shrink VirtualBox image

Some steps to reduce the size of VirtualBox image files. Originally for VirtualBox’s VDI format, but it can be applied for some other formats as well (OVA, OVF, … ) .

Given we have a VirtualBox image harddisk file called my_hd.vdi , which is used for running a guest OS on your VirtualBox. We’ll try to compact the size of the file by following steps:

Zero out the unnecessary spaces of the hard-disk

If the guest OS is Linux, we can use the dd tool, it can be applied to not only all Linux distributions but also other Unix-based OS (like OpenBSD, FreeBSD, . . . ) . Although you should be careful as a typo in the command with dd may result in formatting the hard disk or similar issues.

$ dd if=/dev/zero of=/bigemptyfile
$ sudo rm -rf /bigemptyfile

If the free space on the guest OS is not much left, you can use the following alternative:

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill


Another way (if you are in single mode, or have root priviledge) is to use zerofree tool: Install it with apt-get (Ubuntu/Debian) or yum (CentOS/SUSE) :

$ sudo apt-get install zerofree

Then utilize zerofree for the mounted hard disk (usually /dev/sda1 or /dev/hda1 ) :

zerofree /dev/sda1


If the guest OS is Windows, you can use either nullfile.exe or sdelete.exe to zero out the partitions (drives) :

> sdelete -c -z C:\

Ok, now shutdown the guest OS completely and resize the image hard disk on the host OS.


Use the tool of VirtualBox to shrink the size

You can do that with the VBoxManage.exe (Win) or VBoxManage (Linux), by this command:

VBoxManage modifyhd --compact my_hd.vdi

Example on Windows:

C:\Program Files\Oracle\VirtualBox> VBoxManage modifyhd --compact "D:\VirtualBox\my_hd.vdi"

Examples on Linux:

$ VBoxManage modifyhd "/home/ducquoc/my_hd.vdi" --compact

Besides, you may use the old tool of VirtualBox ( vditool ) , or the clonehd feature of VBoxManage to compact the hard disk image, too.

vditool shrink my_hd.vdi

VBoxManage clonehd my_hd.vdi my_hd_clone.vdi 
VBoxManage clonehd my_hd.vdi my_hd_new.vmdk --format VMDK

In new versions of VirtualBox (4.x+) , there is also has a GUI for clonehd to clone the VDI instead of using command-line. The decrease in that VDI size can be up to 70% , I’ve seen myself : 15.3GB -> 9.3GB, 11.9 GB -> 6.0 GB, 7.2 GB -> 2.4 GB, etc. . .


UPDATED: For VMWare fans, there is a counterpart for VMDK images too. After zero out the unnecessary spaces, we can use vmware-vdiskmanager to shrink the image size:

vmware-vdiskmanager -k my_hd.vmdk


Example on Windows:

C:\Program Files\VMWare\VMWare Server> vmware-vdiskmanager.exe -k "D:\Virtual Machines\my_hd.vmdk"


Examples on Linux:

$ vmware-vdiskmanager -k "/home/ducquoc/my_hd.vmdk"

And for VMWare ESX(i) the tool is vmkfstools .


Good luck,



A coder, husband and brother...
This entry was posted in Coding, Linux. Bookmark the permalink.

8 Responses to Shrink VirtualBox image

  1. Pingback: VirtualBox 디스크 이미지 크기를 줄이는 방법 « kyuls

  2. A common issue with RedHat-based Linux (such as CentOS, FedoraCore, …) is the *sendmail* service may hangup several minutes when booting (very common when you run Linux in VM). The root cause of that is DNS lookup problem.
    You can fix that issue by simply adding to your hosts file (/etc/hosts ) some IP for your hostname (if you are in a network using “domain” , apply the IP for your fully qualified domain name too): localhost localhost.localdomain myserver

    (If you don’t know the domain, you can look at “search” section in /etc/resolv.conf , or lookup the DNS server listed there).
    See also:

  3. Pingback: PuTTy X11Forward Xming | DucQuoc's Blog

  4. Pingback: Logging best practices | DucQuoc's Blog

  5. Thanks for finally talking about >Shrink VirtualBox image | DucQuoc’s Blog <Loved it!

  6. Luis says:

    Excelent and practical. Will try it now.

  7. HK says:

    thank you. It worked perfectly.

  8. Pingback: Kubernetes Minikube Windows | DucQuoc's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s