Install ArchLinux on Raspberry Pi

Install ArchLinux ARM

After years of idling, I want to use my Raspberry Pi 3 as a gateway for accessing my home from the Internet. Since IPv6 is generally avaliable now.

The first thing is to install an operating system. I want to use ArchLinux ARM, which has support for Raspberry Pi. The installtion is quite simple. To make it work, just extract the tarball then overwrite the rootfs in sdcard.

Provided with just copying files is so convenient, there is no need to remove and plug sdcard.

Insights from vps2arch

vps2arch can easily turn the operating system of a vps into ArchLinux on the fly easily, something sounds like to repair the tire of a running car.

While replacing the old system file with new one, some libraries and executables are invoked. While some of them are deleted before new one copied, the replacing process can no longer goes. The system will stuck at an awkward state. Nothing can be done, and only doom to be broken.

But it is still possible. vps2arch use ld.so from the new rootfs, which is unchanged during the replacing process. Whereby all the files can be safely replaced.

Deploy rootfs on the fly

Here, I download and extract the rootfs tarball into a temporary folder. Then delete files of old system. Up to now, everything in $PATH was gone. Almost nothing can be run from shell.

Then we use ld.so and other system utilities to copy rootfs. As the new files populate the root directory, some commands can be run again.

mkdir -p /test
cd /test
curl -sS http://mirrors.ustc.edu.cn/archlinuxarm/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz | tar -xzv

cd /

if command -v chattr >/dev/null 2>&1; then
    find / -type f \( ! -path '/dev/*' -and ! -path '/proc/*' -and ! -path '/sys/*' -and ! -path '/selinux/*' -and ! -path "/test/*" \) \
        -exec chattr -i {} + 2>/dev/null || true
fi

# Delete *all* files from /boot
rm -rf /boot/*

# Delete *all* files from /
find / \( ! -path '/dev/*' -and ! -path '/proc/*' -and ! -path '/sys/*' -and ! -path '/selinux/*' -and ! -path '/test/*' -and ! -path '/boot/*' \) -delete 2>/dev/null || true

/test/usr/lib/ld-*.so.* --library-path "/test/usr/lib" "/test/bin/cp" -rp /test/* / 

rm -rf /test

pacman-key --init
pacman-key --populate archlinuxarm

Here is a script which borrows some code from vps2arch. You can find the script at pi2arch

After the rootfs is deployed, restart and a brand new archlinux is appeared.

sync; reboot -f

Relogin and request a full system upgrade.

pacman -Syyu

References

https://archlinuxarm.org/platforms/armv8/broadcom/raspberry-pi-3

https://mcfx.us/posts/2021-10-08-reinstall-rpi-without-sd-card-reader/

https://github.com/felixonmars/vps2arch