-#!/bin/bash -x
+#!/bin/bash
# Setup dhcp server to point to tftp server,
# and depending on the type, setup the tftp server.
set -eE -o pipefail
trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
-if [[ ! $1 ]]; then
+
+usage() {
+ cat <<EOF
+Usage: ${0##*/} [OPTIONS] TYPE [HOST]
+One line description
+
+TYPE is one of arch, plain, fai, or : for no pxe server.
+HOST makes the pxe server only for that specific host
+
+-h|--help Print help and exit
+-- Subsequent arguments are never treated as options
+-p Persist. Otherwise, wait for 2 dhcp acks then remove.
+EOF
+ exit $1
+}
+
+##### begin command line parsing ########
+
+persist=false
+args=()
+while [[ $1 ]]; do
+ case $1 in
+ --) shift; break ;;
+ -h|--help) usage ;;
+ -p) persist=true; shift ;;
+ *) args+=("$1"); shift ;;
+ esac
+done
+args+=("$@")
+
+
+read type host <<<"${args[@]}"
+
+if [[ ! $type ]]; then
echo "$0: error: exptected 1 argument of type"
+ usage 1
+fi
+
+if [[ $host ]]; then
+ host_tag="tag:$host,"
fi
-action=$1
+
+case $type in
+ :|true) persist=true ;;
+esac
+
+##### end command line parsing ########
+
+sv() {
+ echo "$@"
+ "$@"
+}
arch() {
- plain-tftp
- cat <<'EOF'
+ cat <<EOF
dhcp-option-force=209,boot/syslinux/archiso.cfg
dhcp-option-force=210,/arch/
-dhcp-boot=/arch/boot/syslinux/lpxelinux.0
+dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
EOF
}
-plain-tftp() {
+plain() {
# if arch was used before, this additionally needs
# the tftp link in /mnt/usb to be changed.
- cat <<'EOF'
-enable-tftp
-tftp-root=/mnt/usb/tftpboot
-dhcp-boot=pxelinux.0
+ cat <<EOF
+dhcp-boot=${host_tag}pxelinux.0
EOF
}
fai() {
- cat <<'EOF'
-dhcp-boot=fai/pxelinux.0,faiserver.lan,faiserver.lan
+ cat <<EOF
+$set_host_tag
+dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
EOF
}
+echo "setting config type: $type"
+$type | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart #
+if [[ $type == arch ]]; then arch-pxe-mount; fi"
-$action | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
-if [[ $action == arch ]]; then arch-pxe-mount; fi"
+
+if ! $persist; then
+ if [[ $host ]]; then
+ host_regex=" $host"
+ fi
+ regex=".*DHCPACK.*$host_regex$"
+ i=0
+ tmp=$(mktemp)
+ while (( i != 2 )) && read line; do
+ if [[ $line =~ $regex ]]; then
+ i=$((i+1))
+ echo $line
+ fi
+ done < <(ssh wrt logread -f)
+ sv sleep 5
+ sv "$BASH_SOURCE" :
+fi