Compress virtual machine disks of Hyper-V

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/en/post/2025/03/02/compress-disks-of-hyper-v/

Recently, my virtual machine disks of Hyper-V became very large, then I decided to compress them. Here is how I did it.

  1. Enter the virtual machine, and execute the following commands for each disk’s root path:

    1dd if=/dev/zero of=/path/to/disk/zero-file # Write a zero-bytes file to the root path of the a disk
    2sync
    3rm /zero-file
    
  2. Shut down the virtual machine.

  3. Open a PowerShell with administrator privileges, and execute the following command for each vhdx file:

    1Optimize-VHD -Path "C:\path\to\disk.vhdx" -Mode Full
    

All done.

Actually, this method can only do a little compression. If the disk is still too large, you may consider creating a new vhdx file and migrating the data to it.

comments powered by Disqus

Translations: