]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Enhanced the logging and did minor code cleanups.
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Jun 2010 21:47:55 +0000 (23:47 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Jun 2010 21:47:55 +0000 (23:47 +0200)
functions
functions.device
functions.util

index 5c5372d9f48619a4f9ac6a2c1591fbd120a73f81..fb0fbd6e54378175576bd53c6dcea608af3bf2de 100644 (file)
--- a/functions
+++ b/functions
@@ -6,3 +6,8 @@ done
 
 # Reading in network tool configuration
 network_config_read
+
+# Create run dir
+if ! [ -d "${RUN_DIR}" ]; then
+       mkdir ${RUN_DIR}
+fi
index 2063a83cbfbd1f58a332bff6e44ab71eec3d25ed..c4851fcca0ea0fdabe579128781405cab723a7ab 100644 (file)
@@ -19,7 +19,7 @@
 #                                                                             #
 ###############################################################################
 
-function devicify() { 
+function devicify() {
        local device=${1}
 
        if device_exists ${device}; then
@@ -191,7 +191,34 @@ function device_get_address() {
 }
 
 function device_set_address() {
-       device_set_mac $@
+       local device=${1}
+       local addr=${2}
+       
+       if ! device_exists ${device}; then
+               error "Device '${device}' does not exist."
+               return ${EXIT_ERROR}
+       fi
+
+       log INFO "Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
+
+       local up
+       if device_is_up ${device}; then
+               device_set_down ${device}
+               up=1
+       fi
+
+       ip link set ${device} address ${addr}
+       local ret=$?
+
+       if [ "${up}" = "1" ]; then
+               device_set_up ${device}
+       fi
+
+       if [ "${ret}" != "0" ]; then
+               error_log "Could not set address '${addr}' on device '${device}'."
+       fi
+
+       return ${ret}
 }
 
 function devices_get_all() {
@@ -248,8 +275,20 @@ function device_get_free() {
        echo "${destination}"
 }
 
-# Should be renamed to device_set_name at some time
 function device_rename() {
+       warning_log "Called deprecated function 'device_rename'"
+
+       device_set_name $@
+}
+
+function device_hash() {
+       local device=${1}
+
+       macify ${device} | tr -d ':'
+}
+
+# Give the device a new name
+function device_set_name() {
        local source=$1
        local destination=$(device_get_free ${2})
 
@@ -271,17 +310,6 @@ function device_rename() {
        fi
 }
 
-function device_hash() {
-       local device=${1}
-
-       macify ${device} | tr -d ':'
-}
-
-# Give the device a new name
-function device_set_name() {
-       device_rename $@
-}
-
 # Set device up
 function device_set_up() {
        local device=$(devicify ${1})
@@ -306,23 +334,9 @@ function device_set_down() {
 
 # Set new address to a device
 function device_set_mac() {
-       local port=${1}
-       local mac=${2}
+       warning_log "Called deprecated function 'device_set_mac'"
 
-       local up
-       if device_is_up ${port}; then
-               device_set_down ${port}
-               up=1
-       fi
-
-       ip link set ${port} address ${mac}
-       local ret=$?
-
-       if [ "${up}" = "1" ]; then
-               device_set_up ${port}
-       fi
-
-       return ${ret}
+       device_set_address $@
 }
 
 function device_get_mtu() {
@@ -338,20 +352,38 @@ function device_get_mtu() {
 
 # Set mtu to a device
 function device_set_mtu() {
-       local port=${1}
+       local device=${1}
        local mtu=${2}
 
+       if ! device_exists ${device}; then
+               error "Device '${device}' does not exist."
+               return ${EXIT_ERROR}
+       fi
+       
+       local oldmtu=$(device_get_mtu ${device})
+
+       if [ "${oldmtu}" = "${mtu}" ]; then
+               # No need to set mtu.
+               return ${EXIT_OK}
+       fi
+
+       log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
+
        local up
-       if device_is_up ${port}; then
-               device_set_down ${port}
+       if device_is_up ${device}; then
+               device_set_down ${device}
                up=1
        fi
 
-       ip link set ${port} mtu ${mtu}
+       ip link set ${device} mtu ${mtu}
        local ret=$?
 
        if [ "${up}" = "1" ]; then
-               device_set_up ${port}
+               device_set_up ${device}
+       fi
+
+       if [ "${ret}" != "0" ]; then
+               error_log "Could not set mtu '${mtu}' on device '${device}'."
        fi
 
        return ${ret}
@@ -360,6 +392,8 @@ function device_set_mtu() {
 function device_discover() {
        local device=${1}
 
+       log INFO "Running discovery process on device '${device}'."
+
        local hook
        for hook in $(hooks_get_all); do
                hook_exec ${hook} discover ${device}
@@ -381,37 +415,57 @@ function device_virtual_create() {
                mac=$(mac_generate)
        fi
 
+       log INFO "Creating virtual device '${newport}' with address '${mac}'."
+
        # Bring up the parent device
        # XXX Do we need this here?
        #device_set_up ${port}
 
        vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
        vconfig add ${port} ${vid} >/dev/null
-       [ $? -ne 0 ] && return ${EXIT_ERROR}
+       
+       if [ $? -ne ${EXIT_OK} ]; then
+               error_log "Could not create virtual device '${newport}'."
+               return ${EXIT_ERROR}
+       fi
 
        # The device is expected to be named like ${port}.${vid}
        # and will be renamed to the virtual schema
        device_set_name ${port}.${vid} ${newport}
 
+       if [ $? -ne ${EXIT_OK} ]; then
+               error_log "Could not set name of virtual device '${newport}'."
+               return ${EXIT_ERROR}
+       fi
+
        # Setting new mac address
        device_set_address ${newport} ${mac}
+       
+       if [ $? -ne ${EXIT_OK} ]; then
+               error_log "Could not set address '${mac}' to virtual device '${newport}'."
+               return ${EXIT_ERROR}
+       fi
 
        # Bring up the new device
        device_set_up ${newport}
 
-       log DEBUG "Created virtual device ${newport} (${mac})"
        return ${EXIT_OK}
 }
 
 function device_virtual_remove() {
        local device=$(devicify ${1})
 
+       log INFO "Removing virtual device '${device}' with address '$(macify ${devive})'."
+
        device_set_down ${device}
 
        vconfig rem ${device} >/dev/null
-       [ $? -ne 0 ] && return ${EXIT_ERROR}
 
-       log DEBUG "Removed virtual device ${device}"
+       if [ $? -ne ${EXIT_OK} ]; then
+               error_log "Could not remote virtual device '${newport}'."
+               return ${EXIT_ERROR}
+       fi
+
        return ${EXIT_OK}
 }
 
@@ -421,6 +475,8 @@ function device_bonding_create() {
 
        [ -z "${mac}" ] && mac=$(mac_generate)
 
+       log INFO "Creating bonding device '${device}' (${mac})."
+
        echo "+${device}" > /sys/class/net/bonding_masters
        device_set_mac ${mac}
        device_set_up ${device}
@@ -429,6 +485,8 @@ function device_bonding_create() {
 function device_bonding_remove() {
        local device=$(devicify ${1})
 
+       log INFO "Remove bonding device '${device}'."
+
        device_set_down ${device}
        echo "-${device}" > /sys/class/net/bonding_masters
 }
@@ -437,12 +495,17 @@ function bonding_set_mode() {
        local device=${1}
        local mode=${2}
 
+       log INFO "Setting bonding mode on '${device}' '${mode}'."
+
        echo "${mode}" > /sys/class/net/${device}/bonding/mode
 }
 
 function bonding_enslave_device() {
        local device=$(devicify ${1})
        local slave=$(devicify ${2})
+       shift 2
+
+       log INFO "Enslaving slave '${slave}' to '${device}'."
 
        device_set_down ${slave}
        echo "+${slave}" > /sys/class/net/${device}/bonding/slaves
@@ -462,6 +525,8 @@ function bridge_attach_device() {
                return ${EXIT_ERROR}
        fi
 
+       log INFO "Attaching device '${device}' to bridge '${bridge}'."
+
        # XXX device_set_up ${device} # Do we need this here?
 
        brctl addif ${bridge} ${device}
@@ -481,7 +546,9 @@ function bridge_detach_device() {
                return ${EXIT_ERROR}
        fi
 
-       brctl delif ${zone} ${device}
+       log INFO "Detaching device '${device}' from bridge '${bridge}'."
+
+       brctl delif ${bridge} ${device}
 
        device_set_down ${device}
 }
index 9eb5afe3e7b9e440e0c34d4924f595815e05338a..c3b3989e2e23206739547d080e61235575e0cf56 100644 (file)
@@ -24,11 +24,21 @@ function error() {
        echo -e " ${FAIL}ERROR${NORMAL}  : $@" >&2
 }
 
+function error_log() {
+       error "$@"
+       log ERROR "$@"
+}
+
 # Print a pretty warn message
 function warning() {
        echo -e " ${WARN}WARNING${NORMAL}: $@" >&2
 }
 
+function warning_log() {
+       warning "$@"
+       log WARNING "$@"
+}
+
 function listsort() {
        local i
        for i in $@; do
@@ -52,6 +62,8 @@ function config_write() {
        # Check if all values to be written are sane
        config_check
 
+       log DEBUG "Writing configuration file ${config_file}."
+
        > ${config_file}
 
        local param
@@ -78,7 +90,8 @@ function network_config_set() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        *=*)
-                               eval ${1}                       
+                               log INFO "Setting configuration option '${1}'".                 
+                               eval ${1}
                                ;;
                        *)
                                warning "Invalid parameter given: ${1}"