Raspberry Pi 4 Ubuntu USB Boot (No SD Card)

43K720

Intro: Raspberry Pi 4 Ubuntu USB Boot (No SD Card)

The instructions are below, and will guide you on booting the Raspberry Pi 4 without an SD Card.

If you do not want to follow the steps, there are pre-built images on the original post. Just flash these images to a USB drive, and you are good to go (so long as you have an EEPROM that supporst USB booting -- step 3)

STEP 1: Download Ubuntu for Raspberry Pi From Ubuntu Site

Download the Ubuntu image for raspberry pi 4 form the Ubuntu official website.

https://ubuntu.com/download/raspberry-pi

STEP 2: Write Image to USB Disk

Flash the image to a USB drive. This can be a USB stick, or a USB SSD. I would recommend using Balena Etcher on Windows and MacOS. If you are using Ubuntu, the built-in Image Writer will work just fine.

https://www.balena.io/etcher/

STEP 3: Update Raspberry Pi EEPROM

For this step, there are several sub-steps. If you have already updated the Raspberry Pi EEPROM to the "stable" release, then you can skip this step.

First, you must write the RaspberryPiOS image (https://www.raspberrypi.org/downloads/raspberry-pi-os/) to an SD card.

Second, boot the Raspberry Pi, and edit the /etc/default/rpi-eeprom-update file by typing

sudo nano /etc/default/rpi-eeprom-update

and change the "FIRMWARE_RELEASE_STATUS" entry from critical to stable.

Third, run

sudo rpi-eeprom-update -a 

from the terminal, and allow the update to finish.

See https://www.raspberrypi.org/documentation/hardware/raspberrypi/booteeprom.md for further details if you need them

STEP 4: Update Raspberry Pi Firmware

Download the updated firmware files from the raspberry pi github site (https://github.com/raspberrypi/firmware/tree/master/boot).

Copy all *.dat and *.elf files to the Ubuntu boot partition on the USB driver flashed with the Ubuntu image from step 2. (Overwrite the files that were previously there)

STEP 5: Decompress the Kernel

The Raspberry Pi 4 bootloader cannot take a compressed kernel image. You must manually decompress this before your first boot.

To do this on linux, open the boot partition of the Ubuntu USB and run

zcat vmlinuz > vmlinux 

from the terminal.

You can do this on Windows using 7-zip and extracting the vmlinuz file. Just be sure to rename the extracted file to vmlinux.

STEP 6: Update the Config.txt File

The config.txt file has the startup options for the various RaspberryPi boards. Update the information for the Raspberry Pi 4. Replace the section for [pi4] with the following:

[pi4]
max_framebuffers=2 dtoverlay=vc4-fkms-v3d boot_delay kernel=vmlinux initramfs initrd.img followkernel

STEP 7: Create Auto-Decompression Script

During an update to Ubuntu or one of its many packages, apt will create a new kernel image. This image will be compressed, and will cause the Raspberry Pi not to boot after the update. In order to fix this, a script needs to be created to decompress the new kernel images after updates.

Create a script called auto_decompress_kernel in the boot partition. This can be done with most text editors. In Linux, I would recommend either nano or Atom, in Windows I would recommend Atom (Note for you Windows users using Text Edit, be sure to remove the "TXT" file extension. If you do not, this will not work). The script should contain the following code:

#!/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<br>

STEP 8: Create Another Script

In order for the script that we just created to get called every time a package is installed, we need to create another script.

This script needs to be created within the Ubuntu filesystem. If you are doing this setup on a linux system, you can perform this part before your first boot, if you are on Windows or MacOS, you will need to do this after your first boot.

Create this script in the /etc/apt/apt.conf.d/ directory, and name it 999_decompress_rpi_kernel

sudo nano /etc/apt/apt.conf.d/999_decompress_rpi_kernel

The code should be:

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

Once this has been created, you will need to make the script executable. This can be done using the following code:

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

STEP 9: Enjoy Ubuntu on the Raspberry Pi 4

Now you can boot Ubuntu on a USB enabled drive.

10 Comments

I tried both with a fresh Ubuntu server and a server that was use for a month and customised. The server is headless. Both boot but use 100% cpu. Sometimes I can ssh into it, but mostly not. When I put an SD card into the slot, all activity stops. It even loses network connection. As soon as the SD card is removed the cpu is again 100% busy. The green led lights constantly. What did I miss?
[solved] Boot hang (3 / 4 minutes) - Linux Mint Forums it wil hang 5 minutes on first boot!

Btw I used raspbian on the pi to change the files on the usb, which seemed inpossible on Windows, on Raspbian everything worked fine.

use zcat -qf vmlinuz > vmlinux

als in script!!! zcat $CKPATH > $DKPATH
change to
zcat -qf $CKPATH > $DKPATH

and I don't believe the <br> should be in de auto_decompress_kernel file

tnx anyway it works :)
My concern is this. Why would anyone follow a tutorial that can't be bothered to date when it's written? These instructions seem quite dated and realistically they might work for 10% of people.

Example my rpi4 is not set to critical or stable. it's set to default. is my e-prom version newer than yours? Yet, you want me to update it regardless?

Seriously people, be smarter. Follow a recent tutorial with a recent date. Not one that is old or undated like this one.
Mark, that wasn't very constructive. First I note that the date is made available, it sais "Published Sep 20th, 2020". Secondly, I have found several older and less comprehensive tutorials but nothing newer at this point. Thirdly, your statement is quite discouraging for whoever made a contribution like this to write a tutorial, and it's also draining my energy to even see a comment like this.
I removed the `<br>` segment, which probably is an HTML artifact rather than relevant code. Both in step 7 and step 8
Just before seeing this post, I had finished installing Ubuntu 64 Server on a Pi 4 set up for USB booting. I used the Pi imager to write the USB-SSD card. The system seems to work fine without the manual decompression process. I did note that you had to do a manual reboot to point to the upgraded kernel. Is is still necessary to add the script for continuous decompression of kernel updates?
After installing Ubuntu on my SSD, I can't access it on my computer. How am I supposed to do step 4? Little confusing I must say.
how to uninstall ubuntu bootable files from an SSD drive? I've tried right clicking and choosing "format" but it's still showing the files. Would like to use the SSD drive for somthing else so want to reformat it. Thanks
I guess you're on Windows? You can delete them in stead of formatting the drive. It will only clean up part of the drive, though, because the boot-files are on a separate (small) partition.
You might try right-clicking on "This PC" in Explorer and select "Manage". Then, go to "Disk Management". Here you can remove and recreate partitions.
BE CAREFUL though, you can easily destroy data on any and all of your hard drives. Google how to use that application if you haven't done it before!
Had some issues C&P-ing the script. As always, has to do with end-of-line characters.
Easy to solve, though: just run 'dos2unix <scriptname>'.
Is 'dos2unix' not installed? 'sudo apt install dos2unix'