Skip to content

Instantly share code, notes, and snippets.

@yogaxpto
Last active February 10, 2024 22:34
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save yogaxpto/9495fb9f76a6321aa483e0f73d4ffaf2 to your computer and use it in GitHub Desktop.
Save yogaxpto/9495fb9f76a6321aa483e0f73d4ffaf2 to your computer and use it in GitHub Desktop.
Arch Linux ARM USB Boot on Raspberry Pi 4

Arch Linux ARM USB Boot on Raspberry Pi 4B

Index

  1. Requirements
  2. Flash latest EEPROM
  3. Change Raspberry Boot Order
  4. Install Arch Linux on USB device
    1. Format USB device
    2. (Optional) Migrate to hybrid MBR with GPT
    3. Create Filesystem and copy files
    4. Update USB Boot Partitions
  5. Final Configurations
  6. Resources

Requirements

  • Raspberry Pi 4B
  • SD card with RaspbianOS
  • USB device intended to use as boot device

Flash latest EEPROM

After booting the raspberry with the sd card, update the system:

sudo apt update
sudo apt full-upgrade

Then we can update the latest firmware:

sudo rpi-eeprom-update -a

Restart to apply the changes.

Change Raspberry Boot Order

After the first reboot run:

sudo raspi-config

Then choose Advanced Options -> Boot Order -> USB Boot.

Restart to apply the changes.

Install Arch Linux on USB device

Format USB Device

After the second reboot, follow the following instructions to install Arch Linux according to the Arch Linux Documentation:
https://archlinuxarm.org/platforms/armv8/broadcom/raspberry-pi-4

Replace sdX in the following instructions with the device name for the USB device as it appears on your computer.

Start fdisk to partition the SD card:

fdisk /dev/sdX

At the fdisk prompt, delete old partitions and create a new one:

  1. Type o. This will clear out any partitions on the drive.
  2. Type p to list partitions. There should be no partitions left.
  3. Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +200M for the last sector.
  4. Type t, then c to set the first partition to type W95 FAT32 (LBA).
  5. 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.

(Optional) Migrate to hybrid MBR with GPT

Note: This step is not necessary for most USB devices. Use this if your device has more than 2TB of capacity and you need to have a partition with more than 2TB of space.

At the gdisk prompt, enter recovery mode to create hybrid MBR:

  1. Type r to use recovery options.
  2. Type h to make hybrid MBR.
  3. Type 1 to select the first partition.
  4. Type n to not format the first partition to EFI.
  5. Type n to not set bootable flag.
  6. Type n to not protect any other partition.
  7. Type w to write changes.
  8. Type y to confirm changes and exit.

At the gdisk prompt, recreate the second partition:

  1. Type d to delete a partition.
  2. Type 2 to select the second parition.
  3. Type n to add a new partition.
  4. Type ENTER to select the default partition number.
  5. Type ENTER to select the default first sector.
  6. Type ENTER to select the default last sector.
  7. Type ENTER to select the default partition type (Linux Filesystem).
  8. Type w to write changes.
  9. Type y to confirm changes and exit.

Create Filesystem and copy files

Create and mount the FAT filesystem:

mkfs.vfat /dev/sdX1
mkdir boot
mount /dev/sdX1 boot

Create and mount the ext4 filesystem:

mkfs.ext4 /dev/sdX2
mkdir root
mount /dev/sdX2 root

Download and extract the root filesystem (as root, not via sudo):

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

Move boot files to the first partition:

