> 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/ask/flat/chapter-23-embedded-platforms-and-development-environments/di-23.3-jie-shu-mei-pai-linux-jian-rong-ceng.md).

# 23.3 Raspberry Pi Linux Compatibility Layer

Arch Linux ARM (ALARM) provides rolling-update software repositories for ARM devices such as the Raspberry Pi. This section provides an automated script for deploying an Arch Linux compatibility layer on a FreeBSD Raspberry Pi system.

## Arch Linux Distribution Overview

The following is the complete automated deployment script:

```sh
#!/bin/sh

rootdir=/compat/arch                                  # Set Arch Linux installation root directory
url="https://mirrors.jlu.edu.cn/archlinuxarm/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz"   # Arch Linux ARM image download URL

echo "begin to install Arch Linux ..."
echo "check modules ..."

# Check linux module
if [ "$(sysrc -n linux_enable)" = "NO" ]; then      # Check if linux module is enabled
        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         # Enable linux module
                        ;;
        esac
fi
echo "start linux"
service linux start                                   # Start linux compatibility layer

# Check dbus
if ! /usr/bin/which -s dbus-daemon; then             # Check if dbus-daemon exists
        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                  # Install dbus
                ;;
        esac
fi

if [ "$(sysrc -n dbus_enable)" != "YES" ]; then      # Check if dbus is enabled
        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         # Enable dbus
                        ;;
        esac
fi
echo "start dbus"
service dbus start                                   # Start dbus service

echo "now we will bootstrap Arch Linux"

fetch ${url}                                        # Download Arch Linux ARM image
mkdir -p ${rootdir}                                # Create installation root directory
tar xpvf ArchLinuxARM-rpi-aarch64-latest.tar.gz -C ${rootdir} --numeric-owner   # Extract image to root directory
rm ArchLinuxARM-rpi-aarch64-latest.tar.gz         # Remove the archive

if [ ! "$(sysrc -f /boot/loader.conf -qn nullfs_load)" = "YES" ]; then   # Check if nullfs is enabled
        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   # Enable nullfs
                ;;
        esac
fi

if ! kldstat -n nullfs >/dev/null 2>&1; then       # Check if nullfs module is loaded
        echo "load nullfs module"
        kldload -v nullfs                          # Dynamically load nullfs module
fi

echo "mount some fs for linux"
echo "devfs ${rootdir}/dev devfs rw,late 0 0" >> /etc/fstab       # Configure devfs
echo "tmpfs ${rootdir}/dev/shm tmpfs rw,late,size=1g,mode=1777 0 0" >> /etc/fstab   # Configure tmpfs
echo "fdescfs ${rootdir}/dev/fd fdescfs rw,late,linrdlnk 0 0" >> /etc/fstab         # Configure fdescfs
echo "linprocfs ${rootdir}/proc linprocfs rw,late 0 0" >> /etc/fstab                # Configure linprocfs
echo "linsysfs ${rootdir}/sys linsysfs rw,late 0 0" >> /etc/fstab                    # Configure linsysfs
echo "/tmp ${rootdir}/tmp nullfs rw,late 0 0" >> /etc/fstab                           # Configure /tmp
#echo "/home ${rootdir}/home nullfs rw,late 0 0" >> /etc/fstab                        # Optional: configure /home
mount -al                                           # Mount all file systems

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   # Write sysctl configuration
                sysctl compat.linux.osrelease=6.12.63                   # Apply sysctl configuration
                ;;
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                   # Remove original resolv.conf
                echo "nameserver 223.5.5.5" >> ${rootdir}/etc/resolv.conf  # Set DNS
                chroot ${rootdir} /bin/bash -c "pacman-key --init"         # Initialize pacman keyring
                chroot ${rootdir} /bin/bash -c "pacman-key --populate archlinuxarm"  # Populate keyring

                echo 'Server = https://mirrors.ustc.edu.cn/archlinuxarm/$arch/$repo' > ${rootdir}/etc/pacman.d/mirrorlist  # Set to USTC mirror
                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   # Uncomment DisableSandbox to enable the option
                grep -n 'DisableSandbox' ${rootdir}/etc/pacman.conf
                chroot ${rootdir} /bin/bash -c "pacman -Syyu --noconfirm"       # Update system
                echo "Refresh key"
                chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm archlinuxcn-keyring"   # Install archlinuxcn-keyring
                echo "Install yay"
                chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm yay base base-devel nano wqy-zenhei"  # Install base software and yay
                echo "Create user"
                chroot ${rootdir} /bin/bash -c "useradd -G wheel -m test"          # Create user test and add to wheel group
                echo "Now modify the sudo configuration"
                echo '%wheel ALL=(ALL) ALL' >> ${rootdir}/etc/sudoers                # Configure wheel group sudo privileges
                echo '%sudo ALL=(ALL:ALL) ALL' >> ${rootdir}/etc/sudoers             # Configure sudo group sudo privileges
                echo "change fakeroot"
                chroot ${rootdir} /bin/bash -c "pacman -S --noconfirm fakeroot-tcp"  # Install fakeroot-tcp
                echo "Make localised settings"
                echo 'zh_CN.UTF-8 UTF-8' >> ${rootdir}/etc/locale.gen                 # Enable Chinese UTF-8 locale
                chroot ${rootdir} /bin/bash -c "locale-gen"                           # Generate locale settings
                echo "all done."
                ;;
esac
echo "Now you can run '#chroot /compat/arch/ /bin/bash' into Arch Linux"
```

The directory structure is as follows:

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

## References

* Tonooo. Raspberry Pi 4 に Arch Linux をインストール\[EB/OL]. \[2026-03-25]. <https://tonooo71.github.io/posts/archlinux-on-raspberrypi4>. A detailed tutorial on installing Arch Linux ARM on the Raspberry Pi 4.
* FreeBSD Project. FreeBSD Handbook: Linux Binary Compatibility\[EB/OL]. \[2026-03-25]. <https://wiki.freebsd.org/Linuxulator>. FreeBSD official Linux compatibility layer documentation.


---

# 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:

```
GET https://book.bsdcn.org/ask/flat/chapter-23-embedded-platforms-and-development-environments/di-23.3-jie-shu-mei-pai-linux-jian-rong-ceng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
