Methods of Compacting a Disk Image file

November 7, 2019 | 4 minute read
Mikael Fransson
Consulting Cloud Solutions Architect
Text Size 100%:

Introduction

When you create Virtual Machines there are a few options for the virtual hard disk file type and it's storage. Besides the different formats you can choose between dynamically allocated or fixed size files.

  • A fixed size will immediately occupy the size you specify. It will perform a little bit better but it will take longer to create. If you specify it to be 60GB it will take that space on your host regardless if you fill the VM with anything or not.
  • A dynamic disk file will be created instantly and grow as it needs space. It will however only grow, not shrink, hence this blog post. Since the disk file grows on a need basis it will also spend some resources doing so you might notice a performance impact. Most laptop and desktop users will use this disk type since space is always limited. Most laptops now also uses SSD drives so the performance penalty is no longer a concern.

VirtualBox has some very powerful command-line tools to deal with different aspects of managing virtual machines. Shrinking and recovering disk space on dynamic disk files is one of those tasks that you do from the command line. You can either use Vboxmanage that comes with Virtualbox or you can use a 3rd party tool. Both methods described below.

Use cases

Many of us runs Virtualbox and multiple VM's on our laptops. We don't have unlimited storage. Compacting Virtual Disk files can save GB's

It is now possible to export / upload your Virtualbox VM's to OCI. Compacting your VM's disk file will save bandwidth and time when uploading to OCI.

Preparation

Before you can shrink a VM disk image file you need to zero out all unused disk blocks. So for every file system (this is for linux file systems) in the guest you need to do something like this (as root):

dd if=/dev/zero of=zero.tmp bs=10M; rm -f zero.tmp

There are a couple of alternative methods and tools available but they all basically do the same thing. Write zero's to unused blocks.

For Windows there is a tool called sdelete (secure delete?) that will do this.

sdelete -z

https://docs.microsoft.com/en-us/sysinternals/downloads/sdelete

The VirtualBox way

Stop the VM and run the command below on the command line for every disk file you have.

VBoxManage modifyhd -compact thediskfile.vdi

This also only works on VDI files. If your VM has any other disk image format such as  vmdk you can use the Vboxmanage clone feature.

VBoxManage clonehd myoldfile.vmdk mynewfile.vdi --format VDI

The 3rd party way

There is a free tool called CloneVDI that you can download and use. It's been around for more than 10 years. It’s a Windows tool but as you can see from the screenshot below I use it with Wine (WineBottler is a nice packaging of Wine on macOS, I use the latest development builds) on my mac. You can also install Wine via Homebrew.

Wine and CloneVDI seems to not work on macOS Catalina. Likely due to CloneVDI being a 32 bit app.

This tool will read most any virtual disk format (Reads VDI, VHD, VHDX, VMDK, Parallels HDD, RAW files and physical drives, writes VDI.). I’m just highlighting two features in the image above but there is more to read here. CloneVDI has knowledge about the file system in the VM so in the cloning process it will only write used data blocks to the new VDI file (you need to check the checkbox shown above of course).  

You still need to zero out unused blocks though, see above.

The process of writing a new disk image file is very fast and stable. I have used CloneVDI for years and it has never produced a corrupt output. It also never touches the source disk file so I consider this a safe method. This is my preferred method.

Using CloneVDI

When you clone a file with the settings as shown above you will get a new and smaller disk image file in the same folder as the original. Since it has the same UUID you can simply move or rename the original file and rename the clone to have the name of the original file. Virtualbox will not notice that you replaced the disk image file. Do not delete the original disk file until you verified that the clone works. I have never seen the clone not work but you can't be to cautious with destructive commands...

Using WineBottler to build / run CloneVDI on macOS

These are the settings I used with WinBottler to build a self-contained app. This app will be huge (+1GB) as it contains all the Wine libraries but you don't need to worry about dependencies.

References

Document links to Virtualbox command line usage above

https://www.virtualbox.org/manual/UserManual.html#vboxmanage-modifyvm

https://www.virtualbox.org/manual/UserManual.html#vboxmanage-clonevm

 

Mikael Fransson

Consulting Cloud Solutions Architect

Consulting Cloud Solutions Architect - A-Team


Previous Post

Design considerations for Platform as a Service enhanced availability.

Nick Goddard | 9 min read

Next Post


To OCI and back with Virtualbox

Mikael Fransson | 12 min read