Tuesday, November 15, 2011

zerofree - Zero out space on a virtual machine

Today, I had to shrink a virtual machine size for deployment.

I like the zerofree utility that clears the unallocated blocks in a ext2/ext3/ext4 filesystem.

Take a look at the code snipplet

First go to single user

# init 1

Then do the magic

# for i in `mount | grep sda | grep ext | cut -b 9` ; do mount -o remount,ro /dev/sda$i && zerofree -v /dev/sda$i && mount -o remount,rw /dev/sda$i ; done ;

And here is a sample on a VirtualBox VM

= ^ . ^ =