From f5ee091e42b31c7c519355e9af30b049e3e4bda0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 16 Jan 2016 17:09:47 +0100 Subject: [PATCH] Implement identify feature When executing "network [zone|port|device] XXX identify" the port, device or all ports of the zone (that support this feature) will flash for ten seconds. Ports will also flash when they are attached to or detached from a zone. This is supposed to make plugging in a cable into the right device is easier. Fixes #10969 Signed-off-by: Michael Tremer --- man/network-device.xml | 15 ++++++++++ man/network-zone.xml | 14 ++++++++++ src/bash-completion/network | 6 ++-- src/functions/functions.device | 50 ++++++++++++++++++++++++++++++++++ src/functions/functions.ports | 4 +++ src/functions/functions.util | 16 +++++++++++ src/functions/functions.zone | 35 ++++++++++++++++++++++++ src/network | 7 +++-- 8 files changed, 142 insertions(+), 5 deletions(-) diff --git a/man/network-device.xml b/man/network-device.xml index 3df6347..11dc04e 100644 --- a/man/network-device.xml +++ b/man/network-device.xml @@ -87,6 +87,21 @@ + + + DEVICE identify + + + + + This command only works for Ethernet adapters and will + make those that support this feature flash for a few + seconds. + It is handy to find the right device to put the cable in. + + + + DEVICE discover diff --git a/man/network-zone.xml b/man/network-zone.xml index 012bc84..acf4530 100644 --- a/man/network-zone.xml +++ b/man/network-zone.xml @@ -181,6 +181,20 @@ + + + ZONE identify + + + + + This command will make all ports of the zone flash for + a few seconds so that you can identify the correct network + adapters in the system. + + + + ZONE rename NAME diff --git a/src/bash-completion/network b/src/bash-completion/network index 002a1f9..6f63f1b 100644 --- a/src/bash-completion/network +++ b/src/bash-completion/network @@ -72,7 +72,7 @@ _network_device() { _network_device_subcommand() { local words=( $@ ) - local commands="discover monitor status unlock ussd" + local commands="discover identify monitor status unlock ussd" local cmd="$(_network_find_on_cmdline "${commands}")" if [[ -z "${cmd}" ]]; then COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) @@ -247,7 +247,7 @@ _network_port() { _network_port_subcommand() { local words=( $@ ) - local commands="create down edit remove status up" + local commands="create down edit identify remove status up" local cmd="$(_network_find_on_cmdline "${commands}")" if [[ -z "${cmd}" ]]; then COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) @@ -322,7 +322,7 @@ _network_zone_subcommand() { local words=( $@ ) - local commands="config disable down edit enable port rename status up" + local commands="config disable down edit enable identify port rename status up" local cmd="$(_network_find_on_cmdline "${commands}")" if [[ -z "${cmd}" ]]; then COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) diff --git a/src/functions/functions.device b/src/functions/functions.device index a79b81d..314386b 100644 --- a/src/functions/functions.device +++ b/src/functions/functions.device @@ -707,6 +707,56 @@ device_discover() { done } +device_identify() { + assert [ $# -ge 1 ] + + local device="${1}" + + # Flash for ten seconds by default + local seconds="10" + + # Run in background? + local background="false" + + local arg + while read arg; do + case "${arg}" in + --background) + background="true" + ;; + --seconds=*) + seconds="$(cli_get_val "${arg}")" + ;; + esac + done <<< "$(args $@)" + + assert isinteger seconds + + if ! device_exists "${device}"; then + log ERROR "Cannot identify device ${device}: Does not exist" + return ${EXIT_ERROR} + fi + + if ! device_is_ethernet "${device}"; then + log DEBUG "Cannot identify device ${device}: Not an ethernet device" + return ${EXIT_NOT_SUPPORTED} + fi + + log DEBUG "Identifying device ${device}" + + local command="ethtool --identify ${device} ${seconds}" + local ret=0 + + if enabled background; then + cmd_background "${command}" + else + cmd_quiet "${command}" + ret=$? + fi + + return ${ret} +} + device_has_ip() { local device=${1} local addr=${2} diff --git a/src/functions/functions.ports b/src/functions/functions.ports index 8af5619..40f4eae 100644 --- a/src/functions/functions.ports +++ b/src/functions/functions.ports @@ -413,3 +413,7 @@ ports_lowest_address() { # Get the first element which is the lowest MAC address list_head ${addresses} } + +port_identify() { + device_identify $@ +} diff --git a/src/functions/functions.util b/src/functions/functions.util index 3e5703e..91b5148 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -383,6 +383,22 @@ cmd_clean_environment() { return ${ret} } +# Executes the given command in background +cmd_background() { + cmd_quiet $@ & +} + +# Prints the PID of the process that was started last +cmd_background_get_pid() { + print "${!}" +} + +cmd_background_result() { + local pids=$@ + + wait ${pids} +} + # Increase security of the read command read() { builtin read -r $@ diff --git a/src/functions/functions.zone b/src/functions/functions.zone index baa89f0..a04b083 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -460,6 +460,35 @@ zone_status() { fi } +zone_identify() { + assert [ $# -ge 1 ] + + local zone="${1}" + shift + + assert zone_exists "${zone}" + + log INFO "Identifying zone ${zone}" + local pids + + local pid + local port + for port in $(zone_get_ports "${zone}"); do + # Identify all the ports + port_identify "${port}" --background $@ + + # Save the PIDs of the subprocesses + list_append pids "$(cmd_background_get_pid)" + done + + # Wait until all port_identfy processes have finished + for pid in ${pids}; do + cmd_background_result "${pid}" + done + + return ${EXIT_OK} +} + zone_get_ports() { local zone=${1} @@ -700,6 +729,9 @@ zone_port_attach() { local hook="$(zone_get_hook "${zone}")" assert isset hook + # Make the port briefly flash if supported + port_identify "${port}" --background + hook_zone_exec "${hook}" "port_attach" "${zone}" "${port}" "$@" local ret="${?}" @@ -763,6 +795,9 @@ zone_port_detach() { local hook=$(zone_get_hook "${zone}") assert isset hook + # Make the port briefly flash if supported + port_identify "${port}" --background + hook_zone_exec "${hook}" "port_detach" "${zone}" "${port}" "$@" local ret="${?}" diff --git a/src/network b/src/network index edeb825..632a423 100644 --- a/src/network +++ b/src/network @@ -81,6 +81,9 @@ cli_device() { discover) cli_device_discover ${device} $@ ;; + identify) + device_identify "${device}" $@ + ;; monitor) cli_device_monitor "${device}" $@ ;; @@ -533,7 +536,7 @@ cli_port() { shift 2 case "${action}" in - edit|create|remove|up|down|status) + edit|create|remove|up|down|status|identify) port_${action} "${port}" $@ ;; *) @@ -591,7 +594,7 @@ cli_zone() { rename) cli_zone_rename "${zone}" $@ ;; - config|disable|down|edit|enable|status|up) + config|disable|down|edit|enable|identify|status|up) zone_${action} ${zone} $@ ;; *) -- 2.47.3