VirtualBox Compactification

Shrinking a VDI is sometimes necessary and possible with the right set of tools.

Unfortunately, one of my laptops has a suboptimal distribution of disk space in partitions. The distribution is suboptimal as I couldn't place a certain VM on one disk and use the other disk as a shared one; placing it on one disk either brings the disk close to zero or prevents me from sharing the disk.

The solution was to optimize the existing disk, which was set to dynamically grow. However, from the shown 86 GB only 55 have been effectively used. So there was a potential of 31 GB that could be gained here. However, how do I shrink a VM / the VDI used by a VM using Oracle's VirtualBox?

First, we need to prepare the guest system. We start by removing unnecessary files (e.g., cleanup). Then we run the defrag tool to have an improvement in the disk usage.

At this point we need to download the SDelete tool from Sysinternals and run the following command on the guest system.

.\sdelete.exe c: -z

The drive letter is referring to the VDI drive. So if there are multiple VDIs used inside the guest system we should either run SDelete for all of them, or pick the ones we are after. It could be that the SDelete is stuck at 100%. If this happens just be sure to wait another 5 to 10 minutes before killing the process via CTRL + C.

In the second part we shut down the guest system and continue to work on the host. Here the tool VBoxManage is ready to help us. It contains the verb modifyhd, which comes with some options.

We open a command prompt and change the directory to Oracle's VBox, e.g., C:\Program Files\Oracle\VirtualBox. Now we run the following command:

.\VBoxManage.exe modifyhd thedisk.vdi --compact

In the previous call we replace the thedisk.vdi argument with the path to the VDI to compact. The command will take some time. If everything worked correctly we should have the desired success. Confirm that the VDI shrunk and is still usable.

Created . Last updated .

References

Sharing is caring!