How to Reduce the VMDK Disk Image Size in VirtualBox
Tags: systems, linux, storage, cloud
Published: 2022-07-08 11:33:26

> 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](https://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-image-import.html#upload-image), [Google Cloud](https://cloud.google.com/compute/docs/import/importing-virtual-disks), [DigitalOcean](https://docs.digitalocean.com/products/images/custom-images/how-to/upload/), [VMDK Hosting](https://www.vmdkhosting.com/faq.html) etc. This format of disk images has considerable potential: cases of reaching a size of [64 terabytes](https://vmtoday.com/2013/08/64tb-vmdks-yes-can/) 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](https://www.fastmetrics.com/internet-connection-speed-by-country.php) 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](https://github.com/libyal/libvmdk/blob/main/documentation/VMWare%20Virtual%20Disk%20Format%20(VMDK).asciidoc) 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
```
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
```

⚠️ 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ctk5vk0iof1vv3ldjiq.png)

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
```
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](https://en.wikipedia.org/wiki/VirtualBox#VirtualBox_Disk_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
```
Compact:
	
```
VBoxManage modifyhd box-disk1.vdi --compact
```

Revert it back it to vmdk:

```
VBoxManage clonehd box-disk1.vdi box-disk1.vmdk --format vmdk
rm box-disk1.vdi
```
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_>
```
¡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.