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