# source the real file . ${_REAL_LIB_VSERVER_FUNCTIONS} _REAL_LIB_VSERVER_FUNCTIONS="${_REAL_LIB_VSERVER_FUNCTIONS}" # # own functions # function netnsCreateMac() { local mac getFileValue mac "${1}/mac" if [ -n "$mac" ]; then echo "$mac" return 0 fi getFileValue ctx "$VSERVER_DIR"/context ( printf "0200" printf "%06d" $ctx printf "%02x" "$(basename "$1")" )| sed 's/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5:\6/' } function netnsCreateIfaceName() { local name getFileValue name "${1}/name" if [ -n "$name" ]; then echo "$name" return 0 fi printf "%s%d" "$VSERVER_NAME" "$(basename "$1")" } function _netnsProcessSingleGateway() { local iface local gw getFileValue gw "${1}/gw" "${1}/../gw" [ -n "$gw" ] || return 0 iface="$(netnsCreateIfaceName "$1")" _addInterfaceCmd IP_ROUTE default via "$gw" dev "$iface" } function _netnsAddLocalhost() { if [ -r "${1}/nflags" ]; then if grep -q '^~LBACK_REMAP$' "${1}/nflags"; then INTERFACES=( "${INTERFACES[@]}" "127.0.0.1/8" "::1/128" ) fi fi } function _netnsFilterNetnsUmount() { local args=( "$@" ) while : do case $1 in --) shift; break;; -*) shift;; net:*|/run/netns/*) return 0;; *) break;; esac done ${_REAL_UMOUNT} "${args[@]}" } function _netnsWaitForDad() { local waiting while ip -6 -o addr show tentative | grep -q ' scope global '; do sleep 1 waiting=$((waiting+1)) if [ "$waiting" -ge "$1" ]; then echo "Warning: IPv6 addresses still tentative after ${waiting} seconds; continuing..." return 0 fi done } # # monkey patching # function save_function() { local ORIG_FUNC=$(declare -f $1) local NEWNAME_FUNC="$2${ORIG_FUNC#$1}" eval "$NEWNAME_FUNC" } save_function _generateInterfaceOptions __netns_realGenerateInterfaceOptions function _generateInterfaceOptions() { __netns_realGenerateInterfaceOptions "$@" ret=$? # add commands for default route local netns=0 shopt -s nullglob for net in "$VSERVER_DIR/spline-netns/"*; do test -d "$net" || continue test ! -e "${net}/disabled" || continue _netnsProcessSingleGateway "$net" netns=1 done shopt -u nullglob # if using netns, automatically add localhost [ "$netns" != "0" ] && _netnsAddLocalhost "$VSERVER_DIR" return $ret } save_function _namespaceCleanup __netns_realNamespaceCleanup function _namespaceCleanup() { _REAL_UMOUNT="${_UMOUNT}" _UMOUNT="_netnsFilterNetnsUmount" __netns_realNamespaceCleanup "$@" ret=$? _UMOUNT="${_REAL_UMOUNT}" return $ret } save_function enableInterfaces __netns_realEnableInterfaces function enableInterfaces() { __netns_realEnableInterfaces "$@" ret=$? # It is unpossible to bind on very recently added IPv6 addresses. # They have to pass the duplicate address detection (dad) before # they could be used, so we wait here before continuing. _netnsWaitForDad 10 return $ret } # # disable original netns handling # function _handleNetNS() { # The netns handling of util-vserver activates the netns inside the # namespace, so the netns is not usable on the host. We have to disable # the internal handling to use our variant. So we simply overwrite the # internale function with this no-op. return 0 }