VBoxManage modifyMedium

Published on 24 Apr 2018

I was surprised to see the virtual disk image (VDI) of my Ubuntu 17.10 headless server was at 9 GB instead of 4 GB (as reported by Glances). So I went to investigate with the goal to free up that stray 5 GB.

9.45 GB before Ignore the label. It is actually Ubuntu 17.10 server

After a quick reading session, I learned that files never get deleted from drive, they are just marked as “deleted” by the operating system, or in computer lingo it is called unallocated. The truth is, files are still there. To completely wipe them out, it can be done by overwriting them with zero. I went with zerofree to get this done.

There are two parts for this: first, boot the guest OS into recovery mode (so drive our target drive, /dev/sda1, is not mounted by the oprating system) with root shell to perform wiping out operation with zerofree. The second step is shutting down the VM to perform modifymedium --compact operation with VBoxManage.exe.

Note: I did this operation on my Ubuntu 17.10 headless server with Windows 10 host. On Windows 10 host, I am using ConEmu as my terminal.

zerofree inside guest OS

boot into recovery mode

First, boot into recovery mode. The boot splash screen can go really quick, so pay attention here. What I did was I frantically pressed the <Esc> key so that I could catch the GRUB bootloader. After that, go into the recovery mode and then drop to the root shell.

root shell in recovery mode

The next step is to execute zerofree against target partition. Since I partitioned my Ubuntu with LVM when I first installed it, there was a slight twist. If I partitioned my drive without LVM, I would be expecting my target drive being /dev/sda1. Instead, with the command df, I saw:

/dev/mapper/hendriks--vg-root

… as my partition. No big deal. Let’s run zerofree against that partition.

zerofree -v /dev/mapper/hendriks--vg-root

… and wait for a little bit.

df and zerofree

Once this operation comes to its completion, issue poweroff command to turn off the VM and proceed to the second part on host machine.

ModifyMedium on Windows 10 host

The binary VBoxManage.exe resides within C:\Program Files\Oracle\VirtualBox. Change into this directory first with cd command. After that, run VBoxManage.exe to start the process.

# Change directory
cd "C:/Program Files/Oracle/VirtualBox"

# Run ModifyMedium
./VBoxManage.exe modifymedium --compact ~/path/to/vdi.vdi

This would take a few minutes to finish. Once it is done, you might want to compare the size of the VDI before and after the process.

before & after

acknowledgements

Thanks to an article on How To Geek and a thread on Super User.