c6f5325873
ENI Hotplug / udhcpc script * works with all Alpine versions back to 3.9 * udhcpc handles ENI's primary IPv4 * post-bound/post-renews eth-eni-hook handles secondary IPv4 & IPv6 addresses, route tables, and rules setup-ami tweaks * move scripts to be installed into setup-ami.d/ * move config snippets into setup-ami.d/etc/ (previously embedded in setup-ami)
106 lines
2.1 KiB
Bash
Executable File
106 lines
2.1 KiB
Bash
Executable File
#!/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
|
|
IFACE_DIR="${IFACE_CFG}.d"
|
|
|
|
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"
|
|
cd "$IFACE_DIR"
|
|
cat lo > "$IFACE_CFG.new"
|
|
for i in /sys/class/net/eth*; do
|
|
IFACE="$(basename "$i")"
|
|
[ ! -f "$IFACE" ] && sed -e "s/%%/$IFACE/g" DEFAULT > "$IFACE"
|
|
cat "$IFACE" >> "$IFACE_CFG.new"
|
|
done
|
|
cp -a "$IFACE_CFG" "$IFACE_CFG.bak"
|
|
mv "$IFACE_CFG.new" "$IFACE_CFG"
|
|
}
|
|
|
|
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"
|