more aramo/jammy updates
[automated-distro-installer] / pxe-server
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18
19 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
20
21 x="$(readlink -f "$BASH_SOURCE")"; source "${x%/*}/bash-trace"
22
23
24 usage() {
25 cat <<EOF
26 Usage: ${0##*/} [OPTIONS] [HOST] [TYPE]
27 Configure dnsmasq boot options and fai-chboot if appropriate. This is
28 not general purpose, it has code specific to dhcp servers I run.
29
30 Without TYPE, disable server and fai server. In that case, HOST is only
31 needed for fsf office network.
32
33 HOST A hostname known to the dhcp server, or default for all.
34 TYPE One of arch, parabola, plain, fai.
35
36 -a Don't setup pxe, just Wait for 2 dhcp acks, then disable the pxe
37 server after a delay. First ack is for pxe boot, 2nd ack is
38 for os boot. Sometimes on debian, there is a 3rd one shortly
39 after the 2nd. I can't remember exactly why this caused a
40 problem, but I'm hoping the sleep will take care of it.
41 -d Don't alter dhcp config. Only make sense for fai type, and on network
42 other than home or fsf.
43 -k Pass -k to myfai-chboot.
44 -r Don't redeploy fai config. For example, if there is a different host
45 that is mid-install.
46
47 -S sets FAI_ACTION=sysinfo, see myfai-chboot for more info.
48 -w Setup pxe, then wait like -a.
49 -h|--help Print help and exit
50
51
52 Note, when switching between plain and arch or parabola, you will need to
53 do something like:
54 ssh wrt
55 cd /mnt/usb
56 rm tftpboot
57 ln -s <arch/parabola/debian iso dir> tftpboot
58
59
60 Notes on debugging pxe dhcp tftp:
61
62 For debugging dhcp, add to /etc/dnsmasq.conf: log-dhcp
63
64 Newer openwrt runs dnsmasq with a whitelist of readable files and dirs:
65
66 ps ww :
67 /sbin/ujail -t 5 -n dnsmasq -u -l -r /bin/ubus -r /etc/TZ -r /etc/dnsmasq.conf -r /etc/ethers -r /etc/group -r /etc/hosts -r /etc/passwd -w /tmp/dhcp.leases -r /tmp/dnsmasq.d -r /tmp/hosts -r /tmp/resolv.conf.d -r /usr/bin/jshn -r /usr/lib/dnsmasq/dhcp-script.sh -r /usr/share/dnsmasq/dhcpbogushostname.conf -r /usr/share/dnsmasq/rfc6761.conf -r /usr/share/dnsmasq/trust-anchors.conf -r /usr/share/libubox/jshn.sh -r /var/etc/dnsmasq.conf.cfg01411c -w /var/run/dnsmasq/ -- /usr/sbin/dnsmasq -C /var/etc/dnsmasq.conf.cfg01411c -k -x /var/run/dnsmasq/dnsmasq.cfg01411c.pid
68
69 logging tftp requests:
70 /etc/default/tftpd-hpa:
71 add -vv:
72 TFTP_OPTIONS="--secure -vv"
73 jr -u tftpd-hpa -f
74
75 Note: Uses GNU getopt options parsing style
76 EOF
77 exit $1
78 }
79
80 pre="${0##*/}:"
81 m() { printf "$pre %s\n" "$*"; "$@"; }
82 e() { printf "$pre %s\n" "$*"; }
83 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
84
85 PATH="/a/exe:$PATH"
86
87 ##### begin command line parsing ########
88
89 dhcp=true
90 redep=true
91 acks=2
92 wait=false
93 fsf=false
94
95 case $HOSTNAME in
96 x3|kw) fsf=true ;;
97 esac
98
99 chboot_args=()
100 temp=$(getopt -l help adkrSwh "$@") || usage 1
101 eval set -- "$temp"
102 while true; do
103 case $1 in
104 -a) wait=true; set=false; shift ;;
105 -d) dhcp=false; shift ;;
106 -k) chboot_args+=(-k); shift ;;
107 -r) redep=false; shift ;;
108 -S) chboot_args+=(-S); shift ;;
109 -w) wait=true; set=true; shift ;;
110 -h|--help) usage ;;
111 --) shift; break ;;
112 *) echo "$0: Internal error!" ; exit 1 ;;
113 esac
114 done
115
116 read -r host type <<<"$@"
117
118 case $# in
119 [01]);;
120 2)
121 case $type in
122 arch|parabola) cmd=archlike ;;
123 fai) cmd=fai ;;
124 *)
125 echo "$0: error expected type of arch|parabola|fai"
126 echo
127 usage 1
128 ;;
129 esac
130 ;;
131 *)
132 echo "$0: error: expected 0-2 arguments"
133 echo
134 usage 1
135 ;;
136 esac
137
138
139 if $wait && ! $dhcp; then
140 echo "$0: error -w conflicts with -d, choose one or other"
141 exit 1
142 fi
143
144
145 if [[ $host && $host != default ]]; then
146 host_tag="tag:$host,"
147 fi
148
149 ##### end command line parsing ########
150
151 archlike() {
152 cat <<EOF
153 ${host_tag}209,boot/syslinux/${type}iso.cfg
154 ${host_tag}210,/${type}/
155 ${host_tag}option:bootfile-name,/${type}/boot/syslinux/lpxelinux.0
156 EOF
157 }
158
159 plain() {
160 # if arch based was used before, this additionally needs
161 # the tftp link in /mnt/usb to be changed.
162 cat <<EOF
163 ${host_tag}option:bootfile-name,pxelinux.0
164 EOF
165 }
166
167 fai() {
168 cat <<EOF
169 ${host_tag}option:bootfile-name,pxelinux.0
170 ${host_tag}option:server-ip-address,$faiserverip
171 ${host_tag}option:tftp-server,$faiserverip
172 EOF
173 # Note, previously used normal dnsmasq option, but it requires dnsmasq
174 # restart, which causes momentary dns failures, which can bork an
175 # install.
176 #
177 # dhcp-boot=${host_tag}pxelinux.0,faiserver.b8.nz,faiserver.b8.nz
178 }
179
180 ack-wait() {
181 if $fsf; then
182 wait_cmd="ssh tarantula.office.fsf.org tail -n0 -f /var/log/syslog"
183 else
184 wait_cmd="ssh cmc logread -f"
185 fi
186 wait_count=$1
187 if [[ $host ]]; then
188 if $fsf; then
189 host_regex=" $(getent hosts kw | awk '{print $1}' | sed 's/\./\\./g')"
190 else
191 host_regex=" $host"
192 fi
193 fi
194 regex=".*DHCPACK.*$host_regex\b"
195 i=0
196 while (( i != wait_count )) && read -r line; do
197 if [[ $line =~ $regex ]]; then
198 i=$((i+1))
199 echo $line
200 fi
201 done < <($wait_cmd ||:) # tail returns 2 it seems
202 m sleep 20
203 }
204
205 set-pxe() {
206 $dhcp || return 0
207 if $fsf; then
208 if [[ ! $cmd ]]; then
209 e "removing pxe for $host on tarantula"
210 ssh tarantula.office.fsf.org bash -e <<EOF
211 sed -ri 's/^( *host +$host *\{).*/\1/' /etc/dhcp/dhcpd.conf
212 systemctl restart isc-dhcp-server
213 EOF
214 elif [[ $cmd == fai ]]; then
215 e "adding pxe for $host on tarantula"
216 ssh tarantula.office.fsf.org bash -e <<EOF
217 sed -ri 's/^( *host +$host *\{).*/\1 next-server faiserver.office.fsf.org; filename "pxelinux.0";/' /etc/dhcp/dhcpd.conf
218 systemctl restart isc-dhcp-server
219 EOF
220 fi
221 else
222 e "updating dnsmasq.conf:"
223 m $cmd
224 ${cmd:-:}|ssh cmc "dd of=/var/run/dnsmasq/dhcpopts.conf; /etc/init.d/dnsmasq reload
225 $([[ $type == arch || $type == parabola ]] && echo archlike-pxe-mount)"
226 fi
227 }
228
229 type -t host &>/dev/null || sudo apt-get -y install dnsutils
230 faiserverip=$(host faiserver | sed -rn 's/^\S+ has address //p;T;q' ||:)
231 if [[ ! $faiserverip || $faiserverip =~ [[:space:]] ]]; then
232 echo "$0: error: failed to get \$faiserverip, got: $faiserverip"
233 exit 1
234 fi
235
236
237 if $set; then
238 set-pxe
239 if [[ $type == fai ]]; then
240 if $redep; then
241 m fai-redep
242 fi
243 m myfai-chboot ${chboot_args[@]} $host
244 else
245 # This will fail if faiserver is not setup, so ignore any
246 # failure and don't bother us about it.
247 myfai-chboot &>/dev/null ||:
248 fi
249 fi
250
251 if $wait; then
252 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
253 # roughly 4 seconds apart. Earlier
254 # versions did just 1. Now testing on a vm, it does 1.
255 # bleh.
256 echo "waiting for $acks dhcp acks then disabling pxe"
257 ack-wait $acks
258 type=
259 unset cmd
260 set-pxe
261
262 # previously tried waiting for one more ack then disabling faiserver,
263 # since it can contain sensitive info, so turn it off when not in use,
264 # but disabling that for now as it's inconvenient to clean this
265 # up and run it in the background etc.
266
267 # if [[ $type == fai ]]; then
268 # echo "waiting for 1 dhcp ack then disabling fai server"
269 # ack-wait 1
270 # faiserver-disable
271 # fi
272 fi