VMWare Fusion on Mac OSX allows you to shrink virtual disks easily for Windows virtual machines right from the graphical interface (Virtual Machine -> Settings -> General -> Clean Up Virtual Machine) but there isn’t the same option to do this with Linux, so you need to do it from the command line.
1. Clean up space
First of all you should clean up whatever space you can on the Linux virtual machine by deleting uncessary files, etc. After doing that, run this command as the root user on the Linux VM to zero out any free space – if you don’t do it, then the shrinking process won’t reclaim any space:
cat /dev/zero > zero; sync; sleep 1; rm -f zero
You’ll see this message when it has finished:
cat: write error: No space left on device
That’s OK, because the idea was to fill the disk up and then delete the file afterwards.
2. Shut down the virtual machine
You can’t reclaim space with the virtual machine running, so shut it down first.
3. Run the shrinking process
Open up Terminal on your Mac, change to the directory the virtual machine files are in (for example I have mine at ~/Virtual Machines/<vm name>) and then run the following commands, replacing “Virtual Disk.vmdk” with the actual name of your virtual disk image.
Note that if the filename contains spaces then the filename parameter to the command needs quotes around it, as in my examples.
The following examples assume VMWare Fusion is installed in Applications at the OSX disk root.
The first command defragments the disk:
/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager -d "Virtual Disk.vmdk"
I got the message “VixDiskLib: Invalid configuration file parameter. Failed to read configuration file.” when running this command. I don’t know what this error means, but the command worked successfully and this was the other output:
Defragment: 100% done. Defragmentation completed successfully.
The second command then shrinks the disk:
/Applications/VMware Fusion.app/Contents/Library/vmware-vdiskmanager -k "Virtual Disk.vmdk"
Once again, I got the message “VixDiskLib: Invalid configuration file parameter. Failed to read configuration file.” but again it all worked fine, and this was the output
Shrink: 100% done. Shrink completed successfully.
I managed to reclaim 9GB of wasted space from doing this.
What is the virtual disk filename?
When you do a directory listing, you’ll see a bunch of files and directories like this:
564d0102-8ca8-949e-169e-7f39e33745b7.vmem 564d13fe-4467-4a08-e14b-10c083090f70.vmem 564d13fe-4467-4a08-e14b-10c083090f70.vmem.lck/ Debian 7 64-bit-03868d7d.vmem Debian 7 64-bit.nvram Debian 7 64-bit.plist Debian 7 64-bit.vmsd Debian 7 64-bit.vmx* Debian 7 64-bit.vmx.lck/ Debian 7 64-bit.vmxf Virtual Disk-s001.vmdk Virtual Disk-s002.vmdk Virtual Disk-s003.vmdk Virtual Disk-s004.vmdk Virtual Disk-s005.vmdk Virtual Disk-s006.vmdk Virtual Disk-s007.vmdk Virtual Disk-s008.vmdk Virtual Disk-s009.vmdk Virtual Disk-s010.vmdk Virtual Disk-s011.vmdk Virtual Disk.vmdk Virtual Disk.vmdk.lck/ caches/ startMenu.plist vmmcores-1.gz vmmcores-2.gz vmware-0.log vmware-1.log vmware-2.log vmware.log
The disk images have the .vmdk file extension and in the above example have been split into 11 different files with numbered filenames. You don’t need to defrag and shrink the numbered filenames, just the one without the numbers.
So in the above example, ignore Virtual Disk-s001.vmdk to Virtual Disk-s011.vmdk and run the command on Virtual Disk.vmdk.
If you do attempt to run the command on one of the numbered files, you’ll get an error message like this one:
The file '/.../Virtual Disk-s001.vmdk' appears to be a sub-component of a virtual disk. Did you mean '/.../Virtual Disk.vmdk'? Failed to defragment: The specified file is not a virtual disk (0x3ebf).
I ran the above commands on VMWare Fusion 7.1.1 on OSX Yosemite 10.10.3.