Kroko Just another WordPress weblog

July 11, 2020

PXE Boot Raspberry Pi4 with Mikrotik* DHCP and TFTP server.

Filed under: Uncategorized — admin @ 9:53 pm

*You also need another machine to provide NFS share.

Setup Mikrotik DHCP and optional TFTP server:

192.168.0.28 – is the TFTP server form linux, but you can use TFTP server that is already installed on mikrotik router.

192.168.0.153 – is your Pi4 ip

serial of your pi can be found with this command: cat /sys/firmware/devicetree/base/serial-number|tail -c 9

On Mikrotik router:

/ip dhcp-server option add code=43 name=pi4-43 value="s' Raspberry Pi Boot'"

/ip dhcp-server option add code=60 name=pi-60 value="s'PXEClient'"

/ip dhcp-server option add code=66 name=pi4-66 value="s'192.168.0.28'"

# change ip and mac address with your PI data.

/ip dhcp-server lease add address=192.168.0.153 dhcp-option=pi4-43,pi-60,pi4-66 mac-address=DC:A6:32:B1:1A:AE server=local_lan

# activate tftp

/ip tftp add real-filename=tftp/ req-filename=.*

# now enter with ftp on mikrotik and create a folder tftp. In that folder you need to create a folder with the serial of your raspbery and put inside the content of /boot folder.

Setup TFTP server  on Centos 7

yum install tftp tftp-server xinetd

nano /etc/xinetd.d/tftp

service tftp

{

socket_type= dgram

protocol= udp

wait= yes

user= root

server= /usr/sbin/in.tftpd

server_args = -s /tftpboot

disable = no

per_source= 11

cps= 100 2

flags= IPv4

}

Enable and Start TFTP Service

systemctl enable xinetd
systemctl enable tftp

systemctl start xinetd
systemctl start tftp

Setup NFS server on Centos 7:

yum install nfs-utils

systemctl enable rpcbind

systemctl enable nfs-server

systemctl enable nfs-lock

systemctl enable nfs-idmap

systemctl start rpcbind

systemctl start nfs-server

systemctl start nfs-lock

systemctl start nfs-idmap

[root@pxeserver ~]# cat /etc/exports

/nfs/pi4.1 *(rw,sync,no_subtree_check,no_root_squash)

/nfs/pi4.2 *(rw,sync,no_subtree_check,no_root_squash)

systemctl restart nfs-server

——

Prepare Raspbian image on NFS server (it’s faster that copying the files from Raspbery Pi 4 SD card)

yum install unzip kpartx

mkdir -p /nfs/pi4.1

wget https://downloads.raspberrypi.org/raspios_lite_armhf_latest

unzip raspios_lite_armhf_latest

kpartx -a -v 2020-05-27-raspios-buster-lite-armhf.img

mkdir rootmnt

mkdir bootmnt

mount /dev/mapper/loop0p2 rootmnt/

mount /dev/mapper/loop0p1 bootmnt/

cp -a rootmnt/* /nfs/pi4.1/

cp -a bootmnt/* /nfs/pi4.1/boot/

cd /nfs/pi4.1/boot

umount /dev/mapper/loop0p1

umount /dev/mapper/loop0p2

losetup -d /dev/loop0

kpartx -d /dev/loop0

sudo mkdir -p /tftpboot/d5f1ba04

echo "/nfs/pi4.1/boot /tftpboot/d5f1ba04 none defaults,bind 0 0" | sudo tee -a /etc/fstab

sudo mount /tftpboot/d5f1ba04

sudo chmod 777 /tftpboot

touch /nfs/pi4.1/boot/ssh

sed -i /UUID/d /nfs/pi4.1/etc/fstab

echo "console=serial0,115200 console=tty root=/dev/nfs nfsroot=192.168.0.28:/nfs/pi4.1,vers=3 rw ip=dhcp rootwait elevator=deadline" | sudo tee /nfs/pi4.1/boot/cmdline.txt

echo "/nfs/pi4.1 *(rw,sync,no_subtree_check,no_root_squash)" | sudo tee -a /etc/exports

exportfs -r

Turn off swap:

dphys-swapfile swapoff

If you see strange errors in dmesg related to emmc0 and you don’t need a SD card you can solve by adding this to /boot/config.txt (/nfs/pi4.1/boot/config.txt):

dtparam=sd_poll_once=on

Prepare Bootloader on Raspberry Pi4:

sudo apt-get update

sudo apt-get upgrade

wget https://github.com/raspberrypi/rpi-eeprom/raw/master/firmware/stable/pieeprom-2020-06-15.bin

rpi-eeprom-config pieeprom-2020-06-15.bin > bootconf.txt

sed -i s/0x41/0x421/g bootconf.txt

# make sure you see 0x421 on boot options after this

rpi-eeprom-config --out pieeprom-2020-06-15-netboot.bin --config bootconf.txt pieeprom-2020-06-15.bin

sudo rpi-eeprom-update -d -f ./pieeprom-2020-06-15-netboot.bin

Now reboot, let it boot again then remove the card and reboot.

Powered by WordPress