# 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
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
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}
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}