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