2020-09-12 17:57:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# vim: set ts=4 et:
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
PROC="$(basename "$0")[$$]"
|
|
|
|
|
|
|
|
DEBUG=
|
|
|
|
|
|
|
|
log() {
|
|
|
|
[ -z "$DEBUG" ] && [ "$1" = "debug" ] && return
|
|
|
|
FACILITY="kern.$1"
|
|
|
|
shift
|
|
|
|
logger -s -p "$FACILITY" -t "$PROC" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -z "$MDEV" ]; then
|
|
|
|
log err "MDEV env not defined"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
RTABLE="${MDEV#eth}"
|
|
|
|
let RTABLE+=1000
|
|
|
|
|
|
|
|
IFACE_CFG=/etc/network/interfaces
|
|
|
|
|
|
|
|
ip() {
|
|
|
|
v=-4
|
|
|
|
if [ "$1" = '-4' ] || [ "$1" = '-6' ]; then
|
|
|
|
v="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
OP="$2"
|
|
|
|
[ "$OP" = show ] && LEV=debug || LEV=info
|
|
|
|
if /sbin/ip "$v" "$@" || [ -n "$FAIL_OK" ]; then
|
|
|
|
log "$LEV" "OK: ip $v $*"
|
|
|
|
else
|
|
|
|
log err "FAIL: ip $v $*"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
assemble_interfaces() {
|
|
|
|
log info "Rebuilding $IFACE_CFG"
|
2020-11-15 07:49:42 +00:00
|
|
|
/etc/network/assemble-interfaces
|
2020-09-12 17:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface_up() {
|
|
|
|
log info "Bringing up $MDEV"
|
|
|
|
ifup "$MDEV"
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_interface() {
|
|
|
|
log info "Cleaning up $MDEV"
|
|
|
|
|
|
|
|
# kill related udhcpc
|
|
|
|
kill "$(cat "/run/udhcpc.$MDEV.pid")"
|
|
|
|
|
|
|
|
# tidy up /run/ifstate, if it exists
|
|
|
|
[ -f /run/ifstate ] && sed -i -e "/^$MDEV=/d" /run/ifstate
|
|
|
|
|
|
|
|
# remove related rules
|
|
|
|
for V in 4 6; do
|
|
|
|
for P in $(ip -"$V" rule show table "$RTABLE" | cut -d: -f1); do
|
|
|
|
ip -"$V" rule del pref "$P"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
log info "STARTING: $ACTION $MDEV"
|
|
|
|
|
|
|
|
if exec 200>>"$IFACE_CFG"; then
|
|
|
|
if flock 200; then
|
|
|
|
case $ACTION in
|
|
|
|
add|"")
|
|
|
|
assemble_interfaces
|
|
|
|
interface_up
|
|
|
|
;;
|
|
|
|
remove)
|
|
|
|
assemble_interfaces
|
|
|
|
cleanup_interface
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
log err "Unknown action '$ACTION'"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
log err "Unable to flock $IFACE_CFG"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
log err "Unable to assign fd 200 to flock $IFACE_CFG"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
log info "FINISHED: $ACTION $MDEV"
|