Skip to content

Instantly share code, notes, and snippets.

@rickbassham
Last active August 7, 2023 19:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickbassham/bad0a6ef53d567acae5cf6be04d6d7b5 to your computer and use it in GitHub Desktop.
Save rickbassham/bad0a6ef53d567acae5cf6be04d6d7b5 to your computer and use it in GitHub Desktop.

Raspberry Pi 4 Boot Ubuntu from USB SSD

This is a copy/paste guide to booting Ubuntu 20.04 from an SSD on a Raspberry Pi 4.

The majority of these commands came from this forum post: https://www.raspberrypi.org/forums/viewtopic.php?t=278791

Prep-work

This section only needs to be done the first time for a given Raspbery Pi 4. It flashes the EEPROM on the device to support booting from USB.

Install Raspbian

Using an SD card, boot into Raspbian (Raspberry Pi OS) as usual.

sudo apt update
sudo apt full-upgrade
sudo sed -i 's/FIRMWARE_RELEASE_STATUS="critical"/FIRMWARE_RELEASE_STATUS="stable"/' /etc/default/rpi-eeprom-update
sudo rpi-eeprom-update -d -a
sync
sudo reboot
vcgencmd bootloader_version
sudo shutdown -h now

We are now done with Raspbian. Remove your SD card from the Raspberry Pi at this time.

Flash Ubuntu to SSD

Using your method of choice, flash Ubuntu 20.04.1 LTS to your SSD.

After flashing your SSD, before moving it to your Raspberry Pi, do the following in the system-boot partition.

Note, if you get an error with zcat on a Mac, use brew to install coreutils and use gzcat instead of zcat.

zcat vmlinuz > vmlinux

cat << 'EOF' | sudo tee -a usercfg.txt
[pi4]
max_framebuffers=2
dtoverlay=vc4-fkms-v3d
boot_delay
kernel=vmlinux
initramfs initrd.img followkernel
EOF

cat << 'EOF' | sudo tee auto_decompress_kernel
#!/bin/bash -e

#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
EOF

chmod +x auto_decompress_kernel

You can now eject your SSD, and plug it into your Raspberry Pi. Remember, do NOT put your SD card in the Raspberry Pi. Turn it on.

Auto Decompress Kernels

Now, ssh to your Raspberry Pi. By default it will be ssh ubuntu@ubuntu.

Once booted and you are ssh'ed in, we need to add an apt trigger to auto decompress kernels as they are installed, otherwise you won't be able to boot after a kernel upgrade.

cat << 'EOF' | sudo tee /etc/apt/apt.conf.d/999_decompress_rpi_kernel
DPkg::Post-Invoke {"/bin/bash /boot/firmware/auto_decompress_kernel"; };
EOF

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

That's it. You should be able to boot from your SSD, and enjoy a much faster experience with your Raspberry Pi 4.

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