Skip to content

Instantly share code, notes, and snippets.

@Matho
Created November 6, 2022 19:18
Show Gist options
  • Save Matho/e19da93c367114f330d1dd076a0ed392 to your computer and use it in GitHub Desktop.
Save Matho/e19da93c367114f330d1dd076a0ed392 to your computer and use it in GitHub Desktop.
Boot from SSD connected via USB 3 on Raspberry PI 4 8GB

Boot from SSD connected via USB 3 on Raspberry PI 4 8GB

6th November 2022

I have decided to stop using microSD cards in my Raspberry PI mini cluster. I'm afraid, that the card will fail in few months, so I have decided to switch to SSD disk. The prices are almost similar to prices of microSD cards (price of usb to ssd adapter not included).

In my cluster, I'm using Ubuntu 20.04. Ubuntu 20.04 was released before the EEPROM update to RPi4 which allowed USB booting. In this tutorial we will show how to prepare USB booting on Ubuntu 20.04.

Hardware

I have bought:

Some users recommends 3.5A power supply, for case you plugin the SSD disk to USB port of Raspberry Pi. My power supply is only 3A, but seems it works. This SSD model use only 1,31 W, based on product parameters.

Copy data from microSD to SSD

  • turn off Raspberry PI
  • insert microSD card to your notebook (or to microSD adapter)
  • unmount the card and umount also the ssd disk
  • run dd command to copy data from microSD card to SSD drive (replace X with source device and Y with target device)
$ sudo dd if=/dev/sdX of=/dev/sdY status=progress bs=16M
  • if the SSD has higher capacity, start Gparted software after dd has passed
  • click on the partition you want to resize
  • from menu select Partition > Resize/Move
  • move slider to the right
  • confirm action
  • choose menu Edit > Apply all operations
  • click apply

Now we have copied the content from microSD card to SSD disk

Boot Raspberry Pi from SSD via USB

All Raspberry PI 4B model with 8GB ram and also some 4GB ram models supports boot from USB. But there are issues with usb boot on Ubuntu 20.04 - it is not supported by default. We need to update EEPROM, but in the end after this changes it will work.

Following lines are prepared based on this two articles:

I have extracted most of this informations from this two articles. You can follow my instructions below instead of that links - my instructions are tested in November 2022 and there are few changes compared to the above links. If you need explanation of some of the step, you can always check that links for more info.

Steps 1-4. Flash Raspberry PI to new microSD card

I recommend to download Raspberry PI Imager. It is software which can download and flash bootable operating system on your rpi.

  • lets install Raspberry PI OS 64bit with 0,8 GB image size to new microSD card (do not overwrite your existing filesystem, use another card)
  • select target drive and select input image
  • before you start flashing, open settings (the cog in the bottom right corner)
  • set credentials:
    • username: pi
    • password: raspberry
  • flash the device

Insert the card to RPI and turn it on.

Login to wifi router admin to detect, which IP was assigned to the rpi - for me it was 10.0.2.122

Step 5 Upgrade the system

This will update the OS on your newly flashed microSD. Do NOT upgrade system on your Ubuntu 20.04 project! Use the newly flashed card!

Run
$ sudo apt update && sudo apt full-upgrade -y

6 Firmware release channel

We will not change the firmware release channel to beta, as this version of OS has supported firmware already in default channel. Do not change it to beta, keep it as is without changes.

7 Install the bootloader

Check, if pieeprom-2022-04-26.bin exists. If yes, select that value

$ sudo su
$ cd /lib/firmware/raspberrypi/bootloader/default/
$ rpi-eeprom-update -d -f pieeprom-2022-04-26.bin

Output:

*** INSTALLING pieeprom-2022-04-26.bin  ***

CURRENT: Thu 29 Apr 16:11:25 UTC 2021 (1619712685)
UPDATE: Tue 26 Apr 10:24:28 UTC 2022 (1650968668)
BOOTFS: /boot
Using recovery.bin for EEPROM update

EEPROM updates pending. Please reboot to apply the update.
To cancel a pending update run "sudo rpi-eeprom-update -r".

8 Reboot

$ sudo reboot 

Verify that eeprom has been updated:

$ sudo vcgencmd bootloader_version

9 Mount partitions

Plug the SSD disk to the Raspberry Pi USB 2 port (black port) and mount its partitions:

$ lsblk

$ sudo mkdir /mnt/boot && sudo mount /dev/sda1 /mnt/boot
$ sudo mkdir /mnt/principal && sudo mount /dev/sda2 /mnt/principal

10 Copy content from /boot to /mnt/boot

