]> git.ipfire.org Git - people/ms/network.git/commitdiff
Implement identify feature
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 Jan 2016 16:09:47 +0000 (17:09 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 Jan 2016 16:11:32 +0000 (17:11 +0100)
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 <michael.tremer@ipfire.org>
man/network-device.xml
man/network-zone.xml
src/bash-completion/network
src/functions/functions.device
src/functions/functions.ports
src/functions/functions.util
src/functions/functions.zone
src/network

index 3df6347179fd91f37b12399742389223496c35eb..11dc04ee1a6c4c9a91b5275bbca3616e0e070de5 100644 (file)
                                </listitem>
                        </varlistentry>
 
+                       <varlistentry>
+                               <term>
+                                       <command><replaceable>DEVICE</replaceable> identify</command>
+                               </term>
+
+                               <listitem>
+                                       <para>
+                                               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.
+                                       </para>
+                               </listitem>
+                       </varlistentry>
+
                        <varlistentry>
                                <term>
                                        <command><replaceable>DEVICE</replaceable> discover</command>
index 012bc8490fc14407bcf50d3b1810a171f644537e..acf4530d98e9bba28864146d9a952e6e9b64a804 100644 (file)
                                </listitem>
                        </varlistentry>
 
+                       <varlistentry>
+                               <term>
+                                       <command><replaceable>ZONE</replaceable> identify</command>
+                               </term>
+
+                               <listitem>
+                                       <para>
+                                               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.
+                                       </para>
+                               </listitem>
+                       </varlistentry>
+
                        <varlistentry>
                                <term>
                                        <command><replaceable>ZONE</replaceable> rename <replaceable>NAME</replaceable></command>
index 002a1f9a93adc0778c39ba0991ea02327c020867..6f63f1b3955fa02c5d90b88d6ad739601f783df1 100644 (file)
@@ -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}") )
index a79b81d58e6216e0121d3b9aebbcde546facf44f..314386b947d754487e09ec88e9db655506198898 100644 (file)
@@ -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}
index 8af5619ed96c8763ef791ef3bb1c3579cd6e4711..40f4eaea6ebf4d39c078d6b6970a88de3bd98b0f 100644 (file)
@@ -413,3 +413,7 @@ ports_lowest_address() {
        # Get the first element which is the lowest MAC address
        list_head ${addresses}
 }
+
+port_identify() {
+       device_identify $@
+}
index 3e5703e5198707b66f394570b1d708148e5bcdca..91b5148ca56d1836a09f910539f51833acd7693e 100644 (file)
@@ -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 $@
index baa89f04bde320f569ad929e8c827f88be4068f7..a04b08317017e3cee48854c99c947b0fe1bfd9d9 100644 (file)
@@ -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="${?}"
 
index edeb825d9116f2b40eaee53cc5d511ee7a7df7dd..632a423bd5b8e5782a6fb92ef37210b45d3a25f1 100644 (file)
@@ -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} $@
                                ;;
                        *)