-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-dhcp.sh
executable file
·83 lines (71 loc) · 2.37 KB
/
start-dhcp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
INVENTORY="inventory.yaml"
MATCHBOX_HOST="matchbox.lan"
MATCHBOX_PORT="8080"
MATCHBOX_URL="http://${MATCHBOX_HOST}:${MATCHBOX_PORT}/"
MATCHBOX_IP="10.10.10.1"
# use provided BASE_INTERFACE or use first ether interface
BASE_INTERFACE=${BASE_INTERFACE:-$(ip -j l l | jq '[.[]|select(.link_type=="ether")][0] | .ifname')}
INTERFACE="vlan0"
NTP_SERVER="10.10.10.1"
DHCP_START="10.10.10.10"
DHCP_END="10.10.10.100"
HOSTSFILE="hosts.rpi4"
function inc_ip() {
read -r A B C D <<<"${1//./ }"
(( D += 1 ))
echo "$A.$B.$C.$D"
}
function write_hostsfile() {
[ -f "$HOSTSFILE" ] && rm $HOSTSFILE
CUR_IP="$DHCP_START"
readarray hosts < <(yq -o=j -I=0 '.hosts[]' ${INVENTORY} )
for host in "${hosts[@]}"; do
HOST=$(echo "$host" | yq '.hostname' -)
MAC=$(echo "$host" | yq '.mac' -)
SERIAL=$(echo "$host" | yq '.serial' -)
echo "$MAC,${CUR_IP},$HOST" >> $HOSTSFILE
# symlink_uefi
ln -sf rpi4uefiboot "tftpboot/$SERIAL"
# increment ip... TODO check bounds
CUR_IP=$(inc_ip "$CUR_IP")
done
}
# check if the configured $INTERFACE exists
CHECK_INTERFACE=$(ip -j l l | jq ".[] | select(.ifname == \"${INTERFACE}\")")
if [ -z "$CHECK_INTERFACE" ]; then
echo "Please create $INTERFACE, e.g:
sudo ip l a link $BASE_INTERFACE type vlan id 200
sudo ip l l
sudo ip a a 10.10.10.1/24 dev $INTERFACE
sudo ip l s $INTERFACE up
sudo firewall-cmd --change-zone=$INTERFACE --zone=trusted
"
exit 1
fi
# download firmware
./download-firmware.sh
# write hostsfile and symlink uefi firmware for hosts in inventory
write_hostsfile
# run dnsmasq
sudo podman run -ti --rm --cap-add=NET_RAW,NET_ADMIN --net=host -v "$PWD/tftpboot":/var/lib/tftpboot:Z -v "$PWD/hosts.rpi4":/etc/hosts.rpi4:Z ghcr.io/toanju/dnsmasq-rpi:latest \
--no-daemon \
--interface=${INTERFACE} \
--bind-dynamic \
--enable-tftp \
--tftp-root=/var/lib/tftpboot \
--log-queries \
--log-dhcp \
--dhcp-range=${DHCP_START},${DHCP_END} \
--dhcp-hostsfile=/etc/hosts.rpi4 \
--dhcp-option=option:ntp-server,${NTP_SERVER} \
--dhcp-ignore=tag:!known \
--dhcp-mac=set:rpi,e4:5f:01:*:*:* \
--dhcp-mac=set:rpi,dc:a6:32:*:*:* \
--pxe-service=tag:rpi,0,"Raspberry Pi Boot" \
--dhcp-match=set:armefi64,option:client-arch,11 \
--dhcp-boot=tag:armefi64,ipxe.arm64.efi \
--dhcp-userclass=set:ipxe,iPXE \
--dhcp-boot=tag:ipxe,${MATCHBOX_URL}boot.ipxe \
--no-hosts \
--address=/${MATCHBOX_HOST}/${MATCHBOX_IP}