> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-42-zhang-qian-ru-shi-ping-tai-ji-kai-fa-huan-jing/di-42.3-jie-shu-mei-pai-linux-jian-rong-ceng.md).

# 42.3 树莓派 Linux 兼容层

Arch Linux ARM 移植版（ALARM）针对树莓派等 ARM 设备提供滚动更新的软件源。本节给出在 FreeBSD 树莓派系统上部署 Arch Linux 兼容层的自动化脚本。

## Arch Linux 发行版概述

以下是完整的自动化部署脚本内容：

```sh
#!/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 ..."

# 检查 linux 模块
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 兼容层

# 检查 dbus
if ! /usr/bin/which -s dbus-daemon; then             # 检查 dbus-daemon 是否存在
        echo "dbus-daemon not found. install it [Y|n]"
        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"

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"
        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 "configuration not completed"
		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"
echo ""
echo "the script can also perform some initial configuration"
echo "if agree:"
echo "   set resolv.conf to Ali DNS"
echo "   init pacman keyring"
echo "   use USTC mirror"
echo "continue?[Y|n]"
read answer
case $answer in
	[Nn][Oo]|[Nn])
		echo "set your Arch Linux by yourself."
		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 "Enabling DisableSandbox in pacman.conf to avoid landlock error on this kernel"
		sed -E -i '' 's/^[[:space:]]*#[[:space:]]*DisableSandbox/DisableSandbox/' ${rootdir}/etc/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"   # 安装 archlinuxcn-keyring
		echo "Install yay"
		chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm yay base base-devel nano 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"
```

目录结构如下：

```sh
/compat/arch/
├── dev/
│   ├── shm/
│   └── fd/
├── proc/
├── sys/
├── tmp/
├── home/
└── etc/
    ├── resolv.conf
    ├── pacman.conf
    ├── pacman.d/
    │   └── mirrorlist
    ├── locale.gen
    └── sudoers
```

## 参考文献

* Tonooo. Raspberry Pi 4 に Arch Linux をインストール\[EB/OL]. \[2026-03-25]. <https://tonooo71.github.io/posts/archlinux-on-raspberrypi4>. 在树莓派 4 上安装 Arch Linux ARM 的详细教程。
* FreeBSD Project. FreeBSD Handbook: Linux Binary Compatibility\[EB/OL]. \[2026-03-25]. <https://wiki.freebsd.org/Linuxulator>. FreeBSD 官方 Linux 兼容层文档。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/di-42-zhang-qian-ru-shi-ping-tai-ji-kai-fa-huan-jing/di-42.3-jie-shu-mei-pai-linux-jian-rong-ceng.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
