Shrink guest on hosted platform

From VI-Toolkit
Jump to: navigation, search

Zero out unused space in a Linux VM

Before we try to shrink the virtual disk files, we should try to remove any unneeded files from the virtual machine to free space.

Clean up your repositories:

For example, on Debian-based VMs, you can run

apt-get clean all

to clear out the local repository of retrieved package files.

On red hat based VMs that would be:

yum clean

Then shut down any services that depend on having enough available disk space to run.

The reason for this is that we are going to write out zero's to the file system until the file system is full and we do not want any application - for example a database system like mySQL or postgreSQL - to run out of disk space while we are zeroing out the file system. If mySQL would try to write when your disk is full you risk a corrupted database. So this step is important!

Next run:

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

to fill the unused space with zeros and then remove the "zero.fill" that has all the zero's.

As this command writes to the disk until it runs out of disk space, you will get an error "No space left on the device". This is expected.

Note that you have multiple partitions that you have to repeat the above command to zero out for each partition. Adjust "zero.fill" to the relevant mountpoint, for example if you have a separate partition for home it would become:

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

Zero out unused space on a Windows VM

To do the same with a windows VM, you can use Microsoft's tool sdelete.

Run it as

sdelete -z c:

To clean out the free space on disk c:


As of sdelete v1.6 -c and -z have changed meanings, many instructions say -c zeros free space, this is no longer the case, it zeros the space then fills with random data in accordance with DOD spec: DOD 5220.22-M, random data will prevent the reclaim logic from working.

The trigger to zero space with 0x00 has changed to -z!

Shrink the disk

Then power down the VM and open a terminal on the Linux host.

Navigate to the directory where the .vmdk files are located, e.g.:

cd /var/lib/vmware/Virtual\ Machines/Ubuntu\ Desktop/

You can shrink the .vmdk file as follows:

vmware-vdiskmanager -k Ubuntu\ Desktop.vmdk

On Windows the command also uses the -k option, so I leave that for you as an exercise.

Note that vmware-vdiskmanager is probably not in your search path, so you might have to prepend the vmware-vdiskmanager command with the actual path with the command is located.