Newer version of DAHDI hotplug scripts

A newer version of the scripts fully adapted to pinned spans:
* handle_device does not run dahdi_cfg.
* A separate UDEV rule script for that: span_config. Should also work
  for the non-pinned case.
* span_assignments, span_types: add actions 'auto' (manually enable all)
  and 'dumpconfig' (dump current status in the format of configuration
  file).
* Fixed name of span_types and span_assignments (no '-').
* spantype.conf renamed span-types.conf: configuration files do have a
  dash.
* Those two are useful programs, insstalled to /usr/sbin.

Signed-off-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>
This commit is contained in:
Oron Peled 2013-09-23 13:59:14 +02:00 committed by Tzafrir Cohen
parent 025985d9b7
commit 00af777a97
6 changed files with 192 additions and 55 deletions

View File

@ -104,9 +104,8 @@ ifeq (1,$(PBX_HDLC))
endif endif
MAN_PAGES:=$(wildcard $(BINS:%=doc/%.8)) MAN_PAGES:=$(wildcard $(BINS:%=doc/%.8))
PINNED_DATA_SCRIPTS:=dahdi_cfg_device_args handle_device \ PINNED_DATA_SCRIPTS:=handle_device span_config
span_assignments span_types PINNED_UTILS:=dahdi_map span_assignments span_types
PINNED_UTILS:=dahdi_map
PINNED_CONF:=pinned-spans.conf spantype.conf PINNED_CONF:=pinned-spans.conf spantype.conf
TEST_BINS:=patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint TEST_BINS:=patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint

View File

@ -1,3 +1,4 @@
# DAHDI devices with ownership/permissions for running as non-root # DAHDI devices with ownership/permissions for running as non-root
SUBSYSTEM=="dahdi", OWNER="asterisk", GROUP="asterisk", MODE="0660" SUBSYSTEM=="dahdi", OWNER="asterisk", GROUP="asterisk", MODE="0660"
SUBSYSTEM=="dahdi_devices", RUN="/usr/share/dahdi/handle_device" SUBSYSTEM=="dahdi_devices", RUN="/usr/share/dahdi/handle_device"
SUBSYSTEM=="dahdi_spans", RUN="/usr/share/dahdi/span_config"

View File

@ -2,7 +2,8 @@
# #
# /usr/share/dahdi/handle_device # /usr/share/dahdi/handle_device
# #
# Called by UDEV when a span goes online/offline to assign spans # Called by UDEV when a dahdi device is added/removed
#
me=`basename $0` me=`basename $0`
dir=`dirname $0` dir=`dirname $0`
@ -22,26 +23,38 @@ PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
set -e set -e
run_dahdi_cfg() { #echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
args="-c $DAHDICONFDIR/system.conf $@"
echo "Running dahdi_cfg $args"
dahdi_cfg $args
}
echo >&2 "$0($ACTION): DEBUG($# args): '$*'" # Check if we can safely do our job
if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then
echo "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" | $LOGGER
exit 0
fi
if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -eq 1 ]; then
echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER
exit 0
fi
# Can we pass a different value so we can use
# alternate (testing) configuration?
# Meanwhile, make it hard-coded.
DAHDICONFDIR='/etc/dahdi'
export DAHDICONFDIR
case "$ACTION" in case "$ACTION" in
add) add)
echo "$ACTION: $DEVPATH" | $LOGGER echo "$ACTION: $DEVPATH" | $LOGGER
# FIXME: need a way to add custom environment here: # Can have alternate dahdi configuration directory for debugging
# export DAHDICONFDIR="/tmp/xortel/dahdi" # export DAHDICONFDIR="/tmp/xortel/dahdi"
span_types set "/sys/$DEVPATH" 2>&1 | $LOGGER
span_assignments add "/sys/$DEVPATH" 2>&1 | $LOGGER # Don't block udev for too long
dahdi_cfg_device_args | while read args; do (
run_dahdi_cfg $args 2>&1 | $LOGGER span_types set "/sys$DEVPATH"
done span_assignments add "/sys$DEVPATH"
) 2>&1 < /dev/null | $LOGGER &
;; ;;
remove) remove)
# Nothing to do yet...
echo "$ACTION: $DEVPATH" | $LOGGER echo "$ACTION: $DEVPATH" | $LOGGER
;; ;;
*) *)

