How to compact a Linux .VHDX

Andy

Well-known member
Creative Team
User ID
1
Joined
7 Jan 2019
Messages
1,315
Reaction score
87
Points
48
Do you have a Linux VM on a Hyper-V with a bloaty .VHDX?

Then you are on the right page, follow these instructions down below:

The idea is you need to TRIM your thin-provisioned drive first before you can compact your .VHDX
  1. Run fstrim on the Linux system.
    fstrim has become my personal favourite method when it comes to trimming SSD on Linux systems, here are some of the reasons:
    • It works very quickly
    • It doesn't cause unnecessary wear on SSDs
    • It ships in the default tool set of most distributions
    • It's ridiculously simple to use
    Usage:
    Bash:
    sudo fstrim /
  2. Now we need to shut down our Linux VM (I couldn't find any better way than shutting down as Optimize-VHD cmdlet requires full access over the .VHDX while doing its job) -- if you find a better way, please share with us here by commenting down below.
  3. Run PowerShell as administrator and then run Optimize-VHD on the target .VHDX.
    Example Usage:
    Optimize-VHD 'D:\CANA\Virtual Hard Disks\CANA.vhdx' -Mode Full
My D drive which was 6 GB free of 465 GB total disk space, by performing the maintenance above, I managed to get 164 GB free of 465 GB total disk space. That's heaps of disk space reclaim 🌈🥳
 
 Short URL:
Back
Top