fix some bugs and mix other things
[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.
27
28 Without HOST TYPE, disable server and fai server.
29
30 HOST A hostname known to the dhcp server, or default for all.
31 TYPE One of arch, parabola, plain, fai.
32
33 -d Don't alter dhcp config. Only make sense for fai type, and on network
34 other than home network.
35 -r Don't redeploy fai config. For example, if there is a different host
36 that is mid-install.
37
38 -a Don't setup pxe, just Wait for 2 dhcp acks, then disable the pxe
39 server after a delay. First ack is for pxe boot, 2nd ack is
40 for os boot. Sometimes on debian, there is a 3rd one shortly
41 after the 2nd. I can't remember exactly why this caused a
42 problem, but I'm hoping the sleep will take care of it.
43 -S sets FAI_ACTION=sysinfo, see myfai-chboot for more info.
44 -k Pass -k to myfai-chboot.
45 -w Setup pxe, then wait like -a.
46 -h|--help Print help and exit
47
48
49 Note, when switching between plain and arch or parabola, you will need to
50 do something like:
51 ssh wrt
52 cd /mnt/usb
53 rm tftpboot
54 ln -s <arch/parabola/debian iso dir> tftpboot
55
56
57 Note: Uses GNU getopt options parsing style
58 EOF
59 exit $1
60 }
61
62 ##### begin command line parsing ########
63
64 dhcp=true
65 redep=true
66 acks=2
67 wait=false
68 chboot_args=()
69 temp=$(getopt -l help adrSkwh "$@") || usage 1
70 eval set -- "$temp"
71 while true; do
72 case $1 in
73 -a) wait=true; set=false; shift ;;
74 -d) dhcp=false; shift ;;
75 -r) redep=false; shift ;;
76 -S) chboot_args+=(-S); shift ;;
77 -k) chboot_args+=(-k); shift ;;
78 -w) wait=true; set=true; shift ;;
79 -h|--help) usage ;;
80 --) shift; break ;;
81 *) echo "$0: Internal error!" ; exit 1 ;;
82 esac
83 done
84
85 read -r host type <<<"$@"
86
87 case $# in
88 0);;
89 2)
90 case $type in
91 arch|parabola) cmd=archlike ;;
92 fai) cmd=fai ;;
93 *)
94 echo "$0: error expected type of arch|parabola|fai"
95 echo
96 usage 1
97 ;;
98 esac
99 ;;
100 *)
101 echo "$0: error: expected 0 or 2 arguments"
102 echo
103 usage 1
104 ;;
105 esac
106
107
108 if $wait && ! $dhcp; then
109 echo "$0: error -w conflicts with -d, choose one or other"
110 exit 1
111 fi
112
113
114 if [[ $host && $host != default ]]; then
115 host_tag="tag:$host,"
116 fi
117
118 ##### end command line parsing ########
119
120 e() {
121 echo "$@"
122 "$@"
123 }
124
125 archlike() {
126 cat <<EOF
127 dhcp-option-force=209,boot/syslinux/${type}iso.cfg
128 dhcp-option-force=210,/${type}/
129 dhcp-boot=${host_tag}/${type}/boot/syslinux/lpxelinux.0
130 EOF
131 }
132
133 plain() {
134 # if arch based was used before, this additionally needs
135 # the tftp link in /mnt/usb to be changed.
136 cat <<EOF
137 dhcp-boot=${host_tag}pxelinux.0
138 EOF
139 }
140
141 fai() {
142 cat <<EOF
143 dhcp-boot=${host_tag}pxelinux.0,faiserver.b8.nz,faiserver.b8.nz
144 EOF
145 }
146
147 ack-wait() {
148 wait_count=$1
149 if [[ $host ]]; then
150 host_regex=" $host"
151 fi
152 regex=".*DHCPACK.*$host_regex$"
153 i=0
154 while (( i != wait_count )) && read -r line; do
155 if [[ $line =~ $regex ]]; then
156 i=$((i+1))
157 echo $line
158 fi
159 done < <(ssh wrt logread -f)
160 e sleep 20
161 }
162
163 set-pxe() {
164 $dhcp || return 0
165 echo "$0: updating dnsmasq.conf:"
166 $cmd
167 ${cmd:-:}|ssh wrt "cedit pxe /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
168 $([[ $type == arch || $type == parabola ]] && echo archlike-pxe-mount)"
169 }
170
171
172 if $set; then
173 set-pxe
174 if [[ $type == fai ]]; then
175 e myfai-chboot ${chboot_args[@]} $host
176 if $redep; then
177 e fai-redep
178 fi
179 else
180 # This will fail if faiserver is not setup, so ignore any
181 # failure and don't bother us about it.
182 myfai-chboot &>/dev/null ||:
183 fi
184 fi
185
186 if $wait; then
187 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
188 # roughly 4 seconds apart. Earlier
189 # versions did just 1. Now testing on a vm, it does 1.
190 # bleh.
191 echo "waiting for $acks dhcp acks then disabling pxe"
192 ack-wait $acks
193 type=
194 set-pxe
195
196 # previously tried waiting for one more ack then disabling faiserver,
197 # since it can contain sensitive info, so turn it off when not in use,
198 # but disabling that for now as it's inconvenient to clean this
199 # up and run it in the background etc.
200
201 # if [[ $type == fai ]]; then
202 # echo "waiting for 1 dhcp ack then disabling fai server"
203 # ack-wait 1
204 # faiserver-disable
205 # fi
206 fi