#!/bin/sh # 指定脚本使用的 shell
rootdir=/compat/arch # 定义 Arch 兼容层根目录
url="https://ftp.sjtu.edu.cn/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst" # Arch Linux bootstrap 下载地址
echo "Starting Arch Linux installation..." # 输出安装开始信息
echo "check modules ..." # 输出模块检查信息
# check linux module
if [ "$(sysrc -n linux_enable)" = "NO" ]; then # 检查 Linux 内核模块是否启用
echo "linux module should be loaded. Continue?(Y|n)" # 提示是否继续
read answer # 读取用户输入
case $answer in # 处理用户选择
[Nn][Oo]|[Nn])
echo "linux module not loaded" # 提示模块未加载
exit 1 # 退出脚本
;;
[Yy][Ee][Ss]|[Yy]|"")
sysrc linux_enable=YES # 启用 Linux 内核模块
;;
esac
fi
echo "start linux" # 输出启动 Linux 信息
service linux start # 启动 Linux 内核模块
# check dbus
if ! /usr/bin/which -s dbus-daemon;then # 检查 dbus-daemon 是否存在
echo "dbus-daemon not found. install it [Y|n]" # 提示安装 dbus
read answer # 读取用户输入
case $answer in
[Nn][Oo]|[Nn])
echo "dbus not installed" # 提示未安装 dbus
exit 2 # 退出脚本
;;
[Yy][Ee][Ss]|[Yy]|"")
pkg install -y dbus # 安装 dbus
;;
esac
fi
if [ "$(sysrc -n dbus_enable)" != "YES" ]; then # 检查 dbus 服务是否启用
echo "dbus should be enabled. Continue? (Y|n)" # 提示是否继续
read answer # 读取用户输入
case $answer in
[Nn][Oo]|[Nn])
echo "dbus not running" # 提示 dbus 未运行
exit 2 # 退出脚本
;;
[Yy][Ee][Ss]|[Yy]|"")
service dbus enable # 启用 dbus 服务开机自启
;;
esac
fi
echo "start dbus" # 输出启动 dbus 信息
service dbus start # 启动 dbus 服务
echo "now we will bootstrap Arch Linux" # 输出 bootstrap 开始信息
fetch ${url} # 下载 Arch Linux bootstrap 压缩包
mkdir /compat # 创建挂载目录
tar --use-compress-program=unzstd -xpvf archlinux-bootstrap-x86_64.tar.zst -C /compat --numeric-owner # 解压压缩包
rm archlinux-bootstrap-x86_64.tar.zst # 删除压缩包
mv /compat/root.x86_64 ${rootdir} # 重命名解压目录
if [ ! "$(sysrc -f /boot/loader.conf -qn nullfs_load)" = "YES" ]; then # 检查 nullfs 是否加载
echo "nullfs_load should load. continue? (Y|n)" # 提示是否继续
read answer # 读取用户输入
case $answer in
[Nn][Oo]|[Nn])
echo "nullfs not load" # 提示未加载
exit 3 # 退出脚本
;;
[Yy][Ee][Ss]|[Yy]|"")
sysrc -f /boot/loader.conf nullfs_load=yes # 设置 nullfs 开机自启
;;
esac
fi
if ! kldstat -n nullfs >/dev/null 2>&1;then # 检查 nullfs 模块是否已加载
echo "load nullfs module" # 输出加载信息
kldload -v nullfs # 加载 nullfs 模块
fi
echo "mount some fs for linux" # 输出挂载文件系统信息
echo "devfs ${rootdir}/dev devfs rw,late 0 0" >> /etc/fstab # devfs 挂载
echo "tmpfs ${rootdir}/dev/shm tmpfs rw,late,size=1g,mode=1777 0 0" >> /etc/fstab # tmpfs 挂载
echo "fdescfs ${rootdir}/dev/fd fdescfs rw,late,linrdlnk 0 0" >> /etc/fstab # fdescfs 挂载
echo "linprocfs ${rootdir}/proc linprocfs rw,late 0 0" >> /etc/fstab # linprocfs 挂载
echo "linsysfs ${rootdir}/sys linsysfs rw,late 0 0" >> /etc/fstab # linsysfs 挂载
echo "/tmp ${rootdir}/tmp nullfs rw,late 0 0" >> /etc/fstab # /tmp 挂载
#echo "/home ${rootdir}/home nullfs rw,late 0 0" >> /etc/fstab # /home 挂载,可选
mount -al # 挂载 fstab 中所有文件系统
echo "For Arch Linux, we should change 'compat.linux.osrelease'. continue? (Y|n)" # 提示修改 Linux 内核版本
read answer # 读取用户输入
case $answer in
[Nn][Oo]|[Nn])
echo "close to success" # 提示跳过修改
exit 4
;;
[Yy][Ee][Ss]|[Yy]|"")
echo "compat.linux.osrelease=6.12.63" >> /etc/sysctl.conf # 持久化设置 Linux 内核版本
sysctl compat.linux.osrelease=6.12.63 # 立即生效
;;
esac
echo "complete!" # 输出完成信息
echo "to use: chroot ${rootdir} /bin/bash" # 提示使用 chroot 进入 Arch 兼容层
echo ""
echo "but for ease of use, I can do some initial config" # 提示可进行初始化配置
echo "if agree:" # 提示用户选择
echo " I set resolv.conf to ali dns" # 将 DNS 设置为阿里 DNS
echo " init pacman keyring" # 初始化 pacman 密钥环
echo " use tsinghua mirror" # 使用清华镜像
echo "continue?[Y|n]" # 提示是否继续
read answer # 读取用户输入
case $answer in
[Nn][Oo]|[Nn])
echo "set up your Arch Linux by yourself. Bye!" # 提示用户自行配置
exit 0
;;
[Yy][Ee][Ss]|[Yy]|"")
echo "nameserver 223.5.5.5" >> ${rootdir}/etc/resolv.conf # 设置 DNS
chroot ${rootdir} /bin/bash -c "pacman-key --init" # 初始化 pacman 密钥
chroot ${rootdir} /bin/bash -c "pacman-key --populate archlinux" # 导入官方密钥
cat ${rootdir}/etc/pacman.d/mirrorlist > mlst.tmp # 备份镜像列表
echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch' > ${rootdir}/etc/pacman.d/mirrorlist # 设置清华镜像
cat mlst.tmp >> ${rootdir}/etc/pacman.d/mirrorlist # 恢复原有镜像列表内容
rm mlst.tmp # 删除临时文件
echo '[archlinuxcn]' >> ${rootdir}/etc/pacman.conf # 添加 archlinuxcn 仓库
echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch' >> ${rootdir}/etc/pacman.conf # 设置 archlinuxcn 镜像
echo "Refresh sources and systems" # 输出刷新信息
echo "Now we will enable DisableSandbox for pacman or you will get error: restricting filesystem access failed because landlock is not supported by the kernel!" # 提示禁用 Sandbox
sed -E -i '' 's/^[[:space:]]*#[[:space:]]*DisableSandbox/DisableSandbox/' ${rootdir}/etc/pacman.conf # 取消注释 DisableSandbox
grep -n 'DisableSandbox' ${rootdir}/etc/pacman.conf # 验证 DisableSandbox
chroot ${rootdir} /bin/bash -c "pacman -Syyu --noconfirm" # 更新系统
echo "Refresh key" # 输出刷新密钥信息
chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm archlinuxcn-keyring" # 安装 archlinuxcn-keyring
echo "Install yay" # 输出安装 yay 信息
chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm yay base base-devel nano wqy-zenhei" # 安装软件
echo "Create user" # 输出创建用户信息
chroot ${rootdir} /bin/bash -c "useradd -G wheel -m test" # 创建用户 test 并加入 wheel 组
echo "Now modify the sudo configuration" # 输出修改 sudoers 信息
echo '%wheel ALL=(ALL) ALL' >> ${rootdir}/etc/sudoers # 允许 wheel 组 sudo 权限
echo '%sudo ALL=(ALL:ALL) ALL' >> ${rootdir}/etc/sudoers # 允许 sudo 组 sudo 权限
echo "change fakeroot" # 输出安装 fakeroot-tcp 信息
chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm fakeroot-tcp" # 安装 fakeroot-tcp
echo "Make localised settings" # 输出本地化设置信息
echo 'zh_CN.UTF-8 UTF-8' >> ${rootdir}/etc/locale.gen # 添加中文 UTF-8
chroot ${rootdir} /bin/bash -c "locale-gen" # 生成语言环境
echo "all done." # 输出完成信息
;;
esac
echo "Now you can run '#chroot /compat/arch/ /bin/bash' to enter Arch Linux" # 提示用户进入 Arch Linux