bug fixes for jessie & stretch
[automated-distro-installer] / pxe-server
1 #!/bin/bash
2
3 # Setup dhcp server to point to tftp server,
4 # and depending on the type, setup the tftp server.
5
6 # usage: $0 TYPE
7 # default distro is the base debian/fedora type. others are fai & arch.
8 # for no pxe server, use a no-op like : or true.
9
10 set -eE -o pipefail
11 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
12
13
14 usage() {
15 cat <<EOF
16 Usage: ${0##*/} [OPTIONS] TYPE [HOST]
17 One line description
18
19 TYPE is one of arch, plain, fai, or : for no pxe server.
20 HOST makes the pxe server only for that specific host
21
22 -h|--help Print help and exit
23 -- Subsequent arguments are never treated as options
24 -p Persist. Otherwise, wait for 2 dhcp acks then remove.
25 -r Don't redeploy fai config.
26 EOF
27 exit $1
28 }
29
30 ##### begin command line parsing ########
31
32 persist=false
33 args=()
34 redep=true
35 while [[ $1 ]]; do
36 case $1 in
37 --) shift; break ;;
38 -h|--help) usage ;;
39 -r) redep=false; shift ;;
40 -p) persist=true; shift ;;
41 *) args+=("$1"); shift ;;
42 esac
43 done
44 args+=("$@")
45
46
47 read type host <<<"${args[@]}"
48
49 if [[ ! $type ]]; then
50 echo "$0: error: exptected 1 argument of type"
51 usage 1
52 fi
53
54 if [[ $host ]]; then
55 host_tag="tag:$host,"
56 fi
57
58 case $type in
59 :|true) persist=true ;;
60 esac
61
62 ##### end command line parsing ########
63
64 sv() {
65 echo "$@"
66 "$@"
67 }
68
69 arch() {
70 cat <<EOF
71 dhcp-option-force=209,boot/syslinux/archiso.cfg
72 dhcp-option-force=210,/arch/
73 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
74 EOF
75 }
76
77 plain() {
78 # if arch was used before, this additionally needs
79 # the tftp link in /mnt/usb to be changed.
80 cat <<EOF
81 dhcp-boot=${host_tag}pxelinux.0
82 EOF
83 }
84
85 fai() {
86 cat <<EOF
87 $set_host_tag
88 dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
89 EOF
90 }
91
92 ack-wait() {
93 wait_count=$1
94 if [[ $host ]]; then
95 host_regex=" $host"
96 fi
97 regex=".*DHCPACK.*$host_regex$"
98 i=0
99 tmp=$(mktemp)
100 while (( i != wait_count )) && read line; do
101 if [[ $line =~ $regex ]]; then
102 i=$((i+1))
103 echo $line
104 fi
105 done < <(ssh wrt logread -f)
106 sv sleep 5
107 }
108
109 set-pxe() {
110 ${1:-$type} | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
111 if [[ $type == arch ]]; then arch-pxe-mount; fi"
112 }
113
114 set-pxe
115
116 if [[ $type == fai ]]; then
117 if $redep; then
118 fai-redep
119 fi
120 faiserver-enable
121 fi
122
123 if ! $persist; then
124 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
125 # roughly 4 seconds apart. Earlier
126 # versions did just 1. Whatever.
127 echo "waiting for 3 dhcp acks then disabling pxe"
128 ack-wait 3
129 set-pxe :
130 if [[ $type == fai ]]; then
131 # fai server can contain sensitive info, so turn it off
132 # when it's not in use.
133 echo "waiting for 1 dhcp ack then disabling fai server"
134 ack-wait 1
135 faiserver-disable
136 fi
137 fi