Shrinking VMware virtual disk from command line

While shrinking virtual disks is very functional using VMware tools from the guest machine while it is running, from time to time it may be convenient to do it offline, from command line. Here's the procedure to do that (linux host, windows guest):

First we have to mount the virtual disk on the host machine, to prepare it for shrinking:

vmware-mount vmachine.vmdk /mnt

If your guest is also windows OS, you need to make sure that you have ntfs-3g installed on your system. Most linux distributions nowadays have it enabled by default.

Next, we run the prepare step which wipes the unused sectors of your disk, or something like that. In any case, it helps the shrinking process a lot so make sure you don't skip this step:

vmware-vdiskmanager -p /mnt

When preparing is over, we can unmount the disk with:

vmware-mount -x

For some odd reason, unmounting always fails for me, returning error similar to this:

Failed to unmount all partitions on disk '/path/to/vmachine.vmdk': A loop device operation failed
umount: /var/run/vmware/fuse/13424109670462286091: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))

Fortunately that small problem is easily solved by typing:

losetup -d /dev/loop0

And then again:

vmware-mount -x

Our virtual disk is now ready for shrinking, which will compact the virtual disk by 10-50%, depending on the fragmentation:

vmware-vdiskmanager -k vmachine.vmdk

Hope you find this small tutorial useful.