From: Michael Tremer Date: Sun, 27 May 2012 12:07:06 +0000 (+0000) Subject: Move all of the device renaming code to network-hotplug-rename. X-Git-Tag: 004~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2eaf16f3712046a905b148310092533e98d2c455;p=network.git Move all of the device renaming code to network-hotplug-rename. The port hooks get an additional method called _hotplug() which handles the actual hotplugging stuff. To rename devices, there is now the _hotplug_rename() function which returns either EXIT_TRUE or EXIT_FALSE. --- diff --git a/functions.constants b/functions.constants index 52f35649..56baa6e3 100644 --- a/functions.constants +++ b/functions.constants @@ -50,6 +50,9 @@ EXIT_ERROR=1 EXIT_CONF_ERROR=2 EXIT_ERROR_ASSERT=3 +EXIT_TRUE=0 +EXIT_FALSE=1 + STATUS_UP=0 STATUS_DOWN=1 STATUS_NOCARRIER=2 diff --git a/functions.device b/functions.device index f0dd1330..3c1005db 100644 --- a/functions.device +++ b/functions.device @@ -568,41 +568,3 @@ function device_get_tx_errors() { __device_get_file ${device} statistics/tx_errors } - -function device_hotplug() { - local device=${1} - shift - - assert isset device - - # Just check if the device has already vanished. - device_exists ${device} || return ${EXIT_ERROR} - - if ! device_is_free ${device}; then - log ERROR "The device '${device}' is in use." - return ${EXIT_ERROR} - fi - - if ! device_is_real ${device}; then - log DEBUG "Don't rename any virtual devices." - return ${EXIT_OK} - fi - - for port in $(ports_get_all); do - port_cmd hotplug ${port} ${device} - if [ $? -eq ${EXIT_OK} ]; then - echo "${port}" - return ${EXIT_OK} - fi - done - - # If no port configuration could be found, we search for the next - # unused name and return that. - local port=$(port_find_free ${PORT_PATTERN}) - - log DEBUG "Could not find an existing port configuration for '${device}'." - log DEBUG "${device} --> ${port}" - - echo "${port}" - return ${EXIT_OK} -} diff --git a/header-port b/header-port index bb319a4f..bb169174 100644 --- a/header-port +++ b/header-port @@ -19,7 +19,7 @@ # # ############################################################################### -. /lib/network/functions +. /usr/lib/network/functions HOOK=$(basename ${0}) INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN" @@ -40,7 +40,7 @@ done function run() { case "${action}" in - edit|add|create|rem|up|down|status|info|hotplug) + edit|add|create|rem|up|down|status|info|hotplug|hotplug_rename) _${action} $@ ;; esac @@ -49,8 +49,21 @@ function run() { exit ${EXIT_ERROR} } +# This function is called after a device has been plugged +# into the system and got its correct name. +# The function is intended to create child ports and things +# like that. function _hotplug() { - exit ${EXIT_ERROR} + exit ${EXIT_OK} +} + +# This function gets called when a device is plugged in +# to determine the right name. +# The first argument is the port which should be tested +# against the second argument which is the device that +# has been plugged in. +function _hotplug_rename() { + exit ${EXIT_FALSE} } function _info() { diff --git a/hooks/ports/ethernet b/hooks/ports/ethernet index 499889ad..5d93fc81 100755 --- a/hooks/ports/ethernet +++ b/hooks/ports/ethernet @@ -60,7 +60,7 @@ function _down() { exit ${EXIT_OK} } -function _hotplug() { +function _hotplug_rename() { local port=${1} local device=${2} diff --git a/udev/network-hotplug-rename b/udev/network-hotplug-rename index 1678deb6..0d3767d9 100755 --- a/udev/network-hotplug-rename +++ b/udev/network-hotplug-rename @@ -38,11 +38,44 @@ assert isset INTERFACE # Log what we are doing here. log DEBUG "Called for interface '${INTERFACE}'." +# Just check if the device has already vanished. +device_exists ${INTERFACE} || exit ${EXIT_ERROR} + # Acquiring lock for this operation. lock_acquire ${LOCKFILE} -# Get the name of that device from the configuration -# or return a new one if it is unknown. -device_hotplug ${INTERFACE} +# Check if the device is already in use and +# prevent the script to touch it in any way. +if ! device_is_free ${INTERFACE}; then + log ERROR "The device '${INTERFACE}' is in use." + exit ${EXIT_ERROR} +fi + +# Determine the type of the device and then see what +# we need to do with it. +type=$(device_get_type ${INTERFACE}) +log DEBUG "Interface '${INTERFACE}' is of type '${type}'." + +case "${type}" in + ethernet|real) + # Search within all the port configurations + # if this port has already been configured. + for port in $(ports_get_all); do + port_cmd hotplug_rename ${port} ${INTERFACE} &>/dev/null + if [ $? -eq ${EXIT_TRUE} ]; then + echo "${port}" + exit ${EXIT_OK} + fi + done + + # If no port configuration could be found, + # we search for the next unused name and return that. + port=$(port_find_free ${PORT_PATTERN}) + echo "${port}" + + log DEBUG "Could not find an existing port configuration for '${INTERFACE}'." + log DEBUG "${INTERFACE} --> ${port}" + ;; +esac -exit $? +exit ${EXIT_OK}