Shrink guest on hosted platform

From VI-Toolkit
Revision as of 14:20, 15 November 2016 by Wila (talk | contribs) (Created page with "==== 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 spac...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to 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

Next, reboot your VM from a live CD and mount the file systems.

The reason we are doing this from a live CD is that we are going to write out zero's to the file system 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 "reboot from a live CD" step is important!

from your live CD, check out the filesystem of your VM, for example:
fdisk -l

will show you:


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

to fill the unused space with zeros.

If you do not have a Live CD, then you can run the command to zero out the unused space as root, but remember to shut down services like databases first.


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, 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 -r Ubuntu\ Desktop.vmdk

On Windows the command also uses the -r 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.