If you’ve created your VirtualBox VM with VDI that dynamically allocates space, you should know that the VDI size is not the same as the size of your VM guest filesystem. VDI size equals to the max. size your guest filesystem ever had and VirtualBox is not shrinking it back automatically. To shrink it back to the guest filesystem size, do the following:

Boot into your VM from Linux live CD/DVD
This step is not required if you you’re not going to shrink root filesystem.

I’ve used Oracle Enterprise Linux 6 iso with rescue mode.
You can use any Linux live CD/DVD that has dd and vgchange (for LVM) utilities. Most of them do.

Fill the empty space on each volume
If you’re using LVM, first do the following:

1
2
3
4
# to see the volumes in /dev/mappper
vgchange -a y       
# list the volumes
ls /dev/mapper

Following applies to any volume that can be mounted by your Linux live CD/DVD.
Create mount point for volume:

1
mkdir /mnt/root

Mount volume:

1
2
3
mount /dev/mapper/your_lvm_lv /mnt/root
# e.g. /dev/mapper/vg_oel-lv_root /mnt/root
# or for non LVM, e.g. mount /dev/sda1 /mnt/root

Change directory to the mounted volume:

1
cd /mnt/root

Fill empty space with zeroes:

1
2
# this may take some time
dd if=/dev/zero of=empty.file

Remove the file:

1
rm empty.file

Sync filesystem:

1
sync

Change the directory:

1
cd

Unmount the volume:

1
umount /mnt/root

Do the same for other volumes…
Shutdown the VM:

1
shutdown -h now  # or halt if you're missing shutdown command

Shrink/Compact the VDI

1
VBoxManage modifyhd --compact your.vdi

VBoxManage can be found in VirtualBox installation directory. I’ve put it to the PATH so that I can run VBoxManage from everywhere.

Enjoy!

, , , , ,
Trackback

no comment untill now

Add your comment now