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