VMware Tips
Tips for VMware Workstation (Linux and Windows), VMware Fusion (Mac), VMware Server, VMware ESXi Server, VI3 and vSphere {4,5}...
How to shrink a Virtual Machine (Dynamic virtual disks)? Reduce the size of your VMs, claim free space.
1. Online mode (running VMware Tools inside the guest OS)
Windows Guest OS
Linux Guest OS
2. Offline mode (without running the guest OS, running command line tools on Host OS)
Using
vmware-vdiskmanager -k
NOTE: swith -k shrink the virtual disk, is available on Linux, Mac and Windows.
Examples
Linux
vmware-vdiskmanager -k "/home/terry/VM/Arch/ArchLinux.vmdk"
Mac OS X
/Library/Application\ Support/VMware\ Fusion/vmware-vdiskmanager -k vm.vmdk
Windows
vmware-vdiskmanager.exe -k "D:\VMware\Linux\linux.vmdk"
NOTE: To shrink Linux guest OS, zero out all free disk space for all partitions/Virtual Hard Disks is required! It works with LVM. The vmware-vdiskmanager -k removes blocks that only contains zeroes. For this operation to be effective, it is required to zero out free space in the guest system using a suitable software tool. Microsoft provides the sdelete tool for Windows guests. Execute sdelete -z (-c does NOT work any more in sdelete v1.6 and above, -z is for virtual disk optimization - zero out) in the guest to zero the free disk space before compressing the virtual disk image. Shrink works both for base images and for diff images created as part of a snapshot (VirtualBox).
Zero Out Disk space
Linux
cat /dev/zero > zero.fill; sync; sleep 1; sync;rm -f zero.fill
NOTE: It will be interrupted when disk is full, see the output: cat: write error: No space left on the device
Equivalent command
dd bs=4096 if=/dev/zero of=/mountpoint/of/partition/empty_file ; rm -f /mountpoint/of/partition/empty_file
- you will probably find that the vmdk is a few hundred M bigger
- init 0
- run the vmware-vdiskmanager -k again
- check the size of the vmdk
Mac OS X
Disk Utility - Erase - Zero Out data
Or use dd as Linux
Solaris
cat /dev/zero > zero; sync; sleep 1; sync;rm -f zero
Windows
sdelete -c c:
NOTE: http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx
Reference
http://www.howtoforge.com/how-to-shrink-vmware-virtual-disk-files-vmdk
http://h0bbel.p0ggel.org/shrinking-vmdk-fileshttp://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvdi
Tested on Debian GNU/Linux and Ubuntu
- Ubuntu x86
ubuntu.vmdk
After filling with zeroes, Debian 3.66G -> 3.85G
After shrinking 1.89, bingo! - Arch Linux x86_64
arch linux.vmdk
5472M -> 4931M - Oracle Enterprise Linux 5 with LVM
filled /var and /usr logical volume (partition)
5996M -> 4916M
VMware Fusion on Mac OS X
In Terminal
/Library/Application\ Support/VMware\ Fusion/vmware-vdiskmanager -k vm.vmdk
Increasing the size of a virtual disk
/Library/Application\ Support/VMware\ Fusion/vmware-vdiskmanager -x 100Gb vm.vmdk
Merge split 2GB VMDK files into a single file
Synopsis
vmware-vdiskmanager -r <sourcedisk> -t <0|2|4|5|6> <targetdisk>
Example
vmware-vdiskmanager -r src.vmdk -t 0 dst.vmdk
Explanation
-r <sourcedisk> <targetdisk>
Converts (clones)thevirtualdisknamedby<sourcedisk>, creatinganewvirtual disk named by <targetdisk>.
- For local <targetdisk> the -t option is required to specify type
- For remote <targetdisk> on an ESX host, use the -h, -u, and -f options
- For the virtual machine to recognize the converted virtual disk, edit the virtual machine settings to remove the existing virtual disk from thevirtual machine,then add the converted disk to the virtual machine.
-t [0|1|2|3|4|5]
Specifies the virtual disk type. This option is required when you create or convert a virtual disk.
Choose one of the following types:
- 0 – create a growable virtual disk contained in a single file (monolithic sparse).
- 1 – create a growable virtual disk split into 2GB files (split sparse).
- 2 – create a preallocated virtual disk contained in a single file (monolithic flat).
- 3 – create a preallocated virtual disk split into 2GB files (split flat).
- 4 – create a preallocated virtual disk compatible with ESX server(VMFS flat).
- 5 – create a compressed disk optimized for streaming
Reference: Virtual Disk Manager User’s Guide