ifupdown-ng/executor-scripts/linux/link

113 lines
2.5 KiB
Text
Raw Permalink Normal View History

2020-07-26 09:36:31 +00:00
#!/bin/sh
[ -n "$VERBOSE" ] && set -x
is_vlan() {
case "$IFACE" in
*#*) return 1 ;;
*:*) return 1 ;;
vlan*.*) return 1 ;;
vlan*)
IF_VLAN_ID="${IFACE#vlan}"
[ -n "${IF_VLAN_RAW_DEVICE:-}" ] && return 0
return 1
;;
*.*)
IF_VLAN_RAW_DEVICE="${IFACE%.*}"
IF_VLAN_ID="${IFACE##*.}"
return 0
;;
*)
[ -z "${IF_VLAN_ID:-}" ] && return 1
[ -z "${IF_VLAN_RAW_DEVICE:-}" ] && return 1
return 0
;;
esac
}
case "$PHASE" in
depend)
# vlan-raw-device
if is_vlan; then
echo "$IF_VLAN_RAW_DEVICE"
# veth-peer-name
elif [ "${IF_LINK_TYPE}" = "veth" -a "${IF_VETH_PEER_NAME}" ]; then
echo "${IF_VETH_PEER_NAME}"
fi
;;
create)
if [ "${IF_LINK_TYPE}" = "dummy" ]; then
if [ -d "/sys/class/net/${IFACE}" ]; then
iface_type=$(ip -d link show dev "${IFACE}" | head -n3 | tail -n1 | awk '{ print $1 }')
if [ "${iface_type}" != 'dummy' ]; then
echo "Interface ${IFACE} exists but is of type ${iface_type} instead of dummy"
exit 1
fi
exit 0
fi
${MOCK} ip link add "${IFACE}" type dummy
elif [ "${IF_LINK_TYPE}" = "veth" ]; then
if [ ! -d "/sys/class/net/${IFACE}" ]; then
ARGS=""
if [ "${IF_VETH_PEER_NAME}" ]; then
ARGS="peer ${IF_VETH_PEER_NAME}"
fi
${MOCK} ip link add "${IFACE}" type veth ${ARGS}
fi
elif is_vlan; then
if [ -d "/sys/class/net/${IFACE}" ]; then
exit 0
fi
if [ -z "${MOCK}" ]; then
if [ ! -d "/sys/class/net/${IF_VLAN_RAW_DEVICE}" ]; then
echo "Underlay device ${IF_VLAN_RAW_DEVICE} for ${IFACE} does not exist"
exit 1
fi
if ! [ -d /proc/net/vlan ]; then
echo "Loading 8021q kernel module for VLAN support"
${MOCK} modprobe 8021q
fi
fi
${MOCK} ip link add link "${IF_VLAN_RAW_DEVICE}" name "${IFACE}" type vlan id "${IF_VLAN_ID}"
fi
;;
up)
IF_LINK_OPTIONS="$IF_LINK_OPTIONS"
[ -n "$IF_MTU" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS mtu $IF_MTU"
[ -n "$IF_HWADDRESS" ] && IF_LINK_OPTIONS="$IF_LINK_OPTIONS address $IF_HWADDRESS"
${MOCK} ip link set up dev "${IFACE}" ${IF_LINK_OPTIONS}
# Set alias is configured
if [ "${IF_ALIAS}" ]; then
${MOCK} ip link set alias "${IF_ALIAS}" dev "${IFACE}"
fi
;;
down)
# Don't complain about a vanished interface when downing it
if [ -z "${MOCK}" -a ! -d "/sys/class/net/${IFACE}" ]; then
exit 0
fi
${MOCK} ip link set down dev "${IFACE}"
;;
destroy)
if [ "${IF_LINK_TYPE}" = "dummy" ] || [ "${IF_LINK_TYPE}" = "veth" ] || is_vlan; then
if [ -z "${MOCK}" -a ! -d "/sys/class/net/${IFACE}" ]; then
exit 0
fi
${MOCK} ip link del "${IFACE}"
fi
;;
esac