use latest btrfs-progs to match linux-libre
[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 ##### begin command line parsing ########
65
66 dhcp=true
67 redep=true
68 acks=2
69 wait=false
70 fsf=false
71
72 case $HOSTNAME in
73 x2|x3|kw) fsf=true ;;
74 esac
75
76 chboot_args=()
77 temp=$(getopt -l help adkrSwh "$@") || usage 1
78 eval set -- "$temp"
79 while true; do
80 case $1 in
81 -a) wait=true; set=false; shift ;;
82 -d) dhcp=false; shift ;;
83 -k) chboot_args+=(-k); shift ;;
84 -r) redep=false; shift ;;
85 -S) chboot_args+=(-S); shift ;;
86 -w) wait=true; set=true; shift ;;
87 -h|--help) usage ;;
88 --) shift; break ;;
89 *) echo "$0: Internal error!" ; exit 1 ;;
90 esac
91 done
92
93 read -r host type <<<"$@"
94
95 case $# in
96 [01]);;
97 2)
98 case $type in
99 arch|parabola) cmd=archlike ;;
100 fai) cmd=fai ;;
101 *)
102 echo "$0: error expected type of arch|parabola|fai"
103 echo
104 usage 1
105 ;;
106 esac
107 ;;
108 *)
109 echo "$0: error: expected 0-2 arguments"
110 echo
111 usage 1
112 ;;
113 esac
114
115
116 if $wait && ! $dhcp; then
117 echo "$0: error -w conflicts with -d, choose one or other"
118 exit 1
119 fi
120
121
122 if [[ $host && $host != default ]]; then
123 host_tag="tag:$host,"
124 fi
125
126 ##### end command line parsing ########
127
128 e() {
129 echo "$@"
130 "$@"
131 }
132
133 archlike() {
134 cat <<EOF
135 dhcp-option-force=209,boot/syslinux/${type}iso.cfg
136 dhcp-option-force=210,/${type}/
137 dhcp-boot=${host_tag}/${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 dhcp-boot=${host_tag}pxelinux.0
146 EOF
147 }
148
149 fai() {
150 cat <<EOF
151 dhcp-boot=${host_tag}pxelinux.0,faiserver.b8.nz,faiserver.b8.nz
152 EOF
153 }
154
155 ack-wait() {
156 if $fsf; then
157 wait_cmd="ssh tarantula tail -n0 -f /var/log/syslog"
158 else
159 wait_cmd="ssh wrt logread -f"
160 fi
161 wait_count=$1
162 if [[ $host ]]; then
163 if $fsf; then
164 host_regex=" $(getent hosts kw | awk '{print $1}' | sed 's/\./\\./g')"
165 else
166 host_regex=" $host"
167 fi
168 fi
169 regex=".*DHCPACK.*$host_regex\b"
170 i=0
171 while (( i != wait_count )) && read -r line; do
172 if [[ $line =~ $regex ]]; then
173 i=$((i+1))
174 echo $line
175 fi
176 done < <($wait_cmd ||:) # tail returns 2 it seems
177 e sleep 20
178 }
179
180 set-pxe() {
181 $dhcp || return 0
182 if $fsf; then
183 if [[ ! $cmd ]]; then
184 echo "$0: removing pxe for $host on tarantula"
185 ssh tarantula bash -e <<EOF
186 sed -ri 's/^( *host +$host *\{).*/\1/' /etc/dhcp3/dhcpd.conf
187 /etc/init.d/dhcp3-server restart
188 EOF
189 elif [[ $cmd == fai ]]; then
190 echo "$0: adding pxe for $host on tarantula"
191 ssh tarantula bash -e <<EOF
192 sed -ri 's/^( *host +$host *\{).*/\1 next-server faiserver.office.fsf.org; filename "pxelinux.0";/' /etc/dhcp3/dhcpd.conf
193 /etc/init.d/dhcp3-server restart
194 EOF
195 fi
196 else
197 echo "$0: updating dnsmasq.conf:"
198 $cmd
199 ${cmd:-:}|ssh wrt "cedit pxe /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
200 $([[ $type == arch || $type == parabola ]] && echo archlike-pxe-mount)"
201 fi
202 }
203
204
205 if $set; then
206 set-pxe
207 if [[ $type == fai ]]; then
208 e myfai-chboot ${chboot_args[@]} $host
209 if $redep; then
210 e fai-redep
211 fi
212 else
213 # This will fail if faiserver is not setup, so ignore any
214 # failure and don't bother us about it.
215 myfai-chboot &>/dev/null ||:
216 fi
217 fi
218
219 if $wait; then
220 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
221 # roughly 4 seconds apart. Earlier
222 # versions did just 1. Now testing on a vm, it does 1.
223 # bleh.
224 echo "waiting for $acks dhcp acks then disabling pxe"
225 ack-wait $acks
226 type=
227 unset cmd
228 set-pxe
229
230 # previously tried waiting for one more ack then disabling faiserver,
231 # since it can contain sensitive info, so turn it off when not in use,
232 # but disabling that for now as it's inconvenient to clean this
233 # up and run it in the background etc.
234
235 # if [[ $type == fai ]]; then
236 # echo "waiting for 1 dhcp ack then disabling fai server"
237 # ack-wait 1
238 # faiserver-disable
239 # fi
240 fi