4 DEFAULT
=/etc
/default
/$FS
5 LOGGER
="/usr/bin/logger -t ${FS}-hotplug"
6 LOG_ERR
="$LOGGER -p user.err --"
7 LOG_NOTICE
="$LOGGER -p user.notice --"
8 LOG_WARN
="$LOGGER -p user.warn --"
10 [ "$ACTION" = ifup
] ||
exit 0
12 [ -f $DEFAULT ] && .
$DEFAULT
14 [ -n "$FS_HOTPLUG_INTERFACE" ] ||
exit 0
16 [ "$INTERFACE" = "$FS_HOTPLUG_INTERFACE" ] ||
exit 0
18 pgrep
$FS &> /dev
/null
20 $LOG_NOTICE Stopping
$FS
21 /etc
/init.d
/$FS stop
&> /dev
/null
22 pgrep
$FS &> /dev
/null
24 $LOG_ERR Failed to stop
$FS
27 $LOG_NOTICE $FS stopped
31 [ "$FS_HOTPLUG_TIMEOUT" -gt 0 ] 2> /dev
/null ||
unset FS_HOTPLUG_TIMEOUT
32 TIMEOUT
="${FS_HOTPLUG_TIMEOUT:-60}"
34 # Mount condition, idea lifted from OpenWrt wiki
35 [ -n "$FS_HOTPLUG_MOUNTPOINT" ] && {
37 if ! [ -d "$FS_HOTPLUG_MOUNTPOINT" ]; then
38 $LOG_ERR "$FS_HOTPLUG_MOUNTPOINT" not a valid mount point
42 mnt
="$FS_HOTPLUG_MOUNTPOINT"
46 while [ -n "$notReady" -a $timeout -gt 0 ]; do
47 if [ "$notReady" != start
]; then
48 $LOG_NOTICE "$mnt" not yet mounted
, timeout
in $timeout s
50 timeout
=$
(($timeout-5))
55 result
=$
(cat /proc
/mounts |
awk '{print $2}' |
grep "^$mnt\$")
56 if [ -z "$result" ]; then
57 notReady
="$mnt not ready yet"
61 if [ -n "$notReady" ]; then
62 $LOG_ERR "$mnt" still not mounted
63 $LOG_ERR Not starting
$FS
66 $LOG_NOTICE "$mnt" mounted
72 [ -n "$FS_HOTPLUG_NTPD" ] && {
74 type ntpq
&> /dev
/null
76 $LOG_ERR ntpq utility not found
80 pgrep ntpd
&> /dev
/null ||
{
81 $LOG_ERR ntpd not running
88 result
=$
(uci get
'system.ntp.enabled' 2> /dev
/null
)
89 [ "$result" -eq 1 ] 2> /dev
/null
&& {
90 $LOG_WARN BusyBox NTP client _and_ ntpd running
93 while [ -n "$notReady" -a $timeout -gt 0 ]; do
94 if [ "$notReady" != start
]; then
95 $LOG_NOTICE System
time not
in sync yet
, timeout
in $timeout s
97 timeout
=$
(($timeout-5))
102 result
=$
(ntpq
-c 'timeout 300' -c 'rv 0 stratum' 2> /dev
/null | \
103 awk -F '=' '{print $2}' |
grep -o -E '^[0-9]+')
104 if [ -z $result ]; then
105 $LOG_WARN Failed to extract stratum from ntpd
106 notReady
="unable to extract stratum"
108 $LOG_NOTICE ntpd stratum
$result
109 if [ $result -lt 16 ] 2> /dev
/null
; then
110 result
=$
(ntpq
-c 'timeout 300' -c 'rv 0 offset' 2> /dev
/null \
111 |
awk -F '=' '{print $2}' |
grep -o -E '^-?[0-9]+')
112 if [ -z $result ]; then
113 $LOG_WARN Failed to extract offset from ntpd
114 notReady
="unable to extract offset"
116 # "-0" looks stupid, so remove "-"
117 result
=$
(echo $result |
grep -o '[0-9]*')
118 $LOG_NOTICE ntpd to system
time offset \
+\
/\
- $result ms
119 # If offset < 100 ms consider system time in sync
120 [ $result -lt 100 ] || notReady
="system time not in sync yet"
123 notReady
="ntpd not in sync yet"
128 if [ -n "$notReady" ]; then
129 $LOG_ERR System
time still not
in sync
130 $LOG_ERR Not starting
$FS
133 $LOG_NOTICE System
time in sync
138 /etc
/init.d
/$FS start
&> /dev
/null
139 # Wait a bit in order for pgrep to be able to find the new process
141 pgrep
$FS &>/dev
/null
142 if [ $?
-eq 0 ]; then
143 $LOG_NOTICE Started
$FS due to
\"ifup
"$INTERFACE"\" event
145 $LOG_ERR Start of
$FS due to
\"ifup
"$INTERFACE"\" event failed