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