I am using WSL2 on my Windows 10 Home. I noticed Windows is reporting that vhdx ext4 partition is 105GB on my disk. But I also have wsl2 added as network drive - and there it is reported just as 89 GB. I already tried running diskpart and compact vhdx space there, it freed a bunch of GBs but I am wondering if I can do something more? Where this size difference is coming from?
1 Answer
The "Compact" operation can only remove unused areas at disk level (i.e. where the VHDX knows that the specific sectors of the virtual disk are unused). For this to work, the guest OS must inform the (virtual) disk about which sectors are in fact unused by the filesystem, as the disk itself doesn't understand the filesystem that it holds – otherwise if you delete several GB worth of files inside the VM, that space will still be seen as "occupied" from the outside.
The "discard" mechanism used by Hyper-V VHDs is exactly the same TRIM/UNMAP command as with SSDs – for Linux, the guest needs to run fstrim
periodically (e.g. using fstrim.timer) or the filesystem needs to have the discard
mount option specified.
# sudo fstrim -av
/: 19.9 GiB (21382381568 bytes) trimmed on /dev/sda1
fstrim -av
within the VM?