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