View File

@ -1,15 +1,23 @@
#! /bin/sh #! /bin/sh
# #
# /usr/share/dahdi/span_assignments: # /usr/sbin/span_assignments:
# #
# this script can be used both from udev and # this script can be used both from udev and
# from the command line to assign/unassign and list # from the command line to assign/unassign and list
# current assignments. # current span assignments.
#
# It uses a configuration file: $DAHDICONFDIR/pinned-spans.conf
# (default DAHDICONFDIR=/etc/dahdi)
# #
# The first argument is an action: # The first argument is an action:
# "add" to assign (spans which are not already assigned) # "auto" - trigger driver auto_assign attribute for given devices
# "remove" to unassign (spans which are not already unassigned) # (no configuration file is used)
# "list" to show all spans (with/without assignments) # "add" - assign (spans which are not already assigned), according
# to /etc/dahdi/pinned-spans.conf configuration file
# "remove" - unassign spans which are not already unassigned
# "list" - human-readable list of all spans (with/without assignments)
# "dumpconfig" - dump current assignments in a /etc/dahdi/pinned-spans.conf
# compatible format
# #
# Without further arguments, it operates on all existing spans # Without further arguments, it operates on all existing spans
# With one or more sysfs dahdi_devices it is limited to those. # With one or more sysfs dahdi_devices it is limited to those.
@ -26,7 +34,7 @@ DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf" pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf"
usage() { usage() {
echo >&2 "Usage: $0 {add|remove|list} [devpath ...]" echo >&2 "Usage: $0 {auto|add|remove|list|dumpconfig} [devpath ...]"
exit 1 exit 1
} }
@ -66,22 +74,26 @@ show_devices() {
spanno='-' spanno='-'
basechan='-' basechan='-'
fi fi
printf "%-4s %-12s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location" printf "%-8s %-14s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
done | sort -n done | sort -n
done done
} }
dump_config() { dump_config() {
echo '#'
echo "# Autogenerated by $0 on `date`"
echo "# Map devices + local spans to span + base channel number"
echo ''
for device in $DEVICES for device in $DEVICES
do do
hw_id=`cat "$device/hardware_id"` hw_id=`cat "$device/hardware_id"`
location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'` location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
if [ "$hw_id" != '' ]; then if [ "$hw_id" != '' ]; then
id="$hw_id" id="$hw_id"
else else
id="@$location" id="$location"
fi fi
echo "# Device: [$hw_id] $location"
for local_spanno in `cut -d: -f1 "$device/spantype"` for local_spanno in `cut -d: -f1 "$device/spantype"`
do do
span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \ span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
@ -90,11 +102,12 @@ dump_config() {
spanno=`echo $span | sed 's/^.*-//'` spanno=`echo $span | sed 's/^.*-//'`
name=`cat 2>/dev/null "$device/$span/name"` name=`cat 2>/dev/null "$device/$span/name"`
basechan=`cat 2>/dev/null "$device/$span/basechan"` basechan=`cat 2>/dev/null "$device/$span/basechan"`
else
spanno='-'
fi
printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan" printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
done | sort -n else
echo "# Skipped unassigned local span $local_spanno"
fi
done | sort
echo ''
done done
} }
@ -168,18 +181,28 @@ match_device() {
} }
assign_devices() { assign_devices() {
if [ ! -f "$pinned_spans_conf" ]; then
echo >&2 "$0: Missing '$pinned_spans_conf'"
exit 1
fi
echo "using '$pinned_spans_conf'"
for device in $DEVICES for device in $DEVICES
do do
match_device "$device" match_device "$device"
done done
} }
auto_assign_devices() {
for device in $DEVICES
do
echo "auto-assign $device"
echo 1 > "$device/auto_assign"
done
}
case "$action" in case "$action" in
list) auto)
show_devices auto_assign_devices
;;
dump)
dump_config
;; ;;
add) add)
assign_devices assign_devices
@ -187,6 +210,12 @@ add)
remove) remove)
unassign_all_spans unassign_all_spans
;; ;;
list)
show_devices
;;
dumpconfig)
dump_config
;;
*) *)
usage usage
;; ;;

