From ea0552be7910c1b18907bd24128b8b3c1df56a38 Mon Sep 17 00:00:00 2001 From: Joywayer Date: Thu, 28 May 2026 15:30:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=83=A8=E7=BD=B2=E5=90=8E=20WSL2=20?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8=20root=20=E7=94=A8?= =?UTF-8?q?=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 安装后自动写入 /etc/wsl.conf (default=root, systemd=false) - Invoke-WSL 改为 --user root 执行所有命令 - 跳过 OOBE 用户创建流程(--user root 启动) - 重启 WSL2 使 wsl.conf 立即生效 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- claude-dev-stack/deploy.ps1 | 43 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/claude-dev-stack/deploy.ps1 b/claude-dev-stack/deploy.ps1 index 1f55c06..6ed94ac 100644 --- a/claude-dev-stack/deploy.ps1 +++ b/claude-dev-stack/deploy.ps1 @@ -135,6 +135,31 @@ if ($distroInstalled) { Write-Warn "首次安装可能需要重启,重启后重新运行脚本" } +# ── Step 1b: 首次 OOBE 处理 & 设置 root 为默认用户 ────────── +# Ubuntu 首次安装会停在 OOBE 要求创建用户;用 --root 启动可跳过 +Write-Info "初始化 WSL2 发行版(确保 root 可用)..." +$oobeDone = $false +for ($i = 0; $i -lt 3; $i++) { + wsl -d $WSL_DISTRO --user root -- bash -c "exit 0" 2>$null + if ($LASTEXITCODE -eq 0) { $oobeDone = $true; break } + Start-Sleep 3 +} +if (-not $oobeDone) { + # 触发 OOBE 完成(无交互,接受默认) + wsl -d $WSL_DISTRO -- bash -c "exit 0" 2>$null + Start-Sleep 5 +} + +# 写入 /etc/wsl.conf(完整覆盖,避免重复块) +$wslConfContent = "[boot]`nsystemd=false`n`n[user]`ndefault=root`n" +wsl -d $WSL_DISTRO --user root -- bash -c "printf '[boot]\nsystemd=false\n\n[user]\ndefault=root\n' > /etc/wsl.conf" 2>$null +Write-OK "/etc/wsl.conf 已写入 (default=root, systemd=false)" + +# 重启使 wsl.conf 生效 +wsl --terminate $WSL_DISTRO 2>$null +Start-Sleep 2 +Write-OK "WSL2 已重启,后续命令将以 root 运行" + # ── WSL2 执行辅助函数 ───────────────────────────────────────── function Invoke-WSL { param([string]$Command, [switch]$IgnoreError) @@ -143,10 +168,10 @@ function Invoke-WSL { # 多行命令:base64 编码后在 bash 内解码执行,完全规避 PowerShell 管道 CRLF 问题 $CleanCmd = $Command -replace "`r`n","`n" -replace "`r","`n" $b64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($CleanCmd)) - $result = wsl -d $WSL_DISTRO -- bash -c "echo '$b64' | base64 -d | bash" 2>&1 | + $result = wsl -d $WSL_DISTRO --user root -- bash -c "echo '$b64' | base64 -d | bash" 2>&1 | ForEach-Object { ($_ -replace "`0","").ToString() } } else { - $result = wsl -d $WSL_DISTRO -- bash -c $Command 2>&1 | + $result = wsl -d $WSL_DISTRO --user root -- bash -c $Command 2>&1 | ForEach-Object { ($_ -replace "`0","").ToString() } } @@ -159,22 +184,12 @@ function Invoke-WSL { } # 用 exit code 方式测试连通性,避免输出编码干扰 -wsl -d $WSL_DISTRO -- bash -c "exit 0" 2>$null +wsl -d $WSL_DISTRO --user root -- bash -c "exit 0" 2>$null if ($LASTEXITCODE -ne 0) { Write-Fail "无法访问 WSL2 发行版 '$WSL_DISTRO'" exit 1 } -Write-OK "WSL2 ($WSL_DISTRO) 连接正常" - -# 获取 WSL2 默认登录用户(OOBE 完成后可能是非 root) -$wslUser = (wsl -d $WSL_DISTRO -- bash -c "whoami" 2>&1) -replace "`0","" | - Where-Object { $_ -match "^\w+$" } | Select-Object -First 1 -if (-not $wslUser) { $wslUser = "root" } -$wslUser = $wslUser.Trim() -Write-OK "WSL2 默认用户: $wslUser" -if ($wslUser -ne "root") { - Write-Info "非 root 用户,系统级命令将通过 sudo 执行" -} +Write-OK "WSL2 ($WSL_DISTRO) 连接正常 (root)" # ── Step 1a: 配置 .wslconfig(mirrored 网络模式) ──────────── $wslCfgPath = "$env:USERPROFILE\.wslconfig"