Shrink guest on hosted platform

From VI-Toolkit
Jump to navigation Jump to search

Note that the technique described below to zero out the unused space on the guest OS will in fact make your guest virtual disk grow to the maximum size first. For each byte that is changed to zero the virtual disk will need to claim a byte. This means that while you can use the technique to reclaim disk space after the unused space is zero'd out, it is important to have enough space before you start.

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:

Careful!

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.

Shrink using vmware tools

Nowadays you can also shrink the guest by using the shrink feature as offered via vmware tools.

On linux in order to use this run:

sudo vmware-toolbox-cmd disk shrink /

Where "/" is the mount point of the partition that you want to shrink.

On Windows in order to use this run:

C:\Program Files\VMware\VMware Tools>VMwareToolboxCmd.exe disk shrink <location>

where <location> is the drive you want to shrink

On OS X / macOS the command to use is:

sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink /