Shrink guest on hosted platform: Difference between revisions

 
(5 intermediate revisions by the same user not shown)
Line 94: Line 94:
=== Zero out using fstrim ===
=== 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 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.
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 137: Line 183:
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.
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:
You will have to change the ownership back to your own user, eg:
1,274

edits