drshapeless blog

Install Arch Linux on Raspberry Pi

Create at: 2022-06-24 Update at: 2022-06-24

This post is a step-by-step guide of installing Arch Linux on Raspberry Pi. The post is based on the guide on Arch Linux ARM official website. What separate this post and the official guide is that, it only focuses on the new Raspberry Pi 4 model and a 64-bit OS. Readers following this guide does not need to make decision about whether a command is for your machine. You can braindeadly copy and paste and everything should work.

Be careful, most of the commands here require root permission. You can either switch to a super user (if you know what you are doing), or you can use sudo.

Prepare an SD card

An SD card is a must to boot into your Pi, since making the Pi boots from USB requires extra tinkering.

This is not true for installing Raspberry Pi OS, (formerly called Raspbian), the official OS can be booted from USB.

Partition

Since the Pi requires a boot partition of FAT32, we have to manually make a boot partition.

The following is directly from the Arch Linux ARM website.

Connect your SD card to your machine, I am on an Artix Linux machine and using a 128GB sandisk extreme pro.

According to this gist, If you are on a Mac, be careful that the built-in SD card slot may not be able to perform partitioning. An external SD card reader is needed. I did try on my M1 MacBook Pro, but not only the built-in SD card reader didn't work, the external one also did not work. It gave an error related to missing i386 library. This may be because of the ARM nature of Apple silicon.

After you have connected your SD card.

Find out your device using lsblk.

lsblk

Assume my device is at /dev/sdb. Use fdisk.

fdisk /dev/sdb

These steps are directly from the official guide.

  1. Type o. This will purge any partitions on the drive.
  2. Type p to list partitions. To check if any partition is still present.
  3. To create the boot partition: Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +512M for the last sector.
  4. Type t, then c to set the first partition to type W95 FAT32 (LBA).
  5. To create the root partition: Type n, then p for primary, 2 for the second partition on the drive, and then press ENTER twice to accept the default first and last sector.
  6. Write the partition table and exit by typing w.

In the original guide, the size for the boot partition is 200M, but since I have a lot of space, I allocated 512M in case something is wrong. Normally, 200M is quite over-kill. However, I notice that some guides floating on the Internet, especially those of content farms, use 100M for boot partition. Do NOT do this. 100M is not enough for Arch Linux ARM.

Format

mkfs.vfat /dev/sdb1
mkfs.ext4 /dev/sdb2

Mount

In a whatever directory, (I personally create a folder called raspberrypi), create a boot and a root directory. These are for mounting the SD card.

Some people prefers mounting the SD card in /mnt, I normally would do that to any other devices. However, since the SD card of Pi is likely to be a single use case. Creating directories in /mnt does not feel right to me.

mkdir raspberrypi
cd raspberrypi
mkdir boot
mkdir root
mount /dev/sdb1 ./boot
mount /dev/sdb2 ./root

Download and extract the Arch Linux ARM image

The extract command, bsdtar must be prefixed with sudo or run by root, otherwise, your image is not usable.

wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz
bsdtar -xpf ArchLinuxARM-rpi-armv7-latest.tar.gz -C root
sync

The sync command takes a lot of time, be patient. (This is just for safety only, if you are bold enough, you can skip the sync command.)

Move boot partition

mv ./root/boot/* ./boot

Update the fstab

sed -i 's/mmcblk0/mmcblk1/g' ./root/etc/fstab

You can also open the fstab with a text editor and change "mmcblk0" to "mmcblk1".

Unmount

umount boot root

Connect to your Pi

If you are plugging an ethernet cable into your Pi. You can check out the ip of the Pi in your router interface.

The default password for user "alarm" is "alarm", for "root" is "root".

You can ssh into your Pi and start setting up things as a headless machine.

But if you are using Wifi, things get a bit complicated.

To connect to Wifi, we need to have wpa_supplicant and dhcpcd, which have already been installed in the image. (For a braindead solution, you can find a way to install networkmanager.)

You can't do it in a headless way. Plug in the monitor and keyboard. And boot into your Pi.

wpa_passphrase "ssid" "passpharse" > /etc/wpa_supplicant/wpa_supplicant.conf

In /etc/wpa/wpa, the network session is already generated by the previous command. Add other lines on top if you want to use wpa_supplicant command to connect to Wifi. You can omit this part if you only use service daemon.

# Giving configuration update rights to wpa_cli
ctrl_interface=/run/wpa_supplicant
ctrl_interface_group=wheel
update_config=1

# AP scanning
ap_scan=1

# ISO/IEC alpha2 country code in which the device is operating
country=US

# network section generated by wpa_passphrase
network={
        ssid="ssid"
        #psk="passpharse"
        psk=<hash>
}

The default init system for Arch Linux ARM is systemd. Start the wpa_supplicant and dhcpcd service.

systemctl enable wpa_supplicant
systemctl enable dhcpcd

The service will automatically start.

To check if you have connected to the Internet, check if the wlan0 part has an ip address.

ip a

In case you want to have a static local ip for the ease of connecting from other machine.

In /etc/dhcpcd.conf, add these to the bottom and restart the dhcpcd service.

interface wlan0
static ip_address=192.168.0.150/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1 8.8.8.8
systemctl restart dhcpcd

After all, you can unplug you monitor and keyboard and use the Pi as a headless machine.

If you don't bother to check the ip of your Pi every time you want to connect to it. (Every restart may change the local ip.) You can setup avahi, for more, see Arch Wiki.

Post installation

Install the keychain.

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

Update the package repo. Always use -Syyu on your first update to force the update of the repo database.

pacman -Syyu

You can also set up parallel downloads for pacman to speed up package updates.

In /etc/pacman.conf, uncomment this.

ParallelDownloads = 5

I have a very fast Internet, I always set it to 10.

Conclusion

Installing Arch Linux ARM on a Raspberry Pi is not difficult, but there are always some little things that fuck up the installation or usage. A lot of them comes from the ownership issue when copying or extracting from the image.

For example, the sudo command in the Pi may be broken. Giving an error like "/usr/bin/sudo must be owned by uid 0 and have the setuid bit set".

This can be easily fixed using:

chmod 4755 /usr/bin/sudo

If you encounter an error, don't panic. Copy and paste the error to the search engine, some other guys might have faced the same frustration with you a long time ago, and had the balls to ask on the Internet.

Most of the error can be fixed by a simple search. Re-read my post detailedly to see if anything is missing. I am not a Linux wizard, if I can do it, you can do it.