0

I found a few commands like configuring networking or optimizing the virtual disk that require shutting down WSL, but every time I do that via "wsl --shutdown", it causes Windows 10 to lock up. There isn't a blue screen, it just freezes until removing power.

How can I shut down WSL2 to allow compacting the virtual hard drives without restarting Windows?

theSparky
  • 111
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 05 '23 at 16:08
  • 1
    (1) Do you do exit before the shutdown? (2) This problem seems more frequent with NVIDIA graphic cards, is that your case and are you running graphical programs in WSL? (3) Is your WSL fully updated? – harrymc Sep 05 '23 at 16:52
  • Thanks for the hints! Windows is at version 10.0.19045.3324 and The most recent version of Windows Subsystem for Linux is already installed. I had the same issue right at boot with no terminals open, and only one distro having been started. I closed the terminal instead of issuing exit. – theSparky Sep 05 '23 at 17:25
  • @theSparky - Do you have the legacy WSL installed or the one installed through the Windows Store? – Ramhound Sep 05 '23 at 17:31
  • I don't remember if I installed through the MS Store or not. The wsl --version command shows WSL 1.2.5.0 with Kernel 5.15.90.1, but it seems there are quite a few newer pre-release versions than that. The distros I have installed are all configured as WSL2 – theSparky Sep 05 '23 at 18:15

1 Answers1

1

Other have seem to have similar issues: https://www.reddit.com/r/bashonubuntuonwindows/comments/10zotx1/wsl_not_starting_wsl_shutdown_hangs_vmmem_process/

And for me, this batch was the only way I could fully shut down WSL2 so I could optimize all my distro's virtual drives without Windows locking up:

@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

theSparky
  • 111