2011-10-07 03:03:00 +08:00
|
|
|
#! /bin/sh
|
|
|
|
#
|
2013-11-26 23:15:13 +08:00
|
|
|
# /usr/sbin/dahdi_span_assignments:
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
# this script can be used both from udev and
|
|
|
|
# from the command line to assign/unassign and list
|
2013-09-23 19:59:14 +08:00
|
|
|
# current span assignments.
|
|
|
|
#
|
2013-11-19 00:14:34 +08:00
|
|
|
# It uses a configuration file: $DAHDICONFDIR/assigned-spans.conf
|
2013-09-23 19:59:14 +08:00
|
|
|
# (default DAHDICONFDIR=/etc/dahdi)
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
# The first argument is an action:
|
2013-09-23 19:59:14 +08:00
|
|
|
# "auto" - trigger driver auto_assign attribute for given devices
|
|
|
|
# (no configuration file is used)
|
|
|
|
# "add" - assign (spans which are not already assigned), according
|
2013-11-19 00:14:34 +08:00
|
|
|
# to /etc/dahdi/assigned-spans.conf configuration file
|
2013-09-23 19:59:14 +08:00
|
|
|
# "remove" - unassign spans which are not already unassigned
|
|
|
|
# "list" - human-readable list of all spans (with/without assignments)
|
2013-11-19 00:14:34 +08:00
|
|
|
# "dumpconfig" - dump current assignments in a /etc/dahdi/assigned-spans.conf
|
2013-09-23 19:59:14 +08:00
|
|
|
# compatible format
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
# Without further arguments, it operates on all existing spans
|
|
|
|
# With one or more sysfs dahdi_devices it is limited to those.
|
|
|
|
#
|
2013-09-29 16:09:56 +08:00
|
|
|
# We may use alternative "keys" for device matching:
|
|
|
|
# * Available keys:
|
|
|
|
# - "hwid" - Hardware id attribute from sysfs
|
|
|
|
# - "@location" - Location attribute from sysfs (embeded inside '<>')
|
|
|
|
# - "/devpath" - The sysfs absolute devpath
|
|
|
|
#
|
|
|
|
# * During "dumpconfig", for each device we take the first available key:
|
|
|
|
# - The preference is: "hwid" or else "@location" or else "/devpath"
|
|
|
|
# - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable
|
|
|
|
# or the '{-k|--key} key' command line option.
|
|
|
|
#
|
|
|
|
# * During "add":
|
|
|
|
# - Any key match is valid (hwid/location/devpath)
|
|
|
|
# - Shell globs (wildcards: '*', '?', '[...]') may be optionally used.
|
|
|
|
#
|
|
|
|
# Command line options:
|
|
|
|
# - The '-h|--help' show a usage message.
|
|
|
|
# - The '-n|--dry-run' affects the "add" and "remove" operations.
|
|
|
|
# - The '-v|--verbose' currently shows device matches during "add" operation.
|
|
|
|
# - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
|
|
|
|
# variable.
|
|
|
|
#
|
2011-10-07 03:03:00 +08:00
|
|
|
# Examples:
|
2013-11-26 23:15:13 +08:00
|
|
|
# dahdi_span_assignments list
|
|
|
|
# dahdi_span_assignments add # all unassigned devices
|
|
|
|
# dahdi_span_assignments add /sys/bus/dahdi_devices/devices/astribanks:xbus-00
|
|
|
|
# dahdi_span_assignments remove # all assigned devices
|
|
|
|
# dahdi_span_assignments -k location dumpconfig
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
devbase='/sys/bus/dahdi_devices/devices'
|
|
|
|
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
|
2013-11-27 02:55:09 +08:00
|
|
|
DAHDISASSIGNEDSPANSCONF="${DAHDIASSIGNEDSPANSCONF:-"${DAHDICONFDIR}/assigned-spans.conf"}"
|
2013-09-29 16:09:56 +08:00
|
|
|
SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
|
|
|
|
dry_run=
|
|
|
|
verbose=
|
2011-10-07 03:03:00 +08:00
|
|
|
|
|
|
|
usage() {
|
2013-09-29 16:09:56 +08:00
|
|
|
echo >&2 "Usage: $0 [options] action [devpath ...]"
|
|
|
|
echo >&2 " action:"
|
|
|
|
echo >&2 " auto - trigger driver auto_assign attribute for given devices"
|
2013-11-19 00:14:34 +08:00
|
|
|
echo >&2 " add - assign spans, according to /etc/dahdi/assigned-spans.conf"
|
2013-09-29 16:09:56 +08:00
|
|
|
echo >&2 " remove - unassign spans"
|
|
|
|
echo >&2 " list - human-readable list of all spans"
|
|
|
|
echo >&2 " dumpconfig - dump current state as new configuration"
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 " options:"
|
|
|
|
echo >&2 " -h|--help - Show this help"
|
|
|
|
echo >&2 " -n|--dry-run - For 'add/remove' actions"
|
|
|
|
echo >&2 " -v|--versbose - Show matches during 'add' action"
|
|
|
|
echo >&2 " -k|--key <k> - Override prefered key during dumpconfig action"
|
2011-10-07 03:03:00 +08:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2013-09-29 16:09:56 +08:00
|
|
|
# Parse command line options
|
|
|
|
TEMP=`getopt -o hnvk: --long help,dry-run,verbose,key: -n "$0" -- "$@"`
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
echo >&2 "Bad options"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Note the quotes around `$TEMP': they are essential!
|
|
|
|
eval set -- "$TEMP"
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
-n|--dry-run)
|
|
|
|
dry_run='true'
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-v|--verbose)
|
|
|
|
verbose='true'
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-k|--key)
|
|
|
|
SPAN_ASSIGNMENTS_KEY="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Internal error!"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
if [ "$#" -eq 0 ]; then
|
2013-09-29 16:09:56 +08:00
|
|
|
echo >&2 "Missing action argument"
|
2011-10-07 03:03:00 +08:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
action="$1"
|
|
|
|
shift
|
|
|
|
|
2013-09-29 16:09:56 +08:00
|
|
|
# Validate SPAN_ASSIGNMENTS_KEY
|
|
|
|
case "$SPAN_ASSIGNMENTS_KEY" in
|
|
|
|
hwid|location|devpath)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo >&2 "Bad SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
if [ ! -d "$devbase" ]; then
|
|
|
|
echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Use given devices or otherwise, all existing devices
|
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
DEVICES="$@"
|
|
|
|
else
|
|
|
|
DEVICES=`echo $devbase/*`
|
|
|
|
fi
|
|
|
|
|
2013-09-29 16:09:56 +08:00
|
|
|
# Beware of special characters in attributes
|
|
|
|
attr_clean() {
|
|
|
|
cat "$1" 2>/dev/null | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
|
|
|
|
}
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
show_devices() {
|
|
|
|
|
|
|
|
for device in $DEVICES
|
|
|
|
do
|
2013-09-29 16:09:56 +08:00
|
|
|
devpath=`cd "$device" && pwd -P`
|
|
|
|
location='@'`attr_clean "$device/location"`
|
|
|
|
hardware_id=`attr_clean "$device/hardware_id"`
|
2011-10-07 03:03:00 +08:00
|
|
|
for local_spanno in `cut -d: -f1 "$device/spantype"`
|
|
|
|
do
|
|
|
|
span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
|
|
|
|
sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'`
|
|
|
|
if [ "$span" != '' ]; then
|
|
|
|
spanno=`echo $span | sed 's/^.*-//'`
|
|
|
|
name=`cat 2>/dev/null "$device/$span/name"`
|
|
|
|
basechan=`cat 2>/dev/null "$device/$span/basechan"`
|
|
|
|
else
|
|
|
|
spanno='-'
|
|
|
|
basechan='-'
|
|
|
|
fi
|
2013-09-29 16:09:56 +08:00
|
|
|
printf "%-8s %-14s %s %s\n" "$local_spanno:$spanno:$basechan" "[$hardware_id]" "$location" "$devpath"
|
2011-10-07 03:03:00 +08:00
|
|
|
done | sort -n
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_config() {
|
2013-09-23 19:59:14 +08:00
|
|
|
echo '#'
|
|
|
|
echo "# Autogenerated by $0 on `date`"
|
|
|
|
echo "# Map devices + local spans to span + base channel number"
|
|
|
|
echo ''
|
2011-10-07 03:03:00 +08:00
|
|
|
for device in $DEVICES
|
|
|
|
do
|
2013-09-29 16:09:56 +08:00
|
|
|
devpath=`cd "$device" && pwd -P`
|
|
|
|
location=`attr_clean "$device/location"`
|
|
|
|
hardware_id=`attr_clean "$device/hardware_id"`
|
|
|
|
if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then
|
|
|
|
id="$hardware_id"
|
|
|
|
elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then
|
|
|
|
id="@$location"
|
2011-10-07 03:03:00 +08:00
|
|
|
else
|
2013-09-29 16:09:56 +08:00
|
|
|
id="$devpath"
|
2011-10-07 03:03:00 +08:00
|
|
|
fi
|
2013-09-29 16:09:56 +08:00
|
|
|
echo "# Device: [$hardware_id] @$location $devpath"
|
2011-10-07 03:03:00 +08:00
|
|
|
for local_spanno in `cut -d: -f1 "$device/spantype"`
|
|
|
|
do
|
|
|
|
span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
|
|
|
|
sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'`
|
|
|
|
if [ "$span" != '' ]; then
|
|
|
|
spanno=`echo $span | sed 's/^.*-//'`
|
|
|
|
name=`cat 2>/dev/null "$device/$span/name"`
|
|
|
|
basechan=`cat 2>/dev/null "$device/$span/basechan"`
|
2013-09-23 19:59:14 +08:00
|
|
|
printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
|
2011-10-07 03:03:00 +08:00
|
|
|
else
|
2013-09-23 19:59:14 +08:00
|
|
|
echo "# Skipped unassigned local span $local_spanno"
|
2011-10-07 03:03:00 +08:00
|
|
|
fi
|
2013-09-23 19:59:14 +08:00
|
|
|
done | sort
|
|
|
|
echo ''
|
2011-10-07 03:03:00 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
unassign_all_spans() {
|
|
|
|
for device in $DEVICES
|
|
|
|
do
|
|
|
|
find "$device" -follow -maxdepth 1 -name 'span-*' -type d | \
|
|
|
|
sort | while read spandir; do
|
|
|
|
local_spanno=`cat "$spandir/local_spanno"`
|
2013-09-29 16:09:56 +08:00
|
|
|
if [ "$dry_run" = true ]; then
|
|
|
|
echo "(dry-run) unassign $device $local_spanno"
|
|
|
|
continue
|
|
|
|
fi
|
2011-10-07 03:03:00 +08:00
|
|
|
echo "unassign $device $local_spanno"
|
|
|
|
if ! echo "$local_spanno" > "$device/unassign_span"; then
|
|
|
|
echo >&2 "$0: failed unassigning '$local_spanno' in '$device'"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Allow comments and empty lines in config file
|
|
|
|
filter_conf() {
|
2013-11-27 02:55:09 +08:00
|
|
|
sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISASSIGNEDSPANSCONF"
|
2011-10-07 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assign_device_spans() {
|
|
|
|
device="$1"
|
|
|
|
for s in $spanspecs
|
|
|
|
do
|
|
|
|
local_spanno=`echo "$s" | cut -d: -f1`
|
|
|
|
spanno=`echo "$s" | cut -d: -f2`
|
|
|
|
span="$device/span-$spanno"
|
2013-09-29 16:09:56 +08:00
|
|
|
if [ "$dry_run" = true ]; then
|
|
|
|
echo "(dry-run) assign $device: $s"
|
|
|
|
continue
|
|
|
|
fi
|
2011-10-07 03:03:00 +08:00
|
|
|
if [ -d "$span" ]; then
|
|
|
|
span_local_spanno=`cat "$span/local_spanno"`
|
|
|
|
if [ "$span_local_spanno" != "$local_spanno" ]; then
|
|
|
|
echo "WARNING: $span_local_spanno != $local_spanno"
|
|
|
|
fi
|
2013-09-29 16:09:56 +08:00
|
|
|
echo "$device [$local_spanno] already assigned to span $spanno. Skipping..."
|
2011-10-07 03:03:00 +08:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
echo "assign $device: $s"
|
|
|
|
if ! echo "$s" > "$device/assign_span"; then
|
|
|
|
echo >&2 "$0: failed assigning '$s' to '$device'"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
match_device() {
|
|
|
|
device="$1"
|
2013-09-29 16:09:56 +08:00
|
|
|
devpath=`cd "$device" && pwd -P`
|
|
|
|
location='@'`attr_clean "$device/location"`
|
2011-10-07 03:03:00 +08:00
|
|
|
hardware_id=`attr_clean "$device/hardware_id"`
|
|
|
|
filter_conf | while read id spanspecs
|
|
|
|
do
|
2013-09-29 16:09:56 +08:00
|
|
|
# We use case to enable shell-style globbing in configuration
|
|
|
|
case "$hardware_id" in
|
|
|
|
$id)
|
|
|
|
[ "$verbose" = true ] && echo "match by hwid ($id ~ $hardware_id): $spanspecs"
|
|
|
|
assign_device_spans "$device"
|
|
|
|
;;
|
|
|
|
esac
|
2011-10-07 03:03:00 +08:00
|
|
|
# We use case to enable shell-style globbing in configuration
|
|
|
|
case "$location" in
|
|
|
|
$id)
|
2013-09-29 16:09:56 +08:00
|
|
|
[ "$verbose" = true ] && echo "match by location ($id ~ $location): $spanspecs"
|
2011-10-07 03:03:00 +08:00
|
|
|
assign_device_spans "$device"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
# We use case to enable shell-style globbing in configuration
|
2013-09-29 16:09:56 +08:00
|
|
|
case "$devpath" in
|
2011-10-07 03:03:00 +08:00
|
|
|
$id)
|
2013-09-29 16:09:56 +08:00
|
|
|
[ "$verbose" = true ] && echo "match by devpath ($id ~ $devpath): $spanspecs"
|
2011-10-07 03:03:00 +08:00
|
|
|
assign_device_spans "$device"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
assign_devices() {
|
2013-11-27 02:55:09 +08:00
|
|
|
if [ ! -f "$DAHDISASSIGNEDSPANSCONF" ]; then
|
|
|
|
echo >&2 "$0: Missing '$DAHDISASSIGNEDSPANSCONF'"
|
2013-09-23 19:59:14 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2013-11-27 02:55:09 +08:00
|
|
|
echo "using '$DAHDISASSIGNEDSPANSCONF'"
|
2011-10-07 03:03:00 +08:00
|
|
|
for device in $DEVICES
|
|
|
|
do
|
|
|
|
match_device "$device"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2013-09-23 19:59:14 +08:00
|
|
|
auto_assign_devices() {
|
|
|
|
for device in $DEVICES
|
|
|
|
do
|
|
|
|
echo "auto-assign $device"
|
2013-09-29 16:09:56 +08:00
|
|
|
if [ "$dry_run" != true ]; then
|
|
|
|
echo 1 > "$device/auto_assign"
|
|
|
|
fi
|
2013-09-23 19:59:14 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
case "$action" in
|
2013-09-23 19:59:14 +08:00
|
|
|
auto)
|
|
|
|
auto_assign_devices
|
2011-10-07 03:03:00 +08:00
|
|
|
;;
|
|
|
|
add)
|
|
|
|
assign_devices
|
|
|
|
;;
|
|
|
|
remove)
|
|
|
|
unassign_all_spans
|
|
|
|
;;
|
2013-09-23 19:59:14 +08:00
|
|
|
list)
|
|
|
|
show_devices
|
|
|
|
;;
|
|
|
|
dumpconfig)
|
|
|
|
dump_config
|
|
|
|
;;
|
2011-10-07 03:03:00 +08:00
|
|
|
*)
|
2013-09-29 16:09:56 +08:00
|
|
|
echo >&2 "Bad action='$action'"
|
2011-10-07 03:03:00 +08:00
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|