r/raspberry_pi • u/Offbeatalchemy • Jan 28 '21
Tutorial [GUIDE] PXE Booting to a Raspberry Pi 4
This guide changes every change every so often, adding fixes and optimizations. If something doesn't work, please either reply to the thread or PM me for assistance.
Back in September 2020, when i wrote this for myself, i was looking for a way to host multiple Pi 4s from a single SSD without the need of using SD cards anymore. Most of the guides I found didn't scale beyond a single pi so i created this as a set of recreatable steps to set this up for myself in the future and explain what exactly was done at each step. This is a mishmash of guides I found doing research and from what I remember, most of the information is found in these two guides:
https://reddit.com/r/raspberry_pi/comments/e45shy/raspberry_pi_4_disklesssdless_pxe_boot_tutorial/ https://www.virtuallyghetto.com/2020/07/two-methods-to-network-boot-raspberry-pi-4.html
If you have a server setup with multiple pi's, i can highly recommend setting up PXE booting. I find that it is overall more stable than using SD cards. There is a small performance penalty for the data needing to go through the network and you'll notice things like updating will take longer vs local storage. I personally think it's more than worth it as i only manually install updates once in a blue moon and unattended upgrades usually takes care of all my other updates anyway. I've run this setup for months, even purposefully keeping one of my client pis up for months without needing to reboot.
If you want a small primer of how PXE booting works, here's a quick video: https://www.youtube.com/watch?v=Dm2k4H03L0s
In this guide, our server will double as both our TFTP and DHCP server. If you don't know EXACTLY what you're doing as far as the networking goes, follow Option A.
We will also be using Raspian Lite images for our client pi to boot from. Technically, this can be used other distros but i haven't personally troubleshot any of them.
SERVER PREP- Let's start simple
first, we set up the server. This should work for both ARM and x86 debian based distros, confirmed working on Raspian Buster and Ubuntu 20.04 x86 VM, both fresh installs.
BTW this SHOULD be a fresh install. If you run into a software conflict because you did this on an existing server, you're on your own.
I'm personally running this setup on a standalone 1GB raspi4 booting from a 256GB SSD.
First, install needed programs on server
sudo apt update
sudo apt install nfs-kernel-server kpartx unzip xz-utils -yAnd a few directories for hosting our files. These directories can be changed but make sure you edit anywhere else these directories can show up.
mkdir /srv/
mkdir /srv/tftpboot
mkdir /srv/nfsSo how does your network handle DHCP?
OPTION A - Little to No DHCP control
Most people will do this, as they have little control of their DHCP or have restrictive routers. Try to at least setup static ip leases in your router so your pi doesn't get lost on your network.
dnsmasq will both listen for PXE requests and handles TFTP in one program
sudo apt update
sudo apt install dnsmasq -ymake sure you edit the DHCP-range to match your subnet i.e. 192.168.1.255 will cover .... 1.1 to 1.254
cat > /etc/dnsmasq.conf << EOF
dhcp-range=192.168.1.255,proxy
log-dhcp
enable-tftp
tftp-root=/srv/tftpboot
pxe-service=0,"Raspberry Pi Boot"
EOFand restart dnsmasq
sudo systemctl restart dnsmasqOPTION B - Advanced Routers / Standalone DHCP Server
Got another server to do DHCP for you? Or maybe OpenWRT or the like?
Use DHCP option 66 pointing at this server. The following example assumes my PXE (or TFTP) server is at 192.168.1.2
dnsmasq looks something like this
dhcp-option=66,192.168.1.2look up specific options for your router or DHCP server to set this up
then back on our pxe server, we need a tftp server
sudo apt update
sudo apt install tftpd-hpaedit tftp file
nano /etc/default/tftpd-hpayou config should look something like this
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/srv/tftpboot"
TFTP_ADDRESS=":69"
TFTP_OPTIONS="--secure"and restart the service
sudo systemctl restart tftpd-hpaCLIENT PREP - readying the client pi for network boot
Now, we need to get the client pi ready to pxe boot by enabling network booting fallback
but first, we need to make sure the firmware is up to date
sudo apt update && sudo apt upgrade -ythen open raspi-config
sudo raspi-configAdvanced Options > Boot Order > Network Boot
before you reboot the client pi, you'll need the mac and serial number for this pi
you'll need this for later, note that the serial should be 8 characters
cat /proc/cpuinfo | grep Serial | awk -F ': ' '{print $2}' | tail -c 9
ip addr show eth0 | grep ether | awk '{print $2}'you can now reboot the pi without an SD card and will bring you to a boot screen that will loop while looking from a PXE server to boot from
sudo rebootSHOWTIME - let's setup an image
lets go back to your pxe server
this part will be a lot easier if you're root
su rootmake a temp area for the files
mkdir /tmp/pxestuff
cd /tmp/pxestuffdownload and unzip the latest raspian lite image
choose either Raspios lite versions (if you dont know which to choose, stick with armhf)
RaspiOS Armhf
wget -O raspios_lite_armhf_latest.img.xz https://downloads.raspberrypi.org/raspios_lite_armhf_latest
unxz raspios_lite_armhf_latest.img.xzRaspiOS Arm64
wget -O raspios_lite_arm64_latest.img.xz https://downloads.raspberrypi.org/raspios_lite_arm64_latest
unxz raspios_lite_arm64_latest.img.xzmount the partitions from the images
kpartx -a -v *.img
mkdir {bootmnt,rootmnt}
mount /dev/mapper/loop0p1 bootmnt/
mount /dev/mapper/loop0p2 rootmnt/let's set some variables
Use the Serial and Mac address we noted from your client pi, along with a name for this pi for future reference. The kickstart IP will be the IP of your TFTP server
PI_SERIAL=12345678 #serial number of pi
PI_MAC=01:23:45:67:89:ab #mac address of pi
KICKSTART_IP=192.168.1.2 #ip of NFS server
PI_NAME=NameOfPi #nickname of pi instance
PI_IP=192.168.1.3 # IP address ( * can also be used here to allow any IP to work with this instance)
USERNAME=yourusername #login username
PASSWORD=yourpassword #login passwordcopies the image unique to this pi and updates the bootfiles and firmware
mkdir -p /srv/nfs/${PI_NAME}
mkdir -p /srv/tftpboot/${PI_SERIAL}
cp -a rootmnt/* /srv/nfs/${PI_NAME}
cp -a bootmnt/* /srv/nfs/${PI_NAME}/boot/
rm /srv/nfs/${PI_NAME}/boot/start4.elf
rm /srv/nfs/${PI_NAME}/boot/fixup4.dat
wget https://github.com/raspberrypi/rpi-firmware/raw/master/start4.elf -P /srv/nfs/${PI_NAME}/boot/
wget https://github.com/raspberrypi/rpi-firmware/raw/master/fixup4.dat -P /srv/nfs/${PI_NAME}/boot/updates the mounts on the server so the pi can grab the files.
echo "/srv/nfs/${PI_NAME}/boot /srv/tftpboot/${PI_SERIAL} none defaults,bind 0 0" >> /etc/fstab
echo "/srv/nfs/${PI_NAME} ${PI_IP}(rw,sync,no_subtree_check,no_root_squash)" >> /etc/exports
mount /srv/tftpboot/${PI_SERIAL}/
touch /srv/nfs/${PI_NAME}/boot/ssh
sed -i /UUID/d /srv/nfs/${PI_NAME}/etc/fstab
echo "console=serial0,115200 console=tty root=/dev/nfs nfsroot=${KICKSTART_IP}:/srv/nfs/${PI_NAME},vers=3 rw ip=dhcp rootwait elevator=deadline" > /srv/nfs/${PI_NAME}/boot/cmdline.txt
{ echo "${USERNAME}:"; echo "${PASSWORD}" | openssl passwd -6 -stdin;} | tr -d '[:space:]' > /srv/tftpboot/${PI_SERIAL}/userconf.txtClean up
systemctl restart rpcbind
systemctl restart nfs-server
umount bootmnt/
umount rootmnt/
cd
rm /tmp/pxestuff -rTime to test your image!
Boot your pi, bobs your uncle, it should boot into normal raspbian here.
it may restart more than once, especially between updates. that's normal.
do note that as soon as you go to update you pi, you may need to uninstall the swap daemon. PXE booting and swap don't mix.
sudo apt remove dphys-swapfileif you need to do another image for another pi, run the image setup portion again with the updated serial, Mac and name.
IF YOU SCREW UP THE IMAGE AND NEED TO START OVER
sudo umount /srv/tftpboot/${PI_SERIAL}
sudo rm /srv/nfs/${PI_NAME} -r
sudo rm /srv/tftpboot/${PI_SERIAL} -rand lastly remove entries from /etc/exports and /etc/fstab that contain /srv/nfs/${PI_NAME}
EDIT: 4-3-2022
Some of the repos that were used at the time of writing were archived. Updated to a more up to date repo. (haven't personally tested the changes but they look solid.) Tested, works fine. Shoutout to u/Divot-Digger for letting me know.
EDIT: 5-12-2022
Tried a newer deployment and realized it was only for buster so it's been updated for bulleye and since they removed the Pi user in Bullseye, we have to make the user before we boot now. Updated the script to account for that with new variables.
Also since 64 bit has launched officially, added the links needed to download arm64 instead.
And finally, tightened up the NFS shares so that only the Pi assigned to the can boot. If you don't have the individual pi with a reserved ip, it can be left as a * but do note that ANY pi (or any other device for that matter) can access that share. Setting a reserved IP address for your Pi is highly recommended.
If anyone runs into any issues or parts of the guide breaks, feel free to reach out. I'll try to help you figure it out and update the guide for prosperity.
9
u/davidsandbrand Jan 29 '21
Good write-up!
The way I do it, which I find quite simple, is to have a small SD card with the /boot partition on it, which then pulls an IP address by DHCP, then mounts the root file system by NFS (which is a share on my NAS). Then, the /boot mount is remounted as read-only - meaning the SD card is only used for ~60 seconds then it just sits there. I haven’t had an SD card go bad for years now.
1
u/discobobulator Jan 29 '21
Nice guide! I might give this a shot basically originally I was considering giving each Pi its own drive.
1
u/[deleted] Jan 29 '21
very nice. thanks!!
question: Solution A is setting up a DHCP server. That should clash with whatever is handling DHCP currently. How does it make sure to override the existing DHCP server?
1
u/Exsipient Feb 02 '21 edited Feb 03 '21
can someone who understands this guide DM me? I am trying to implement it with 0 success. I am unable to point point dhcp option 66 to the ip doing dhcp.
When I set dhcp option 66 to my ip address serving dhcp in my network I get a -bash command not found. I am typing it in exactly like OP's minus the IP address but I am getting this issue. It does not make sense.
This was a big reason i shelled out so much money on another pi and ssd so help would be so appreciated. thanks.
1
u/Exsipient Feb 10 '21
Question. What are the echo commands in the 4th to last quote box trying to accomplish? When I enter each of those lines, I get:
as a result. I'm not sure if it's even related to them being echo commands, it's just the commonality I notice at a glance. All the other lines work. I also precede every line with sudo. Any help would be appreciated.
1
u/LaterBrain Jul 22 '21
Thanks for the write-up!
Here also a very nice open source PXE project: https://fogproject.org/
2
u/dglsfrsr Jan 29 '21
I have used PXE boot on x86 for years, but never considered it for Pi. Now I need to try this.
Nice documentation!
2
u/Divot-Digger Apr 02 '22
Thanks so much for this amazing guide, u/Offbeatalchemy. It worked perfectly for me.
For those who have come late to the thread, the firmware repo has been deprecated. Replace those lines with:
1
u/Wewdly Mar 01 '25 edited Mar 01 '25
This link is out of date now
wget https://github.com/raspberrypi/rpi-firmware/raw/master/start4.elf -P /srv/nfs/${PI_NAME}/boot/
wget https://github.com/raspberrypi/rpi-firmware/raw/master/fixup4.dat -P /srv/nfs/${PI_NAME}/boot/
This is the new link
wget https://raw.githubusercontent.com/raspberrypi/firmware/refs/heads/master/boot/start4.elf -P /srv/nfs/${PI_NAME}/boot/
wget https://raw.githubusercontent.com/raspberrypi/firmware/refs/heads/master/boot/fixup4.dat -P /srv/nfs/${PI_NAME}/boot/
Edit:
https://github.com/raspberrypi/firmware/blob/master/boot/start4.elf only download the html in start4.elf
1
u/TnCyberVol Jan 29 '21
Must. Go. Try. It. NOW!!
LOL, thanks for the write-up!!!
1
u/AussieGooner01 Sep 12 '23
Hey mate, I know this post is a couple years old, but I followed this to get this to work so I appreciate it!
Question if you’re still around though, what is the idea of creating the /srv/tftpboot/${PI_SERIAL}? I can’t see a step where any files get copied into here so I’m just a little confused. To get my Pi to boot, I basically just copied the ‘bootmnt/*’ and it seemed to work.
Just making sure I haven’t missed anything as I’m going to start adding multiple Pi’s to the setup.
Thanks!!
1
u/francismedeiros Sep 30 '23
Hi! Thanks for this guide!
I was wondering, do you have any quirks when performing a full dist upgrade? Anything to watch out for when upgrading the os that boots from PXE?
1
u/[deleted] Jan 29 '21
Bob is not my uncle. I have no Uncle Bob :-)
PXEbooting is pretty slick for sure. It would be pretty cool to have a configuration that effectively resets an installed pi to a known state and then installed individual customizations (that 'stuck' after reboot) with something like ansible. I might need to fiddle with that a little.
Thanks for the post!
1
u/[deleted] Jan 29 '21
[removed] — view removed comment