5a157636b943f8eed2e65d2177eb5f14aef7086b
[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
20 x="$(readlink -f "$BASH_SOURCE")"; source "${x%/*}/bash-trace"
21
22
23 usage() {
24 cat <<EOF
25 Usage: ${0##*/} [OPTIONS] [HOST] [TYPE]
26 Configure dnsmasq boot options and fai-chboot if appropriate. This is
27 not general purpose, it has code specific to dhcp servers I run.
28
29 Without TYPE, disable server and fai server. In that case, HOST is only
30 needed for fsf office network.
31
32 HOST A hostname known to the dhcp server, or default for all.
33 TYPE One of arch, parabola, plain, fai.
34
35 -a Don't setup pxe, just Wait for 2 dhcp acks, then disable the pxe
36 server after a delay. First ack is for pxe boot, 2nd ack is
37 for os boot. Sometimes on debian, there is a 3rd one shortly
38 after the 2nd. I can't remember exactly why this caused a
39 problem, but I'm hoping the sleep will take care of it.
40 -d Don't alter dhcp config. Only make sense for fai type, and on network
41 other than home or fsf.
42 -k Pass -k to myfai-chboot.
43 -r Don't redeploy fai config. For example, if there is a different host
44 that is mid-install.
45
46 -S sets FAI_ACTION=sysinfo, see myfai-chboot for more info.
47 -w Setup pxe, then wait like -a.
48 -h|--help Print help and exit
49
50
51 Note, when switching between plain and arch or parabola, you will need to
52 do something like:
53 ssh wrt
54 cd /mnt/usb
55 rm tftpboot
56 ln -s <arch/parabola/debian iso dir> tftpboot
57
58
59 Note: Uses GNU getopt options parsing style
60 EOF
61 exit $1
62 }
63
64 pre="${0##*/}:"
65 m() { printf "$pre %s\n" "$*"; "$@"; }
66 e() { printf "$pre %s\n" "$*"; }
67 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
68
69 ##### begin command line parsing ########
70
71 dhcp=true
72 redep=true
73 acks=2
74 wait=false
75 fsf=false
76
77 case $HOSTNAME in
78 x3|kw) fsf=true ;;
79 esac
80
81 chboot_args=()
82 temp=$(getopt -l help adkrSwh "$@") || usage 1
83 eval set -- "$temp"
84 while true; do
85 case $1 in
86 -a) wait=true; set=false; shift ;;
87 -d) dhcp=false; shift ;;
88 -k) chboot_args+=(-k); shift ;;
89 -r) redep=false; shift ;;
90 -S) chboot_args+=(-S); shift ;;
91 -w) wait=true; set=true; shift ;;
92 -h|--help) usage ;;
93 --) shift; break ;;
94 *) echo "$0: Internal error!" ; exit 1 ;;
95 esac
96 done
97
98 read -r host type <<<"$@"
99
100 case $# in
101 [01]);;
102 2)
103 case $type in
104 arch|parabola) cmd=archlike ;;
105 fai) cmd=fai ;;
106 *)
107 echo "$0: error expected type of arch|parabola|fai"
108 echo
109 usage 1
110 ;;
111 esac
112 ;;
113 *)
114 echo "$0: error: expected 0-2 arguments"
115 echo
116 usage 1
117 ;;
118 esac
119
120
121 if $wait && ! $dhcp; then
122 echo "$0: error -w conflicts with -d, choose one or other"
123 exit 1
124 fi
125
126
127 if [[ $host && $host != default ]]; then
128 host_tag="tag:$host,"
129 fi
130
131 ##### end command line parsing ########
132
133 archlike() {
134 cat <<EOF
135 ${host_tag}209,boot/syslinux/${type}iso.cfg
136 ${host_tag}210,/${type}/
137 ${host_tag}option:bootfile-name,/${type}/boot/syslinux/lpxelinux.0
138 EOF
139 }
140
141 plain() {
142 # if arch based was used before, this additionally needs
143 # the tftp link in /mnt/usb to be changed.
144 cat <<EOF
145 ${host_tag}option:bootfile-name,pxelinux.0
146 EOF
147 }
148
149 fai() {
150 cat <<EOF
151 ${host_tag}option:bootfile-name,pxelinux.0
152 ${host_tag}option:server-ip-address,$faiserverip
153 ${host_tag}option:tftp-server,$faiserverip
154 EOF
155 # Note, previously used normal dnsmasq option, but it requires dnsmasq
156 # restart, which causes momentary dns failures, which can bork an
157 # install.
158 #
159 # dhcp-boot=${host_tag}pxelinux.0,faiserver.b8.nz,faiserver.b8.nz
160 }
161
162 ack-wait() {
163 if $fsf; then
164 wait_cmd="ssh tarantula.office.fsf.org tail -n0 -f /var/log/syslog"
165 else
166 wait_cmd="ssh cmc logread -f"
167 fi
168 wait_count=$1
169 if [[ $host ]]; then
170 if $fsf; then
171 host_regex=" $(getent hosts kw | awk '{print $1}' | sed 's/\./\\./g')"
172 else
173 host_regex=" $host"
174 fi
175 fi
176 regex=".*DHCPACK.*$host_regex\b"
177 i=0
178 while (( i != wait_count )) && read -r line; do
179 if [[ $line =~ $regex ]]; then
180 i=$((i+1))
181 echo $line
182 fi
183 done < <($wait_cmd ||:) # tail returns 2 it seems
184 m sleep 20
185 }
186
187 set-pxe() {
188 $dhcp || return 0
189 if $fsf; then
190 if [[ ! $cmd ]]; then
191 e "removing pxe for $host on tarantula"
192 ssh tarantula.office.fsf.org bash -e <<EOF
193 sed -ri 's/^( *host +$host *\{).*/\1/' /etc/dhcp3/dhcpd.conf
194 /etc/init.d/dhcp3-server restart
195 EOF
196 elif [[ $cmd == fai ]]; then
197 e "adding pxe for $host on tarantula"
198 ssh tarantula.office.fsf.org bash -e <<EOF
199 sed -ri 's/^( *host +$host *\{).*/\1 next-server faiserver.office.fsf.org; filename "pxelinux.0";/' /etc/dhcp3/dhcpd.conf
200 /etc/init.d/dhcp3-server restart
201 EOF
202 fi
203 else
204 e "updating dnsmasq.conf:"
205 m $cmd
206 ${cmd:-:}|ssh cmc "dd of=/etc/dnsmasq-dhcpopts.conf; /etc/init.d/dnsmasq reload
207 $([[ $type == arch || $type == parabola ]] && echo archlike-pxe-mount)"
208 fi
209 }
210
211 type -t host &>/dev/null || sudo apt-get -y install dnsutils
212 faiserverip=$(host faiserver | sed -rn 's/^\S+ has address //p;T;q' ||:)
213 if [[ ! $faiserverip || $faiserverip =~ [[:space:]] ]]; then
214 echo "$0: error: failed to get \$faiserverip, got: $faiserverip"
215 exit 1
216 fi
217
218
219 if $set; then
220 set-pxe
221 if [[ $type == fai ]]; then
222 if $redep; then
223 m fai-redep
224 fi
225 m myfai-chboot ${chboot_args[@]} $host
226 else
227 # This will fail if faiserver is not setup, so ignore any
228 # failure and don't bother us about it.
229 myfai-chboot &>/dev/null ||:
230 fi
231 fi
232
233 if $wait; then
234 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
235 # roughly 4 seconds apart. Earlier
236 # versions did just 1. Now testing on a vm, it does 1.
237 # bleh.
238 echo "waiting for $acks dhcp acks then disabling pxe"
239 ack-wait $acks
240 type=
241 unset cmd
242 set-pxe
243
244 # previously tried waiting for one more ack then disabling faiserver,
245 # since it can contain sensitive info, so turn it off when not in use,
246 # but disabling that for now as it's inconvenient to clean this
247 # up and run it in the background etc.
248
249 # if [[ $type == fai ]]; then
250 # echo "waiting for 1 dhcp ack then disabling fai server"
251 # ack-wait 1
252 # faiserver-disable
253 # fi
254 fi