| 1 |
#! /bin/sh |
| 2 |
# |
| 3 |
# /usr/sbin/dahdi_span_assignments: |
| 4 |
# |
| 5 |
# this script can be used both from udev and |
| 6 |
# from the command line to assign/unassign and list |
| 7 |
# current span assignments. |
| 8 |
# |
| 9 |
# It uses a configuration file: $DAHDICONFDIR/assigned-spans.conf |
| 10 |
# (default DAHDICONFDIR=/etc/dahdi) |
| 11 |
# |
| 12 |
# The first argument is an action: |
| 13 |
# "auto" - trigger driver auto_assign attribute for given devices |
| 14 |
# (no configuration file is used) |
| 15 |
# "add" - assign (spans which are not already assigned), according |
| 16 |
# to /etc/dahdi/assigned-spans.conf configuration file |
| 17 |
# "remove" - unassign spans which are not already unassigned |
| 18 |
# "list" - human-readable list of all spans (with/without assignments) |
| 19 |
# "dumpconfig" - dump current assignments in a /etc/dahdi/assigned-spans.conf |
| 20 |
# compatible format |
| 21 |
# |
| 22 |
# Without further arguments, it operates on all existing spans |
| 23 |
# With one or more sysfs dahdi_devices it is limited to those. |
| 24 |
# |
| 25 |
# We may use alternative "keys" for device matching: |
| 26 |
# * Available keys: |
| 27 |
# - "hwid" - Hardware id attribute from sysfs |
| 28 |
# - "@location" - Location attribute from sysfs (embeded inside '<>') |
| 29 |
# - "/devpath" - The sysfs absolute devpath |
| 30 |
# |
| 31 |
# * During "dumpconfig", for each device we take the first available key: |
| 32 |
# - The preference is: "hwid" or else "@location" or else "/devpath" |
| 33 |
# - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable |
| 34 |
# or the '{-k|--key} key' command line option. |
| 35 |
# |
| 36 |
# * During "add": |
| 37 |
# - Any key match is valid (hwid/location/devpath) |
| 38 |
# - Shell globs (wildcards: '*', '?', '[...]') may be optionally used. |
| 39 |
# |
| 40 |
# Command line options: |
| 41 |
# - The '-h|--help' show a usage message. |
| 42 |
# - The '-n|--dry-run' affects the "add" and "remove" operations. |
| 43 |
# - The '-v|--verbose' currently shows device matches during "add" operation. |
| 44 |
# - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment |
| 45 |
# variable. |
| 46 |
# |
| 47 |
# Examples: |
| 48 |
# dahdi_span_assignments list |
| 49 |
# dahdi_span_assignments add # all unassigned devices |
| 50 |
# dahdi_span_assignments add /sys/bus/dahdi_devices/devices/astribanks:xbus-00 |
| 51 |
# dahdi_span_assignments remove # all assigned devices |
| 52 |
# dahdi_span_assignments -k location dumpconfig |
| 53 |
# |
| 54 |
|
| 55 |
devbase='/sys/bus/dahdi_devices/devices' |
| 56 |
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}" |
| 57 |
DAHDISASSIGNEDSPANSCONF="${DAHDIASSIGNEDSPANSCONF:-"${DAHDICONFDIR}/assigned-spans.conf"}" |
| 58 |
SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid} |
| 59 |
dry_run= |
| 60 |
verbose= |
| 61 |
|
| 62 |
usage() { |
| 63 |
echo >&2 "Usage: $0 [options] action [devpath ...]" |
| 64 |
echo >&2 " action:" |
| 65 |
echo >&2 " auto - trigger driver auto_assign attribute for given devices" |
| 66 |
echo >&2 " add - assign spans, according to /etc/dahdi/assigned-spans.conf" |
| 67 |
echo >&2 " remove - unassign spans" |
| 68 |
echo >&2 " list - human-readable list of all spans" |
| 69 |
echo >&2 " matched - found spans matched in configuration" |
| 70 |
echo >&2 " unmatched - found spans not matched in configuration" |
| 71 |
echo >&2 " dumpconfig - dump current state as new configuration" |
| 72 |
echo >&2 "" |
| 73 |
echo >&2 " options:" |
| 74 |
echo >&2 " -h|--help - Show this help" |
| 75 |
echo >&2 " -n|--dry-run - For 'add/remove' actions" |
| 76 |
echo >&2 " -v|--versbose - Show matches during 'add' action" |
| 77 |
echo >&2 " -k|--key <k> - Override prefered key during dumpconfig action" |
| 78 |
exit 1 |
| 79 |
} |
| 80 |
|
| 81 |
# Parse command line options |
| 82 |
TEMP=`getopt -o hnvk: --long help,dry-run,verbose,key: -n "$0" -- "$@"` |
| 83 |
if [ $? != 0 ]; then |
| 84 |
echo >&2 "Bad options" |
| 85 |
usage |
| 86 |
fi |
| 87 |
|
| 88 |
# Note the quotes around `$TEMP': they are essential! |
| 89 |
eval set -- "$TEMP" |
| 90 |
|
| 91 |
while true ; do |
| 92 |
case "$1" in |
| 93 |
-h|--help) |
| 94 |
usage |
| 95 |
;; |
| 96 |
-n|--dry-run) |
| 97 |
dry_run='true' |
| 98 |
shift |
| 99 |
;; |
| 100 |
-v|--verbose) |
| 101 |
verbose='true' |
| 102 |
shift |
| 103 |
;; |
| 104 |
-k|--key) |
| 105 |
SPAN_ASSIGNMENTS_KEY="$2" |
| 106 |
shift |
| 107 |
shift |
| 108 |
;; |
| 109 |
--) |
| 110 |
shift |
| 111 |
break |
| 112 |
;; |
| 113 |
*) |
| 114 |
echo >&2 "Internal error!" |
| 115 |
exit 1 |
| 116 |
;; |
| 117 |
esac |
| 118 |
done |
| 119 |
|
| 120 |
if [ "$#" -eq 0 ]; then |
| 121 |
echo >&2 "Missing action argument" |
| 122 |
usage |
| 123 |
fi |
| 124 |
action="$1" |
| 125 |
shift |
| 126 |
|
| 127 |
# Validate SPAN_ASSIGNMENTS_KEY |
| 128 |
case "$SPAN_ASSIGNMENTS_KEY" in |
| 129 |
hwid|location|devpath) |
| 130 |
;; |
| 131 |
*) |
| 132 |
echo >&2 "Bad SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)" |
| 133 |
usage |
| 134 |
;; |
| 135 |
esac |
| 136 |
|
| 137 |
if [ ! -d "$devbase" ]; then |
| 138 |
echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)" |
| 139 |
exit 1 |
| 140 |
fi |
| 141 |
|
| 142 |
# Use given devices or otherwise, all existing devices |
| 143 |
if [ "$#" -gt 0 ]; then |
| 144 |
DEVICES="$@" |
| 145 |
else |
| 146 |
DEVICES=`ls -d $devbase/* 2>/dev/null` |
| 147 |
fi |
| 148 |
|
| 149 |
# Beware of special characters in attributes |
| 150 |
attr_clean() { |
| 151 |
cat "$1" 2>/dev/null | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_' |
| 152 |
} |
| 153 |
|
| •154 |
get_device_spannos() { |
| 155 |
device=$1 |
| 156 |
sp_file="dahdi_spantype" |
| 157 |
if [ ! -f "$device/$sp_file" ]; then |
| 158 |
sp_file="spantype" |
| 159 |
fi |
| 160 |
cut -d: -f1 "$device/$sp_file" |
| 161 |
} |
| 162 |
|
| 163 |
show_devices() { |
| 164 |
|
| 165 |
for device in $DEVICES |
| 166 |
do |
| 167 |
devpath=`cd "$device" && pwd -P` |
| 168 |
location='@'`attr_clean "$device/location"` |
| 169 |
hardware_id=`attr_clean "$device/hardware_id"` |
| •170 |
for local_spanno in `get_device_spannos "$device"` |
| 171 |
do |
| 172 |
span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \ |
| 173 |
sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'` |
| 174 |
if [ "$span" != '' ]; then |
| 175 |
spanno=`echo $span | sed 's/^.*-//'` |
| 176 |
name=`cat 2>/dev/null "$device/$span/name"` |
| 177 |
basechan=`cat 2>/dev/null "$device/$span/basechan"` |
| 178 |
else |
| 179 |
spanno='-' |
| 180 |
basechan='-' |
| 181 |
fi |
| 182 |
printf "%-8s %-14s %s %s\n" "$local_spanno:$spanno:$basechan" "[$hardware_id]" "$location" "$devpath" |
| 183 |
done | sort -n |
| 184 |
done |
| 185 |
} |
| 186 |
|
| 187 |
dump_config() { |
| 188 |
echo '#' |
| 189 |
echo "# Autogenerated by $0 on `date`" |
| 190 |
echo "# Map devices + local spans to span + base channel number" |
| 191 |
echo '' |
| 192 |
for device in $DEVICES |
| 193 |
do |
| 194 |
devpath=`cd "$device" && pwd -P` |
| 195 |
location=`attr_clean "$device/location"` |
| 196 |
hardware_id=`attr_clean "$device/hardware_id"` |
| 197 |
if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then |
| 198 |
id="$hardware_id" |
| 199 |
elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then |
| 200 |
id="@$location" |
| 201 |
else |
| 202 |
id="$devpath" |
| 203 |
fi |
| 204 |
echo "# Device: [$hardware_id] @$location $devpath" |
| •205 |
for local_spanno in `get_device_spannos "$device"` |
| 206 |
do |
| 207 |
span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \ |
| 208 |
sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'` |
| 209 |
if [ "$span" != '' ]; then |
| 210 |
spanno=`echo $span | sed 's/^.*-//'` |
| 211 |
name=`cat 2>/dev/null "$device/$span/name"` |
| 212 |
basechan=`cat 2>/dev/null "$device/$span/basechan"` |
| 213 |
printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan" |
| 214 |
else |
| 215 |
echo "# Skipped unassigned local span $local_spanno" |
| 216 |
fi |
| 217 |
done | sort |
| 218 |
echo '' |
| 219 |
done |
| 220 |
} |
| 221 |
|
| 222 |
unassign_all_spans() { |
| 223 |
for device in $DEVICES |
| 224 |
do |
| 225 |
find "$device" -follow -maxdepth 1 -name 'span-*' -type d | \ |
| 226 |
sort | while read spandir; do |
| 227 |
local_spanno=`cat "$spandir/local_spanno"` |
| 228 |
if [ "$dry_run" = true ]; then |
| 229 |
echo >&2 "(dry-run) unassign $device $local_spanno" |
| 230 |
continue |
| 231 |
fi |
| 232 |
echo >&2 "unassign $device $local_spanno" |
| 233 |
if ! echo "$local_spanno" > "$device/unassign_span"; then |
| 234 |
echo >&2 "$0: failed unassigning '$local_spanno' in '$device'" |
| 235 |
fi |
| 236 |
done |
| 237 |
done |
| 238 |
} |
| 239 |
|
| 240 |
# Allow comments and empty lines in config file |
| 241 |
filter_conf() { |
| 242 |
sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISASSIGNEDSPANSCONF" |
| 243 |
} |
| 244 |
|
| 245 |
assign_device_spans() { |
| 246 |
device="$1" |
| 247 |
for s in $spanspecs |
| 248 |
do |
| 249 |
local_spanno=`echo "$s" | cut -d: -f1` |
| 250 |
spanno=`echo "$s" | cut -d: -f2` |
| 251 |
span="$device/span-$spanno" |
| 252 |
if [ "$dry_run" = true ]; then |
| 253 |
echo "(dry-run) assign $device: $s" |
| 254 |
continue |
| 255 |
fi |
| 256 |
if [ -d "$span" ]; then |
| 257 |
span_local_spanno=`cat "$span/local_spanno"` |
| 258 |
if [ "$span_local_spanno" != "$local_spanno" ]; then |
| 259 |
echo >&2 "WARNING: $span_local_spanno != $local_spanno" |
| 260 |
fi |
| 261 |
echo >&2 "$device [$local_spanno] already assigned to span $spanno. Skipping..." |
| 262 |
continue |
| 263 |
fi |
| 264 |
echo >&2 "assign $device: $s" |
| 265 |
if ! echo "$s" > "$device/assign_span"; then |
| 266 |
echo >&2 "$0: failed assigning '$s' to '$device'" |
| 267 |
fi |
| 268 |
done |
| 269 |
} |
| 270 |
|
| 271 |
match_device() { |
| 272 |
device="$1" |
| 273 |
devpath=`cd "$device" && pwd -P` |
| 274 |
location='@'`attr_clean "$device/location"` |
| 275 |
hardware_id=`attr_clean "$device/hardware_id"` |
| 276 |
filter_conf | while read id spanspecs |
| 277 |
do |
| 278 |
# We use case to enable shell-style globbing in configuration |
| 279 |
case "$hardware_id" in |
| 280 |
$id) |
| 281 |
[ "$verbose" = true ] && echo >&2 "match by hwid ($id ~ $hardware_id): $spanspecs" |
| 282 |
assign_device_spans "$device" |
| 283 |
;; |
| 284 |
esac |
| 285 |
# We use case to enable shell-style globbing in configuration |
| 286 |
case "$location" in |
| 287 |
$id) |
| 288 |
[ "$verbose" = true ] && echo >&2 "match by location ($id ~ $location): $spanspecs" |
| 289 |
assign_device_spans "$device" |
| 290 |
;; |
| 291 |
esac |
| 292 |
# We use case to enable shell-style globbing in configuration |
| 293 |
case "$devpath" in |
| 294 |
$id) |
| 295 |
[ "$verbose" = true ] && echo >&2 "match by devpath ($id ~ $devpath): $spanspecs" |
| 296 |
assign_device_spans "$device" |
| 297 |
;; |
| 298 |
esac |
| 299 |
done |
| 300 |
} |
| 301 |
|
| 302 |
assign_devices() { |
| 303 |
if [ ! -f "$DAHDISASSIGNEDSPANSCONF" ]; then |
| 304 |
echo >&2 "$0: Missing '$DAHDISASSIGNEDSPANSCONF'" |
| 305 |
exit 1 |
| 306 |
fi |
| 307 |
echo >&2 "using '$DAHDISASSIGNEDSPANSCONF'" |
| 308 |
for device in $DEVICES |
| 309 |
do |
| 310 |
match_device "$device" |
| 311 |
done |
| 312 |
} |
| 313 |
|
| 314 |
auto_assign_devices() { |
| 315 |
for device in $DEVICES |
| 316 |
do |
| 317 |
echo >&2 "auto-assign $device" |
| 318 |
if [ "$dry_run" != true ]; then |
| 319 |
echo 1 > "$device/auto_assign" |
| 320 |
fi |
| 321 |
done |
| 322 |
} |
| 323 |
|
| 324 |
dev_match_conf() { |
| 325 |
local devpath="$1" |
| 326 |
local location="$2" |
| 327 |
local hardware_id="$3" |
| 328 |
local local_spanno="$4" |
| 329 |
filter_conf | while read id spanspecs |
| 330 |
do |
| 331 |
spanno=`echo "$spanspecs" | cut -d: -f1` |
| 332 |
match_dev=no |
| 333 |
# We use case to enable shell-style globbing in configuration |
| 334 |
case "$hardware_id" in |
| 335 |
$id) |
| 336 |
match_dev=yes |
| 337 |
;; |
| 338 |
esac |
| 339 |
# We use case to enable shell-style globbing in configuration |
| 340 |
case "$location" in |
| 341 |
$id) |
| 342 |
match_dev=yes |
| 343 |
;; |
| 344 |
esac |
| 345 |
# We use case to enable shell-style globbing in configuration |
| 346 |
case "$devpath" in |
| 347 |
$id) |
| 348 |
match_dev=yes |
| 349 |
;; |
| 350 |
esac |
| 351 |
if [ "$match_dev" = 'yes' -a "$local_spanno" = "$spanno" ]; then |
| 352 |
#printf "%-8s (%s) %-14s %s %s\n" "$local_spanno" "$spanno" "[$hardware_id]" "$location" "$devpath" |
| 353 |
echo "[$hardware_id]:$local_spanno" |
| 354 |
fi |
| 355 |
done |
| 356 |
} |
| 357 |
|
| 358 |
list_devices() { |
| 359 |
wanted="$1" |
| 360 |
if [ ! -f "$DAHDISASSIGNEDSPANSCONF" ]; then |
| 361 |
echo >&2 "$0: Missing '$DAHDISASSIGNEDSPANSCONF'" |
| 362 |
exit 1 |
| 363 |
fi |
| 364 |
echo >&2 "using '$DAHDISASSIGNEDSPANSCONF'" |
| 365 |
for device in $DEVICES |
| 366 |
do |
| 367 |
devpath=`cd "$device" && pwd -P` |
| 368 |
location='@'`attr_clean "$device/location"` |
| 369 |
hardware_id=`attr_clean "$device/hardware_id"` |
| •370 |
for local_spanno in `get_device_spannos "$device"` |
| 371 |
do |
| 372 |
found=`dev_match_conf "$devpath" "$location" "$hardware_id" "$local_spanno"` |
| 373 |
if [ "$wanted" = "unmatched" ]; then |
| 374 |
[ -z "$found" ] && echo "[$hardware_id]:$local_spanno" |
| 375 |
else |
| 376 |
[ -z "$found" ] || echo "[$hardware_id]:$local_spanno" |
| 377 |
fi |
| 378 |
done |
| 379 |
done |
| 380 |
} |
| 381 |
|
| 382 |
case "$action" in |
| 383 |
auto) |
| 384 |
auto_assign_devices |
| 385 |
;; |
| 386 |
add) |
| 387 |
assign_devices |
| 388 |
;; |
| 389 |
remove) |
| 390 |
unassign_all_spans |
| 391 |
;; |
| 392 |
list) |
| 393 |
show_devices |
| 394 |
;; |
| 395 |
dumpconfig) |
| 396 |
dump_config |
| 397 |
;; |
| 398 |
matched) |
| 399 |
list_devices "matched" |
| 400 |
;; |
| 401 |
unmatched) |
| 402 |
list_devices "unmatched" |
| 403 |
;; |
| 404 |
*) |
| 405 |
echo >&2 "Bad action='$action'" |
| 406 |
usage |
| 407 |
;; |
| 408 |
esac |