Debian Codegraph @

netconsole-setup

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 set -eu
3
4 # Copyright: 2018 1&1 IONOS Cloud GmbH
5 # Authors: Daniel Swarbrick <[email protected]>
6 # Benjamin Drung <[email protected]>
7 #
8 # Permission to use, copy, modify, and/or distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
11 #
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 CONFIGFS_PATH=/sys/kernel/configscip-shell filesystem . . `/sys/kernel/config`.
21 NETCONSOLE_PATH=$CONFIGFS_PATH/netconsole
22
23 is_ip_addressscip-shell shell shell-project 0.0.0 is_ip_address().() {
24 local address="$1"
25 local returncode=1
26
27 # Check for IPv4 or IPv6 address
28 if [[ "$address" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$ ]]; then
29 returncode=0
30 fi
31 return "$returncode"
32 }
33
34 ip_to_macscip-shell shell shell-project 0.0.0 ip_to_mac().() {
35 local address="$1"
36 local from_interface="$2"
37
38 mac=$(ip -o neigh show to "$address" dev "$from_interface" | grepscip-shell system . . grep. -oE "lladdr [^ ]*" | cutscip-shell system . . cut. -d ' ' -f 2)
39 if testscip-shell system . . test. -z "$mac"; then
40 # Run arping to determine the MAC address for the given IP address
41 if arping -V 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -q iputils; then
42 # iputils' arping
43 mac=$(arping -c 1 -f -I "$from_interface" "$address" | sedscip-shell system . . sed. -n 's/^.* reply from [^ ]* \[\(.*\)\].*$/\1/p' | headscip-shell system . . head. -n 1)
44 mac="${mac,,}"
45 else
46 # Assume Thomas Habet's arping
47 mac=$(arping -c 1 -i "$from_interface" -r "$address" | headscip-shell system . . head. -n 1)
48 fi
49 fi
50 echoscip-shell system . . echo. "$mac"
51 }
52
53 if testscip-shell system . . test. "$#" -eq 0; then
54 echoscip-shell system . . echo. "${0##*/}: No target specified. Doing nothing." >&2
55 exit 0
56 fi
57
58 if [scip-shell system . . `[`. ! -d $CONFIGFS_PATH ]; then
59 echoscip-shell system . . echo. "${0##*/}: Kernel config directory $CONFIGFS_PATH not present." >&2
60 echoscip-shell system . . echo. "${0##*/}: Loading kernel module 'configfs'..." >&2
61 modprobe configfs
62 if command -v udevadm >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
63 udevadm settle
64 fi
65 fi
66
67 if ! mountscip-shell system . . mount. | grepscip-shell system . . grep. -qw "$CONFIGFS_PATH"; then
68 echoscip-shell system . . echo. "${0##*/}: configfs not mounted to '$CONFIGFS_PATH'; mounting configfs..." >&2
69 mountscip-shell system . . mount. -t configfs configfs $CONFIGFS_PATH
70 fi
71
72 if [scip-shell system . . `[`. ! -d $NETCONSOLE_PATH ]; then
73 if modprobe -n --first-time netconsole >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
74 echoscip-shell system . . echo. "${0##*/}: Loading kernel module 'netconsole'..." >&2
75 modprobe netconsole
76 else
77 echoscip-shell system . . echo. "${0##*/}: Error: netconsole dynamic config directory '$NETCONSOLE_PATH' not present." >&2
78 exit 1
79 fi
80 fi
81
82 for option in "$@"; do
83 if [[ "$option" =~ ^((\+)?([0-9]+)?@)?(.+)$ ]]; then
84 if testscip-shell system . . test. "${BASH_REMATCH[2]}" = "+"; then
85 extended=1
86 else
87 extended=0
88 fi
89 remote_port=${BASH_REMATCH[3]}
90 target=${BASH_REMATCH[4]}
91 else
92 echoscip-shell system . . echo. "${0##*/}: Failed to parse option '$option'. Expected format: [[+][target-port]@]target-host" >&2
93 continue
94 fi
95
96 target_path=$NETCONSOLE_PATH/$target
97
98 if is_ip_addressscip-shell shell shell-project 0.0.0 is_ip_address(). "$target"; then
99 to_ip="$target"
100 else
101 to_ip=$(LANG=C getentscip-shell system . . getent. hosts -- "$target" | grepscip-shell system . . grep. -oE "^([0-9a-fA-F.:]+)" || truescip-shell system . . true.)
102 fi
103
104 if testscip-shell system . . test. -z "$to_ip"; then
105 echoscip-shell system . . echo. "${0##*/}: Skipped host '$target'. No IP found for it." >&2
106 continue
107 fi
108
109 route=$(ip -o route get "$to_ip")
110 from_interface=$(echoscip-shell system . . echo. "$route" | grepscip-shell system . . grep. -oE "dev [^ ]*" | cutscip-shell system . . cut. -d ' ' -f 2)
111 from_ip=$(echoscip-shell system . . echo. "$route" | grepscip-shell system . . grep. -oE "src [^ ]*" | cutscip-shell system . . cut. -d ' ' -f 2)
112 to_gateway=$(echoscip-shell system . . echo. "$route" | grepscip-shell system . . grep. -oE "via [^ ]*" | cutscip-shell system . . cut. -d ' ' -f 2)
113 if testscip-shell system . . test. -z "$to_gateway"; then
114 # If there is no "via" in the output of the ip route lookup, then the packets will
115 # be sent directory to the target (without any additional hops)
116 to_gateway=$to_ip
117 fi
118
119 to_mac=$(ip_to_macscip-shell shell shell-project 0.0.0 ip_to_mac(). "$to_gateway" "$from_interface")
120 if testscip-shell system . . test. -z "$to_mac"; then
121 echoscip-shell system . . echo. "${0##*/}: $target: Cannot resolve MAC address of $to_gateway." >&2
122 exit 1
123 fi
124
125 mkdirscip-shell system . . mkdir. -p "$target_path"
126
127 declare -A changes=( [dev_name]="$from_interface" [local_ip]="$from_ip"
128 [remote_ip]="$to_ip" [remote_mac]="$to_mac" [extended]="$extended" )
129 if testscip-shell system . . test. -n "$remote_port"; then
130 changes[remote_port]="$remote_port"
131 fi
132
133 for config in "${!changes[@]}"; do
134 if testscip-shell system . . test. "$(catscip-shell system . . cat. "$target_path/$config")" = "${changes[$config]}"; then
135 # Setting already in the correct state
136 unset "changes[$config]"
137 fi
138 done
139
140 enabled=$(catscip-shell system . . cat. "$target_path/enabled")
141 if testscip-shell system . . test. "${#changes[@]}" -eq 0 && testscip-shell system . . test. "$enabled" -eq 1; then
142 echoscip-shell system . . echo. "${0##*/}: $target already configured correctly." >&2
143 else
144 echoscip-shell system . . echo. "${0##*/}: $target: Configure sending logs to $to_ip via MAC $to_mac from $from_ip on interface '$from_interface'." >&2
145
146 if testscip-shell system . . test. "$enabled" -ne 0; then
147 echoscip-shell system . . echo. 0 > "$target_path/enabled"
148 fi
149 for config in "${!changes[@]}"; do
150 echoscip-shell system . . echo. "${changes[$config]}" > "$target_path/$config"
151 done
152 echoscip-shell system . . echo. 1 > "$target_path/enabled"
153 fi
154 done
155
156 # shellcheck disable=SC2034
157 read -r console_log_level default_log_level minimum_log_level maximum_log_level < /proc/sys/kernel/printkscip-shell filesystem . . `/proc/sys/kernel/printk`.
158 log_level=$(( console_log_level > minimum_log_level ? console_log_level - 1 : console_log_level ))
159 echoscip-shell system . . echo. "<${log_level}>${0##*/}: Test log message to verify netconsole configuration." > /dev/kmsgscip-shell filesystem . . `/dev/kmsg`.