Skip to content

Commit a0a742c

Browse files
committed
DAHDI: Massive overhaul of start/stop/restart logic.
* Add merged (but not in a release) patch to fix dahdi spantype. This is needed for manual span assignments to work properly. * Massive overhaul of "stop" and "start" commands to be more robust, comprehensive, and user friendly. These processes now work reliably for me and have always been a little fickle in the past. * Replace the lightweight start logic for "restart" with the existing and now greatly improved dedicated "start" logic.
1 parent e8b9c3f commit a0a742c

File tree

2 files changed

+102
-58
lines changed

2 files changed

+102
-58
lines changed

phreaknet.1.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ the best Asterisk and DAHDI experience.
258258
**edit**
259259
: Edit local PhreakScript source directly
260260

261+
**touch**
262+
: Show PhreakScript file path and last modification
263+
261264
# OPTIONS
262265

263266
**-h**

phreaknet.sh

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# PhreakScript
44
# (C) 2021-2024 Naveen Albert, PhreakNet, and others - https://github.com/InterLinked1/phreakscript ; https://portal.phreaknet.org ; https://docs.phreaknet.org
5-
# v1.1.4 (2024-09-11)
5+
# v1.1.5 (2024-09-15)
66

77
# Setup (as root):
88
# cd /usr/local/src
@@ -13,6 +13,7 @@
1313
# phreaknet install
1414

