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