Skip to content

Commit

Permalink
Failproof hhnet script
Browse files Browse the repository at this point in the history
Fixes #370

Signed-off-by: Pau Capdevila <[email protected]>
  • Loading branch information
pau-hedgehog committed Feb 5, 2025
1 parent d1db916 commit 938bd3a
Showing 1 changed file with 55 additions and 15 deletions.
70 changes: 55 additions & 15 deletions pkg/hhfab/hhnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,93 @@

set -e

function wait_for_interface_state() {
local iface=$1
local state=$2 # "up" or "down"
local max_attempts=30
local attempt=0

while [ $attempt -lt $max_attempts ]; do
if [ "$state" = "up" ]; then
ip l s "$iface" | grep -q "state UP" && return 0
else
ip l s "$iface" | grep -q "state DOWN" && return 0
fi
sleep 0.1
attempt=$((attempt + 1))
done
return 1
}

function cleanup() {
for i in {0..3}; do
sudo ip l d "bond$i" 2> /dev/null || true
if ip l s "bond$i" &>/dev/null; then
sudo ip l s "bond$i" down 2>/dev/null || true
wait_for_interface_state "bond$i" "down"
sudo ip link delete "bond$i" 2>/dev/null || true
fi
done

for i in {1..9}; do
sudo ip l s "enp2s$i" down 2> /dev/null || true
for j in {1000..1020}; do
sudo ip l d "enp2s$i.$j" 2> /dev/null || true
done
if ip l s "enp2s$i" &>/dev/null; then
sudo ip l s "enp2s$i" down 2>/dev/null || true
wait_for_interface_state "enp2s$i" "down"
for j in {1000..1020}; do
if ip l s "enp2s$i.$j" &>/dev/null; then
sudo ip l s "enp2s$i.$j" down 2>/dev/null || true
wait_for_interface_state "enp2s$i.$j" "down"
sudo ip link delete "enp2s$i.$j" 2>/dev/null || true
fi
done
fi
done

sleep 1
sleep 2
}

function setup_bond() {
local bond_name=$1

sudo ip l a "$bond_name" type bond miimon 100 mode 802.3ad
sudo ip l a "$bond_name" type bond miimon 100 mode 802.3ad 2>/dev/null || return 1

for iface in "${@:2}"; do
if ! ip l s "$iface" &>/dev/null; then
return 1
fi
# cannot enslave interface if it is up
sudo ip l s "$iface" down 2> /dev/null || true
sudo ip l s "$iface" master "$bond_name"
sudo ip l s "$iface" down 2>/dev/null
wait_for_interface_state "$iface" "down"
sudo ip l s "$iface" master "$bond_name" 2>/dev/null || return 1
done

sudo ip l s "$bond_name" up
sudo ip l s "$bond_name" up 2>/dev/null
wait_for_interface_state "$bond_name" "up"
}

function setup_vlan() {
local iface_name=$1
local vlan_id=$2

sudo ip l s "$iface_name" up
sudo ip l a link "$iface_name" name "$iface_name.$vlan_id" type vlan id "$vlan_id"
sudo ip l s "$iface_name.$vlan_id" up
if ! ip l s "$iface_name" &>/dev/null; then
return 1
fi

sudo ip l s "$iface_name" up 2>/dev/null
wait_for_interface_state "$iface_name" "up"

sudo ip l a link "$iface_name" name "$iface_name.$vlan_id" type vlan id "$vlan_id" 2>/dev/null || return 1
sudo ip l s "$iface_name.$vlan_id" up 2>/dev/null
wait_for_interface_state "$iface_name.$vlan_id" "up"
}

function get_ip() {
local iface_name=$1
local ip=""
local max_attempts=300 # 5 minutes
local attempt=0

while [ -z "$ip" ]; do
attempt=$((attempt + 1))
ip=$(ip a s "$iface_name" | awk '/inet / {print $2}')
ip=$(ip a s "$iface_name" 2>/dev/null | awk '/inet / {print $2}')
[ "$attempt" -ge "$max_attempts" ] && break
sleep 1
done
Expand Down

0 comments on commit 938bd3a

Please sign in to comment.