small dns changes
[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 # Setup dhcp server to point to tftp server,
19 # and depending on the type, setup the tftp server.
20
21 # usage: $0 TYPE
22 # default distro is the base debian/fedora type. others are fai & arch.
23 # for no pxe server, use a no-op like : or true.
24
25 set -eE -o pipefail
26 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
27
28
29 usage() {
30 cat <<EOF
31 Usage: ${0##*/} [OPTIONS] [HOST TYPE]
32 Configure dnsmasq pxe server options and fai-chboot if appropriate.
33
34
35 Without HOST TYPE, disable pxe server and fai server.
36 TYPE is one of arch, plain, fai.
37 HOST is a hostname known to the dhcp server, or default for all
38 Note, when switching between plain and arch, you will need to
39 do something like:
40 ssh wrt
41 cd /mnt/usb
42 rm tftpboot
43 ln -s <arch/debian iso dir> tftpboot
44
45
46 -r Don't redeploy fai config. For example, if there is a different host
47 that is mid-install.
48
49 -a Don't setup pxe, just Wait for 2 dhcp acks, then disable the pxe
50 server after a delay. First ack is for pxe boot, 2nd ack is
51 for os boot. Sometimes on debian, there is a 3rd one shortly
52 after the 2nd. I can't remember exactly why this caused a
53 problem, but I'm hoping the sleep will take care of it.
54
55 -w Setup pxe, then wait like -a.
56 -h|--help Print help and exit
57
58
59 Note: Uses GNU getopt options parsing style
60 EOF
61 exit $1
62 }
63
64 ##### begin command line parsing ########
65
66 args=()
67 redep=true
68 acks=2
69 wait=false
70 temp=$(getopt -l help harw "$@") || usage 1
71 eval set -- "$temp"
72 while true; do
73 case $1 in
74 -a) wait=true; set=false; shift ;;
75 -r) redep=false; shift ;;
76 -w) wait=true; set=true; shift ;;
77 -h|--help) usage ;;
78 --) shift; break ;;
79 *) echo "$0: Internal error!" ; exit 1 ;;
80 esac
81 done
82
83 read host type <<<"$@"
84
85 case $# in
86 0|2);;
87 *)
88 echo "$0: error: expected 0 or 2 arguments"
89 echo
90 usage 1
91 ;;
92 esac
93
94 if [[ $host ]]; then
95 host_tag="tag:$host,"
96 fi
97
98 ##### end command line parsing ########
99
100 e() {
101 echo "$@"
102 "$@"
103 }
104
105 arch() {
106 cat <<EOF
107 dhcp-option-force=209,boot/syslinux/archiso.cfg
108 dhcp-option-force=210,/arch/
109 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
110 EOF
111 }
112
113 plain() {
114 # if arch was used before, this additionally needs
115 # the tftp link in /mnt/usb to be changed.
116 cat <<EOF
117 dhcp-boot=${host_tag}pxelinux.0
118 EOF
119 }
120
121 fai() {
122 cat <<EOF
123 $set_host_tag
124 dhcp-boot=${host_tag}pxelinux.0,faiserver.lan,faiserver.lan
125 EOF
126 }
127
128 ack-wait() {
129 wait_count=$1
130 if [[ $host ]]; then
131 host_regex=" $host"
132 fi
133 regex=".*DHCPACK.*$host_regex$"
134 i=0
135 tmp=$(mktemp)
136 while (( i != wait_count )) && read line; do
137 if [[ $line =~ $regex ]]; then
138 i=$((i+1))
139 echo $line
140 fi
141 done < <(ssh wrt logread -f)
142 e sleep 20
143 }
144
145 set-pxe() {
146 ${type:-:}|ssh wrt "cedit pxe /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
147 $([[ $type == arch ]] && echo arch-pxe-mount)"
148 }
149
150
151 if $set; then
152 set-pxe
153 if [[ $type == fai ]]; then
154 e myfai-chboot $host
155 if $redep; then
156 e fai-redep
157 fi
158 else
159 e myfai-chboot
160 fi
161 fi
162
163 if $wait; then
164 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
165 # roughly 4 seconds apart. Earlier
166 # versions did just 1. Now testing on a vm, it does 1.
167 # bleh.
168 echo "waiting for $acks dhcp acks then disabling pxe"
169 ack-wait $acks
170 type=
171 set-pxe
172
173 # previously tried waiting for one more ack then disabling faiserver,
174 # since it can contain sensitive info, so turn it off when not in use,
175 # but disabling that for now as it's inconvenient to clean this
176 # up and run it in the background etc.
177
178 # if [[ $type == fai ]]; then
179 # echo "waiting for 1 dhcp ack then disabling fai server"
180 # ack-wait 1
181 # faiserver-disable
182 # fi
183 fi