fix: 彻底绕过 Ubuntu WSL OOBE 交互提示

Ubuntu OOBE 检查 UID>=1000 的普通用户是否存在
- 创建占位用户 wsluser (uid=1000) 满足检查
- 删除 OOBE profile.d 脚本
- /etc/wsl.conf 单一 [user] 块覆盖写入

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-05-28 15:46:08 +08:00
parent b892ee6e65
commit afbbb031d3

View File

@@ -136,24 +136,28 @@ if ($distroInstalled) {
}
# ── 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
}
# Ubuntu OOBE 检测:若无 UID≥1000 的普通用户则弹交互提示
# 解决方案:创建占位用户满足检查 + wsl.conf 设 default=root
Write-Info "初始化 WSL2跳过 OOBE设置 root 为默认用户)..."
$initCmd = @'
# 创建占位用户,满足 Ubuntu OOBE "至少有一个真实用户" 的检查
id wsluser &>/dev/null || useradd -m -s /bin/bash -u 1000 wsluser
# 写入 /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)"
# 覆盖写入 /etc/wsl.conf单一 [user] 块,避免重复冲突)
printf '[boot]\nsystemd=false\n\n[user]\ndefault=root\n' > /etc/wsl.conf
# 禁用 Ubuntu OOBE 自动运行脚本(如存在)
rm -f /etc/profile.d/01-wsl-oobe.sh 2>/dev/null || true
rm -f /usr/lib/ubuntu-advantage/ua-messaging.service 2>/dev/null || true
true
'@
# 用 --user root 初始化 rootfs本身也会跳过 OOBE 交互)
wsl -d $WSL_DISTRO --user root -- bash -c "exit 0" 2>$null
Start-Sleep 2
# 写入配置
$b64Init = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($initCmd))
wsl -d $WSL_DISTRO --user root -- bash -c "echo '$b64Init' | base64 -d | bash" 2>$null
Write-OK "/etc/wsl.conf 已写入 (default=root),占位用户已创建"
# 重启使 wsl.conf 生效
wsl --terminate $WSL_DISTRO 2>$null