使用“wsl --shutdown”命令会导致 Windows 崩溃

使用“wsl --shutdown”命令会导致 Windows 崩溃

我发现一些命令(例如配置网络或优化虚拟磁盘)需要关闭 WSL,但每次我通过“wsl --shutdown”执行这些操作时,都会导致 Windows 10 锁定。没有蓝屏,只是冻结,直到断电。

如何关闭 WSL2 以允许压缩虚拟硬盘而不重新启动 Windows?

答案1

其他人似乎也有类似的问题: https://www.reddit.com/r/bashonubuntuonwindows/comments/10zotx1/wsl_not_starting_wsl_shutdown_hangs_vmmem_process/

对我来说,这批操作是我可以完全关闭 WSL2 的唯一方法,这样我就可以优化我发行版的所有虚拟驱动器,而不会让 Windows 锁定:

@echo off
@setlocal
set  WSL_UTF8=1
setlocal enableextensions
setlocal enabledelayedexpansion

:: Terminate only the running distros
wsl --list --verbose
echo.
set "command=wsl --list --verbose"
for /F "USEBACKQ skip=1 delims=(" %%i in (`%command%`) do (
    set "wslDistro=%%i"
    set "wslDistroRunning=!wslDistro:Stopped=!"
    if "!wslDistro!"=="!wslDistroRunning!" (
        set "wslDistro=!wslDistro:~2!"
        set "wslDistroVersion=!wslDistro:*Running=!"
        call set "wslDistro=%%wslDistro:!wslDistroVersion!=%%"
        set "wslDistro=!wslDistro:Running=!"
        echo Terminating !wslDistro!
        wsl --terminate !wslDistro!
    )
)
timeout 1
echo.
wsl --list --verbose
echo.

:: Kill the subsystem
taskkill /IM wslhost.exe /F
taskkill /IM wslservice.exe /F
taskkill /IM wslrelay.exe /F
timeout 1
taskkill /IM wslhost.exe /F
wsl --shutdown
echo.

:: Compact virtual disks
pushd %USERPROFILE%\AppData\Local\Packages\
for /D %%i in ("*") do (
    set "wslDisk=%CD%\%%i\LocalState\ext4.vhdx"
    if exist !wslDisk! (
        (   echo select vdisk file=%CD%\%%i\LocalState\ext4.vhdx
            echo attach vdisk readonly
            echo compact vdisk
            echo detach vdisk
            echo exit
        ) | diskpart
    ) else (
        rem file doesn't exist
    )
)


wsl --list --verbose
echo.
pause

相关内容