Skip to main content

VirtualBox: How To Shrink VDI File

One area where Parallels is much better than VirtualBox is in the ease of compacting a VM disk to what's actually in use.

I've found that VM disks just keep increasing in size, regardless of what you might delete. Parallels makes compacting easy - all you do is select "Shrink..." from the Parallels menu.

The situation with VirtualBox is more complicated 😀
While there is a command line "compact" option, this relies on unused stuff being zero - which it won't be for things that got deleted...

However there is a tool (zerofree) which you can run in the VM and it sets all the free space on the disk to zero, which can then be compacted in VirtualBox.

Here are the steps I've been using:

Step 1: Install zerofree in the VM (Linux in my case):

$ sudo apt install zerofree

Step 2: Empty Trash and delete unneeded files

Step 3: Boot into a recovery session

  • Enter root password.

Step 4: Remount disk as read-only

# mount -n -o remount,ro /dev/sda1

Step 5: Run zerofree on disk

# zerofree -v /dev/sda1

Step 6: Shutdown VM

Step 7: Shrink VDI File from Host

$ VBoxManage modifymedium disk <your disk>.vdi --compact

Let me know if there is an easier way?!
In particular, it would be great if the zeroing part could be done from the host environment.

Comments

Popular posts from this blog

My Work in new Top Trumps Birds of Prey Pack

The new Top Trumps "Birds of Prey" pack has my picture illustrating the Secretary Bird card 😀 Here's the original picture: From Flickr

On BBC Springwatch !

BBC Springwatch featured one of my Goldfinch pictures last night: Goldfinch on Springwatch Shame they spelt my name wrong though! See the original on Flickr.

Emacs and MacOS Catalina

Catalina introduced a lot of security changes and the most intrusive is probably all the popups asking to give permission for apps to access directories under Home, like Documents. Worse still, apps which weren't written to handle the new security measures might just fail silently with no clues for the user. A solution is to give apps like Emacs "Full Disk Access" under "Security & Privacy" in Preferences, to give unfettered access to your files and avoid all the popups and silent failures. Sounds good, but that doesn't actually work for Emacs because "Emacs" in the app bundle is actually a Ruby script which decides which flavour of Emacs executable to run. This never mattered before, but it does under Catalina because MacOS thinks the executable is /usr/bin/ruby . Conventional wisdom is therefore to give "Full Disk Access" to Ruby. While this does work, I've always been uncomfortable giving all Ruby scripts full access...