There are some claims around that quick wins may be made on ext4 disks by using fstrim to release unused blocks, by running
, however, in practice this will give you fairly limited gains, or none at all. I believe the actual effect of trim is largely misunderstood, as it’s function is _not_ to zero out blocks (which is what you need for compacting) but just communicating which blocks the filesystem no longer cares about to the underlying storage. So while fstrim will tell your disk: i dont care anymore about blocks 1,4,5 and 18 – this does not actualy clear the content of those blocks.
So, with the fstrim legend out of the way, how will we go about actualy zeroing out the blocks so we can use full compacting of HYPER-V vhdx files ? It is actualy quite easy – tho time consuming (you knew that was coming didnt you)
- Download systemresquecd from the creators website here
- Shut down your virtual machine
- Mount the resqueue image as the cd image, and boot your virtual machine from it
- use #lsblk (list block devices) to locate the name of the disk you want to compact (eg. /dev/sdb). lsblk will display a table of all block devices found by the requeue cd, and from here you should be able to identify teh disk you are looking for by size. Most of the time the name will likely be the same as in the running system, but that is no given, so always verify which disk you want to target.
- run #zerofree /dev/sdb (replace with your disk) to zero out empty blocks (this can take a LONG time)
- Shut down the virtual machine and remove the boot cd image from it
- Compact your virtual machine disk from powershell: Optimize-VHD -Mode Full – Path ‘p:\yourdir\yourdisk.vhdx’
This information is compiled using Ubuntu Server LTS version 18.04.1 and systemresquecd-6.0.2, Hyper-V on server 2016.