Saturday, October 22, 2011

Shrink VirtualBox disk images

In Linux (Debian) guest:
  • apt-get install zerofree
    zerofree is a small program which uses the ext2fs library to get a list of all the blocks, checks if the current block is free but non-zero (i.e. has been used at some point) and fills it with zeroes again. The utility also works on ext3 or ext4 filesystems. This allows the VBoxManage tool to compact the VDI.
  • init 1
    to go to single mode.
  • mount -n -o remount,ro -t ext2 /dev/XXX /MOUNTPOINT"
    to remount all partitions read only (chec the available filesystem with df).
    • -n : not try to update "/etc/mtab" (which might be on the now read only disk)
    • -t ext2 : not need journaling.
  • fsck.ext2 -f /dev/XXX
    to check the filesystem (if necessary).
  • zerofree /dev/XXX
  • fsck.ext2 -f /dev/XXX
    again to check the filesystem (if necessary).
  • mount -t ext4 -o remount,rw /dev/XXX /MOUNTPOINT
    to remount the partitions back to normal.
  • Shutdown the virtual machine.
In Windows guest:
  • Perform hard disk defragmentation.
  • Download sdelete from http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx
  • sdelete -c DRIVE:
  • Shutdown the virtual machine
In host:
  • VboxManage modifyhd /path/to/the/vmdiskimage.vdi --compact
Ref:
- http://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvdi
- http://intgat.tigress.co.uk/rmy/uml/sparsify.html
- http://forums.virtualbox.org/viewtopic.php?t=1198
- http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06

No comments: