various fixes and improvements
[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 EOF
26 exit $1
27 }
28
29 ##### begin command line parsing ########
30
31 persist=false
32 args=()
33 while [[ $1 ]]; do
34 case $1 in
35 --) shift; break ;;
36 -h|--help) usage ;;
37 -p) persist=true; shift ;;
38 *) args+=("$1"); shift ;;
39 esac
40 done
41 args+=("$@")
42
43
44 read type host <<<"${args[@]}"
45
46 if [[ ! $type ]]; then
47 echo "$0: error: exptected 1 argument of type"
48 usage 1
49 fi
50
51 if [[ $host ]]; then
52 host_tag="tag:$host,"
53 fi
54
55 case $type in
56 :|true) persist=true ;;
57 esac
58
59 ##### end command line parsing ########
60
61 sv() {
62 echo "$@"
63 "$@"
64 }
65
66 arch() {
67 cat <<EOF
68 dhcp-option-force=209,boot/syslinux/archiso.cfg
69 dhcp-option-force=210,/arch/
70 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
71 EOF
72 }
73
74 plain() {
75 # if arch was used before, this additionally needs
76 # the tftp link in /mnt/usb to be changed.
77 cat <<EOF
78 dhcp-boot=${host_tag}pxelinux.0
79 EOF
80 }
81
82 fai() {
83 cat <<EOF
84 $set_host_tag
85 dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
86 EOF
87 }
88
89 echo "setting config type: $type"
90 $type | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart #
91 if [[ $type == arch ]]; then arch-pxe-mount; fi"
92
93
94 if ! $persist; then
95 if [[ $host ]]; then
96 host_regex=" $host"
97 fi
98 regex=".*DHCPACK.*$host_regex$"
99 i=0
100 tmp=$(mktemp)
101 while (( i != 2 )) && read line; do
102 if [[ $line =~ $regex ]]; then
103 i=$((i+1))
104 echo $line
105 fi
106 done < <(ssh wrt logread -f)
107 sv sleep 5
108 sv "$BASH_SOURCE" :
109 fi