fixup initial faiserver bootstrap & small bugs
[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 -p Persist. Otherwise, wait for dhcp acks then remove.
40 -r Don't redeploy fai config.
41 -a Wait for 2 dhcp acks instead of the default 3. Some distros
42 do 2, some do 3.
43 EOF
44 exit $1
45 }
46
47 ##### begin command line parsing ########
48
49 persist=false
50 args=()
51 redep=true
52 acks=3
53 while [[ $1 ]]; do
54 case $1 in
55 --) shift; break ;;
56 -h|--help) usage ;;
57 -r) redep=false; shift ;;
58 -p) persist=true; shift ;;
59 -a) acks=2; shift ;;
60 *) args+=("$1"); shift ;;
61 esac
62 done
63 args+=("$@")
64
65
66 read type host <<<"${args[@]}"
67
68 if [[ ! $type ]]; then
69 echo "$0: error: exptected 1 argument of type"
70 usage 1
71 fi
72
73 if [[ $host ]]; then
74 host_tag="tag:$host,"
75 fi
76
77 case $type in
78 :|true) persist=true ;;
79 arch) acks=2 ;;
80 esac
81
82 ##### end command line parsing ########
83
84 sv() {
85 echo "$@"
86 "$@"
87 }
88
89 arch() {
90 cat <<EOF
91 dhcp-option-force=209,boot/syslinux/archiso.cfg
92 dhcp-option-force=210,/arch/
93 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
94 EOF
95 }
96
97 plain() {
98 # if arch was used before, this additionally needs
99 # the tftp link in /mnt/usb to be changed.
100 cat <<EOF
101 dhcp-boot=${host_tag}pxelinux.0
102 EOF
103 }
104
105 fai() {
106 cat <<EOF
107 $set_host_tag
108 dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
109 EOF
110 }
111
112 ack-wait() {
113 wait_count=$1
114 if [[ $host ]]; then
115 host_regex=" $host"
116 fi
117 regex=".*DHCPACK.*$host_regex$"
118 i=0
119 tmp=$(mktemp)
120 while (( i != wait_count )) && read line; do
121 if [[ $line =~ $regex ]]; then
122 i=$((i+1))
123 echo $line
124 fi
125 done < <(ssh wrt logread -f)
126 sv sleep 5
127 }
128
129 set-pxe() {
130 ${1:-$type} | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
131 if [[ $type == arch ]]; then arch-pxe-mount; fi"
132 }
133
134 set-pxe
135
136 if [[ $type == fai ]]; then
137 if $redep; then
138 fai-redep
139 fi
140 faiserver-enable
141 fi
142
143 if ! $persist; then
144 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
145 # roughly 4 seconds apart. Earlier
146 # versions did just 1. Now testing on a vm, it does 1.
147 # bleh.
148 echo "waiting for $acks dhcp acks then disabling pxe"
149 ack-wait $acks
150 set-pxe :
151 if [[ $type == fai ]]; then
152 # fai server can contain sensitive info, so turn it off
153 # when it's not in use.
154 echo "waiting for 1 dhcp ack then disabling fai server"
155 ack-wait 1
156 faiserver-disable
157 fi
158 fi