$ cd /mnt/boot
$ sudo cp /boot/*.elf /mnt/boot/ 
$ sudo cp /boot/*.dat /mnt/boot/

11 Extract the vmlinuz file on the boot partition

sudo su
cd /mnt/boot
zcat vmlinuz > vmlinux

12 Update the config.txt file

Update the section [pi4] on config.txt file (located on /mnt/boot) as follow:

sudo su
vi /mnt/boot/config.txt

Add the next lines and save the file.

dtoverlay=vc4-fkms-v3d 
boot_delay 
kernel=vmlinux 
initramfs initrd.img followkernel

13 Script for auto decompression

Now we’ll add a script for auto decompression of the Kernel in boot partition (/mnt/boot/) let’s name it auto_decompress_kernel

$ cd /mnt/boot/
$ sudo vi auto_decompress_kernel

Content of file:

#!/bin/bash -e
# auto_decompress_kernel script
#Set Variables 
BTPATH=/boot/firmware 
CKPATH=$BTPATH/vmlinuz 
DKPATH=$BTPATH/vmlinux  
#Check if compression needs to be done. 
if [ -e $BTPATH/check.md5 ]; then  
   if md5sum --status --ignore-missing -c $BTPATH/check.md5; then
      echo -e "\e[32mFiles have not changed, Decompression not needed\e[0m"  
      exit 0  
   else 
      echo -e "\e[31mHash failed, kernel will be compressed\e[0m"  
   fi 
fi
#Backup the old decompressed kernel 
mv $DKPATH $DKPATH.bak  
if [ ! $? == 0 ]; then  
   echo -e "\e[31mDECOMPRESSED KERNEL BACKUP FAILED!\e[0m"  
   exit 1 
else  
   echo -e "\e[32mDecompressed kernel backup was successful\e[0m" 
fi  
#Decompress the new kernel 
echo "Decompressing kernel: "$CKPATH".............."  
zcat $CKPATH > $DKPATH  
if [ ! $? == 0 ]; then  
   echo -e "\e[31mKERNEL FAILED TO DECOMPRESS!\e[0m"  
   exit 1 
else  
   echo -e "\e[32mKernel Decompressed Succesfully\e[0m" 
fi  
#Hash the new kernel for checking 
md5sum $CKPATH $DKPATH > $BTPATH/check.md5  
if [ ! $? == 0 ]; then  
   echo -e "\e[31mMD5 GENERATION FAILED!\e[0m"  
else 
   echo -e "\e[32mMD5 generated Succesfully\e[0m" 
fi  
#Exit 
exit 0 

Make it executable:

sudo chmod +x auto_decompress_kernel

14 Decompress the rpi kernel

We’ll add a script 999_decompress_rpi_kernel in /mnt/principal/etc/apt/apt.conf.d/ folder to decompress the rpi kernel

$ cd /mnt/principal/etc/apt/apt.conf.d/
$ vi 999_decompress_rpi_kernel

Content:

DPkg::Post-Invoke {"/bin/bash /boot/firmware/auto_decompress_kernel"; };

Make it executable:

sudo chmod +x /mnt/principal/etc/apt/apt.conf.d/999_decompress_rpi_kernel

15 Turn off rpi

Switch off the Raspberry Pi and unplug the microSD card. Then boot from USB 2.

USB 3 booting

For the first time, I was unable to boot via USB 3 (blue usb port on rpi) So I put the disk to USB 2. Then, it booted.

When my disk was connected to USB 2, the speed results was:

HDParm disk read: 32 MB /sec  
HDParm cached disk read: 33 MB/sec   
DD Disk write 27 MB/sec   

The fix for USB 3 is described in article https://jamesachambers.com/raspberry-pi-4-ubuntu-20-04-usb-mass-storage-boot-guide/comment-page-2 in section Fix (some) USB Adapter Problems Using Quirks

$ sudo lsusb

My adapter is Bus 001 Device 003: ID 152d:1576 JMicron Technology Corp. / JMicron USA Technology Corp. So my adapter id is 152d:1576

sudo vi /boot/firmware/cmdline.txt 

Add the following entry into the very front of cmdline.txt (insert space after this command):

usb-storage.quirks=152d:1576:u

Now shutdown the rpi. Plug the ssd disk to USB 3 port (blue one) and plugin power to start rpi.

You can check your disk read/write performance via:

sudo hdparm -Tt /dev/sda2

Alternatively, you can use this script:

sudo curl https://raw.githubusercontent.com/TheRemote/PiBenchmarks/master/Storage.sh | sudo bash

My results (USB 3 after the fix):

HDParm disk read: 252 MB /sec
HDParm cached disk read: 195 MB/sec
DD Disk write 90 MB/sec

As we can see, this is not the full speed of SSD, but it is pretty good improvement compared to microSD card. And the biggest advantage is the longer lifespan of storage medium.

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