Shrink a vmdk Virtualbox disk image

I didn't need to install VMWare nor convert back to VMDK so I used

https://scotch.io/tutorials/how-to-create-a-vagrant-base-box-from-an-existing-one#toc-make-the-box-as-small-as-possible

Inside the host:

sudo yum clean all
sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
cat /dev/null > ~/.bash_history && history -c && exit

Then after the guest is shut down:

$ vboxmanage clonehd --format vdi centos-7-1-1.x86_64.vmdk newdisk.vdi
$ ls -lh
-rwx------+ 1 Chloe None  39G Mar 26 14:52 centos-7-1-1.x86_64.vmdk
-rwx------+ 1 Chloe None  22G Mar 26 15:01 newdisk.vdi

It also allows compaction later

$ vboxmanage modifyhd newdisk.vdi --compact

Inside VirtualBox GUI, I selected 'Choose Virtual Hard Disk File' to select the new file.


I could not get rubo77s solution above vmware-vdiskmanager solution to work, I believe it has dependencies on vmware workstation or vmware player, neither of which I have, I did find the executable and it gave me errors.

I was able to solve this by using his zero command

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

then using virtualboxes export to .ova tool.

this will result in the ova stripping/compressing the zeroed space. then you can re-import it.


I found a solution:

First inside the VM fill all free space with zeros:

cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill

In your Host, install vmware-vdiskmanager from the VMware Knowledge Base:

cd /tmp/
wget http://kb.vmware.com/selfservice/viewAttachment.do?attachID=1023856-vdiskmanager-linux.7.0.1.zip&documentID=1023856
unp 1023856-vdiskmanager-linux-7.0.1.zip
mv 1023856-vmware-vdiskmanager-linux.7.0.1 /usr/bin/vmware-vdiskmanager
chmod +x /usr/bin/vmware-vdiskmanager

Take care, that you have enough free disk-space before you start, you need the MV grows to double size during the process.

Then compress it with:

/usr/bin/vmware-vdiskmanager -k ~/VirtualBox\ VMs/<virtual disk.vmdk>

Source

Tags:

Virtualbox