Skip to content

Commit a956b9b

Browse files
authored
Merge pull request raspberrypi#406 from liamfraser/master
Add pxetools for easy netboot server. Improve netboot documentation.
2 parents cbdc466 + 14425da commit a956b9b

File tree

7 files changed

+385
-16
lines changed

7 files changed

+385
-16
lines changed
-65.6 KB
Binary file not shown.

hardware/raspberrypi/bootmodes/msd.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
This tutorial explains how to boot your Raspberry Pi 3 from a USB mass storage device such as a flash drive or USB hard disk. Be warned that this feature is experimental and may not work with all USB mass storage devices.
33

44
## Program USB Boot Mode
5-
Before a Pi will network boot, it needs to be booted with a config option to enable USB boot mode. Enabling this config option requires a special `start.elf` and `bootcode.bin` file.
5+
Before a Pi will network boot, it needs to be booted with a config option to enable USB boot mode. Enabling this config option requires a special `start.elf` and `bootcode.bin` file. These can be installed by using the "next" branch on rpi-update.
66

77
Go to the [Downloads page](https://www.raspberrypi.org/downloads/raspbian/) and install Raspbian onto an SD card using `Win32DiskImager` if you are on Windows, or `dd` if you are on Linux/Mac. Boot the Pi.
88

9-
First, prepare the `/boot` directory with new `start.elf` and `bootcode.bin` files:
9+
First, prepare the `/boot` directory with experimental boot files:
1010
```
11-
cd /boot
12-
sudo rm start.elf bootcode.bin start_* fixup*
13-
sudo wget https://github.com/raspberrypi/documentation/raw/master/hardware/raspberrypi/bootmodes/start.elf
14-
sudo wget https://github.com/raspberrypi/documentation/raw/master/hardware/raspberrypi/bootmodes/bootcode.bin
15-
sudo sync
11+
# If on raspbian lite you need to install rpi-update before you can use it:
12+
$ sudo apt-get update; sudo apt-get install rpi-update
13+
$ sudo BRANCH=next rpi-update
1614
```
1715

1816
Then enable USB boot mode with:
@@ -37,7 +35,6 @@ Now that your Pi 3 is USB boot-enabled, we can prepare a USB storage device to b
3735
We will start by using parted to create a 100MB fat32 partition, followed by a Linux ext4 partition that will take up the rest of the disk.
3836

3937
```
40-
sudo umount /dev/sda
4138
sudo parted /dev/sda
4239
4340
(parted) mktable msdos
@@ -70,9 +67,25 @@ sudo mkdir /mnt/target
7067
sudo mount /dev/sda2 /mnt/target/
7168
sudo mkdir /mnt/target/boot
7269
sudo mount /dev/sda1 /mnt/target/boot/
70+
sudo apt-get update; sudo apt-get install rsync
7371
sudo rsync -ax --progress / /boot /mnt/target
7472
```
7573

74+
Regenerate ssh host keys:
75+
```
76+
cd /mnt/target
77+
sudo mount --bind /dev dev
78+
sudo mount --bind /sys sys
79+
sudo mount --bind /proc proc
80+
sudo chroot /mnt/target
81+
rm /etc/ssh/ssh_host*
82+
dpkg-reconfigure openssh-server
83+
exit
84+
sudo umount dev
85+
sudo umount sys
86+
sudo umount proc
87+
```
88+
7689
Edit `/boot/cmdline.txt` so that it uses the USB storage device as the root filesystem instead of the SD card.
7790

7891
```
@@ -86,6 +99,7 @@ sudo sed -i "s,/dev/mmcblk0p,/dev/sda," /mnt/target/etc/fstab
8699

87100
Finally, unmount the target filesystems, and power off the Pi.
88101
```
102+
cd ~
89103
sudo umount /mnt/target/boot
90104
sudo umount /mnt/target
91105
sudo poweroff

hardware/raspberrypi/bootmodes/net_tutorial.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
This tutorial is written to explain how to set up a simple DHCP / TFTP server which will allow you to boot a Raspberry Pi 3 from the network. The tutorial assumes you have an existing home network, and want to use a Raspberry Pi for the **server**. You will need a second Pi 3 as a **client** to be booted. Only one SD card is needed because the **client** will be booted from the **server** after the initial client configuration.
33

44
## Client configuration
5-
Before a Pi will network boot, it needs to be booted with a config option to enable USB boot mode. Enabling this config option requires a special `start.elf` and `bootcode.bin` file.
5+
Before a Pi will network boot, it needs to be booted with a config option to enable USB boot mode. Enabling this config option requires a special `start.elf` and `bootcode.bin` file. These can be installed by using the "next" branch on rpi-update.
66

77
Install Raspbian lite (or heavy if you want) from the [Downloads page](https://www.raspberrypi.org/downloads/raspbian/) onto an SD card using `Win32DiskImager` if you are on Windows, or `dd` if you are on Linux/Mac. Boot the **client** Pi.
88

99
### Program USB Boot Mode
10-
First, prepare the `/boot` directory with new `start.elf` and `bootcode.bin` files:
10+
First, prepare the `/boot` directory with experimental boot files:
1111
```
12-
cd /boot
13-
sudo rm start.elf bootcode.bin start_* fixup*
14-
sudo wget https://github.com/raspberrypi/documentation/raw/master/hardware/raspberrypi/bootmodes/start.elf
15-
sudo wget https://github.com/raspberrypi/documentation/raw/master/hardware/raspberrypi/bootmodes/bootcode.bin
16-
sudo sync
12+
# If on raspbian lite you need to install rpi-update before you can use it:
13+
$ sudo apt-get update; sudo apt-get install rpi-update
14+
$ sudo BRANCH=next rpi-update
1715
```
1816

1917
Then enable USB boot mode with:
@@ -45,10 +43,17 @@ sudo rsync -xa --progress --exclude /nfs / /nfs/client1
4543

4644
Regenerate ssh host keys on client filesystem by chrooting into it:
4745
```
48-
sudo chroot /nfs/client1
46+
cd /nfs/client1
47+
sudo mount --bind /dev dev
48+
sudo mount --bind /sys sys
49+
sudo mount --bind /proc proc
50+
sudo chroot .
4951
rm /etc/ssh/ssh_host_*
5052
dpkg-reconfigure openssh-server
5153
exit
54+
sudo umount dev
55+
sudo umount sys
56+
sudo umount proc
5257
```
5358

5459
You need to find the settings of your local network. You need to find the address of your router (or gateway), which you can find with:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## pxetools
2+
We have created a Python script that is used internally to quickly set up Pi's that will network boot. It takes a serial number, which you can find in `cat /proc/cpuinfo`, an owner name and the name of the Pi. It then creates a root filesystem for that Pi from a raspbian image. There is also a --list option which will print out the IP address of the Pi, and remove an option. The follwing instructions describe how to set up the environment required by the script starting from a fresh Raspbian lite image. It might be a good idea to mount a hard disk or flash drive on /nfs so that your SD card isn't providing filesystems to multiple Pi's. This is left as an exercise for the reader.
3+
4+
```
5+
sudo raspi-config
6+
# Pick expand filesystem option
7+
# Finish
8+
# Reboot
9+
10+
sudo wget https://raw.githubusercontent.com/raspberrypi/documentation/master/hardware/raspberrypi/bootmodes/pxetools/prepare_pxetools
11+
bash prepare_pxetools
12+
13+
prepare_pxetools should prepare everything you need to use pxetools
14+
```
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
sudo apt-get update; sudo apt-get -y upgrade
6+
sudo apt-get install -y rpi-update
7+
sudo BRANCH=next rpi-update
8+
9+
sudo apt-get install python3 python3-pip ipcalc
10+
sudo pip3 install tabulate
11+
12+
# Get network info
13+
NAMESERVER=$(cat /etc/resolv.conf | grep nameserver | head -n 1 | cut -d " " -f2)
14+
GATEWAY=$(ip -4 route | grep default | cut -d " " -f3)
15+
IP=$(ifconfig eth0 | grep "inet addr" | cut -d " " -f 12 | cut -d ":" -f 2)
16+
BRD=$(ifconfig eth0 | grep "inet addr" | cut -d " " -f 14 | cut -d ":" -f 2)
17+
NETMASK=$(ifconfig eth0 | grep "inet addr" | cut -d " " -f 16 | cut -d ":" -f 2)
18+
NETWORK=$(ip -4 addr show dev eth0 | grep inet | cut -d " " -f6 | xargs ipcalc | grep Network | cut -d " " -f4)
19+
20+
echo "IP: $IP"
21+
echo "Netmask: $NETMASK"
22+
echo "Broadcast: $BRD"
23+
echo "Nameserver: $NAMESERVER"
24+
echo "Gateway: $GATEWAY"
25+
26+
echo "Setting static IP using above information"
27+
28+
cat << EOF | sudo tee /etc/network/interfaces
29+
auto lo
30+
iface lo inet loopback
31+
32+
auto eth0
33+
iface eth0 inet static
34+
address $IP
35+
netmask $NETMASK
36+
gateway $GATEWAY
37+
EOF
38+
39+
sudo systemctl restart networking
40+
41+
# In case it is already set
42+
sudo chattr -i /etc/resolv.conf
43+
44+
echo "Setting nameserver"
45+
cat << EOF | sudo tee /etc/resolv.conf
46+
nameserver $NAMESERVER
47+
EOF
48+
49+
# Prevent DNSMasq from changing
50+
sudo chattr +i /etc/resolv.conf
51+
52+
sudo apt-get install -y nfs-kernel-server dnsmasq iptables-persistent unzip nmap kpartx
53+
54+
sudo mkdir -p /nfs
55+
sudo mkdir -p /tftpboot
56+
sudo cp -r /boot /tftpboot/base
57+
sudo chmod -R 777 /tftpboot
58+
59+
echo "Writing dnsmasq.conf"
60+
cat << EOF | sudo tee /etc/dnsmasq.conf
61+
port=0
62+
dhcp-range=$BRD,proxy
63+
bind-interfaces
64+
log-dhcp
65+
enable-tftp
66+
log-facility=/var/log/dnsmasq
67+
tftp-root=/tftpboot
68+
pxe-service=0,"Raspberry Pi Boot"
69+
EOF
70+
71+
# Flush any rules that might exist
72+
sudo iptables -t raw --flush
73+
74+
# Create the DHCP_clients chain in the 'raw' table
75+
sudo iptables -t raw -N DHCP_clients || true
76+
77+
# Incoming DHCP, pass to chain processing DHCP
78+
sudo iptables -t raw -A PREROUTING -p udp --dport 67 -j DHCP_clients
79+
80+
# Deny clients not in chain not listed above
81+
sudo iptables -t raw -A DHCP_clients -j DROP
82+
83+
sudo iptables-save | sudo tee /etc/iptables/rules.v4
84+
85+
# Start services
86+
sudo systemctl enable dnsmasq
87+
sudo systemctl restart dnsmasq
88+
sudo systemctl enable nfs-kernel-server
89+
sudo systemctl restart nfs-kernel-server
90+
sudo systemctl enable rpcbind
91+
sudo systemctl restart rpcbind
92+
93+
echo "Getting latest Raspbian lite image to use as NFS root"
94+
# Get latest Raspbian lite image
95+
sudo mkdir -p /nfs/bases
96+
cd /nfs/bases
97+
sudo wget -O raspbian_latest.zip https://downloads.raspberrypi.org/raspbian_lite_latest
98+
sudo unzip raspbian_latest.zip
99+
sudo rm raspbian_latest.zip
100+
101+
sudo wget -O /usr/local/sbin/pxetools https://raw.githubusercontent.com/raspberrypi/documentation/master/hardware/raspberrypi/bootmodes/pxetools/pxetools
102+
sudo chmod +x /usr/local/sbin/pxetools
103+
104+
sudo sed -i "s,LAN = \"10.3.14.0/24\",LAN = \"$NETWORK\"," /usr/local/sbin/pxetools
105+
sudo sed -i "s,NFS_IP = \"10.3.14.18\",NFS_IP = \"$IP\"," /usr/local/sbin/pxetools
106+
107+
echo "Now run sudo pxetools --add \$serial"

0 commit comments

Comments
 (0)