Created
June 15, 2024 02:08
-
-
Save mvandermeulen/661f8c7c5dc2cee60254cd1b670e9355 to your computer and use it in GitHub Desktop.
proxmox-if-mtu-setup.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export HOSTNAME=$(/usr/bin/hostname -A) | |
export LOG_FACILITY="user.6" | |
LOG_FILE="net-if-setup.log" | |
LOG_PATH="/var/log" | |
export FLP="${LOG_PATH}/${LOG_FILE}" | |
IFNAME="enx803f5df75c57" | |
BRNAME="vmbr0" | |
MTU="9000" | |
REACHABILITY_TEST_ADDR="10.8.4.1" | |
REACHABILITY_TEST_COUNT="4" | |
REACHABILITY_TEST_PCT_FILE="/tmp/reachability_target_pct" | |
REACHABILITY_TEST_MAC_FILE="/tmp/reachability_target_mac" | |
WAIT_TIME=5 | |
function dlog() { | |
local msg="$1"; /usr/bin/logger -t "${HOSTNAME}" -p "${LOG_FACILITY}" "${msg}"; | |
NOW=$(/usr/bin/date +"%d-%m-%Y %T"); echo "${NOW} ${msg}" >> "${FLP}"; | |
} | |
function get_mtu() { | |
local interface_name="$1"; | |
local interface_mtu=$(/usr/sbin/ip link show "${interface_name}" | /usr/bin/egrep -o "mtu\s([1-9][0-9]{2,4})" | /usr/bin/awk '{print $NF}') | |
dlog "Detected existing mtu of ${interface_mtu} on interface ${interface_name}" | |
echo "${interface_mtu}" | |
} | |
function reachability_check() { | |
local count=$1; local target="$2"; | |
[[ -f "${REACHABILITY_TEST_PCT_FILE}" ]] && rm -f "${REACHABILITY_TEST_PCT_FILE}" && dlog "Deleted ${REACHABILITY_TEST_PCT_FILE}" | |
[[ -f "${REACHABILITY_TEST_MAC_FILE}" ]] && rm -f "${REACHABILITY_TEST_MAC_FILE}" && dlog "Deleted ${REACHABILITY_TEST_MAC_FILE}" | |
dlog "Reachability test to ${target}" | |
local reachability_result=$(/usr/bin/ping -q -c $count "${target}" | /usr/bin/egrep "packet loss") | |
dlog "Reachability test output: ${reachability_result}" | |
local pct=$(echo "$reachability_result" | /usr/bin/egrep -o "([0-9]{1,3}\%?) packet loss" | /usr/bin/awk '{print $NR}' | /usr/bin/egrep -o "^[0-9]{1,3}") | |
echo "${pct}" > "${REACHABILITY_TEST_PCT_FILE}" | |
/usr/sbin/arp -an | /usr/bin/egrep "\(${target}\)" | /usr/bin/awk '{print $4}' > "$REACHABILITY_TEST_MAC_FILE" | |
if grep -q "incomplete" "${REACHABILITY_TEST_MAC_FILE}"; then | |
dlog "Failed to resolve mac address for ${target}" | |
else | |
local target_mac=$(cat "${REACHABILITY_TEST_MAC_FILE}" | egrep -o "([0-9a-z]{2}\:?){6}"); | |
dlog "Resolved mac address ${target_mac} for ${target}" | |
fi | |
echo $pct | |
} | |
EXISTING_IF_MTU=$(get_mtu "${IFNAME}"); EXISTING_BR_MTU=$(get_mtu "${BRNAME}") | |
echo "Interface ${IFNAME} mtu: ${EXISTING_IF_MTU}"; echo "Interface ${BRNAME} mtu: ${EXISTING_BR_MTU}" | |
if [[ "${EXISTING_IF_MTU}" == "${MTU}" && "${EXISTING_BR_MTU}" == "${MTU}" ]]; then | |
LOGMSG="MTU has already been configured on this host. Exiting."; dlog "${LOGMSG}"; echo "${LOGMSG}"; exit 0; | |
fi | |
LOGMSG="Configuring interfaces with ${MTU} mtu"; dlog "$LOGMSG"; echo "$LOGMSG"; | |
/usr/sbin/ip link set "${IFNAME}" mtu "${MTU}" \ | |
&& /usr/sbin/ip link set "${BRNAME}" mtu "${MTU}" \ | |
&& dlog "Configured mtu of ${MTU} on interfaces: ${IFNAME}, ${BRNAME}" | |
LOGMSG="Verifying interface mtu updated to value ${MTU}"; dlog "$LOGMSG"; echo "$LOGMSG"; | |
NEW_IF_MTU=$(get_mtu "${IFNAME}"); NEW_BR_MTU=$(get_mtu "${BRNAME}") | |
if [[ "${NEW_IF_MTU}" == "${MTU}" ]]; then | |
LOGMSG="Verified interface ${IFNAME} mtu: ${NEW_IF_MTU}"; | |
else | |
LOGMSG="Error: Interface ${IFNAME} mtu is ${NEW_IF_MTU}"; | |
fi | |
dlog "${LOGMSG}"; echo "${LOGMSG}"; | |
if [[ "${NEW_BR_MTU}" == "${MTU}" ]]; then | |
LOGMSG="Verified interface ${BRNAME} mtu: ${NEW_BR_MTU}"; | |
else | |
LOGMSG="Error: Interface ${BRNAME} mtu is ${NEW_BR_MTU}"; | |
fi | |
dlog "${LOGMSG}"; echo "${LOGMSG}"; | |
for item in "1" "2" "3" "4"; do | |
sleep "${WAIT_TIME}"; LOSS_PCT=$(reachability_check $REACHABILITY_TEST_COUNT "$REACHABILITY_TEST_ADDR"); | |
LOGMSG="Packet loss to target node: ${LOSS_PCT}"; dlog "$LOGMSG"; echo "$LOGMSG"; | |
if [[ "${LOSS_PCT}" == "0" ]]; then | |
LOGMSG="Connectivity confirmed ok. Exiting"; dlog "$LOGMSG"; echo "$LOGMSG"; break; | |
else | |
if [[ "${item}" == "4" ]]; then | |
LOGMSG="Lost connectivity with ${REACHABILITY_TEST_ADDR} when mtu configured to ${MTU}. Restoring previous configuration"; dlog "$LOGMSG"; echo "$LOGMSG"; | |
/usr/sbin/ip link set "${BRNAME}" mtu "${EXISTING_BR_MTU}"; /usr/sbin/ip link set "${IFNAME}" mtu "${EXISTING_IF_MTU}"; | |
sleep "${WAIT_TIME}"; NEW_LOSS_PCT=$(reachability_check $REACHABILITY_TEST_COUNT "$REACHABILITY_TEST_ADDR"); | |
if [[ "${LOSS_PCT}" != "0" ]]; then | |
LOGMSG="Reachability check failed after mtu restore. Rebooting in 20 seconds"; dlog "$LOGMSG"; echo "$LOGMSG"; | |
sleep 20 && reboot; | |
fi | |
fi | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment