]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Improve function that creates virtual devices.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Jun 2010 13:37:11 +0000 (15:37 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Jun 2010 13:37:11 +0000 (15:37 +0200)
This function grabs unused and preconfigured devices and
does a lot more checking if it operates correctly.

functions.device

index c4851fcca0ea0fdabe579128781405cab723a7ab..9342a532261ea196f4b21ae8beac4b800d9ee78e 100644 (file)
@@ -110,6 +110,12 @@ function device_is_virtual() {
 
 # Check if the device has virtual devices
 function device_has_virtuals() {
+       local device=${1}
+
+       if device_is_virtual ${device}; then
+               return 1
+       fi
+
        if [ ! -e "/proc/net/vlan/config" ]; then
                return 1
        fi
@@ -248,12 +254,12 @@ function device_is_free() {
 function device_is_used() {
        local device=$(devicify ${1})
 
-       device_has_vlans ${device} && \
-               return ${EXIT_ERROR}
+       device_has_virtuals ${device} && \
+               return ${EXIT_OK}
        device_is_bonded ${device} && \
-               return ${EXIT_ERROR}
+               return ${EXIT_OK}
 
-       return ${EXIT_OK}
+       return ${EXIT_ERROR}
 }
 
 # XXX to be removed I think
@@ -417,27 +423,67 @@ function device_virtual_create() {
 
        log INFO "Creating virtual device '${newport}' with address '${mac}'."
 
-       # Bring up the parent device
-       # XXX Do we need this here?
-       #device_set_up ${port}
+       local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
 
-       vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
-       vconfig add ${port} ${vid} >/dev/null
-       
-       if [ $? -ne ${EXIT_OK} ]; then
-               error_log "Could not create virtual device '${newport}'."
+       if device_exists ${oldport}; then
+               local differences
+
+               if [ "${oldport}" != "${newport}" ]; then
+                       differences="${differences} name"
+               fi
+               if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
+                       differences="${differences} address"
+               fi
+               
+               echo "differences: $differences"
+
+               if [ -n "${differences}" ]; then
+                       if device_is_used ${oldport}; then
+                               error_log "There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on." 
+                               return ${EXIT_ERROR}
+                       else
+                               log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves."
+                       fi
+               else
+                       log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on."
+
+                       device_set_up ${oldport}
+                       return ${EXIT_OK}
+               fi
+
+       else
+               log DEBUG "Virtual device '${newport}' does not exist, yet."
+
+               vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
+               vconfig add ${port} ${vid} >/dev/null
+               
+               if [ $? -ne ${EXIT_OK} ]; then
+                       error_log "Could not create virtual device '${newport}'."
+                       return ${EXIT_ERROR}
+               fi
+
+               oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
+
+       fi
+
+       assert device_exists ${oldport}
+
+       if ! device_exists ${oldport}; then
+               error "Could not determine the created 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}
+       device_set_name ${oldport} ${newport}
 
        if [ $? -ne ${EXIT_OK} ]; then
                error_log "Could not set name of virtual device '${newport}'."
                return ${EXIT_ERROR}
        fi
 
+       assert device_exists ${newport}
+
        # Setting new mac address
        device_set_address ${newport} ${mac}
        
@@ -469,6 +515,25 @@ function device_virtual_remove() {
        return ${EXIT_OK}
 }
 
+function device_virtual_get_by_parent_and_vid() {
+       local parent=${1}
+       local vid=${2}
+
+       local v_port
+       local v_id
+       local v_parent
+
+       fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
+               while read v_port v_id v_parent; do
+                       if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
+                               echo "${v_port}"
+                               return ${EXIT_OK}
+                       fi
+               done
+
+       return ${EXIT_ERROR}
+}
+
 function device_bonding_create() {
        local device=${1}
        local mac=${2}