From: Michael Tremer Date: Mon, 13 Apr 2009 20:06:39 +0000 (+0200) Subject: Updated networking scripts. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1135a884c018330789f1352f476ccf80f0754d07;p=ipfire-3.x.git Updated networking scripts. --- diff --git a/src/initscripts/core/network b/src/initscripts/core/network index 6eefa9a4f..19c3ff08c 100644 --- a/src/initscripts/core/network +++ b/src/initscripts/core/network @@ -22,7 +22,7 @@ case "${1}" in start) # Start all network interfaces - for zone in $(find ${CONFIG_ZONES}/* -maxdepth 1); do + for zone in $(find ${CONFIG_ZONES}/* -maxdepth 1 2>/dev/null); do [ -d "${zone}" ] || continue /etc/init.d/networking/zone ${zone##*/} up done @@ -31,7 +31,7 @@ case "${1}" in stop) # Stop all network interfaces - for zone in $(find ${CONFIG_ZONES}/* -maxdepth 1); do + for zone in $(find ${CONFIG_ZONES}/* -maxdepth 1 2>/dev/null); do [ -d "${zone}" ] || continue /etc/init.d/networking/zone ${zone##*/} down done diff --git a/src/initscripts/networking/functions b/src/initscripts/networking/functions index 13619cfeb..5bf04f584 100644 --- a/src/initscripts/networking/functions +++ b/src/initscripts/networking/functions @@ -216,3 +216,39 @@ function all_zones() { [ -d "${zone}" ] && echo ${zone} done } + +function run_hooks() { + local action + local dir + local failed + local hook + local hooks + + action=${1} + dir=${2} + shift 2 + + if [ -z "${action}" ] || [ -z "${dir}" ]; then + echo "Not enough parameters given." >&2 + return 1 + fi + + for hook in $(find ${dir} -type f); do + ( + . ${hook} + if [ -n "${HOOK}" -a -x "/etc/init.d/networking/hooks/${HOOK}" ]; then + CONFIG=${hook} /etc/init.d/networking/hooks/${HOOK} ${action} $@ + RET=$? + else + echo -e "${FAILURE}Unable to process ${hook}. Either" + echo -e "${FAILURE}the HOOK variable was not set," + echo -e "${FAILURE}or the specified hook cannot be executed." + message="" + log_failure_msg + fi + exit ${RET} + ) || failed=1 + done + + return ${failed} +} diff --git a/src/initscripts/networking/hooks/ipv4-static b/src/initscripts/networking/hooks/ipv4-static index 13fbbc37e..7a3ca9be5 100644 --- a/src/initscripts/networking/hooks/ipv4-static +++ b/src/initscripts/networking/hooks/ipv4-static @@ -14,38 +14,45 @@ ######################################################################## . /lib/lsb/init-functions +. /etc/init.d/networking/functions [ -n "${CONFIG}" ] && . ${CONFIG} -zone=$1 +zone=$2 -if [ -z "${IP}" ]; then - log_failure_msg "IP variable missing from ${CONFIG}, cannot continue." - exit 1 -fi - -if [ -z "${PREFIX}" -a -z "${PEER}" ]; then - log_warning_msg "PREFIX variable missing from ${CONFIG}, assuming 24." - PREFIX=24 - args="${args} ${IP}/${PREFIX}" -elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then - log_failure_msg "PREFIX and PEER both specified in ${CONFIG}, cannot continue." - exit 1 -elif [ -n "${PREFIX}" ]; then - args="${args} ${IP}/${PREFIX}" -elif [ -n "${PEER}" ]; then - args="${args} ${IP} peer ${PEER}" -fi - -if [ -n "${BROADCAST}" ]; then - args="${args} broadcast ${BROADCAST}" -fi +function check_config() { + if [ -z "${IP}" ]; then + log_failure_msg "IP variable missing, cannot continue." + exit 1 + fi + + if [ -z "${PREFIX}" -a -z "${PEER}" ]; then + log_warning_msg "PREFIX variable missing, assuming 24." + PREFIX=24 + args="${args} ${IP}/${PREFIX}" + elif [ -n "${PREFIX}" -a -n "${PEER}" ]; then + log_failure_msg "PREFIX and PEER both specified, cannot continue." + exit 1 + elif [ -n "${PREFIX}" ]; then + args="${args} ${IP}/${PREFIX}" + elif [ -n "${PEER}" ]; then + args="${args} ${IP} peer ${PEER}" + fi + + if [ -n "${BROADCAST}" ]; then + args="${args} broadcast ${BROADCAST}" + fi + + if [ -n "${SOURCE}" ]; then + args="${args} src ${SOURCE}" + fi +} -if [ -n "${SOURCE}" ]; then - args="${args} src ${SOURCE}" -fi +case "${1}" in + pre-up) + ;; -case "${2}" in - up) + post-up) + check_config MESSAGE="Adding IPv4 address ${IP} to zone ${zone} interface..." ip addr add ${args} dev ${zone} evaluate_retval @@ -60,8 +67,9 @@ case "${2}" in fi fi ;; - - down) + + pre-down) + check_config if [ -n "${GATEWAY}" ]; then MESSAGE="Removing default gateway..." ip route del default @@ -71,14 +79,60 @@ case "${2}" in MESSAGE="Removing IPv4 address ${IP} from zone ${zone}..." ip addr del ${args} dev ${zone} evaluate_retval - ;; - + ;; + + post-down) + ;; + config) - : # TODO - ;; + shift 2 + while [ $# -gt 0 ]; do + case "$1" in + --ip) + IP=$2 + shift 2 + ;; + --prefix) + PREFIX=$2 + shift 2 + ;; + --peer) + PEER=$2 + shift 2 + ;; + --broadcast) + BROADCAST=$2 + shift 2 + ;; + --source) + SOURCE=$2 + shift 2 + ;; + --gateway) + GATEWAY=$2 + shift 2 + ;; + *) + echo "Unknown option: $1" >&2 + exit 1 + ;; + esac + done + check_config + cat << EOF >> ${CONFIG_ZONES}/${zone}/ipv4-static_$IP +HOOK=ipv4-static +IP=$IP +PREFIX=$PREFIX +PEER=$PEER +BROADCAST=$BROADCAST +SOURCE=$SOURCE +GATEWAY=$GATEWAY +EOF + exit $? + ;; *) - echo "Usage: ${0} [interface] {up|down|config}" + echo "Usage: ${0} {pre-up|post-up|pre-down|post-down|config} [interface]" exit 1 ;; esac diff --git a/src/initscripts/networking/hooks/ipv4-static-route b/src/initscripts/networking/hooks/ipv4-static-route index 436154279..9daff1971 100644 --- a/src/initscripts/networking/hooks/ipv4-static-route +++ b/src/initscripts/networking/hooks/ipv4-static-route @@ -15,7 +15,7 @@ . /lib/lsb/init-functions [ -n "${CONFIG}" ] && . ${CONFIG} -zone=$1 +zone=$2 case "${TYPE}" in ("" | "network") @@ -72,7 +72,7 @@ if [ -n "${need_gateway}" ]; then args="${args} via ${GATEWAY}" fi -case "${2}" in +case "${1}" in up) boot_mesg "Adding '${desc}' route to zone ${zone}..." ip route add ${args} dev ${zone} diff --git a/src/initscripts/networking/hooks/mtu b/src/initscripts/networking/hooks/mtu index 685c8df26..93280fe6d 100644 --- a/src/initscripts/networking/hooks/mtu +++ b/src/initscripts/networking/hooks/mtu @@ -16,28 +16,52 @@ ######################################################################## . /lib/lsb/init-functions +. /etc/init.d/networking/functions [ -n "${CONFIG}" ] && . ${CONFIG} -zone=$1 +function check_config() { + if [ -z "${MTU}" ]; then + log_failure_msg "MTU variable missing from ${CONFIG}, cannot continue." + exit 1 + fi + if [ -z "${zone}" ]; then + usage + exit 1 + fi +} + +function usage() { + echo "Usage: ${0} {pre-up|post-up|pre-down|post-down|config} [interface]" +} -if [ -z "${MTU}" ] -then - log_failure_msg "MTU variable missing from ${CONFIG}, cannot continue." - exit 1 -fi +zone=$2 -case "${2}" in - up) +case "${1}" in + pre-up) + ;; + + post-up) + check_config message="Setting the MTU for ${zone} to ${MTU}..." echo "${MTU}" > "/sys/class/net/${zone}/mtu" evaluate_retval standard - ;; + ;; - down) - ;; + pre-down) + check_config + message="Resetting MTU for ${zone} to 1500..." + echo "1500" > "/sys/class/net/${zone}/mtu" + evaluate_retval standard + ;; + + post-down) + ;; + + config) + ;; *) - echo "Usage: ${0} [interface] {up|down}" + usage exit 1 ;; esac diff --git a/src/initscripts/networking/hooks/stp b/src/initscripts/networking/hooks/stp index 4a8f2b0b5..ed128dd35 100644 --- a/src/initscripts/networking/hooks/stp +++ b/src/initscripts/networking/hooks/stp @@ -15,29 +15,35 @@ . /lib/lsb/init-functions [ -n "${CONFIG}" ] && . ${CONFIG} -zone=$1 +zone=$2 -case "${2}" in - up) +case "${1}" in + pre-up) + ;; + + post-up) MESSAGE="Enabling Spanning Tree Protocol on zone ${zone}..." brctl stp ${zone} on evaluate_retval - ;; + ;; - down) + pre-down) MESSAGE="Disabling Spanning Tree Protocol on zone ${zone}..." brctl stp ${zone} off evaluate_retval - ;; + ;; + + post-down) + ;; config) : # TODO - ;; + ;; *) - echo "Usage: ${0} [interface] {up|down|config}" + echo "Usage: ${0} {pre-up|post-up|pre-down|post-down|config} [interface]" exit 1 - ;; + ;; esac # End $NETWORK_DEVICES/services/stp diff --git a/src/initscripts/networking/zone b/src/initscripts/networking/zone index d2ae48b01..ec194f8db 100644 --- a/src/initscripts/networking/zone +++ b/src/initscripts/networking/zone @@ -40,6 +40,8 @@ case "$action" in up) message="Bringing up zone ${zone}..." + run_hooks pre-up ${CONFIG_ZONES}/${zone} ${zone} + # Check if bridge already exists zone_status=`brctl show 2>/dev/null` if ! echo "${zone_status}" | grep -q "^${zone}"; then @@ -48,19 +50,15 @@ case "$action" in ip link set ${zone} up || failed=1 (exit ${failed}) evaluate_retval standard + fi - # Attach ports - for config in ${CONFIG_ZONES}/${zone}/port-*; do - port=${config##*/}; port=${port#port-}; port=${port%%-*} - ZONE=${zone} /etc/init.d/networking/port ${port} attach ${config} - done + # Attach ports + for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do + port=${config##*/}; port=${port#port-}; port=${port%%-*} + ZONE=${zone} /etc/init.d/networking/port ${port} attach ${config} + done - ###/etc/init.d/networking/hooks post-up ${CONFIG_ZONES}/${zone} - else - log_warning_msg - message="Zone ${zone} does already exist." - log_warning_msg - fi + run_hooks post-up ${CONFIG_ZONES}/${zone} ${zone} ;; down) @@ -68,10 +66,10 @@ case "$action" in # Check if bridge already exists zone_status=`brctl show 2>/dev/null` if echo "${zone_status}" | grep -q "^${zone}"; then - #/etc/init.d/networking/hooks pre-down ${CONFIG_ZONES}/${zone} + run_hooks pre-down ${CONFIG_ZONES}/${zone} ${zone} # Detach ports - for config in ${CONFIG_ZONES}/${zone}/port-*; do + for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do port=${config##*/}; port=${port#port-}; port=${port%%-*} ZONE=${zone} /etc/init.d/networking/port ${port} detach ${config} done @@ -81,10 +79,11 @@ case "$action" in brctl delbr ${zone} || failed=1 (exit ${failed}) evaluate_retval standard + + run_hooks post-down ${CONFIG_ZONES}/${zone} ${zone} else - log_warning_msg - message="Zone ${zone} does not exist." - log_warning_msg + log_warning_msg ${message} + log_warning_msg "Zone ${zone} does not exist." fi ;; esac diff --git a/src/network/network b/src/network/network index 163ccd8a0..39091b192 100644 --- a/src/network/network +++ b/src/network/network @@ -169,7 +169,7 @@ function _exit() { function cmd() { decho "Running command: $@" - if verbose; then + if debug; then $@ else $@ >/dev/null @@ -233,6 +233,7 @@ function port_add() { error "Hook ${BOLD}${hook}${NORMAL} does not exist or is not executeable." return 1 fi + cmd /etc/init.d/networking/zone ${zone} up } function port_del() { @@ -320,6 +321,7 @@ function zone_add() { mkdir -p ${CONFIG_ZONES}/${zone} vecho "Successfully added zone ${zone}." + cmd /etc/init.d/networking/zone ${zone} up } function zone_del() { @@ -331,6 +333,7 @@ function zone_del() { return 1 fi + cmd /etc/init.d/networking/zone ${zone} down rm -rf ${CONFIG_ZONES}/${zone} vecho "Successfully removed zone ${zone}." } @@ -393,6 +396,21 @@ while [ "$#" -gt 0 ]; do port_del $@ _exit $? ;; + list) + all_zones + _exit $? + ;; + config) + zone=$1; shift + hook=$1; shift + if [ -x /etc/init.d/networking/hooks/${hook} ]; then + /etc/init.d/networking/hooks/${hook} config ${zone} $@ + RET=$? + else + error "\"${hook}\" is not a known hook." + fi + _exit $RET + ;; help) DO_RELOAD=0 usage zone 0 ;;