| 1 | #!/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/config |
| 21 | NETCONSOLE_PATH=$CONFIGFS_PATH/netconsole |
| 22 | |
| 23 | 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_mac() { |
| 35 | local address="$1" |
| 36 | local from_interface="$2" |
| 37 | |
| 38 | mac=$(ip -o neigh show to "$address" dev "$from_interface" | grep -oE "lladdr [^ ]*" | cut -d ' ' -f 2) |
| 39 | if test -z "$mac"; then |
| 40 | # Run arping to determine the MAC address for the given IP address |
| 41 | if arping -V 2>/dev/null | grep -q iputils; then |
| 42 | # iputils' arping |
| 43 | mac=$(arping -c 1 -f -I "$from_interface" "$address" | sed -n 's/^.* reply from [^ ]* \[\(.*\)\].*$/\1/p' | head -n 1) |
| 44 | mac="${mac,,}" |
| 45 | else |
| 46 | # Assume Thomas Habet's arping |
| 47 | mac=$(arping -c 1 -i "$from_interface" -r "$address" | head -n 1) |
| 48 | fi |
| 49 | fi |
| 50 | echo "$mac" |
| 51 | } |
| 52 | |
| 53 | if test "$#" -eq 0; then |
| 54 | echo "${0##*/}: No target specified. Doing nothing." >&2 |
| 55 | exit 0 |
| 56 | fi |
| 57 | |
| 58 | if [ ! -d $CONFIGFS_PATH ]; then |
| 59 | echo "${0##*/}: Kernel config directory $CONFIGFS_PATH not present." >&2 |
| 60 | echo "${0##*/}: Loading kernel module 'configfs'..." >&2 |
| 61 | modprobe configfs |
| 62 | if command -v udevadm >/dev/null 2>&1; then |
| 63 | udevadm settle |
| 64 | fi |
| 65 | fi |
| 66 | |
| 67 | if ! mount | grep -qw "$CONFIGFS_PATH"; then |
| 68 | echo "${0##*/}: configfs not mounted to '$CONFIGFS_PATH'; mounting configfs..." >&2 |
| 69 | mount -t configfs configfs $CONFIGFS_PATH |
| 70 | fi |
| 71 | |
| 72 | if [ ! -d $NETCONSOLE_PATH ]; then |
| 73 | if modprobe -n --first-time netconsole >/dev/null 2>&1; then |
| 74 | echo "${0##*/}: Loading kernel module 'netconsole'..." >&2 |
| 75 | modprobe netconsole |
| 76 | else |
| 77 | 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 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 | 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_address "$target"; then |
| 99 | to_ip="$target" |
| 100 | else |
| 101 | to_ip=$(LANG=C getent hosts -- "$target" | grep -oE "^([0-9a-fA-F.:]+)" || true) |
| 102 | fi |
| 103 | |
| 104 | if test -z "$to_ip"; then |
| 105 | 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=$(echo "$route" | grep -oE "dev [^ ]*" | cut -d ' ' -f 2) |
| 111 | from_ip=$(echo "$route" | grep -oE "src [^ ]*" | cut -d ' ' -f 2) |
| 112 | to_gateway=$(echo "$route" | grep -oE "via [^ ]*" | cut -d ' ' -f 2) |
| 113 | if 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_mac "$to_gateway" "$from_interface") |
| 120 | if test -z "$to_mac"; then |
| 121 | echo "${0##*/}: $target: Cannot resolve MAC address of $to_gateway." >&2 |
| 122 | exit 1 |
| 123 | fi |
| 124 | |
| 125 | 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 test -n "$remote_port"; then |
| 130 | changes[remote_port]="$remote_port" |
| 131 | fi |
| 132 | |
| 133 | for config in "${!changes[@]}"; do |
| 134 | if test "$(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=$(cat "$target_path/enabled") |
| 141 | if test "${#changes[@]}" -eq 0 && test "$enabled" -eq 1; then |
| 142 | echo "${0##*/}: $target already configured correctly." >&2 |
| 143 | else |
| 144 | echo "${0##*/}: $target: Configure sending logs to $to_ip via MAC $to_mac from $from_ip on interface '$from_interface'." >&2 |
| 145 | |
| 146 | if test "$enabled" -ne 0; then |
| 147 | echo 0 > "$target_path/enabled" |
| 148 | fi |
| 149 | for config in "${!changes[@]}"; do |
| 150 | echo "${changes[$config]}" > "$target_path/$config" |
| 151 | done |
| 152 | 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/printk |
| 158 | log_level=$(( console_log_level > minimum_log_level ? console_log_level - 1 : console_log_level )) |
| 159 | echo "<${log_level}>${0##*/}: Test log message to verify netconsole configuration." > /dev/kmsg |