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)
 
(11 intermediate revisions by the same user not shown)
Line 91: 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].
eg:
# fstrim -a -v
/var: 274.3 MiB (287592448 bytes) trimmed on /dev/sda3
/tmp: 63 MiB (66060288 bytes) trimmed on /dev/sda5
/home: 125 MiB (131072000 bytes) trimmed on /dev/sda6
/boot/efi: 505.2 MiB (529735680 bytes) trimmed on /dev/sda1
/: 5 MiB (5242880 bytes) trimmed on /dev/sda2
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)]
On most modern systems fstrim is being run automatically using systemctl. You can check if it is enabled:
# systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
      Loaded: loaded (/lib/systemd/system/fstrim.timer; disabled; vendor preset: enabled)
      Active: inactive (dead)
    Trigger: n/a
    Triggers: ● fstrim.service
        Docs: man:fstrim
Oh-huh, our system does not have it enabled.. might there be a reason, such as that the UNMAP command is not supported?
If so, the following command would return 0.
# cat /sys/block/sda/queue/discard_max_bytes
1073741824
So UNMAP is supported, but fstrim still is not enabled, lets fix that.
# systemctl enable fstrim.timer
Created symlink /etc/systemd/system/timers.target.wants/fstrim.timer → /lib/systemd/system/fstrim.timer.
and reload systemctl plus check that it now works...
# systemctl start fstrim.timer
# systemctl daemon-reload
# systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
      Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; vendor preset: enabled)
      Active: active (waiting) since Sat 2024-01-06 13:00:46 CET; 6s ago
    Trigger: Mon 2024-01-08 01:17:59 CET; 1 day 12h left
    Triggers: ● fstrim.service
        Docs: man:fstrim
see also:
* https://www.suse.com/support/kb/doc/?id=000019447


== Zero out unused space on a Windows VM ==
== Zero out unused space on a Windows VM ==
Line 106: Line 158:


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 122: Line 182:


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.
On macOS you run it as:
/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k Ubuntu\ Desktop.vmdk
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. So certainly try it first without running as 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 ==
Line 134: Line 204:
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:
Line 147: Line 227:


[https://communities.vmware.com/thread/581576 Solved: Shrink an APFS virtual disk]
[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