From 8895cf8ffcabfd73bde08b446593abb1ec5e4655 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 25 May 2011 15:11:22 +0000 Subject: [PATCH] hotplug: Return port name for a device. --- functions.cli | 21 +++++++++++++++++++++ functions.device | 24 ++++++++++++++++++++++++ functions.ports | 10 ++++++++++ header-port | 6 +++++- hooks/ports/ethernet | 16 ++++++++++++++++ network | 2 +- 6 files changed, 77 insertions(+), 2 deletions(-) diff --git a/functions.cli b/functions.cli index 82a9c6f4..411188da 100644 --- a/functions.cli +++ b/functions.cli @@ -151,6 +151,27 @@ function cli_hostname() { exit ${EXIT_OK} } +function cli_hotplug() { + if cli_help_requested $@; then + cli_usage root-hotplug + exit ${EXIT_OK} + fi + + local command=${1} + shift + + case "${command}" in + device) + device_hotplug $@ + exit $? + ;; + *) + cli_usage root-hotplug + exit ${EXIT_OK} + ;; + esac +} + function cli_port() { if cli_help_requested $@; then cli_usage root-port diff --git a/functions.device b/functions.device index 1ddc7d2c..7367f642 100644 --- a/functions.device +++ b/functions.device @@ -549,3 +549,27 @@ function device_get_tx_errors() { __device_get_file ${device} statistics/tx_errors } + +function device_hotplug() { + local device=${1} + shift + + assert isset device + #assert device_exists ${device} + + if ! device_is_free ${device}; then + log ERROR "The device '${device}' does not look to be unused." + log ERROR "Don't proceed." + # return ${EXIT_ERROR} + fi + + for port in $(ports_get_all); do + port_cmd hotplug ${port} ${device} + if [ $? -eq ${EXIT_OK} ]; then + echo "${port}" + exit ${EXIT_OK} + fi + done + + exit ${EXIT_ERROR} +} diff --git a/functions.ports b/functions.ports index 9cda5641..8aa4cac1 100644 --- a/functions.ports +++ b/functions.ports @@ -23,6 +23,16 @@ function port_dir() { echo "${CONFIG_DIR}/ports" } +function ports_get_all() { + local port + + for port in $(port_dir)/*; do + [ -f "${port}" ] || continue + + basename ${port} + done +} + function port_file() { local port=${1} diff --git a/header-port b/header-port index 95b39d0e..bb319a4f 100644 --- a/header-port +++ b/header-port @@ -40,7 +40,7 @@ done function run() { case "${action}" in - edit|add|create|rem|up|down|status|info) + edit|add|create|rem|up|down|status|info|hotplug) _${action} $@ ;; esac @@ -49,6 +49,10 @@ function run() { exit ${EXIT_ERROR} } +function _hotplug() { + exit ${EXIT_ERROR} +} + function _info() { local port=${1} shift diff --git a/hooks/ports/ethernet b/hooks/ports/ethernet index 2d49f49a..cf37aa05 100755 --- a/hooks/ports/ethernet +++ b/hooks/ports/ethernet @@ -60,6 +60,22 @@ function _down() { exit ${EXIT_OK} } +function _hotplug() { + local port=${1} + local device=${2} + + assert isset port + assert isset device + + config_read $(port_file ${port}) + + if [ "${DEVICE_MAC}" = "$(device_get_address ${device})" ]; then + exit ${EXIT_OK} + fi + + exit ${EXIT_ERROR} +} + function _status() { local port=${1} shift diff --git a/network b/network index e84adff9..54ad8086 100755 --- a/network +++ b/network @@ -42,7 +42,7 @@ case "${action}" in init_run ;; - config|hostname|port|device|zone|start|stop|restart|status|reset) + config|hostname|hotplug|port|device|zone|start|stop|restart|status|reset) cli_${action} $@ ;; -- 2.47.2