Win11 解决 WSL2 无法启动报错
原始故障
- 迁移到 Win11 后发现无法使用复制到 RamDisk 内存盘的 ub20.vhdx 镜像启动 WSL2,报错为
E_ACCESSDENIED - 马上使用管理员权限启动 wsl ,失败
- 过一段时间使用管理员权限启动 wsl,成功
很奇葩的问题,发现不移动 vhdx 的话就能用,长期以为是 RamDisk 的问题。今天想了一下,去手动排查了权限,发现普通用户只有完全权限,手动给予就可以从 ramdisk/ub20.vhdx 启动 wsl 了,上网也搜到了类似的解决方案。
自动化解决方案
整理为了一个 powershell 脚本,修改为实际路径,右键 使用 Powershell 运行 即可,操作步骤:
- 复制 wsl 镜像到内存盘
- 修改权限
- 建立软链
wsl.ps1
# Run as Administrator required
# Self-elevate if not running as Administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process PowerShell -ArgumentList "-ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# ============================================
$wslDir = "R:\wsl"
$source = "D:\wsl\ubuntu20\20rust.vhdx"
$dest = "R:\wsl\20rust.vhdx"
$linkPath = "D:\wsl\ubuntu20\ext4.vhdx"
# ============================================
if (-not (Test-Path $wslDir)) {
New-Item -ItemType Directory -Path $wslDir -Force
Write-Host "Created: $wslDir"
}
Write-Host "Copying file, please wait..."
Copy-Item -Path $source -Destination $dest -Force
Write-Host "Done: $dest"
$acl = Get-Acl -Path $dest
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"BUILTIN\Users",
"FullControl",
"None",
"None",
"Allow"
)
$acl.AddAccessRule($rule)
Set-Acl -Path $dest -AclObject $acl
Write-Host "ACL set: FullControl granted to Users"
if (Test-Path $linkPath) {
Remove-Item -Path $linkPath -Force
Write-Host "Removed old link: $linkPath"
}
New-Item -ItemType SymbolicLink -Path $linkPath -Target $dest
Write-Host "Symlink created: $linkPath -> $dest"
Write-Host "All done."