Shrinking a Linux VMDK disk under VMWare Player 7

I wrote about this problem already earlier in 2015, but after installing VMWare Player 7 and a CentOS based VM, the process did not work anymore for me. I did the dd part to fill the disk with 0, but the Player tool for shrinking did just execute without doing nothing. Running from within the VM the vmware-toolbox command for shrinking a disk returned an error message.

Command: vmware-toolbox-cmd disk list

What happened? I do not know for sure, but it looks like VMWare Player does not like a lot CentOS and XFS for shrinking. For my VM the actual space used inside was reported by df –h as not even 15 GB, while on the physical disk it occupied 25 GB. So, how to get back several GB of space?

There is a tool available from VMware that allows to shrink a vmdk offline. It`s part of VDDK and of their Workstation and more professional products, but not for Player. With the use of Google I found a KBA article where the tool is actually attached. Downloading the tool and put it into the right VMPlayer directory so it can find some DLLs needed for execution. You can also download the VDDK SDK where the tool is included: Download VDDK. After installing the tool, call it with the –k flag to compress a vmdk disk.

Command: vmware-vdiskmanager.exe –k <path to vm>\<Name of disk>.vmdk

The name of the disk can be seen in the directory, it’s the one ending on vmdk.

Result

Execution

After the program finishes

Reduction of disk size

Size before:

Size after:

In total, this saved me 10 GB.

Freeing up space

I like VMs. Sure, everything should be cloud, and AWS, Azure, etc are great (if you can afford them). But nothing beats having a local VM running with 6GB RAM even when you are not online. Cost? 0 $. I am just electively using the laptop I already use. Put a VM on an external USB drive is not a problem. With USB 3.0 the performance is quite good, even with USB 2.0 you can run a VM; starting and stopping takes a while, but once the services are started, they work quite nicely.

To not occupy all the disk space at once, my VMs are configured to dynamically allocate space. This allows me to run VMs from the SSD of the laptop, speeding things up even more. A normal VM takes only a few GB: the OS + software.

A problem is the ext4 file system used by my Linux VMs. Once a space was allocated, it stays allocated. The effect is that a VM is occupying internally 30 GB consumes 100 GB on my hard drive. Why? Adding and deleting files, caches, etc. The DB may take only 20 GB, but adding and deleting 10GB results that the VM occupies 30 GB. Do that for some time and suddenly the VM eats up 100 GB of your hard drive instead of 30GB.

How to regain hard drive space?

VMWare comes with a compact tool to recover space, but that only works ootb with Windows VMs. For Linux VMs with a journaling file system like ext4 some preparations are needed. In short, the preparation is to create huge empty file that only contains 0.

First, stop the running services. The file going to be created will consume the free space of the virtual disk. Running services will get into trouble when they cannot write anymore to the disk. Like log files or real data. To not let your database or Tomcat server crash uncontrolled, stop them before creating the file.

Command to create the file:

dd if=/dev/zero of=/dummy bs=4096

This command lets dd create a file named dummy in /. The file size will be the entire free space of the virtual disk. If you configured your virtual disk to be 500GB and you have 400GB free, the resulting file size will be 400GB. You do not need to have 400GB free on your physical disk where the VM is stored on. As you write zeros, the real space needed will be 0 MB.

After dd filled you file system, it will exit. Delete the dummy file

rm /dummy

Shut down your VM. Open VM Player, open the properties of the VM, select the hard disk you want to compact and start the Compact tool from the Utilities drop down.

This will start the compact tool. After a while you’ll see a success message, informing you that the disk was compacted.