70
span_config Executable file
View File

@ -0,0 +1,70 @@
#! /bin/sh
#
# /usr/share/dahdi/span_config
#
# Called by UDEV when a dahdi span is added/removed
#
me=`basename $0`
dir=`dirname $0`
LOGGER="logger -i -t '$me'"
NAME=`basename "$DEVPATH" | tr -c 'A-Za-z0-9-' '_'`
exec 2> /dev/null
# Always redirect stderr somewhere, otherwise the shell script will die
# when it tries to do I/O related stuff on closed file descriptor.
# Our default is to throw it down the bit-bucket.
#exec 2> /dev/console
## If you wish to trace this script:
#exec 2> "/tmp/${me}.$NAME" 1>&2
# Our directory in the beginning, so we can use local lab setup
PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
set -e
#echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
# Can we pass a different value so we can use
# alternate (testing) configuration?
# Meanwhile, make it hard-coded.
DAHDICONFDIR='/etc/dahdi'
export DAHDICONFDIR
run_dahdi_cfg() {
span_devpath="$1"
# Sanity check
checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l`
if [ "$checkit" != 1 ]; then
$LOGGER "Bad dahdi_cfg (no -S support). Skipping"
exit 0
fi
spanno=`echo "$span_devpath" | sed 's,.*/span-,,'`
basechan=`cat "$span_devpath/basechan"`
channels=`cat "$span_devpath/channels"`
endchan=`expr "$basechan" + "$channels" - 1`
echo "dahdi_cfg: span $spanno <$basechan-$endchan>"
dahdi_cfg \
-c "$DAHDICONFDIR/system.conf" \
-S "$spanno" \
-C "$basechan-$endchan"
asterisk -rx "dahdi create channels $basechan $endchan"
}
case "$ACTION" in
add)
echo "$ACTION: $DEVPATH" | $LOGGER
# Can have alternate dahdi configuration directory for debugging
# export DAHDICONFDIR="/tmp/xortel/dahdi"
run_dahdi_cfg "/sys$DEVPATH" 2>&1 | $LOGGER
;;
remove)
# Nothing to do yet...
echo "$ACTION: $DEVPATH" | $LOGGER
;;
*)
echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
;;
esac

View File

@ -1,34 +1,36 @@
#! /bin/sh #! /bin/sh
# #
# /usr/share/dahdi/span_types: # /usr/sbin/span_types
# #
# this script can be used both from udev and # This script can be used both from udev and
# from the command line for spantype management. # from the command line to manage PRI spans
# type (E1/T1/J1).
# #
# It use a configuration file /etc/dahdi/spantype.conf # Span types can be set only *BEFORE* span are assigned.
# (the format is documented inside this file)
# #
# The first argument is an action: # It reads a configuration file /etc/dahdi/span-types.conf
# "list" to show existing E1/T1/J1 types # (the format is documented inside that file)
# "dump" the same, but in a format (almost) suitable for #
# the configuration file # A mandatory first argument is:
# FIXME: we currently don't have the base channo in sysfs. # list - to show existing E1/T1/J1 types
# "set" actually write the setting to the driver # dumpconfig - the same, but in a format (almost) suitable
# for the configuration file
# set - actually write the setting to the driver
# #
# Examples: # Examples:
# span_types list # span_types list
# span_types dump # span_types dumpconfig
# span_types set # all # span_types set # all devices
# span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00 # span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
# #
devbase='/sys/bus/dahdi_devices/devices' devbase='/sys/bus/dahdi_devices/devices'
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}" DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
spantype_conf="$DAHDICONFDIR/spantype.conf" spantypes_conf="$DAHDICONFDIR/span-types.conf"
usage() { usage() {
echo >&2 "Usage: $0 {list|dump|set} [devpath ...]" echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
exit 1 exit 1
} }
@ -38,6 +40,11 @@ fi
action="$1" action="$1"
shift shift
if [ ! -d "$devbase" ]; then
echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
exit 1
fi
# Use given devices or otherwise, all existing devices # Use given devices or otherwise, all existing devices
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
DEVICES="$@" DEVICES="$@"
@ -46,19 +53,28 @@ else
fi fi
show_spantypes() { show_spantypes() {
echo "# PRI span types (E1/T1/J1)"
for device in $DEVICES for device in $DEVICES
do do
hw_id=`cat "$device/hardware_id"` hw_id=`cat "$device/hardware_id"`
location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'` location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
cat "$device/spantype" | while read st; do cat "$device/spantype" | while read st; do
printf "%-10s %-20s %s\n" "$st" "[$hw_id]" "$location" case "$st" in
*:[ETJ]1)
printf "%-10s %-20s %s\n" \
"$st" "[$hw_id]" "$location"
;;
esac
done | sort -n done | sort -n
done done
} }
dump_config() { dump_config() {
echo '#'
echo "# Autogenerated by $0 on `date`"
echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
echo ''
fmt="%-65s %s\n" fmt="%-65s %s\n"
echo "# Map of dahdi_devices to span types for E1/T1/J1"
printf "$fmt" '# @location/hardware_id' 'span_type' printf "$fmt" '# @location/hardware_id' 'span_type'
for device in $DEVICES for device in $DEVICES
do do
@ -69,19 +85,24 @@ dump_config() {
else else
id="$location" id="$location"
fi fi
#echo "# Device: [$hw_id] $location"
cat "$device/spantype" | while read st; do cat "$device/spantype" | while read st; do
case "$st" in case "$st" in
*:[ETJ]1) *:[ETJ]1)
printf "$fmt" "$id" "$st" printf "$fmt" "$id" "$st"
;; ;;
*)
#echo "# Skipped local span `echo $st | sed 's/:/ -- /'`"
;;
esac esac
done | sort -n done | sort -n
#echo ''
done done
} }
# Allow comments and empty lines in config file # Allow comments and empty lines in config file
filter_conf() { filter_conf() {
sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantype_conf" sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantypes_conf"
} }
conf_spans() { conf_spans() {
@ -121,7 +142,7 @@ device_set_spantype() {
hw_id=`attr_clean "$device/hardware_id"` hw_id=`attr_clean "$device/hardware_id"`
location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'` location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
spanspecs=`conf_spans "$hw_id" "$location"` spanspecs=`conf_spans "$hw_id" "$location"`
echo >&2 "MATCHED($device): $spanspecs" #echo >&2 "MATCHED($device): $spanspecs"
cut -d: -f1 "$attr_file" | while read spanno; do cut -d: -f1 "$attr_file" | while read spanno; do
for sp in $spanspecs for sp in $spanspecs
do do
@ -138,6 +159,10 @@ device_set_spantype() {
} }
set_spantypes() { set_spantypes() {
if [ ! -f "$spantypes_conf" ]; then
echo >&2 "$0: Missing configuration '$spantypes_conf'"
exit 1
fi
for device in $DEVICES for device in $DEVICES
do do
device_set_spantype "$device" device_set_spantype "$device"
@ -148,7 +173,7 @@ case "$action" in
list) list)
show_spantypes show_spantypes
;; ;;
dump) dumpconfig)
dump_config dump_config
;; ;;
set) set)