mv root/boot/* boot

Update USB Boot Partitions

fstab

First, adjust the fstab of the new system by replacing the defined system with the USB system:

sudo vim root/etc/fstab

Change the device /dev/mmcblk1p1 to your boot device /dev/sdX1.

cmdline.txt

Then we need to set the new root device to be used:

sudo vim boot/cmdline.txt

Set the root parameter to your root device /dev/sdX2.

Unmount the two partitions:

umount boot root

You can now boot from the newly created USB device without the need for the SD card

Final Configurations

Connect the USB device into the Raspberry Pi, connect ethernet, and apply 5V power. Use the serial console or SSH to the IP address given to the board by your router. Login as the default user alarm with the password alarm. The default root password is root.

Initialize the pacman keyring and populate the Arch Linux ARM package signing keys:

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

Resources

  1. How To Set Up a Raspberry Pi 4 with Archlinux 64-bit (AArch64) and Full Disk Encryption (+SSH unlock), USB Boot (No SD-Card) and btrfs: https://gist.github.com/XSystem252/d274cd0af836a72ff42d590d59647928
  2. Setting up a SSH Server: https://www.raspberrypi.org/documentation/computers/remote-access.html#setting-up-a-ssh-server
  3. Running Raspbian from USB Devices : Made Easy https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=196778
@mightysashiman
Copy link

Update USB Boot Partitions : fstab update : how is the user supposed to know what block name the USB drive will be given by the raspberry pi? When setting-up the USB drive from within another linux running machine (a virtual one here), it just happens that that USB drive is mapped to /dev/sdb (boot is sdb1, root is sdb2). Wouldn't the raspberry pi map it to /dev/sda if it's the first drive it finds?

Update USB Boot Partitions : cmdline.txt

  1. no file with this name exist in boot, so I have no idea what you are talking about
  2. even if there was, how is "Set the root parameter to your root device /dev/sdX2." feels totally unclear. It would be much better to tell the reader what and how to edit concretely

thanks in advance for clarifying these points :)

@yogaxpto
Copy link
Author

yogaxpto commented Aug 27, 2021

@mightysashiman

Update USB Boot Partitions : fstab update : how is the user supposed to know what block name the USB drive will be given by the

It is assumed in this guide that you use the raspberry pi to setup the USB device. If so, the block name shall be the same as the one used throughout this guide.

Update USB Boot Partitions : cmdline.txt

1. no file with this name exist in boot, so I have no idea what you are talking about

2. even if there was, how is _"Set the root parameter to your root device /dev/sdX2."_ feels totally unclear. It would be much better to tell the reader what and how to edit concretely

This seems to be related to the previous step: https://gist.github.com/yogaxpto/9495fb9f76a6321aa483e0f73d4ffaf2#install-arch-linux-on-usb-device

Any problem with this must be related with the tar.gz extraction.

Good luck!

@wrenby
Copy link

wrenby commented Nov 19, 2021

@mightysashiman

how is the user supposed to know what block name the USB drive will be given by the raspberry pi?

For a more stable alternative, you can replace /dev/sdX2 with /dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. You can call blkid /dev/sdX2 to get the UUID (among other things) of that specific partition, or blkid to do so for every partition available.

@dza89
Copy link

dza89 commented Jan 16, 2022

My cmdline.txt gets overwritten on boot. (Reset to default)
Any ideas?

@yogaxpto
Copy link
Author

@dza89

I can confirm that the problem has occurred to me once. To mitigate it, I've created a backup file in case I need to recreate the file.
I've also set up ausearch to listen for any changes in the cmdline.txt file, but I haven't found the culprit as it only happened once to me, yet.

I would advice you to use the same strategy for now.

@khaktus
Copy link

khaktus commented Jan 17, 2022

Not working. All the instructions I have found say: move everything from root/boot to separately mounted boot folder/partition. In the preliminary steps, boot partition /dev/sdX1 was create with size 100M. However during the move:

mv root/boot/* boot
mv: failed to preserve ownership for 'boot/bcm2710-rpi-3-b.dtb': Operation not permitted
... this repeats dozens of times ...
mv: error writing 'boot/start4db.elf': No space left on device
mv: error writing 'boot/start4.elf': No space left on device
mv: error writing 'boot/start_cd.elf': No space left on device
mv: error writing 'boot/start_db.elf': No space left on device
mv: error writing 'boot/start.elf': No space left on device

Apparently 100MB is not enough for boot partition for ArchLinux?

ls after this failed move shows:
root@PricklyDog:/home/baddog/boot# ls -lah
total 79M
drwxr-xr-x 3 baddog baddog 16K jan 1 1970 .
drwxr-xr-x 32 baddog baddog 4,0K jan 17 16:19 ..
-rw-r--r-- 1 baddog baddog 28K máj 27 2021 bcm2710-rpi-3-b.dtb
-rw-r--r-- 1 baddog baddog 29K máj 27 2021 bcm2710-rpi-3-b-plus.dtb
-rw-r--r-- 1 baddog baddog 27K máj 27 2021 bcm2710-rpi-cm3.dtb
-rw-r--r-- 1 baddog baddog 49K máj 27 2021 bcm2711-rpi-4-b.dtb
-rw-r--r-- 1 baddog baddog 52K nov 16 22:01 bootcode.bin
-rw-r--r-- 1 baddog baddog 708 máj 27 2021 boot.scr
-rw-r--r-- 1 baddog baddog 636 máj 27 2021 boot.txt
-rw-r--r-- 1 baddog baddog 14 máj 27 2021 config.txt
drwxr-xr-x 16 baddog baddog 2,0K jan 17 16:21 dtbs
-rw-r--r-- 1 baddog baddog 3,1K nov 16 22:01 fixup4cd.dat
-rw-r--r-- 1 baddog baddog 5,3K nov 16 22:01 fixup4.dat
-rw-r--r-- 1 baddog baddog 8,2K nov 16 22:01 fixup4db.dat
-rw-r--r-- 1 baddog baddog 3,1K nov 16 22:01 fixup_cd.dat
-rw-r--r-- 1 baddog baddog 7,1K nov 16 22:01 fixup.dat
-rw-r--r-- 1 baddog baddog 10K nov 16 22:01 fixup_db.dat
-rw-r--r-- 1 baddog baddog 39M nov 7 22:39 Image
-rw-r--r-- 1 baddog baddog 14M nov 7 22:39 Image.gz
-rw-r--r-- 1 baddog baddog 16M nov 20 04:55 initramfs-linux-fallback.img
-rw-r--r-- 1 baddog baddog 6,9M nov 20 04:55 initramfs-linux.img
-rw-r--r-- 1 baddog baddog 554K máj 27 2021 kernel8.img
-rw-r--r-- 1 baddog baddog 239 máj 27 2021 mkscr
-rw-r--r-- 1 baddog baddog 781K nov 16 22:01 start4cd.elf
-rw-r--r-- 1 baddog baddog 2,6M jan 17 16:24 start4db.elf
-rw-r--r-- 1 baddog baddog 0 jan 17 16:24 start4.elf
-rw-r--r-- 1 baddog baddog 0 jan 17 16:24 start_cd.elf
-rw-r--r-- 1 baddog baddog 0 jan 17 16:24 start_db.elf
-rw-r--r-- 1 baddog baddog 0 jan 17 16:24 start.elf

Particularly image and image.gz take a lot of space

@yogaxpto
Copy link
Author

@khaktus

This is an odd behaviour: those 2 files do not exist in my machine and it only uses 40 MB of space.
I would suggest that you either create a bigger boot filesystem (mine has 200 MB) or try to remove those big files.

@jrvarma
Copy link

jrvarma commented Mar 29, 2022

RPi Arch Linux aarch64 uses U-Boot as the bootloader. As a result, the cmdline.txt file no longer exists, and Image, Image.gz and two initramfs files do exist in /boot. The USB Boot Partitions are now controlled by boot.txt (and its compiled form boot.scr). I do not understand U-Boot at all, but I do find that boot.txt uses U-Boot environment variables like ${devtype} and ${devnum}, and assume that these would automatically point to the correct partitions when booting from USB. Therefore, I assume that no change is required in this file.

However, I am not able to get USB booting to work. The RPi hangs with the rainbow image after reading config.txt and start.elf.

I also tried the suggestion in https://archlinuxarm.org/forum/viewtopic.php?f=67&t=15697&p=68151&hilit=rpi4+usb+boot&sid=704c9f214b91a676e72fbdb8bce7d3df#p68151 to add pcie_brcmstb into MODULES in mkinitcpio.conf and run mkinitcpio -P to regenerate the initramfs files. But that also did not work. Perhaps the problem is in the Image file which I suspect is supposed to load the initramfs files?

Any suggestion would be helpful.

@jrvarma
Copy link

jrvarma commented Mar 29, 2022

Forgot to mention that your instructions work perfectly for booting RPi Arch Linux armv7. The problem mentioned in my previous comment is only with aarch64.

@mcrosson
Copy link

zfs=rpool/root rootfstype=zfs root=/dev/sda2 rootwait rw

this fixed it for my slow to be discovered usb disk. adapt accordingly

@eduardhojbota
Copy link

I also came across issues when trying to setup arch aarch64 using a usb drive. After some research I was able to make it work. I wrote a guide on how I got it working. Hope it helps!

@p-mng
Copy link

p-mng commented Jun 19, 2022

I also came across issues when trying to setup arch aarch64 using a usb drive. After some research I was able to make it work. I wrote a guide on how I got it working. Hope it helps!

You can actually chroot into the Arch Linux ARM installation from an x86 host, edit the mkinitcpio.conf file and run mkinitcpio -P using this guide, just remember to replace qemu-arm-static with qemu-aarch64-static and :arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static: with :qemu-aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/bin/qemu-aarch64-static: (or use binfmt-manager for the latter). This way you can skip the second alarm installation and file copying.

@eliasjonsson023
Copy link

The above way did not work for me. What I DID find to work was by following this guide; installing EndeavourOS on the Raspberry Pi 4 using an Arch Linux Arm micro SD card.
Forum post that explains how to do

@gustad
Copy link

gustad commented Sep 2, 2023

I can't seem to download the tar.gz file:

/tmp/pi4 # wget http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
--2023-09-02 09:42:54--  http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
Resolving os.archlinuxarm.org... 50.116.36.110
Connecting to os.archlinuxarm.org|50.116.36.110|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://ca.us.mirror.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz [following]
--2023-09-02 09:42:55--  http://ca.us.mirror.archlinuxarm.org/os/ArchLinuxARM-rpi-4-latest.tar.gz
Resolving ca.us.mirror.archlinuxarm.org... 169.229.200.70, 2607:f140:0:32::70
Connecting to ca.us.mirror.archlinuxarm.org|169.229.200.70|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2023-09-02 09:42:55 ERROR 404: Not Found.

@gustad
Copy link

gustad commented Sep 2, 2023

However, I did try http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz but this does not have a /boot/cmdline.txt and it will not boot from USB. If I put an Ubuntu image on the USB stick I'm able to boot via USB.

@mariolima
Copy link

same thing here - no /boot/cmdline.txt
I think the guide might be deprecated since the latest aarch64 release

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