All pastes #1980188 Raw Edit

Mine

public text v1 · immutable
#1980188 ·published 2010-11-03 00:25 UTC
rendered paste body
#!/sbin/runscript
# Copyright (c) 2007-2010 Funtoo Technologies
# All rights reserved. Released under the 2-clause BSD license.

# TODO - generate cached config for /etc/init.d/foo, use them for starting and stopping
# rather than running "live" commands. This way "stop" will work correctly at all times.

# next line: INT = deprecated, support it for backwards compat:
INT=${RC_SVCNAME#netif.}
interface=${interface:-$INT}

[ "$interface" = "tmpl" ] && exit 0

if [ -z "$template" ]; then
	eerror "Please specify a template (template=) for $RC_SVCNAME"
	exit 1
fi

CYAN="[36;01m"
OFF="[0m"
	
DESC2="${template}"
[ -n "$description" ] && DESC2="${description}"

STARTD="Network $DESC2 $CYAN$interface$OFF up"
STOPD="Network $DESC2 $CYAN$interface$OFF down"

require() {
	local missing=""
	for envname in $*; do 
		[ -z "${!envname}" ] && missing="$missing $envname"
	done
	if [ -n "$missing" ]; then
		echo
		eerror "ERROR: Required configuration variable(s) are missing:"
		eerror ""
		for miss in $missing; do
			eerror "    ${CYAN}$miss${OFF}"
		done
		eerror ""
		eerror "Please correct your configuration to address this issue."
		echo
		exit 1
	fi
}

die() { eend 1 "$*"; exit 1; }
netif_depend() { return; }
netif_pre_up() { return; }
netif_post_up() { return; }
netif_pre_down() { return; }
netif_post_down() { return; }

depend() {
	need localmount
	[ "$vlanmode" != "off" ] && need "netif.${trunk}"
	[ -n "$slaves" ] && need $slaves
	netif_depend
}

start() {
	ebegin "$STARTD"
	if [ "$macaddr" != "" ]; then
		/sbin/nameif ${interface} ${macaddr}
	fi
	if [ "$vlanmode" != "off" ]; then
	       	if [ ! -d /proc/net/vlan ]; then
		       	modprobe 8021q
			if [ ! -d /proc/net/vlan ]; then
				eerror "VLAN for interface $interface could not be configured:"
				die " \n802.1q support is not present in this kernel."
			fi
		fi
		require trunk vlan
		ip link add dev ${interface} link ${trunk} type vlan id ${vlan} || die "Couldn't configure VLAN ${vlan} on ${trunk}"
	fi
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE STARTS
	ip addr flush dev $interface
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  AND HERE ENDS
	netif_pre_up
	ip link set $interface up
	if [ -n "$mtu" ]; then
		ip link set $interface mtu $mtu
		if [ -n "$slaves" ]; then
			for slave in ${slaves//netif./}; do
				ip link set $slave mtu $mtu || eend $?
			done
		fi
	fi
	ezroute add; ezresolv add
	eend $?
}

stop() {
	ebegin "$STOPD"
	netif_pre_down
	ezresolv del; ezroute del
	ip link set $interface down
	ip addr flush dev $interface
	netif_post_down
	# VLANS:
	[ "${vlanmode}" != "off" ] && ip link delete ${interface}
	eend 0
}

# DOM, IP, NM, SLAVES, MTU, NS1, NS2, GW are deprecated - this is backwards compat. code:
domain=${domain:-$DOM}
if [ -n "$IP" ]; then
	ipaddr=$IP/$NM
fi
netmask=${netmask:-$NM}
slaves=${slaves:-$SLAVES}
mtu=${mtu:-$MTU}
nameservers="${nameservers:-$NS1 $NS2}"
gateway=${gateway:-$GW}
# COMPAT END

start_pre() {
	if [ -n "$ipaddr $ipaddrs" ]; then
		for i in $ipaddr $ipaddrs; do
			if [ "${i##*/}" = "$i" ]; then
				echo
				ewarn "You probably want to add a netmask to the ipaddr $i defined in your configuration."
				ewarn "Example: ipaddr=\"192.168.0.1/24\""
				echo
			fi
		done
	fi
}

ezdns() {
	# This function generates a resolv.conf file, which ezresolv() passes to resolvconf
	[ -n "$domain" ] && echo "domain $domain"
	for ns in $nameservers; do
		echo "nameserver $ns"
	done
}

ezresolv() {
	# This function calls resolvconf (openresolv) with the correct resolv.conf passed as a here-file
	if [ "$1" = "add" ]; then
		[ -z "`ezdns`" ] && return
		resolvconf -a $interface << EOF || die "Problem adding DNS info for $interface"
`ezdns`
EOF
	else
		resolvconf -d $interface
	fi
}

ezroute() {
	if [ -n "$gateway" ]; then
		ip route $1 default via $gateway
		[ "$1" = "add" ] && [ $? -ne 0 ] && die "Couldn't set route: $*"
	fi
}

if [ -z "$trunk" ]; then
	# auto-detect trunk/vlan ID from interface name
	vlanmode="auto"
	vlan="${interface##*.}"
	trunk="${interface%.*}"
	[ "$vlan" = "$interface" ] && vlanmode="off"
else
	# trunk/vlan ID specified by user in config
	vlanmode="custom"
	require vlan
fi

. /etc/netif.d/$template