Tuesday, September 15, 2015

Shink your Linux VM using UNIX and VMware commands

There are many reasons why you would want to shrink the overall size of your VM.  Here are a few: Conserve Disk Space (the final frontier); Fit the VM on a CD or a DVD; reduce time to transmit the VM over the network; reduce overall backup time.
This entry provides a summary of a basic technique that we use.  I'm sure this is documented elsewhere, however sometimes unique perspectives can be illuminating.


Open two terminal windows and use sudo or become root.  In one terminal, issue the dd command shown in the image below to create a huge file in /tmp to fill up the disk space with zeros. Zeros are highly compressible.  You can repeat the command or change the arguments, depending on the size of the volume.
  • dd if=/dev/zero of=/tmp/full bs=100M count=500
While that is running, check the progress using the df command in the other terminal window. When the disk is full, stop the dump command (ctrl-c) and then remove the huge temporary files and then shutdown your system. 

Next, run the vmware-vdiskmanager.exe utility to decompress and shrink your virtual disk volume.  So first use the -d option, passing in the name of the VMDK file, and when that is completed, run again with the -k option.  In our case, the resultant OVA file went down from 17GB to 1.3GB.  Cool Bytes.
 
(Windows Example is referenced below)

C:\Program Files (x86)\VMware>"C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe"
VMware Virtual Disk Manager - build 1895310.
Usage: vmware-vdiskmanager.exe OPTIONS |
Offline disk manipulation utility
  Operations, only one may be specified at a time:
     -d                   : defragment the specified virtual disk. Only
                            local virtual disks may be defragmented.
     -k                   : shrink the specified virtual disk. Only local
                            virtual disks may be shrunk.