Skip to content

Instantly share code, notes, and snippets.

@diffficult
Last active December 24, 2023 00:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diffficult/6804b07400a327fe3b9da6227282372d to your computer and use it in GitHub Desktop.
Save diffficult/6804b07400a327fe3b9da6227282372d to your computer and use it in GitHub Desktop.
Get your Pi 0 W up and running headless with Arch Linux

Installing Arch Linux ARM on a SD card for Raspberry Pi 0 W and loading wifi credentials for headless usage


Advice

Before starting I highly suggest you create a new directory on your /home/user directory with an appropiate name for this task like rpi0alarm so you can change directory to /home/user/rpi0alarm and go through the following steps.

As I wrote this guide after collecting info from different sources (listed at the end) be mindful of your current path and the commands you are running to not damage your current installation on the host computer you are creating the SD Card.


Replace sdX in the following instructions with the device name for the SD card as it appears on your computer. Use lsblk to check the drives connected to it. If there's a single drive it will show as sda and your SD Card will appears as sdb (partitions being sdb1 and sdb2). You can easily identify it also by it's size (tipically 8/16/32 GB).

  1. Start fdisk to partition the SD card:
$ fdisk /dev/sdX
  1. At the fdisk prompt, delete old partitions and create a new one:

    a. Type o. This will clear out any partitions on the drive.

    b. Type p to list partitions. There should be no partitions left.

    c. Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +100M for the last sector.

    d. Type t, then c to set the first partition to type W95 FAT32 (LBA).

    e. 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.

    f. Write the partition table and exit by typing w.

  2. Create and mount the FAT filesystem:

$ mkfs.vfat /dev/sdX1
$ mkdir boot
$ mount /dev/sdX1 boot
  1. Create and mount the ext4 filesystem:
$ mkfs.ext4 /dev/sdX2
$ mkdir root
$ mount /dev/sdX2 root
  1. Download and extract the root filesystem (as root, not via sudo):
$ wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz
$ bsdtar -xpf ArchLinuxARM-rpi-latest.tar.gz -C root
$ sync
  1. Move boot files to the first partition and unmount both partitions and remove the local directories:
$ mv root/boot/* boot
$ umount boot root
$ sudo rm -R boot root
  1. Create this script to enable Wifi on boot time for headless use through ssh:

    In this step you are about to create a small script that will setup your wifi network so the Raspberry Pi 0 W autoconnects after booting for the first time so you can access it through SSH

    a. After extracting the contents of ArchLinuxARM-rpi-latest.tar.gz into /root and /boot partitions you created on your SD Card, you now need to create on your computer a file:

    $ nano al-wpa-setup.sh
    

    b. This will open a blank nano editor. Paste the following code and save the file (using Ctrl+o and Ctrl+x)

    #!/bin/sh
    set -e
    if [[ $# -ne 3 ]] ; then
        echo "Usage: $0 </dev/disk> <ssid> <passphase>"
    exit 1
    fi
    
    DISK="$1"
    SSID="$2"
    PASS="$3"
    
    if [[ ! -b "${DISK}" ]] ; then
        echo "Not a block device: ${DISK}"
        exit 1
    fi
    
    if [[ "${USER}" != "root" ]] ; then
        echo "Must run as root."
        exit 1
    fi
    
    echo "Mounting ${DISK} into rpi0alarm/root and writing values"
    mkdir root
    mount "${DISK}2" root
    
    cat << EOF >> root/etc/systemd/network/wlan0.network
    [Match]
    Name=wlan0
    
    [Network]
    DHCP=yes
    EOF
    
    wpa_passphrase "${SSID}" "${PASS}" > root/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
    
    ln -s \
    /usr/lib/systemd/system/wpa_supplicant@.service \
    root/etc/systemd/system/multi-user.target.wants/wpa_supplicant@wlan0.service
    
    echo "Unmounting ${DISK} from rpi0alarm/root"
    umount root
    
    echo "Cleaning up and erasing local rpi0alarm/root directory"
    rmdir root

    This script will ask for three parameters DISK, SSID and PASS and then will write those into /root on your SD Card.

    c. We now need to make the script executable:

    chmod +x al-wpa-setup.sh
    

    d. Execute the script and pass the appropiate parameters, /dev/sdX, SSID and wifi_password, in this example your SD Card is sdb, SSID is home_network and the password is drowssap:

    ./al-wpa-setup.sh /dev/sdb home_network drowssap
    
  2. Now we can finally insert the SD card into the Raspberry Pi 0 and connect it to it's 5V power source, the green light will blink a few times and stay on. Give it a minute or so and continue.

  3. Use the serial console or SSH to the IP address given to the board by your router, hostname is alarmpi.

    • Login as the default user alarm with the password alarm.

    • The default root password is root.

    • Successful login will get you to the Raspberry Pi 0 prompt like so,

      Welcome to Arch Linux ARM
      
      Website: http://archlinuxarm.org
        Forum: http://archlinuxarm.org/forum
          IRC: #archlinux-arm on irc.Freenode.net
      Last login: Day Month XX XX:XX:XX:XX YEAR from XXX.XXX.XXX.XXX
      [alarm@alarmpi ~]$
      
  4. Initialize the pacman keyring and populate the Arch Linux ARM package signing keys:

[alarm@alarmpi] $ pacman-key --init
[alarm@alarmpi] $ pacman-key --populate archlinuxarm
  1. As you don't want to run your system as root all the time or switching accounts you need to install sudo, in case you are planning to go with root user you can just skip up to step XX.

    a. To do so, we will have to switch momentarily to root to update our package list and then install sudo

    [alarm@alarmpi] $ su
    

    Enter your root password and from there execute,

    [root@alarmpi] pacman -S sudo
    

    b. Now we have to give the user alarm sudo powers, we need to add this user to the wheel usergroup,

    [root@alarmpi] $ gpasswd -a alarm wheel
    

    c. And we need to edit /etc/sudoers to accept wheel group commands as root. We can do it easily with the inbuilt command visudo,

    [root@alarmpi] $ visudo
    

    You will be now scrolling with vi inside the file and you need to look and edit the following lines, we are going to uncomment this lines by removing the # using the x or the Delete key. As follow are the lines we will be looking for, # %wheel ALL=(ALL) ALL and # %wheel ALL=(ALL) NOPASSWD: ALL,

    ## Uncomment to allow members of group wheel to execute any command
    # %wheel ALL=(ALL) ALL
    
    ## Same thing without a password
    # %wheel ALL=(ALL) NOPASSWD: ALL
    

    After doing that all we have to do is type :x to save and exit. In case there's something wrong or you edited something you shouldn't you can just go and press Esc and type :q! to exit without saving any changes.

    d. To make all this changes take effect we have to reboot the Raspberry Pi 0,

    [root@alarmpi] $ reboot
    

    e. As a final step to check everything went well we can SSH into the Raspberry Pi 0 again and from the user alarm update our pacman cache,

    [alarm@alarmpi] $ sudo pacman -Syy
    

Sources

@ajbt200128
Copy link

Thanks! Super helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment