Shrink guest on hosted platform: Difference between revisions

Jump to navigation Jump to search
added a note to look at the vmdk file ownership after shrinking via the command line.
(added a note to look at the vmdk file ownership after shrinking via the command line.)
(9 intermediate revisions by the same user not shown)
Line 5: Line 5:
Alternatively you can use the "Partially zero out" trick described below.
Alternatively you can use the "Partially zero out" trick described below.


==== Zero out unused space in a Linux VM ====
=== How much free space do you need before shrinking? ===
Also please note that you need more free space in order to be able to run the shrinking process.
The shrinking process consists of making a copy of your virtual disk while omitting the zero'd out blocks.
So how much extra space you need depends on how you configured the virtual disk for your Virtual Machine.
If your Virtual Disk is a single file, then your free space requirement can grow up-to the full size of that virtual disk.


Before we try to shrink the virtual disk files, we should try to remove any unneeded files from the virtual machine to free space.  
If OTOH you are using the split disk scheme where a virtual disk is sliced into multiple files the maximum free space you need is the maximum size a slice can be. Nowadays there's not a fixed slice size, it depends on how big your virtual disk actually is.
 
The split disk scheme since VMware Workstation 11 (VMware Fusion 7), older version of Workstation/Fusion use a 2GB split disk scheme.
 
 
Capacity          Extent size
================================
<=128GB            4GB (increased from 2GB)
>128GB && <2TB    Capacity / 32 (so maximum of 32 extents)
>=2TB              2TB
 
For extra safety I recommend to make sure you have an additional 2GB of free space on top of those requirements as mentioned above.
 
As side note, this also explains why it is generally recommended at the forum by the regulars to use a split disk scheme over single disk files.
This same problem also exists when you want to commit snapshots.
 
=== Using the clean up option in the menu ===
 
When you use the "Clean Up Disks" option in the menu in VMware Workstation 12 or later, then you do not require extra disk space for reclaiming space (see also: [https://docs.vmware.com/en/VMware-Workstation-Pro/14.0/com.vmware.ws.using.doc/GUID-421A1073-BF16-4BC7-AA76-46B954CA438D.html Clean Up a Virtual Hard Disk on Windows Hosts] )
 
Currently this only works for Windows guests where the filesystem is NTFS.
 
== 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.
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 21: 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 38: 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 55: Line 88:
Then run the "sync; sleep 1; sync" from above and remove the zero.fill files.
Then run the "sync; sleep 1; sync" from above and remove the zero.fill files.


If your VM is running on vSphere and you have a NFS LUN then one way to reclaim your space is to storage VMOTION the VM to NFS storage and back and the zero'd out data has been reclaimed. No need to shut down the VM. If this is not for you, then read on.
If your VM is running on VMware vSphere and you have a NFS LUN with enough free space then one way to reclaim your space is to Storage vMotion the VM to NFS storage and back. Once done the zero'd out data has been reclaimed. No need to shut down the VM.  


==== Zero out unused space on a Windows VM ====
If this is not for you, then do read on.
 
== Zero out unused space on a Windows VM ==


To do the same with a windows VM, you can use Microsoft's tool [http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx sdelete].
To do the same with a windows VM, you can use Microsoft's tool [http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx sdelete].
Line 72: Line 107:
The trigger to zero space with 0x00 has changed to -z!
The trigger to zero space with 0x00 has changed to -z!


=== Shrink the disk ===
== Shrink the disk ==


Then power down the VM and open a terminal on the Linux host.
Then power down the VM and open a terminal on the Linux host.
Line 88: Line 123:
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.


=== Shrink using vmware tools ===
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 ==


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 106: Line 150:


  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]
1,274

edits

Navigation menu

Guest

Support my products