1515
## Begin Change Log:
16+
# 2024-09-15 1.1.5 DAHDI: Massive overhaul to DAHDI stop/start/restart logic, fixes for manual span assignment
1617
# 2024-09-11 1.1.4 DAHDI: Target DAHDI 3.4.0, update patches
1718
# 2024-03-17 1.1.3 DAHDI: Only build wanpipe if requested
1819
# 2024-03-09 1.1.2 Asterisk: fix broken patches that no longer applied
@@ -444,6 +445,7 @@ Commands:
444445
applist List Asterisk dialplan applications in current source
445446
funclist List Asterisk dialplan functions in current source
446447
edit Edit PhreakScript
448+
touch Show PhreakScript file path and last modification
447449
448450
Options:
449451
-b, --backtraces Enables getting backtraces
@@ -579,9 +581,12 @@ stop_telephony() {
579581
printf "Asterisk not currently running...\n"
580582
fi
581583
lsmod | grep dahdi
582-
curdrivers=`lsmod | grep "dahdi " | xargs | cut -d' ' -f4-`
584+
curdrivers=`lsmod | grep "dahdi " | xargs | cut -d' ' -f4-` # Space intentionally included, add dahdi_vpmadt032_loader later manually
583585
printf "Current drivers: --- %s ---\n", "$curdrivers"
584586
stop_wanpipe
587+
echo "$curdrivers" | tr ',' '\n' | xargs -i sh -c 'echo Stopping driver: {}; modprobe -r {}'
588+
modprobe -r dahdi_vpmadt032_loader
589+
modprobe -r dahdi_voicebus
585590
service dahdi status
586591
# WARNING WARNING WARNING
587592
# It seems that on a system where wanpipe is in use,
@@ -604,49 +609,7 @@ stop_telephony() {
604609
printf "DAHDI shutdown complete\n"
605610
else
606611
printf "DAHDI is not running, skipping...\n"
607-
fi
608-
}
609-
610-
restart_start_telephony() {
611-
printf "Starting DAHDI...\n"
612-
start_wanpipe
613-
modprobe dahdi
614-
# e.g. modprobe wcte13xp
615-
echo "$curdrivers" | tr ',' '\n' | xargs -i sh -c 'echo Starting driver: {}; modprobe {}'
616-
if ! service dahdi start; then
617-
die "DAHDI failed to start"
618-
fi
619-
if ! dahdi_cfg; then # reload configs for all spans
620-
# I've noticed before the case where the order of the modprobes seems to matter
621-
# For example, with wcte13xp on span 1 and wctdm on span 2,
622-
# it loads fine if wcte13xp is loaded before wctdm, but not vice versa
623-
# Reversing the order has a good chance of making it work on "simple" systems
624-
# that only have a couple cards. More than that, and if it's failing here,
625-
# you might need to debug the modprobe order manually.
626-
printf "First initialization attempt failed, retrying modprobes in reverse order...\n"
627-
# WARNING As above, need to ensure wanpipe is STOPPED before we attempt to stop DAHDI
628-
# And yes, from testing, it really is necessary to do service dahdi restart,
629-
# before we trying modprobing the modules again
630-
stop_wanpipe
631-
service dahdi restart
632-
modprobe dahdi
633-
start_wanpipe
634-
printf "Previously loaded drivers: %s\n", "$curdrivers"
635-
# Reverse the order of the driver modprobes
636-
echo "$curdrivers" | tr ',' '\n' | tac | xargs -i sh -c 'echo Starting driver: {}; modprobe {}'
637-
dahdi_cfg
638-
if [ $? -ne 0 ]; then
639-
dahdi_hardware
640-
die "DAHDI failed to initialize... please try manually modprobe'ing the drivers and rerunning dahdi_genconf and dahdi_cfg."
641-
fi
642-
fi
643-
printf "DAHDI is now running normally...\n"
644-
if [ "$1" = "1" ]; then
645-
rasterisk -x "module load chan_dahdi"
646-
else
647-
service asterisk start
648-
astpid=$( ps -aux | grep "asterisk" | grep -v "grep" | head -n 1 | xargs | cut -d' ' -f2 )
649-
printf "Asterisk now running on pid %s\n" "$astpid"
612+
modprobe -r dahdi # In case the service was already stopped but dahdi module was not yet unloaded
650613
fi
651614
}
652615

@@ -655,31 +618,103 @@ restart_start_telephony() {
655618
# $1 to restart without completely restarting Asterisk
656619
restart_telephony() {
657620
stop_telephony "$1"
658-
restart_start_telephony
621+
start_telephony
659622
}
660623

661624
# Mainly intended to start the telephony drivers on bootup, since this doesn't always happen automatically
662625
start_telephony() {
626+
if [ ! -f /etc/udev/rules.d/dahdi.rules ]; then
627+
echoerr "DAHDI udev rules are missing..."
628+
fi
629+
630+
modprobe --first-time dahdi
631+
if [ $? -ne 0 ]; then
632+
echoerr "DAHDI is still running... stop these modules and try again"
633+
lsmod | grep "dahdi" | xargs | cut -d' ' -f4- # No space in this grep
634+
exit 1
635+
fi
636+
start_wanpipe # This can fail, if wanpipe isn't being used, and that's fine
637+
638+
printf "DAHDI hardware:\n"
639+
dahdi_hardware # List DAHDI hardware. The dahdi module must first be running for this to work.
640+
641+
# Start drivers for any telephony cards, which is needed for span assignment
663642
# sed: Each one ends in - or +, need to ignore that
664-
service dahdi start
665-
modprobe dahdi
666-
start_wanpipe
667643
for driver in $( dahdi_hardware | awk '{print $2}' | tr ',' '\n' | sed 's/.$//' ) ; do
668-
printf "Starting driver: %s\n" "$driver"
669-
modprobe $driver
644+
printf "Starting driver: %s... " "$driver"
645+
modprobe --first-time $driver
670646
if [ $? -ne 0 ]; then
671-
echoerr "Failed to start driver $driver"
647+
echoerr "\nFailed to start driver $driver"
648+
else
649+
printf "started!\n"
672650
fi
673651
done
674-
dahdi_cfg
652+
653+
# Dump detected configuration since span stuff is VERY fickle and prone to breakage
654+
printf "DAHDI module options: "
655+
cat /etc/modprobe.d/dahdi.conf # Dump DAHDI module options
656+
AUTO_ASSIGN_SPANS=$( cat /sys/module/dahdi/parameters/auto_assign_spans | tr -d '\n' )
657+
printf "DAHDI span auto-assignment: %d\n" "$AUTO_ASSIGN_SPANS"
658+
printf "Detected spans:\n"
659+
dahdi_span_assignments list # List detected spans
660+
661+
# Get the spans assigned.
662+
printf "Assigning DAHDI spans...\n"
663+
dahdi_span_assignments remove
664+
if [ "$AUTO_ASSIGN_SPANS" = "0" ]; then
665+
printf "Manually assigning spans according to /etc/dahdi/assigned-spans.conf\n"
666+
dahdi_span_assignments add
667+
else
668+
printf "Automatically assigning spans, ignoring /etc/dahdi/assigned-spans.conf\n"
669+
num_spans=$( dahdi_span_assignments list | wc -l )
670+
if [ $num_spans -gt 1 ]; then
671+
echoerr "Detected that this machine has more than 1 DAHDI span.\nYou are HIGHLY ENCOURAGED to assign the span order explicitly in /etc/dahdi/assigned-spans.conf!"
672+
echoerr "To do this, add 'options dahdi auto_assign_spans=0' to /etc/modprobe.d/dahdi.conf and run 'phreaknet restart'"
673+
sleep 1
674+
fi
675+
dahdi_span_assignments auto
676+
fi
677+
678+
# Run dahdi_genconf to validate that the spans are assigned properly.
679+
# However, don't actually replace /etc/dahdi/system.conf, since that
680+
# would overwrite span configuration and break the system setup...
681+
# just use a temporary configuration file to catch it and compare.
682+
printf "Generating DAHDI channel configuration and checking differences...\n"
683+
684+
# XXX BUGBUG There seems to be a bug where if (and even if?) dahdi_span_assignments auto is not run
685+
# (even if we're manually assigning spans), dahdi_genconf will just hang.
686+
# We can kill it and all the gubbins DAHDI spawns with "pkill dahdi",
687+
# and then things seem to work.
688+
if [ -f /etc/dahdi/system.conf ]; then
689+
# Per the BUGBUG note above: If dahdi_genconf is still running after 2 seconds, kill it and proceed. Don't ask me to explain it, but it works...
690+
DAHDI_CONF_FILE=/tmp/system.conf dahdi_genconf system & (sleep 2; ps -aux | grep "dahdi_genconf" | grep -v "grep" > /dev/null && pkill dahdi && echoerr "Forcibly killed dahdi_genconf")
691+
# The second time around, it SHOULD work instantly and properly dump dahdi_genconf output into /tmp/system.conf
692+
DAHDI_CONF_FILE=/tmp/system.conf dahdi_genconf system & (sleep 2; ps -aux | grep "dahdi_genconf" | grep -v "grep" > /dev/null && pkill dahdi && echoerr "Forcibly killed dahdi_genconf a second time???")
693+
# This will show us what's different between the system.conf dahdi_genconf just wrote to /tmp/system.conf
694+
# and the actual /etc/dahdi/system.conf
695+
# There are likely to be a few changes, but there shouldn't be anything major if everything went well.
696+
# If there's a big deviation, then manual intervention is likely required.
697+
diff -U 0 /etc/dahdi/system.conf /tmp/system.conf # Output with no context, since basically every line is unique already
698+
else
699+
dahdi_genconf -vvvvv
700+
fi
701+
702+
# Finally, run dahdi_cfg
703+
printf "Applying DAHDI channel configuration...\n"
704+
dahdi_cfg -v 2>/dev/null | grep "to configure" # Show number of channels that will be configured
675705
if [ $? -ne 0 ]; then
676706
lsdahdi
677707
die "DAHDI initialization failed... check to ensure all your spans are online and in the expected order"
678708
fi
679-
# Asterisk likely starts automatically, but reload anything DAHDI related
680-
service asterisk start
681-
/sbin/rasterisk -x "module refresh chan_dahdi"
682-
printf "Telephony initialization completed\n"
709+
printf "DAHDI channel configuration applied successfully!\n"
710+
711+
# Finally, make sure the DAHDI service is running so that systemd can keep track of it...
712+
service dahdi start
713+
714+
service asterisk start # Start Asterisk if it's not running already
715+
/sbin/rasterisk -x "module load chan_dahdi" # Load chan_dahdi if Asterisk was already running
716+
/sbin/rasterisk -x "dahdi show channels" # The ultimate test is what DAHDI channels actually show up in Asterisk
717+
echog "Telephony initialization completed"
683718
}
684719

685720
assert_root() {
@@ -1482,6 +1517,9 @@ install_dahdi() {
14821517
phreak_fuzzy_patch "dahdi_kern_61.diff"
14831518
fi
14841519

1520+
# Merged in master, but not yet in a current release
1521+
git_custom_patch "https://github.com/asterisk/dahdi-linux/commit/d932d9fbc8b3559829a76fffcedceb78d1fc1887.diff"
1522+
14851523
KERN_VER_MM=$( uname -r | cut -d. -f1-2 )
14861524
OS_DIST_2=$( printf "$OS_DIST_INFO" | cut -d' ' -f1-2)
14871525
if [ "$KERN_VER_MM" = "4.18" ] && [ "$OS_DIST_2" = "Rocky Linux" ]; then
@@ -2695,7 +2733,7 @@ elif [ "$cmd" = "make" ]; then
26952733
# phreaknet install
26962734

26972735
assert_root
2698-
ln $FILE_PATH /usr/local/sbin/phreaknet
2736+
ln -nsf $FILE_PATH /usr/local/sbin/phreaknet
26992737
if [ $? -eq 0 ]; then
27002738
echo "PhreakScript added to path."
27012739
else
@@ -3710,6 +3748,9 @@ elif [ "$cmd" = "start" ]; then
37103748
start_telephony
37113749
elif [ "$cmd" = "edit" ]; then
37123750
exec nano $FILE_PATH
3751+
elif [ "$cmd" = "touch" ]; then
3752+
printf "%s: " "$FILE_PATH"
3753+
date -r $FILE_PATH
37133754
elif [ "$cmd" = "validate" ]; then
37143755
run_rules
37153756
elif [ "$cmd" = "trace" ]; then

0 commit comments

Comments
 (0)