#!/usr/bin/openrc-run . /opt/etc/functions.openrc description="Creating host bonds or bridges" # Note: extra commands are mostly for debug purposes. extra_commands="create destroy reload" depend() { echo " ${SVCNAME}::depend(): rc_need=\"${rc_need}\"" provide ${SVCNAME} } # Descriptions for bonds and bridges are similar: # # name([eth1[,eth2[,...]])[=IPv4/Bits][;MTUbytes] # # Example of valid list (pay attention: no spaces except delimiter): # HOST_LINKS=" \ # name1(eth1,eth2,eth3)=10.0.0.1/16;1476 \ # name2()=10.0.1.2/24 \ # name3(eth4);1460 \ # name5(eth6,eth7) \ # name7() \ # " link_create() { local _ltype=$1 _name=$2 _ipmask=$3 _mtu=$4 _eth _rv=0 shift; shift; shift; shift if ip link show dev ${_name} > /dev/null 2>&1; then update_me ewarn "${ME}: link ${_name} already exists, destroying..." ip link set down dev ${_name} ip link del ${_name} if ip link show dev ${_name} > /dev/null 2>&1; then eerror "${ME}: unable to destroy link ${_name}" return 1 fi ip link add ${_name} type ${_ltype} || _rv=1 else # # # # Can we create iface $IF of valid type if name $IF is not in use yet? # In bizzare world of Linux, the answer is "mostly". if ! (ip link add ${_name} type ${_ltype} > /dev/null 2>&1); then update_me if ip link show dev ${_name} > /dev/null 2>&1; then ewarn "${ME}: mythical iface ${_name} appeared." _rv=0 else eerror "${ME}: not enough mana to create ${_name}." _rv=1 fi fi # # # fi [ "${_mtu}" = "ignore" ] || ip link set mtu ${_mtu} dev ${_name} || _rv=1 [ "${_ipmask}" = "ignore" ] || ip addr add ${_ipmask} dev ${_name} || _rv=1 for _eth in $@; do ip link set down dev ${_eth} || _rv=1 [ "${_mtu}" = "ignore" ] || ip link set mtu ${_mtu} dev ${_eth} || _rv=1 ip link set ${_eth} master ${_name} || _rv=1 ip link set up dev ${_eth} || _rv=1 done ip link set up dev ${_name} || _rv=1 return ${_rv} } link_destroy() { if ip link show dev $2 > /dev/null 2>&1; then ip link set down dev $2 ip link del $2 fi return 0 } create() { local _ltype if [ -n "${LINK_TYPE}" ]; then _ltype=${LINK_TYPE} else _ltype=$(echo ${SVCNAME} | sed -e 's/host//') fi for_all_links link_create ${_ltype} ${HOST_LINKS} } destroy() { for_all_links link_destroy any ${HOST_LINKS} } reload() { destroy create } start() { local _rv ebegin "Starting \"${SVCNAME}\"" eindent create _rv=$? eoutdent eend ${_rv} return ${_rv} } stop() { ebegin "Stopping service \"${SVCNAME}\"" destroy eend 0 return 0 }