#!/bin/sh # Install into /etc/dhcp and symlink to /etc/dhcp/dhclient-{enter,exit}-hooks.d IFACE=enp9s0f1.4 VRID=4 PRIO=100 DUMMYIP_OLD=192.168.254.254 DUMMYIP_NEW=192.168.254.253 PIDFILE=/var/run/"vrrpd_${IFACE}_${VRID}.pid" HOOKDIR="$(basename "$dir")" ## Debugging #set -x #exec >> /tmp/dhcp.log 2>&1 echo $HOOKDIR $reason $interface >>/tmp/dhcp.log set | grep 'ip_address\|route' | sort >>/tmp/dhcp.log printf '\n\n' >>/tmp/dhcp.log killvrrp () { echo "killvrrp $PIDFILE" >>/tmp/dhcp.log [ -n $old_ip_address ] || return if [ -e "$PIDFILE" ]; then kill "$(cat $PIDFILE)" || true fi } startvrrp () { echo "startvrrp $new_ip_address\n" >>/tmp/dhcp.log [ -n $new_ip_address ] || return echo vrrpd -D -n -i "$interface" -v "$VRID" -p "$PRIO" "$new_ip_address" vrrpd -D -n -i "$interface" -v "$VRID" -p "$PRIO" "$new_ip_address" || exit_status=1 } # dhclient-script insists on fucking with the IP address, we don't want any # of that since vrrpd will do it. neutralize_ip_change () { ip () neutered_ip "$@" } neutered_ip () { [ $# -ge 3 ] || { ip "$@"; return; } if [ "$1" = "-4" -a "$2" = addr -a "$3" = flush ] || [ "$1" = "-4" -a "$2" = addr -a "$3" = add ] || [ "$1" = "-4" -a "$2" = addr -a "$3" = change ] then echo ignored "$@" return else echo =ip "$@" command ip "$@" fi } do_vrrp () { if [ "$HOOKDIR" = "dhclient-enter-hooks.d" ]; then case $reason in BOUND | REBOOT | TIMEOUT) ;; # Just started shouldn't be running RENEW | REBIND) [ $new_ip_address = $old_ip_address] || killvrrp ;; EXPIRE | FAIL | RELEASE | STOP | TIMEOUT) killvrrp ;; esac neutralize_ip_change elif [ "$HOOKDIR" = "dhclient-exit-hooks.d" ]; then case $reason in # Note: TIMEOUT is when we can't reach a dhcp server during # startup but have a valid lease still that we're going to # use. BOUND | REBOOT | TIMEOUT) startvrrp ;; RENEW | REBIND) [ $new_ip_address = $old_ip_address] || startvrrp ;; EXPIRE | FAIL | RELEASE) ;; esac fi } if [ "$interface" = $IFACE ]; then do_vrrp fi