Shrink guest on hosted platform: Difference between revisions

Added a few more words to indicate that these commands are to be run from within the VM
(Added a few more words to indicate that these commands are to be run from within the VM)
Line 35: Line 35:
== Zero out unused space in a Linux VM ==
== 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.  
Before we try to shrink the virtual disk files, we should try to remove any unneeded files from the virtual machine to free space.
More free space means more disk space that can get reclaimed.
 
One of the areas in a VM that can take up a lot of disk space is the repository cache. So I personally tend to clean the cache in the VM before reclaiming disk space at the host.


Clean up your repositories:
Clean up your repositories:


For example, on Debian-based VMs, you can run
For example, on Debian-based VMs, you can run the following command (in the VM)


  apt-get clean all
  apt-get clean all
Line 49: Line 52:
  yum clean
  yum clean


Then shut down any services that depend on having enough available disk space to run.
The next step is important:
 
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!
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:
Next run (also from within the VM):


  cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill
  cat /dev/zero > zero.fill;sync;sleep 1;sync;rm -f zero.fill
Line 66: Line 71:
  cat /dev/zero > /home/zero.fill;sync;sleep 1;sync;rm -f /home/zero.fill
  cat /dev/zero > /home/zero.fill;sync;sleep 1;sync;rm -f /home/zero.fill


=== Zero out a part of your disk ===
=== Zero out a part of your virtual disk ===


If your machine cannot be taken down and you do not want to shut down services that might end up corrupting files because of the "No space left on the device" problem then an idea that might work is to zero out a part of your partition.
If your virtual machine cannot be taken down and you do not want to shut down services that might end up corrupting files because of the "No space left on the device" problem then an idea that might work is to zero out a part of your partition.


This for example creates a 100MB size file filled with zero's
This for example creates a 100MB size file filled with zero's
1,274

edits