Shrink guest on hosted platform: Difference between revisions

Jump to navigation Jump to search
(11 intermediate revisions by the same user not shown)
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
Line 86: Line 91:


If this is not for you, then do read on.
If this is not for you, then do read on.
=== Zero out using fstrim ===
These days there's an alternative way for zero-ing out the parts of the disk that are not used using [https://www.man7.org/linux/man-pages/man8/fstrim.8.html fstrim].
The great thing about this is that it only zero's out the parts of the disk that are not zero and as such the disk does not inflate to its full size.
* [https://forums.freebsd.org/threads/ssd-trim-maintenance.56951/post-328912 Detail comment from Theodore Ts'o on using fstrim]
* [https://github.com/vmware/open-vm-tools/issues/457 Avoid disk expansion while shrinking (github issue tracker)]


== Zero out unused space on a Windows VM ==
== Zero out unused space on a Windows VM ==
Line 101: Line 116:


The trigger to zero space with 0x00 has changed to -z!
The trigger to zero space with 0x00 has changed to -z!
== Zeroing an encrypted disk ==
If you use disk encryption in the guest OS then zero'ing out the disk won't help.
The reason is that a good disk encryption scheme will write out random data to the disk when you write out zero's.
In other words, if you have enabled full disk encryption in the guest then you can not shrink the disk anymore as there is no space to reclaim.


== Shrink the disk ==
== Shrink the disk ==
Line 117: Line 140:


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.
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.
If you need to use "sudo" in order to be able to run the above then beware that the ownership of the .vmdk files might get changed to root.
You will have to change the ownership back to your own user, eg:
sudo chown username: *.vmdk
If you don't change the ownership back you might see errors like:
"Unable to open file .../Virtual Disk.vmdk: Insufficient permission to access file".


== Shrink using vmware tools ==
== Shrink using vmware tools ==


Nowadays you can also shrink the guest by using the shrink feature as offered via vmware tools.
Nowadays you can also shrink the guest by using the shrink feature as offered via vmware tools.
So for clarity all of the following commands are run from within the guest to shrink the disk while the guest is running.


On linux in order to use this run:
On linux in order to use this run:
Line 127: Line 159:
Where "/" is the mount point of the partition that you want to shrink.
Where "/" is the mount point of the partition that you want to shrink.


On Windows in order to use this run:
On Windows in order to use this run (as administrator):


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


where <location> is the drive you want to shrink
where <location> is the drive you want to shrink
For example:
C:\Program Files\VMware\VMware Tools>VMwareToolboxCmd.exe disk shrink c:\
Please disregard any warnings about disk space for the duration of shrink process.
Progress: 62 [=======>  ]
The progress you see here is VMware Tools blanking out the unused space.
At 99% it will popup the "Shrinking Disk" progress dialog at the host that takes care of the actual shrinking process.
If you are logged in via remote desktop then do expect to loose the connection for a bit while the actual shrinking is taking place.


On OS X / macOS the command to use is:
On OS X / macOS the command to use is:


  sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink /
  sudo /Library/Application\ Support/VMware\ Tools/vmware-tools-cli disk shrink /
== Shrink macOS VM with APFS ==
[https://communities.vmware.com/thread/581576 Solved: Shrink an APFS virtual disk]
== External links ==
* [https://docs.vmware.com/en/VMware-Workstation-Pro/16.0/com.vmware.ws.using.doc/GUID-421A1073-BF16-4BC7-AA76-46B954CA438D.html Clean Up a Virtual Hard Disk on Windows Hosts - Workstation 16 Pro documentation]
* [https://docs.vmware.com/en/VMware-Workstation-Pro/16.0/com.vmware.ws.using.doc/GUID-3F94C7B5-19A5-4E91-9709-B17FAA93FF75.html Configuring and Maintaining Virtual Hard Disks - Workstation 16 Pro documentation]
* [https://docs.vmware.com/en/VMware-Fusion/12/com.vmware.fusion.using.doc/GUID-6BB29187-F47F-41D1-AD92-1754036DACD9.html#GUID-6BB29187-F47F-41D1-AD92-1754036DACD9 Clean up a Virtual Machine - VMware Fusion 12 documentation]
1,274

edits

Navigation menu

Guest

Support my products