#!/bin/bash
# fix-tsys7.sh - fixes the two issues found: NFS options string + bond hash
# Runs on the target host directly.
set -euo pipefail

echo "==================================================================="
echo " FIX: NFS options + bond hash policy"
echo "==================================================================="

# --- FIX 1: Simplify NFS options in storage.cfg ---------------------------
echo ""
echo "--- FIX 1: Simplify NFS options (remove version=4.2 conflict) ---"
# Replace the overly-complex options line with a minimal one
# PVE handles vers/rsize/wsize/hard/etc internally; we only need nconnect + noatime
if grep -q 'options.*nconnect=4.*version=4.2' /etc/pve/storage.cfg; then
    # Use sed to replace each options line
    sed -i 's/options nconnect=4,noatime,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,version=4.2/options nconnect=4,noatime/' /etc/pve/storage.cfg
    echo "Fixed: simplified NFS options to 'nconnect=4,noatime'"
else
    echo "Options line already simplified or not present"
fi

echo ""
echo "--- Verify storage.cfg NFS stanzas ---"
grep -A 2 "^nfs: D2" /etc/pve/storage.cfg | head -3
echo "..."
grep -c "options nconnect" /etc/pve/storage.cfg
echo " NFS stanzas with options"

# --- FIX 2: Bond hash policy (force via sysfs + persist in interfaces) ----
echo ""
echo "--- FIX 2: Apply bond xmit_hash_policy=layer3+4 ---"

# 2a: Apply LIVE via sysfs (takes effect immediately, no network reload)
echo "Applying live via sysfs..."
if echo "layer3+4" > /sys/class/net/bond0/bonding/xmit_hash_policy 2>/dev/null; then
    echo "Live sysfs apply: SUCCESS"
else
    echo "Live sysfs apply: FAILED (will persist in config and apply on ifreload)"
fi

# Verify live state
echo ""
echo "Live bond0 hash policy:"
cat /proc/net/bonding/bond0 | grep "Transmit Hash"

# 2b: Persist in /etc/network/interfaces (fix the sed that failed before)
echo ""
echo "Persisting in /etc/network/interfaces..."

# Backup
cp -a /etc/network/interfaces "/root/interfaces.bondfix.$(date +%Y%m%d%H%M%S)"

# Check if already present
if grep -q 'bond-xmit-hash-policy' /etc/network/interfaces; then
    echo "bond-xmit-hash-policy already in interfaces file"
else
    # Use awk to insert after the bond-mode line (more reliable than sed)
    # Match any line containing 'bond-mode' (regardless of indentation)
    awk '
        /bond-mode/ && !done {
            print
            print "\tbond-xmit-hash-policy layer3+4"
            done=1
            next
        }
        { print }
    ' /etc/network/interfaces > /etc/network/interfaces.new

    mv /etc/network/interfaces.new /etc/network/interfaces
    echo "Inserted bond-xmit-hash-policy layer3+4"
fi

echo ""
echo "--- Updated bond0 stanza ---"
awk '/^auto bond0/,/^$/' /etc/network/interfaces

# Final verify
echo ""
echo "--- Final bond0 running state ---"
cat /proc/net/bonding/bond0 | head -20

echo ""
echo "==================================================================="
echo " FIX COMPLETE"
echo "==================================================================="
