]> git.ipfire.org Git - people/stevee/network.git/blobdiff - functions.device
bridge: Replace brctl by ip.
[people/stevee/network.git] / functions.device
index ffff8a75ec8d8529964ae24975ccb7d4cd0b46f6..c9836c0cc9129fedf23e4c2f789f213e289bd535 100644 (file)
@@ -73,6 +73,25 @@ function device_exists() {
        serial_exists ${device}
 }
 
+function device_delete() {
+       local device=${1}
+       assert isset device
+
+       # Nothing to do, it device does not exist.
+       device_exists ${device} || return ${EXIT_OK}
+
+       # Delete the device.
+       cmd_quiet ip link delete ${device}
+       local ret=$?
+
+       if [ ${ret} -ne ${EXIT_OK} ]; then
+               log ERROR "device: Could not delete device '${device}': ${ret}"
+               return ${EXIT_ERROR}
+       fi
+
+       return ${ret}
+}
+
 function device_has_flag() {
        local device=${1}
        local flag=${2}
@@ -95,6 +114,38 @@ function device_is_up() {
        device_has_flag ${device} 0x1
 }
 
+function device_ifindex_to_name() {
+       local idx=${1}
+       assert isset idx
+
+       local device device_idx
+       for device in ${SYS_CLASS_NET}/*; do
+               device=$(basename ${device})
+               device_exists ${device} || continue
+
+               device_idx=$(device_get_ifindex ${device})
+
+               if [ "${device_idx}" = "${idx}" ]; then
+                       print "${device}"
+                       return ${EXIT_OK}
+               fi
+       done
+
+       return ${EXIT_ERROR}
+}
+
+function device_get_ifindex() {
+       local device=${1}
+       assert isset device
+
+       local path="${SYS_CLASS_NET}/${1}/ifindex"
+
+       # Check if file can be read.
+       [ -r "${path}" ] || return ${EXIT_ERROR}
+
+       print "$(<${path})"
+}
+
 # Check if the device is a bonding device
 function device_is_bonding() {
        [ -d "/sys/class/net/${1}/bonding" ]
@@ -118,6 +169,22 @@ function device_is_bridge_attached() {
        [ -d "${SYS_CLASS_NET}/${device}/brport" ]
 }
 
+function device_get_bridge() {
+       local device=${1}
+       assert isset device
+
+       # Check if device is attached to a bridge.
+       device_is_bridge_attached ${device} || return ${EXIT_ERROR}
+
+       local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
+       [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
+
+       local ifindex=$(<${ifindex_path})
+       assert isset ifindex
+
+       device_ifindex_to_name ${ifindex}
+}
+
 # Check if the device is a virtual device
 function device_is_virtual() {
        local device=${1}
@@ -140,6 +207,10 @@ function device_has_virtuals() {
 function device_get_virtuals() {
        local device=${1}
 
+       # If no 8021q module has been loaded into the kernel,
+       # we cannot do anything.
+       [ -r "/proc/net/vlan/config" ] || return ${EXIT_OK}
+
        local dev spacer1 id spacer2 parent
        while read dev spacer1 id spacer2 parent; do
                [ "${parent}" = "${device}" ] && echo "${dev}"