github编辑

18.4 在树莓派上使用 Linux 二进制兼容层

Arch Linux 兼容层

脚本内容如下:

#!/bin/sh

rootdir=/compat/arch                                  # 设置 Arch Linux 安装根目录
url="https://mirrors.jlu.edu.cn/archlinuxarm/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz"   # Arch Linux ARM 镜像下载地址

echo "begin to install Arch Linux ..."                 # 输出安装开始提示
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"                                    # 输出启动提示
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"            # 输出错误信息
                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"      # 输出错误信息
                        exit 2                        # 退出脚本
                        ;;
            [Yy][Ee][Ss]|[Yy]|"")
                        service dbus enable         # 启用 dbus
                        ;;
        esac
fi
echo "start dbus"                                    # 输出启动提示
service dbus start                                   # 启动 dbus 服务

echo "now we will bootstrap Arch Linux"              # 输出开始引导 Arch Linux 提示

fetch ${url}                                        # 下载 Arch Linux ARM 镜像
mkdir -p ${rootdir}                                # 创建安装根目录
tar xpvf ArchLinuxARM-rpi-aarch64-latest.tar.gz -C ${rootdir} --numeric-owner   # 解压镜像到根目录
rm ArchLinuxARM-rpi-aarch64-latest.tar.gz         # 删除压缩包

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"                  # 提示加载 nullfs
        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                                           # 挂载所有文件系统

echo "For Arch Linux, we should change 'compat.linux.osrelease'. continue? (Y|n)"     # 提示修改内核兼容版本
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   # 写入 sysctl 配置
		sysctl compat.linux.osrelease=6.12.63                   # 应用 sysctl 配置
                ;;
esac
echo "complete!"                                       # 输出完成提示
echo "to use: chroot ${rootdir} /bin/bash"            # 提示如何进入 Arch Linux
echo ""
echo "but for easy use ,i can do some init config"    # 提示是否进行初始化配置
echo "if agree:"
echo "   i set resolv.conf to ali dns"
echo "   init pacman keyring"
echo "   use tsinghua mirror"
echo "continue?[Y|n]"
read answer
case $answer in
	[Nn][Oo]|[Nn])
		echo "set your Arch Linux by yourself.bye!"   # 用户选择不进行初始化
		exit 0
		;;
	[Yy][Ee][Ss]|[Yy]|"")
 		rm  ${rootdir}/etc/resolv.conf                   # 删除原 resolv.conf
		echo "nameserver 223.5.5.5" >> ${rootdir}/etc/resolv.conf  # 设置 DNS
		chroot ${rootdir} /bin/bash -c "pacman-key --init"         # 初始化 pacman keyring
		chroot ${rootdir} /bin/bash -c "pacman-key --populate archlinuxarm"  # 填充 keyring

		echo 'Server = https://mirrors.ustc.edu.cn/archlinuxarm/$arch/$repo' > ${rootdir}/etc/pacman.d/mirrorlist  # 设置镜像
  		echo '[archlinuxcn]' >> ${rootdir}/etc/pacman.conf
  		echo 'Server = https://mirror.sjtu.edu.cn/archlinux-cn/aarch64' >>  ${rootdir}/etc/pacman.conf
		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!"   # 提示将启用 DisableSandbox 以避免 pacman 错误
		sed -E -i '' 's/^[[:space:]]*#[[:space:]]*DisableSandbox/DisableSandbox/' ${rootdir}/etc/pacman.conf   # 修改 pacman.conf,取消 DisableSandbox 的注释以启用该选项
		grep -n 'DisableSandbox' ${rootdir}/etc/pacman.conf
		chroot ${rootdir} /bin/bash -c "pacman -Syyu --noconfirm"       # 更新系统
		echo "Refresh key"
    		chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm archlinuxcn-keyring"   # 更新 keyring
		echo "Install yay"
		chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm yay base base-devel nano yay wqy-zenhei"  # 安装软件和 yay
		echo "Create user"
		chroot ${rootdir} /bin/bash -c "useradd -G wheel -m test"          # 创建用户 test 并加入 wheel 组
		echo "Now modify the sudo configuration"
		echo '%wheel ALL=(ALL) ALL' >> ${rootdir}/etc/sudoers                # 配置 wheel 组 sudo 权限
		echo '%sudo ALL=(ALL:ALL) ALL' >> ${rootdir}/etc/sudoers             # 配置 sudo 组 sudo 权限
		echo "change fakeroot"
		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' Into Arch Linux"  # 提示用户进入 Arch Linux

参考资料

最后更新于