fix pxe server leaving background procs
[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] TYPE [HOST]
32 One line description
33
34 TYPE is one of arch, plain, fai, or : for no pxe server.
35 HOST makes the pxe server only for that specific host
36
37 -h|--help Print help and exit
38 -- Subsequent arguments are never treated as options
39 -r Don't redeploy fai config.
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 EOF
46 exit $1
47 }
48
49 ##### begin command line parsing ########
50
51 args=()
52 redep=true
53 acks=2
54 wait=false
55 while [[ $1 ]]; do
56 case $1 in
57 --) shift; break ;;
58 -h|--help) usage ;;
59 -r) redep=false; shift ;;
60 -a) wait=true; shift ;;
61 *) args+=("$1"); shift ;;
62 esac
63 done
64 args+=("$@")
65
66
67 read type host <<<"${args[@]}"
68
69 if [[ ! $type ]]; then
70 echo "$0: error: exptected 1 argument of type"
71 usage 1
72 fi
73
74 if [[ $host ]]; then
75 host_tag="tag:$host,"
76 fi
77
78 ##### end command line parsing ########
79
80 e() {
81 echo "$@"
82 "$@"
83 }
84
85 arch() {
86 cat <<EOF
87 dhcp-option-force=209,boot/syslinux/archiso.cfg
88 dhcp-option-force=210,/arch/
89 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
90 EOF
91 }
92
93 plain() {
94 # if arch was used before, this additionally needs
95 # the tftp link in /mnt/usb to be changed.
96 cat <<EOF
97 dhcp-boot=${host_tag}pxelinux.0
98 EOF
99 }
100
101 fai() {
102 cat <<EOF
103 $set_host_tag
104 dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
105 EOF
106 }
107
108 ack-wait() {
109 wait_count=$1
110 if [[ $host ]]; then
111 host_regex=" $host"
112 fi
113 regex=".*DHCPACK.*$host_regex$"
114 i=0
115 tmp=$(mktemp)
116 while (( i != wait_count )) && read line; do
117 if [[ $line =~ $regex ]]; then
118 i=$((i+1))
119 echo $line
120 fi
121 done < <(ssh wrt logread -f)
122 e sleep 20
123 }
124
125 set-pxe() {
126 ${1:-$type} | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
127 if [[ $type == arch ]]; then arch-pxe-mount; fi"
128 }
129
130 if ! $wait && [[ $type != : ]]; then
131 set-pxe
132
133 if [[ $type == fai ]]; then
134 if $redep; then
135 fai-redep
136 fi
137 faiserver-enable
138 fi
139 fi
140
141 if $wait; then
142 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
143 # roughly 4 seconds apart. Earlier
144 # versions did just 1. Now testing on a vm, it does 1.
145 # bleh.
146 echo "waiting for $acks dhcp acks then disabling pxe"
147 ack-wait $acks
148 set-pxe :
149
150 # previously tried waiting for one more ack then disabling faiserver,
151 # since it can contain sensitive info, so turn it off when not in use,
152 # but disabling that for now as it's inconvenient to clean this
153 # up and run it in the background etc.
154
155 # if [[ $type == fai ]]; then
156 # echo "waiting for 1 dhcp ack then disabling fai server"
157 # ack-wait 1
158 # faiserver-disable
159 # fi
160 fi