Hyper-V virtual machine/WSL/WSA image compression

Compress VHDX image

.vhdxThe images of various subsystems/virtual machine systems of Windows are stored in our hard disk in the form of virtual hard disk (VHDX, suffix ).

In the process of daily use, as the number of files increases, the size of the corresponding virtual disk will also continue to increase. However, due to the characteristics of the virtual machine, even if we manually delete some files, the virtual hard disk cannot release space by itself to reduce the hard disk occupation.

In order to avoid the waste of computer hard disk space, when the capacity of our virtual hard disk is too high, we can run the following code to compress the virtual hard disk to free up space.

diskpart                                # 打开 diskpart 工具
select vdisk file="\path\to\your\vhdx"  # 选择vhdk镜像
attach vdisk readonly                   # 以只读附加镜像
compact vdisk                           # 压缩镜像
detach vdisk                            # 分离镜像
exit                                    # 退出 diskpart

Error resolution

If you encounter Virtual hard disk files must be uncompressed and unencrypted and must not be sparsesuch a prompt, please run the following code on the command line:

fsutil sparse setflag path/to/file.vhdx 0

Then compress the image again to complete the operation.

Guess you like

Origin blog.csdn.net/qq_19577209/article/details/126217277