shell-scripts/bash/special/supermicro_x9dri-ln4f.sh
2026-01-23 12:33:26 +01:00

236 lines
5.5 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ein kleines Skript dass:
# 1. Dateien erstellt, die den Namen der Network-Interfaces an ihren pci-path binden
# 2. Die /etc/network/interfaces auskommentiert falls dort unbekannte interfaces stehen
# 3. alle definierten Interfaces + loopback in die /etc/network/interfaces schreibt
set -euo pipefail
# Standardwert für Interfaces (wenn keine übergeben werden)
interfaces=()
iface_file="/etc/network/interfaces"
name_dir="/etc/systemd/network"
# Verarbeite die übergebenen Shortflags
while getopts "i:f:" opt; do
case $opt in
i)
interfaces+=("$OPTARG")
;;
f)
iface_file="$OPTARG"
;;
*)
echo "Usage: $0 [-f interfaces-file] [-i interface] [-i interface2] ..."
exit 1
;;
esac
done
# Wenn keine Interfaces übergeben wurden, nutze Standard-Interfaces
if [ ${#interfaces[@]} -eq 0 ]; then
interfaces=("eno1" "eno2" "eno3" "eno4")
fi
echo "Die Links für die Interfacenamen werden erstellt:"
# Hier werden die Interface-Namen mit ihren Bussen gelinkt -> einheitlich, persistente Bennenung
create_link_file() {
local file="$name_dir/$1"
local content="$2"
if [[ -f "$file" ]]; then
if diff -q <(printf "%s\n" "$content") "$file" >/dev/null; then
echo "$file existiert bereits und ist korrekt"
return
else
echo "$file existiert, Inhalt unterscheidet sich aktualisiere"
fi
else
echo " $file existiert nicht erstelle"
fi
printf "%s\n" "$content" > "$file"
}
# # Erstelle Links für alle Interfaces
# for iface in "${interfaces[@]}"; do
# create_link_file "10-$iface.link" \
# "[Match]
# Path=pci-0000:06:00.1
# [Link]
# Name=$iface"
# done
create_link_file "10-eno2.link" \
"[Match]
Path=pci-0000:06:00.1
[Link]
Name=eno2"
create_link_file "10-eno3.link" \
"[Match]
Path=pci-0000:06:00.2
[Link]
Name=eno3"
create_link_file "10-eno4.link" \
"[Match]
Path=pci-0000:06:00.3
[Link]
Name=eno4"
echo "Die $iface_file wird angepasst:"
# Funktion zum Auskommentieren der gesamten Datei
comment_out_all() {
local file="$1"
echo "✔ Kommentiere die gesamte Datei aus..."
# Kommt alle Zeilen der Datei aus
sed -i 's/^[[:space:]]*/# /' "$file"
}
# Funktion zum Prüfen, ob ein nicht gewolltes Interface existiert
check_for_unwanted_interfaces() {
local file="$1"
local wanted_interfaces=("lo" "${interfaces[@]}")
local unwanted_found=0
while IFS= read -r line; do
# Leerzeilen & Kommentare überspringen
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
# FIX 1: sauberes Token-Parsing statt Regex
read -r keyword iface proto rest <<<"$line"
[[ "$keyword" == "iface" ]] || continue
# Vergleich bleibt wie bei dir
if [[ ! " ${wanted_interfaces[@]} " =~ " $iface " ]]; then
echo "❌ Unerwünschtes Interface gefunden: $iface"
unwanted_found=1
fi
done < "$file"
return $unwanted_found
}
# Funktion zum Sicherstellen, dass ein Interface existiert
ensure_interface() {
local file="$1"
local iface="$2"
local proto="$3"
[[ -f "$file" ]] || touch "$file"
# Prüfen, ob iface-Zeile existiert
if grep -Eq "^[[:space:]]*iface[[:space:]]+$iface([[:space:]]|\$)" "$file"; then
echo "✔ iface $iface $proto bereits vorhanden"
else
echo " iface $iface $proto fehlt anhängen"
{
echo
echo "iface $iface $proto"
} >> "$file"
fi
# Sicherstellen, dass vor der iface-Zeile ein Zeilenumbruch ist
sed -i "/iface[[:space:]]+$iface[[:space:]]+$proto/i \\ " "$file"
# Prüfen, ob auto <iface> existiert
if grep -Eq "^[[:space:]]*auto[[:space:]]+$iface([[:space:]]|\$)" "$file"; then
echo "✔ auto $iface vorhanden"
else
echo "↺ auto $iface fehlt vor iface einfügen"
# auto direkt vor iface einfügen
sed -i "/^[[:space:]]*iface[[:space:]]\+$iface[[:space:]]\+$proto/i auto $iface" "$file"
fi
}
# Füge die Quelle für interfaces.d hinzu
add_source() {
local file="$1"
if ! grep -Eq "^[[:space:]]*source[[:space:]]+/etc/network/interfaces.d/\*" "$file"; then
echo " source /etc/network/interfaces.d/* fehlt hinzufügen"
echo "source /etc/network/interfaces.d/*" >> "$file"
else
echo "✔ source /etc/network/interfaces.d/* vorhanden"
fi
}
# Füge immer das Loopback Interface hinzu
add_loopback_interface() {
local file="$1"
if ! grep -Eq "^[[:space:]]*iface[[:space:]]+lo[[:space:]]\+inet[[:space:]]+loopback" "$file"; then
echo " iface lo inet loopback fehlt hinzufügen"
{
echo "auto lo"
echo "iface lo inet loopback"
} >> "$file"
else
echo "✔ iface lo inet loopback vorhanden"
fi
}
# Temporäres Deaktivieren von 'set -e'
set +e
# Prüfe auf nicht gewollte Interfaces und kommentiere die Datei aus, wenn notwendig
check_for_unwanted_interfaces "$iface_file"
if [ $? -eq 1 ]; then
# Wenn ein unerwünschtes Interface gefunden wurde, kommentiere die gesamte Datei aus und füge loopback + interfaces.d zu
comment_out_all "$iface_file"
add_source "$iface_file"
add_loopback_interface "$iface_file"
fi
# Wiederherstellen von 'set -e'
set -e
# Setze die Konfiguration für die benötigten Interfaces
for iface in "${interfaces[@]}"; do
ensure_interface "$iface_file" "$iface" "inet dhcp"
done
echo "🔄 Neustart von networking.service..."
systemctl restart networking.service
echo
echo "📋 Status networking.service:"
systemctl status networking.service --no-pager -l
echo
echo "🌐 networkctl list:"
networkctl list
echo
echo "🔍 Detailstatus der Interfaces:"
networkctl status "${interfaces[@]}" || true