Zerofree snippets

This post demonstrates bash zerofree to shrink virtual box disk image

Our goal is to remove unused space from a virtual box disk image file (vdi)

Step one in the recovery root console of the Virtual Box Linux system

Boot the recovery root console and list all partitions. Remount and zerofree the device which you wan’t to shrink: replace /dev/sdx1 with your disk partition f.e. /dev/sda1

#!/bin/bash
fdisk -l
mount -r -o remount /dev/sdx1 
zerofree -v /dev/sdx1
sync
shutdown -h now

Step two in the Windows OS run

VBoxManage modifymedium --compact

VBoxManage.exe modifymedium disk "C:\path\to\mydisk.vdi" --compact