-
Notifications
You must be signed in to change notification settings - Fork 5
/
netconsole.sh
99 lines (84 loc) · 3.57 KB
/
netconsole.sh
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
#!/bin/bash
ACTION=$1
NAME=netconsole
case $ACTION in
start )
# Exit if we already have netconsole loaded
if lsmod | grep -q "^$NAME"; then
echo "Module $NAME is already loaded. Exiting."
exit 1
fi
# Define needed variables
DESTINATION_SERVER_MAC=''
SOURCE_PORT=''
DEFAULT_CONF='/etc/default/netconsole'
# Load variables from defaults
test -r $DEFAULT_CONF && . $DEFAULT_CONF
if [ "$ENABLE_NETCONSOLE" = yes ]; then
# Exit if we have no DESTINATION_SERVER_IP
if [ -z "$DESTINATION_SERVER_IP" ]; then
echo "Please set DESTINATION_SERVER_IP variable in $DEFAULT_CONF or disable $NAME module."
exit 1
fi
# Set GATEWAY_IP and SOURCE_IP from route to DESTINATION_SERVER_IP
read GATEWAY_IP SOURCE_INTERFACE SOURCE_IP <<< $(ip -o route get $DESTINATION_SERVER_IP | grep src | awk '{print $3, $5, $7}')
# If DESTINATION_SERVER_MAC is not defined explicitly we use gateway mac address
if [ -z "$DESTINATION_SERVER_MAC" ]; then
GATEWAY_MAC=$(arp "$GATEWAY_IP" | grep "$SOURCE_INTERFACE" | grep -oE '[a-z0-9:]{17}')
# If we have no arp record for gateway - ping DESTINATION_SERVER_IP to fill arp table and retry
if [ -z "$GATEWAY_MAC" ]; then
# Exit if we have no ping
RESULT=$(type ping >/dev/null 2>1; echo $?)
if [ ! $RESULT -eq 0 ]; then
echo -e "You have no ping utility. Please, install it."
exit 1
fi
ping -n -q -c 3 -i 0.1 $DESTINATION_SERVER_IP > /dev/null
GATEWAY_MAC=$(arp "$GATEWAY_IP" | grep "$SOURCE_INTERFACE" | grep -oE '[a-z0-9:]{17}')
# If we still have no MAC - exiting
if [ -z "$GATEWAY_MAC" ]; then
echo "We can't get GATEWAY_MAC and no DESTINATION_SERVER_MAC set. Exiting."
exit 1
fi
fi
DESTINATION_SERVER_MAC=$GATEWAY_MAC
fi
# Set SOURCE_PORT
if [ -z "$SOURCE_PORT" ]; then
SOURCE_PORT="6666"
fi
# Encrease logging level up to 8, because standard debian has 7 and did not send many messages to netconsole$
# # https://www.kernel.org/doc/Documentation/sysctl/kernel.txt
echo 8 > /proc/sys/kernel/printk
# Form options line
MODULE_OPTIONS="$NAME=$SOURCE_PORT@$SOURCE_IP/$SOURCE_INTERFACE,$DESTINATION_PORT@$DESTINATION_SERVER_IP/$DESTINATION_SERVER_MAC"
# Actualy load module
if modprobe $NAME "$MODULE_OPTIONS"; then
echo "Loaded $NAME."
exit 0
else
echo "Failed to load $NAME."
fi
else
echo "Netconsole is disabled in $DEFAULT_CONF, if you need it, please enable."
exit 1
fi
;;
stop )
if ! lsmod | grep -q "^$NAME"; then
echo "Module $NAME is not loaded. Exiting."
exit 0
fi
if modprobe -r $NAME; then
echo "Unloaded $NAME"
exit 0
else
echo "Failed to unload $NAME"
exit 1
fi
;;
* )
SCRIPT_NAME=`basename $0`
echo "Usage: $SCRIPT_NAME <start|stop>"
;;
esac