问题描述
近期使用 WSL(从 VSCode)时,电脑运行卡顿。随后 Ubuntu 终端直接无法启动,显示黑屏。
问题排查
以管理员身份打开 PowerShell,运行:
wsl --list --verbose
输出 STATE 为'Stopped',说明 WSL 未启动。
再运行:
wsl -d Ubuntu
启动过程卡死。判定是整个 distro 无法运行,还是 shell 启动阶段出了问题。
运行:
wsl -d Ubuntu --exec echo hello
依旧卡住。连最简单的 --exec echo 都挂住,说明问题出在 WSL 服务本身或底层虚拟化。
检查 WSL 服务状态,运行:
Get-Service LxssManager
报错。参考 Microsoft Q&A 文档,再运行:
Get-Service WslService
显示 Status 为'Running',说明 WSL 服务本身没问题。
检查 WSL 配置和内核版本,运行:
wsl --status
仍然卡住。
接下来把 WSL 的 Windows 功能彻底重置一下:
- 以管理员身份打开 PowerShell,先关闭相关 Windows 可选功能:
dism.exe /online /disable-feature /featurename:Microsoft-Windows-Subsystem-Linux /norestart
dism.exe /online /disable-feature /featurename:VirtualMachinePlatform /norestart
dism.exe /online /disable-feature /featurename:HypervisorPlatform /norestart
- 重启 Windows。重启后再次以管理员打开 PowerShell,修复系统映像,运行:
DISM /Online /Cleanup-Image /RestoreHealth
sfc /scannow
时间稍久,完成后重启。
- 再次以管理员 PowerShell 重新启用 WSL 相关功能:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:HypervisorPlatform /all /norestart
- 再次重启。
再次尝试:
wsl --list --verbose
wsl --status
wsl -d Ubuntu --exec echo test
前两步有输出,最后一步仍卡住。
调整思路,测试另外一个发行版:
wsl --list --online
wsl --install -d Debian
wsl -d Debian --exec echo ok
输出正常!说明 WSL2 核心和虚拟化平台没问题,唯一出问题的是 Ubuntu 实例本身已损坏。接下来需要备份虚拟磁盘文件。
运行
wsl --export Ubuntu D:\WSL-backups\ubuntu-backup.tar

