安全地压实不断增长的WSL VHDX图像的大小。
(您喜欢这个公用事业吗?给它A并分享)
Linux(WSL)的Windows子系统使用VHDX图像文件来存储Ext4文件系统,但是当删除文件时,它缺乏缩小图像的有效方法。该实用程序不需要提高凭证,而是压实WSL2发行版的VHDX虚拟驱动器,并实现最小尺寸。默认情况下,它将在信息模式下执行,无需对图像进行操作,为已安装的所有发行版提供以下信息:
如果未指定发行版,它将依次针对所有已安装的图像。它在紧凑的过程中以安全模式运行,以防止在失败的情况下进行任何不需要的副作用。这是一个典型的用例:紧凑的Ubuntu图像并确认:
PS> wslcompact -c Ubuntu
WSL compact, v5.0 2023.02.02 (Groundhog edition)
(C) 2023 Oscar Lopez
wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact
Distro's name: Ubuntu
Image file: C:UsersOkiWSLUbuntuext4.vhdx
Current size: 12864 MB
Estimated size: 7700 ± 188 MB
The estimated process time using an SSD is about 2 minutes.
NOTE: You can safely cancel at any time by pressing Ctrl-C
Import in progress, this may take a few minutes.
The operation completed successfully.
New Image compacted from 12864 MB to 7728 MB
Do you want to apply changes and use the new image (y/N): y
Image replaced for distro: Ubuntu
在安装WSLCompact之前,请确保您的WSL安装是最新的。您可以通过在PowerShell中键入wsl --update来做到这一点。 WSLCompact至少需要WSL版本1.0.0。
安装WSLCompact的方法有两种,请选择您的喜欢:
它需要一个特殊的设置来运行远程脚本。如果您过去设置了它,那么您不再需要它了。如果您不确定,PowerShell只需类型:
Set-ExecutionPolicy RemoteSigned - Scope CurrentUser要安装或更新实用程序,请关闭所有PowerShell实例,打开一个新鲜的实例并类型:
iwr - useb https: // raw.githubusercontent.com / okibcn / wslcompact / main / setup | iex要删除实用程序,请关闭所有PowerShell实例,打开一个新鲜的实例并类型:
Remove-Item " $ ( $ env: PSModulePath .split ( ' ; ' )[ 0 ] ) /WslCompact " - Recurse - Force如果使用SCOP软件包管理器,则可以直接从其存储桶中添加WSLCompact实用程序。输入PowerShell这两行:
scoop bucket add .oki https: // github.com / okibcn / Bucket
scoop install wslcompact要更新应用程序,只需键入:
scoop update wslcompact要删除该应用程序,只需键入:
scoop uninstall wslcompact安装后,用法很简单:
wslcompact列出了所有WSL图像和信息。不会对图像进行动作。wslcompact Ubuntu选择特定的发行版。-c紧凑型选项时,WSLCompact将在确认后修改图像。-d允许数据分区的紧凑。该实用程序可确保最小的尺寸,您最终获得了连续的文件,以便在基于HD的旧系统中更快地访问。如果您需要发行版的名称列表,则可以通过键入wsl -l访问。
Usage: wslcompact [OPTIONS] [DISTROS]
wslcompact compacts the images of WSL distros by removing unsused space.
If no option is provided, it will default to info mode, without modifying any image.
If no distro is provided it will process all the installed images.
NOTE: WSL will be shutdown for compacting the images.
Options:
no opt. Provides name, image file path, current size, and estimated new size information.
-c Compacting mode: process the selected distros compacting the images.
-y replaces selected images without asking for confirmation.
-d Enable the processing of data images. Default is disabled.
-h Prints this help
Examples:
wslcompact
wslcompact -c -d
wslcompact -c -y Ubuntu Kali
如果您的C:驱动器没有足够的时间自由空间,则该程序将不会紧凑该发行版。只需在调用函数之前更改临时文件夹。因此,而不是简单的wslcompact ,而只是做:
$ env: TEMP = " Z:your tempfolder "
wslcompact新的Temp文件夹只能在该PowerShell终端会话中处于活动状态,因此系统的其余部分根本没有问题,并且不会留下垃圾。
一个典型的操作是:
PS> wslcompact
WSL compact, v5.0 2023.02.02 (Groundhog edition)
(C) 2023 Oscar Lopez
wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact
Distro's name: Ubuntu
Image file: C:UsersOkiWSLUbuntuext4.vhdx
Current size: 12864 MB
Estimated size: 7700 ± 188 MB
The estimated process time using an SSD is about 2 minutes.
Distro's name: Kali
Image file: C:UsersOkiWSLKaliext4.vhdx
Current size: 1579 MB
Estimated size: 723 ± 18 MB
The estimated process time using an SSD is about 1 minutes.
Distro's name: Arch
Image file: C:UsersOkiWSLArchext4.vhdx
Current size: 1075 MB
Estimated size: 860 ± 21 MB
The estimated process time using an SSD is about 1 minutes.
用确认的压实Ubuntu图像:
PS> wslcompact -c Ubuntu
WSL compact, v5.0 2023.02.02 (Groundhog edition)
(C) 2023 Oscar Lopez
wslcompact -h for help. For more information visit: https://github.com/okibcn/wslcompact
Distro's name: Ubuntu
Image file: C:UsersOkiWSLUbuntuext4.vhdx
Current size: 12864 MB
Estimated size: 7700 ± 188 MB
The estimated process time using an SSD is about 2 minutes.
NOTE: You can safely cancel at any time by pressing Ctrl-C
Import in progress, this may take a few minutes.
The operation completed successfully.
New Image compacted from 12864 MB to 7728 MB
Do you want to apply changes and use the new image (y/N): y
Image replaced for distro: Ubuntu