Shrink guest on hosted platform: Difference between revisions

From VI-Toolkit
Jump to navigation Jump to search
(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...")
 
Line 28: Line 28:


to fill the unused space with zeros.
to fill the unused space with zeros.
As this command writes out zero's to the disk until it runs out of disk space, you will get an error "No space left on the device". This is expected and is also why we suggest to do this from a Live CD.


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.
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 ====
==== Zero out unused space on a Windows VM ====

Revision as of 15:31, 15 November 2016

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.

As this command writes out zero's to the disk until it runs out of disk space, you will get an error "No space left on the device". This is expected and is also why we suggest to do this from a Live CD.

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.