20.4 Arch Linux 兼容层
最后更新于
# service linux enable # 设置 Linux 兼容层服务开机自启
# service linux start # 启动 Linux 兼容层服务# service dbus enable# service dbus start# sysctl compat.linux.osrelease
compat.linux.osrelease: 5.15.0# echo "compat.linux.osrelease=7.0.11" >> /etc/sysctl.conf # 将内核版本写入 sysctl 配置文件,持久化
# sysctl compat.linux.osrelease=7.0.11 # 立即生效,无需重启# sysctl compat.linux.emul_path=/compat/arch# echo "compat.linux.emul_path=/compat/arch" >> /etc/sysctl.confservice linux restart# mkdir -p /compat/arch # 创建兼容层根目录
# fetch https://ftp.sjtu.edu.cn/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst # 下载 Arch Linux bootstrap 压缩包
# tar --use-compress-program=unzstd -xpvf archlinux-bootstrap-x86_64.tar.zst --strip-components=1 -C /compat/arch --numeric-owner # 解压到 /compat/arch,保持原有 UID/GID;tar 错误通常可忽略# cp /etc/resolv.conf /compat/arch/etc/ # 在 FreeBSD 中将 DNS 配置复制到 Arch 兼容层
# chroot /compat/arch /bin/bash # 切换到 Arch 兼容层环境
# pacman-key --init # 初始化 pacman 密钥环
# pacman-key --populate archlinux # 导入 Arch Linux 官方密钥# ee /compat/arch/etc/pacman.d/mirrorlist # 此时位于 FreeBSD!将下行添加至文件顶部。
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch# sed -E -i '' 's/^[[:space:]]*#[[:space:]]*DisableSandbox/DisableSandbox/' /compat/arch/etc/pacman.conf# grep -n 'DisableSandbox' /compat/arch/etc/pacman.conf# nano /etc/pacman.conf # 将下两行添加至文件底部。
[archlinuxcn]
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch# pacman -S archlinuxcn-keyring# pacman -S base base-devel nano yay wqy-zenhei# pacman -S fakeroot-tcp # 会询问是否卸载 fakeroot,请确认并卸载。# locale-gen#!/bin/sh
BASE_URL="https://ftp.sjtu.edu.cn/archlinux/iso/latest"
rootdir=/compat/arch
url="${BASE_URL}/archlinux-bootstrap-x86_64.tar.zst"
BOOTSTRAP=archlinux-bootstrap-x86_64.tar.zst
SHAFILE="${BASE_URL}/sha256sums.txt"
LinuxKernel=7.0.11
echo "Starting Arch Linux installation..."
echo "Checking required modules..."
# Linux compat
if [ "$(sysrc -n linux_enable)" != "YES" ]; then
echo "Linux module is not enabled. Enable it now? (Y|n)"
read answer
case $answer in
[Nn][Oo]|[Nn])
echo "Linux module not enabled"
exit 1
;;
*)
service linux enable
;;
esac
fi
service linux start
# dbus
if ! command -v dbus-daemon >/dev/null 2>&1; then
echo "dbus-daemon not found. Install D-Bus? [Y|n]"
read answer
case $answer in
[Nn][Oo]|[Nn])
exit 2
;;
*)
pkg install -y dbus
;;
esac
fi
if [ "$(sysrc -n dbus_enable)" != "YES" ]; then
echo "Enable D-Bus now? (Y|n)"
read answer
case $answer in
[Nn][Oo]|[Nn])
exit 2
;;
*)
service dbus enable
;;
esac
fi
service dbus start
echo "compat.linux.osrelease=${LinuxKernel}"
sysctl compat.linux.osrelease=${LinuxKernel}
if ! grep -q '^compat.linux.osrelease=' /etc/sysctl.conf 2>/dev/null; then
echo "compat.linux.osrelease=${LinuxKernel}" >> /etc/sysctl.conf
else
sed -i '' "s|^compat.linux.osrelease=.*|compat.linux.osrelease=${LinuxKernel}|" /etc/sysctl.conf
fi
# bootstrap + checksum + resume + retry
echo "Downloading bootstrap..."
fetch -r -o "${BOOTSTRAP}" "${url}"
[ -f sha256sums.txt ] || fetch -o sha256sums.txt "${SHAFILE}"
verify_checksum() {
EXPECTED=$(awk "/${BOOTSTRAP}/{print \$1}" sha256sums.txt)
ACTUAL=$(sha256 -q "${BOOTSTRAP}")
echo "File: ${BOOTSTRAP}"
echo "Expected SHA256: ${EXPECTED}"
echo "Actual SHA256: ${ACTUAL}"
if [ -z "$EXPECTED" ]; then
echo "Checksum not found in sha256sums.txt"
return 1
fi
if [ "$EXPECTED" != "$ACTUAL" ]; then
return 1
fi
return 0
}
echo "Verifying checksum..."
if ! verify_checksum; then
echo "First verification failed. Removing file and retrying..."
rm -f "${BOOTSTRAP}"
echo "Redownloading bootstrap..."
fetch -r -o "${BOOTSTRAP}" "${url}"
echo "Re-verifying checksum..."
if ! verify_checksum; then
echo "Checksum failed after retry."
exit 1
fi
fi
echo "Checksum OK"
rm -f sha256sums.txt
mkdir -p "${rootdir}"
tar --use-compress-program=unzstd -xpvf "${BOOTSTRAP}" --strip-components=1 -C "${rootdir}" --numeric-owner 2>&1 | grep -v "Error exit delayed from previous errors"
rm -f "${BOOTSTRAP}"
# emulation path
sysctl compat.linux.emul_path="${rootdir}"
if ! grep -q '^compat.linux.emul_path=' /etc/sysctl.conf 2>/dev/null; then
echo "compat.linux.emul_path=${rootdir}" >> /etc/sysctl.conf
else
sed -i '' "s|^compat.linux.emul_path=.*|compat.linux.emul_path=${rootdir}|" /etc/sysctl.conf
fi
echo "compat.linux.emul_path=$(sysctl -n compat.linux.emul_path)"
service linux restart
# initial config
echo "Continue initial Arch setup? [Y|n]"
read answer
case $answer in
[Nn][Oo]|[Nn])
exit 0
;;
esac
grep -q "nameserver 223.5.5.5" "${rootdir}/etc/resolv.conf" 2>/dev/null || \
echo "nameserver 223.5.5.5" >> "${rootdir}/etc/resolv.conf"
grep -q "nameserver 223.6.6.6" "${rootdir}/etc/resolv.conf" 2>/dev/null || \
echo "nameserver 223.6.6.6" >> "${rootdir}/etc/resolv.conf"
chroot "${rootdir}" /bin/bash -c "pacman-key --init"
chroot "${rootdir}" /bin/bash -c "pacman-key --populate archlinux"
cp "${rootdir}/etc/pacman.d/mirrorlist" "${rootdir}/etc/pacman.d/mirrorlist.bak"
{
echo 'Server = https://mirrors.cernet.edu.cn/archlinux/$repo/os/$arch'
cat "${rootdir}/etc/pacman.d/mirrorlist.bak"
} > "${rootdir}/etc/pacman.d/mirrorlist"
rm -f "${rootdir}/etc/pacman.d/mirrorlist.bak"
echo '[archlinuxcn]' >> "${rootdir}/etc/pacman.conf"
echo 'Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch' >> "${rootdir}/etc/pacman.conf"
echo '[arch4edu]' >> "${rootdir}/etc/pacman.conf"
echo 'SigLevel = Never' >> "${rootdir}/etc/pacman.conf"
echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/arch4edu/$arch' >> "${rootdir}/etc/pacman.conf"
chroot "${rootdir}" /bin/bash -c "useradd -G wheel -m ykla"
echo 'ALL ALL=(ALL:ALL) NOPASSWD: ALL' >> "${rootdir}/etc/sudoers"
chroot "${rootdir}" /bin/bash -c "mv /usr/share/libalpm/hooks/21-systemd-tmpfiles.hook /usr/share/libalpm/hooks/21-systemd-tmpfiles.hook.disabled"
sed -i '' 's/^[#[:space:]]*DisableSandbox/DisableSandbox/' "${rootdir}/etc/pacman.conf"
chroot "${rootdir}" /bin/bash -c "pacman -Syyu --noconfirm"
chroot "${rootdir}" /bin/bash -c "pacman -S --noconfirm archlinuxcn-keyring"
chroot "${rootdir}" /bin/bash -c "pacman -S --noconfirm yay base base-devel nano wqy-zenhei"
chroot "${rootdir}" /bin/bash -c "pacman -Syu libunwind libedit --noconfirm --overwrite '*'"
chroot "${rootdir}" /bin/bash -c "pacman -Rdd fakeroot --noconfirm && pacman -S --noconfirm fakeroot-tcp"
echo 'zh_CN.UTF-8 UTF-8' >> "${rootdir}/etc/locale.gen"
chroot "${rootdir}" /bin/bash -c "locale-gen"
echo "Base System ready."
echo "chroot ${rootdir} /bin/bash"