-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathhotplug-event.sh
More file actions
117 lines (96 loc) · 3.25 KB
/
hotplug-event.sh
File metadata and controls
117 lines (96 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Handle hotplug events
#
#jas-update=hotplug-event.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
EXECUTE_COMMAND="" # command to execute in addition to build-in script (receives arguments: $1 = subsystem, $2 = action)
NO_INTEGRATION=false # set to true to disable integration with jacklul/asuswrt-scripts
RUN_EVERY_MINUTE=false # verify that the hotplug configuration is still modified (true/false), this is usually not be needed
load_script_config
hotplug_config() {
lockfile lockwait
case "$1" in
"modify")
if [ -f /etc/hotplug2.rules ]; then
grep -Fq "$script_path" /etc/hotplug2.rules && return # already modified
[ ! -f /etc/hotplug2.rules.bak ] && mv /etc/hotplug2.rules /etc/hotplug2.rules.bak
cat /etc/hotplug2.rules.bak > /etc/hotplug2.rules
cat <<EOT >> /etc/hotplug2.rules
# Added by $script_name
SUBSYSTEM == block, DEVICENAME is set, ACTION ~~ ^(add|remove)$ {
exec "$script_path" event %SUBSYSTEM% %ACTION% ;
}
SUBSYSTEM == net, DEVICENAME is set, ACTION ~~ ^(add|remove)$ {
exec "$script_path" event %SUBSYSTEM% %ACTION% ;
}
EOT
killall hotplug2 2> /dev/null
logecho "Modified hotplug configuration" alert
fi
;;
"restore")
if [ -f /etc/hotplug2.rules ] && [ -f /etc/hotplug2.rules.bak ]; then
rm /etc/hotplug2.rules
cp /etc/hotplug2.rules.bak /etc/hotplug2.rules
killall hotplug2 2> /dev/null
logecho "Restored original hotplug configuration" alert
fi
;;
esac
lockfile unlock
}
case "$1" in
"run")
if [ -f /etc/hotplug2.rules ] && ! grep -Fq "$script_path" /etc/hotplug2.rules; then
hotplug_config modify
fi
;;
"event")
lockfile lockwait "event_${2}_${3}"
case "$2" in
"block"|"net"|"misc")
logecho "Running script (args: '$2' '$3')"
;;
esac
if [ "$NO_INTEGRATION" != true ]; then
# $2 = subsystem, $3 = action
case "$2" in
"block")
execute_script_basename "fstrim.sh" hotplug
execute_script_basename "swap.sh" hotplug
execute_script_basename "entware.sh" hotplug
;;
"net")
execute_script_basename "usb-network.sh" hotplug
;;
esac
fi
[ -n "$EXECUTE_COMMAND" ] && eval "$EXECUTE_COMMAND $2 $3"
lockfile unlock "event_${2}_${3}"
exit
;;
"start")
hotplug_config modify
if [ "$RUN_EVERY_MINUTE" = true ]; then
crontab_entry add "*/1 * * * * $script_path run"
fi
;;
"stop")
crontab_entry delete
hotplug_config restore
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac