various fixes and improvements
[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, 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 -w Setup pxe, then wait like -a.
45 -h|--help Print help and exit
46
47
48 Note, when switching between plain and arch, you will need to
49 do something like:
50 ssh wrt
51 cd /mnt/usb
52 rm tftpboot
53 ln -s <arch/debian iso dir> tftpboot
54
55
56 Note: Uses GNU getopt options parsing style
57 EOF
58 exit $1
59 }
60
61 ##### begin command line parsing ########
62
63 dhcp=true
64 redep=true
65 acks=2
66 wait=false
67 temp=$(getopt -l help adrSwh "$@") || usage 1
68 eval set -- "$temp"
69 while true; do
70 case $1 in
71 -a) wait=true; set=false; shift ;;
72 -d) dhcp=false; shift ;;
73 -r) redep=false; shift ;;
74 -S) chboot_arg=-S; shift ;;
75 -w) wait=true; set=true; shift ;;
76 -h|--help) usage ;;
77 --) shift; break ;;
78 *) echo "$0: Internal error!" ; exit 1 ;;
79 esac
80 done
81
82 read -r host type <<<"$@"
83
84 case $# in
85 0|2);;
86 *)
87 echo "$0: error: expected 0 or 2 arguments"
88 echo
89 usage 1
90 ;;
91 esac
92
93 if $wait && ! $dhcp; then
94 echo "$0: error -w conflicts with -d, choose one or other"
95 exit 1
96 fi
97
98
99 if [[ $host && $host != default ]]; then
100 host_tag="tag:$host,"
101 fi
102
103 ##### end command line parsing ########
104
105 e() {
106 echo "$@"
107 "$@"
108 }
109
110 arch() {
111 cat <<EOF
112 dhcp-option-force=209,boot/syslinux/archiso.cfg
113 dhcp-option-force=210,/arch/
114 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
115 EOF
116 }
117
118 plain() {
119 # if arch was used before, this additionally needs
120 # the tftp link in /mnt/usb to be changed.
121 cat <<EOF
122 dhcp-boot=${host_tag}pxelinux.0
123 EOF
124 }
125
126 fai() {
127 cat <<EOF
128 dhcp-boot=${host_tag}pxelinux.0,faiserver.b8.nz,faiserver.b8.nz
129 EOF
130 }
131
132 ack-wait() {
133 wait_count=$1
134 if [[ $host ]]; then
135 host_regex=" $host"
136 fi
137 regex=".*DHCPACK.*$host_regex$"
138 i=0
139 while (( i != wait_count )) && read -r line; do
140 if [[ $line =~ $regex ]]; then
141 i=$((i+1))
142 echo $line
143 fi
144 done < <(ssh wrt logread -f)
145 e sleep 20
146 }
147
148 set-pxe() {
149 $dhcp || return 0
150 echo "$0: updating dnsmasq.conf:"
151 $type
152 ${type:-:}|ssh wrt "cedit pxe /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
153 $([[ $type == arch ]] && echo arch-pxe-mount)"
154 }
155
156
157 if $set; then
158 set-pxe
159 if [[ $type == fai ]]; then
160 e myfai-chboot $chboot_arg $host
161 if $redep; then
162 e fai-redep
163 fi
164 else
165 # This will fail if faiserver is not setup, so ignore any
166 # failure and don't bother us about it.
167 myfai-chboot &>/dev/null ||:
168 fi
169 fi
170
171 if $wait; then
172 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
173 # roughly 4 seconds apart. Earlier
174 # versions did just 1. Now testing on a vm, it does 1.
175 # bleh.
176 echo "waiting for $acks dhcp acks then disabling pxe"
177 ack-wait $acks
178 type=
179 set-pxe
180
181 # previously tried waiting for one more ack then disabling faiserver,
182 # since it can contain sensitive info, so turn it off when not in use,
183 # but disabling that for now as it's inconvenient to clean this
184 # up and run it in the background etc.
185
186 # if [[ $type == fai ]]; then
187 # echo "waiting for 1 dhcp ack then disabling fai server"
188 # ack-wait 1
189 # faiserver-disable
190 # fi
191 fi