Cover image for WSL空间压缩

WSL空间压缩

字数 325
阅读
访客

时间轴

时间轴

2025-12-29

init

本文介绍了WSL(Windows Subsystem for Linux)空间压缩的方法。文章首先说明了如何定位WSL发行版虚拟磁盘文件的路径,然后强调在压缩前需先关闭WSL,接着通过diskpart工具执行压缩操作,并给出了具体的命令步骤。

.vhdx文件的磁盘路径

把 distribution-name 替换成通过wsl -l -v看到的发行版的名称

123456
wsl -l -v(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath") + "\ext4.vhdx"# 以archlinux为例(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'archlinux' }).GetValue("BasePath") + "\ext4.vhdx"

压缩磁盘

压缩前先关闭wsl

1
wsl --shutdown

然后输入diskpart

1
diskpart

此时弹出一个新的命令行窗口,依次输入下面内容,其中 file 的值替换成上面一步得到的路径

12345678910
# 选择目标磁盘文件select vdisk file="/path/to/your/.vhdx"# 以只读模式连接虚拟磁盘文件attach vdisk readonly# 压缩虚拟磁盘文件compact vdisk# 分离虚拟磁盘文件detach vdisk# 退出exit

参考

评论加载中…