From 6641a75fc9bf1d9b2d3ea8dbab2cfba0ba38132c Mon Sep 17 00:00:00 2001 From: imbytecat Date: Wed, 25 Mar 2026 14:42:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=84=9A=E6=9C=AC=EF=BC=9A?= =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20root-setup=20=E4=B8=BA=20wsl-init?= =?UTF-8?q?=20=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=85=B3=E9=94=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重命名 root-setup.sh 为 wsl-init.sh,明确 WSL 专属 - 添加 WSL 环境检查,防止在非 WSL 环境误执行 - 添加 sudoers 配置,修复 install.sh 无法使用 sudo 的问题 - 去掉 wsl-init 中的 -Syu,避免重复升级 - 备份已有 /etc/wsl.conf 再覆盖 - install.sh 克隆逻辑改为安全检测,避免误删已有仓库 - README 区分 WSL 首次启动和普通 Arch 两条路径 --- README.md | 33 ++++++++++++++++---------- scripts/install.sh | 10 ++++++-- scripts/{root-setup.sh => wsl-init.sh} | 22 +++++++++++++---- 3 files changed, 47 insertions(+), 18 deletions(-) rename scripts/{root-setup.sh => wsl-init.sh} (61%) diff --git a/README.md b/README.md index a07ca3f..e0cb8a8 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,45 @@ # Arch Linux 配置仓库 -使用 dcli 声明式管理 Arch Linux 配置(当前默认主机为 wsl,可扩展到其他主机)。 +使用 dcli 声明式管理 Arch Linux 配置。 +当前默认主机为 `wsl`;非 WSL 环境请先新增/切换 host,再执行同步。 -## 快速开始 +## 使用方式 -### 1. 初始化用户(以 root 身份,仅 WSL 首次) +### A. Arch on WSL 首次启动(默认 root 登录) + +1. 初始化普通用户: ```bash -curl -fsSL https://git.furtherverse.com/imbytecat/archlinux-config/raw/branch/main/scripts/root-setup.sh | bash -s -- <用户名> +curl -fsSL https://git.furtherverse.com/imbytecat/archlinux-config/raw/branch/main/scripts/wsl-init.sh | bash -s -- <用户名> ``` -然后在 PowerShell 中重启 WSL: +2. 在 PowerShell 中重启 WSL: ```powershell wsl --terminate archlinux ``` -### 2. 安装配置(以普通用户身份) +3. 重新进入 Arch WSL 后,以普通用户执行: ```bash curl -fsSL https://git.furtherverse.com/imbytecat/archlinux-config/raw/branch/main/scripts/install.sh | bash -``` - -### 3. 应用配置 - -```bash dcli sync ``` +### B. 普通 Arch 安装(已存在普通用户) + +跳过 WSL 初始化,直接执行: + +```bash +curl -fsSL https://git.furtherverse.com/imbytecat/archlinux-config/raw/branch/main/scripts/install.sh | bash +dcli sync +``` + +> 注意:当前 `config.yaml` 的 `active_host` 是 `wsl`。非 WSL 环境请先新增对应 `hosts/*.yaml` 并切换 `active_host`。 + ## 配置说明 -- `hosts/wsl.yaml` - WSL 配置 +- `hosts/` - 主机配置 - `modules/` - 模块化包管理 - `files/` - 配置文件(自动同步) diff --git a/scripts/install.sh b/scripts/install.sh index ec571d1..0eb472d 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -22,8 +22,14 @@ echo "==> 安装 dcli..." yay -S --needed --noconfirm dcli-arch-git echo "==> 克隆配置仓库..." -rm -rf "$CONFIG_DIR" -git clone "$REPO_URL" "$CONFIG_DIR" +if [ -d "$CONFIG_DIR/.git" ]; then + echo "配置仓库已存在:$CONFIG_DIR,跳过克隆" +elif [ -e "$CONFIG_DIR" ]; then + echo "目标路径已存在且不是 git 仓库:$CONFIG_DIR" + exit 1 +else + git clone "$REPO_URL" "$CONFIG_DIR" +fi echo "" echo "✓ 安装完成!" diff --git a/scripts/root-setup.sh b/scripts/wsl-init.sh similarity index 61% rename from scripts/root-setup.sh rename to scripts/wsl-init.sh index 6652334..9082778 100755 --- a/scripts/root-setup.sh +++ b/scripts/wsl-init.sh @@ -6,15 +6,26 @@ if [ "$(id -u)" -ne 0 ]; then exit 1 fi +if ! grep -qiE '(microsoft|wsl)' /proc/sys/kernel/osrelease 2>/dev/null; then + echo "此脚本仅用于 Arch Linux on WSL 的首次初始化" + exit 1 +fi + USERNAME="${1:-}" if [ -z "$USERNAME" ]; then - echo "用法: ./root-setup.sh <用户名>" - echo "示例: ./root-setup.sh imbytecat" + echo "用法: wsl-init.sh <用户名>" + echo "示例: wsl-init.sh imbytecat" exit 1 fi echo "==> 安装 sudo..." -pacman -Syu --noconfirm sudo +pacman -S --needed --noconfirm sudo + +echo "==> 配置 sudo 权限..." +cat > /etc/sudoers.d/10-wheel << 'EOF' +%wheel ALL=(ALL:ALL) ALL +EOF +chmod 440 /etc/sudoers.d/10-wheel echo "==> 创建用户 $USERNAME..." if id "$USERNAME" &> /dev/null; then @@ -26,13 +37,16 @@ else fi echo "==> 配置 WSL 默认用户..." +if [ -f /etc/wsl.conf ]; then + cp /etc/wsl.conf "/etc/wsl.conf.bak.$(date +%s)" +fi cat > /etc/wsl.conf << EOF [user] default = $USERNAME EOF echo "" -echo "✓ 初始化完成!" +echo "✓ WSL 初始化完成!" echo "" echo "下一步:" echo " 1. 在 PowerShell 中执行: wsl --terminate archlinux"