]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Add some sanity checks when removing a port.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 25 Jul 2010 16:28:35 +0000 (18:28 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 25 Jul 2010 16:28:35 +0000 (18:28 +0200)
functions.ports
header-port
hooks/ports/bonding
hooks/ports/virtual

index feecaa241b02e33c8790ed6c6f64305530d64d78..9cda5641e4fd618f0a11d918c1848c62b7684d36 100644 (file)
@@ -99,10 +99,34 @@ function port_destroy() {
 
        port_exists ${port} || return ${EXIT_OK}
 
-       local attached_zone=$(port_is_attached ${port})
+       # Check if the port is attached to any zone and don't delete it.
+       local ok=${EXIT_OK}
 
+       local attached_zone=$(port_is_attached ${port})
        if [ -n "${attached_zone}" ]; then
-               error "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
+               error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
+               ok=${EXIT_ERROR}
+       fi
+
+       # Check if the port is linked to any other port and don't allow the user
+       # to delete it.
+       local other_port
+       for other_port in $(ports_get); do
+               [ "${other_port}" = "${port}" ] && continue
+
+               if listmatch ${port} $(port_get_parents ${other_port}); then
+                       error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
+                       ok=${EXIT_ERROR}
+               fi
+
+               if listmatch ${port} $(port_get_children ${other_port}); then
+                       error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
+                       ok=${EXIT_ERROR}
+               fi
+       done
+
+       # If ok says we are not okay --> exit
+       if [ ${ok} -ne ${EXIT_OK} ]; then
                return ${EXIT_ERROR}
        fi
 
@@ -136,6 +160,10 @@ function port_status() {
        port_cmd status $@
 }
 
+function port_info() {
+       port_cmd info $@
+}
+
 function port_cmd() {
        local cmd=${1}
        local port=${2}
@@ -194,3 +222,29 @@ function port_find_free() {
                i=$(( ${i} + 1 ))
        done
 }
+
+function port_get_info() {
+       local port=${1}
+       local key=${2}
+
+       assert isset port
+       assert port_exists ${port}
+       assert isset key
+
+       (
+               eval $(port_info ${port})
+               echo "${!key}"
+       )
+}
+
+function port_get_parents() {
+       local port=${1}
+
+       port_get_info ${port} PORT_PARENTS
+}
+
+function port_get_children() {
+       local port=${1}
+
+       port_get_info ${port} PORT_CHILDREN
+}
index d06b2e119903ec8e8982492d0982373cc46283f2..95b39d0e62007d17f22e2347d8709d27b6a2792f 100644 (file)
@@ -22,6 +22,7 @@
 . /lib/network/functions
 
 HOOK=$(basename ${0})
+INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN"
 
 while [ $# -gt 0 ]; do
        case "${1}" in
@@ -39,7 +40,7 @@ done
 
 function run() {
        case "${action}" in
-               edit|add|create|rem|up|down|status)
+               edit|add|create|rem|up|down|status|info)
                        _${action} $@
                        ;;
        esac
@@ -47,3 +48,26 @@ function run() {
        error "Port hook '${HOOK}' didn't exit properly."
        exit ${EXIT_ERROR}
 }
+
+function _info() {
+       local port=${1}
+       shift
+
+       assert isset port
+
+       config_read $(port_file ${port})
+
+       local key
+       local val
+       for key in PORT_PARENTS PORT_CHILDREN; do
+               val="${key}_VAR"
+               val=${!val}
+               eval "${key}=\"${!val}\""
+       done
+
+       for key in ${INFO_SETTINGS}; do
+               echo "${key}=\"${!key}\""
+       done
+
+       exit ${ERROR_OK}
+}
index 9e0443539590575263dbb73c18e91fea33a92b29..0a8535f66445cfb9f0402abd523fa35a3d4c9766 100755 (executable)
@@ -23,6 +23,8 @@
 
 HOOK_SETTINGS="HOOK DEVICE_MAC MIIMON MODE SLAVES"
 
+PORT_CHILDREN_VAR="SLAVES"
+
 DEVICE_MAC=$(mac_generate)
 MIIMON=100
 
index 10f0b80d271c63427d1b388604a8ea891ff638d9..98786656190f0b867d8a27b6d886d767782be1ff 100755 (executable)
@@ -23,6 +23,8 @@
 
 HOOK_SETTINGS="HOOK DEVICE DEVICE_MAC DEVICE_VID"
 
+PORT_PARENTS_VAR="DEVICE"
+
 DEVICE_MAC=$(mac_generate)
 
 function _check() {