DEV Community

Cover image for How to Reduce the VMDK Disk Image Size in VirtualBox
Roman Belshevitz for Otomato

Posted on

How to Reduce the VMDK Disk Image Size in VirtualBox

Yes, the author decided to shake the old days, and remember something more mundane, from his sysadmin everyday life.

There is the more popular format for portable disk images than VirtualBox Disk Image default VDI one, VMDK, which stands for Virtual Machine Disk invented by VMware by the end of 2011. It is widely promoted and supported by major cloud providers such as AWS, Google Cloud, DigitalOcean, VMDK Hosting etc. This format of disk images has considerable potential: cases of reaching a size of 64 terabytes under the control of vSphere hypervisor are described.

In practice, such volumes, of course, are not required. Many IT people are working remotely even in 2022. And on the contrary, the problem is often reversed (hello European and Israeli broadband 🐌 providers with their wonderful rates, asynchronous access and shaped upload!): reduce a disk image as much as possible.

VMDK do allow incremental backups of changes to data from the time of the last backup, unlike VDI does. This makes the backup process for VMDK files much faster compared to VDI and VHD. Unofficial tests also show that VMDK is significantly faster than VDI. VMDK disk image may be streamOptimized for featuring so-called sparse data support.

So, in order to make your virtual machines use less disk space, this article describes how to shrink (compact) the virtual disk files (files with .vmdk extension). Additionally, it is simpler to upload and distribute them toward others.

A standard deletion routine won't make VirtualBox aware that the space is truly free unless it has been set to zero. Run this command after logging in to the virtual machine:

cat /dev/zero > zero.fill; sync; sleep 1; sync; rm -f zero.fill
Enter fullscreen mode Exit fullscreen mode

You may even more reduce consumed disk space by disabling swap file if it is allocated within the same disk as the operating system is. An example:

swapoff /dev/sda4
Enter fullscreen mode Exit fullscreen mode

⚠️ Do not forget to comment out the swap partition in your /etc/fstab file and reboot your virtual machine after doing that.

Open the folder for the VirtualBox virtual machine. The VirtualBox VMs subdirectory in your home directory houses the virtual machines you've set up in VirtualBox (or at least when Ubuntu is your host machine). Each machine has a distinct directory that begins with the name of the machine. To condense a virtual machine's disk, you must open a terminal and navigate to the folder containing its files.

Image description

Change to that directory with cd. Obtain the virtual disk's UUID. The UUID of the disk you wish to shrink must be obtained. Later, I'll explain why. This is the order to accomplish it:

vboxmanage showhdinfo box-disk1.vmdk
Enter fullscreen mode Exit fullscreen mode

This command expects that the vmdk file is located in the same directory as your working directory. If the name of the vmdk file differs, you must of course update it. The reported UUID should be noted because you'll need it later.

Make it into a VDI image. You must use this command to convert the disk to VDI format in order to compress it. Without an intermediate step, it is not possible to do this. The vboxmanage utility is already available to you on the host with VirtualBox installed.

VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format vdi
Enter fullscreen mode Exit fullscreen mode

Compact:

VBoxManage modifyhd box-disk1.vdi --compact
Enter fullscreen mode Exit fullscreen mode

Revert it back it to vmdk:

VBoxManage clonehd box-disk1.vdi box-disk1.vmdk --format vmdk
rm box-disk1.vdi
Enter fullscreen mode Exit fullscreen mode

The initial UUID value. The old vmdk disk file was converted to vdi, compressed, and a new vmdk file was made from the compressed one. Thus, in essence, you have created a new virtual disk with a new UUID that VirtualBox will not recognize unless the original disk's UUID is changed using the command:

vboxmanage internalcommands sethduuid ./box-disk1.vmdk <_original UUID here_>
Enter fullscreen mode Exit fullscreen mode

¡Ahí está! Successfully reduced the size of the virtual disk's image!

Please keep in mind: preserving UUID is not an actual task for every case. You will get a kind of exception in Oracle VirtualBox when you try to add another VM with same UUID.

Good internet speeds to you, in the direction of your cloud provider or hosting company!

P.S.: When writing the article, not a single sysadmin girl was mentally or physically harmed.

Top comments (0)