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