resizeswap.py aktualisiert

This commit is contained in:
2025-12-01 19:31:35 +01:00
parent a3ecdc39f6
commit efe484a9b0

View File

@@ -1,17 +1,15 @@
#!/bin/bash #!/bin/bash
# Swap-Vergrößerungsskript für Debian (mit intelligentem Swap-Management)
# Swap-Vergrößerungsskript für Debian (mit temporärem Swap) # Legt /swapfile an oder vergrößert es auf RAM + 2GB falls nötig
# Vergrößert /swapfile auf RAM + 2GB falls nötig
set -e # Bei Fehler abbrechen set -e # Bei Fehler abbrechen
# Prüfe auf Root-Rechte # Prüfe auf Root-Rechte
if [ "$EUID" -ne 0 ]; then if [ "$EUID" -ne 0 ]; then
echo "Fehler: Dieses Skript muss als root ausgeführ werden" echo "Fehler: Dieses Skript muss als root ausgeführt werden"
exit 1 exit 1
fi fi
# Temporäre Swap-Datei # Temporäre Swap-Datei (nur bei Vergrößerung)
TEMP_SWAP="/swapfile.tmp" TEMP_SWAP="/swapfile.tmp"
TEMP_SWAP_SIZE_MB=10240 # 10 GB TEMP_SWAP_SIZE_MB=10240 # 10 GB
@@ -25,13 +23,79 @@ echo "Erkannter RAM: ${RAM_MB} MB"
TARGET_MB=$((RAM_MB + 2048)) TARGET_MB=$((RAM_MB + 2048))
echo "Zielgröße für Swap: ${TARGET_MB} MB (RAM + 2048 MB)" echo "Zielgröße für Swap: ${TARGET_MB} MB (RAM + 2048 MB)"
# Prüfen ob überhaupt Swap aktiv ist
SWAP_ACTIVE=$(swapon --show | wc -l)
if [ "$SWAP_ACTIVE" -eq 0 ]; then
echo ""
echo "=== KEIN SWAP AKTIV - Erstelle neuen Swap ==="
# Freien Speicherplatz prüfen
AVAILABLE_MB=$(df -BM / | awk 'NR==2 {print $4}' | sed 's/M//')
echo "Verfügbarer Speicher: ${AVAILABLE_MB} MB"
echo "Benötigt: ${TARGET_MB} MB"
if [ "$AVAILABLE_MB" -lt "$TARGET_MB" ]; then
echo "FEHLER: Nicht genug freier Speicherplatz!"
echo "Benötigt: ${TARGET_MB} MB, Verfügbar: ${AVAILABLE_MB} MB"
exit 1
fi
# Prüfen ob /swapfile bereits existiert (aber nicht aktiv)
if [ -f /swapfile ]; then
echo "Warnung: /swapfile existiert aber ist nicht aktiv - wird gelöscht"
rm -f /swapfile
fi
# Schritt 1: /swapfile erstellen
echo ""
echo "[1/3] Erstelle /swapfile mit ${TARGET_MB} MB..."
dd if=/dev/zero of=/swapfile bs=1M count=$TARGET_MB status=progress
chmod 600 /swapfile
# Schritt 2: Swap-Format erstellen
echo ""
echo "[2/3] Formatiere /swapfile als Swap..."
mkswap /swapfile
# Schritt 3: Swap aktivieren
echo ""
echo "[3/3] Aktiviere /swapfile..."
swapon /swapfile
# Prüfen ob in /etc/fstab eingetragen
if ! grep -q "^/swapfile" /etc/fstab; then
echo ""
echo "Füge Eintrag zu /etc/fstab hinzu..."
echo "/swapfile none swap sw 0 0" >> /etc/fstab
fi
echo ""
echo "=== Swap erfolgreich erstellt und aktiviert! ==="
echo ""
echo "Swap-Status:"
swapon --show
echo ""
free -h
else
echo ""
echo "=== SWAP IST AKTIV - Prüfe Größe ==="
swapon --show
# Aktuelle Swap-Größe prüfen # Aktuelle Swap-Größe prüfen
if [ -f /swapfile ]; then if [ -f /swapfile ]; then
CURRENT_SWAP_MB=$(stat -c%s /swapfile | awk '{print int($1/1048576)}') CURRENT_SWAP_MB=$(stat -c%s /swapfile | awk '{print int($1/1048576)}')
echo "Aktuelle Swap-Größe: ${CURRENT_SWAP_MB} MB" echo ""
echo "Aktuelle /swapfile-Größe: ${CURRENT_SWAP_MB} MB"
else else
echo "Warnung: /swapfile existiert nicht, wird neu erstellt" echo ""
CURRENT_SWAP_MB=0 echo "Hinweis: /swapfile existiert nicht, aber anderer Swap ist aktiv"
echo "Dieses Skript verwaltet nur /swapfile"
echo ""
echo "Aktuelle Swap-Konfiguration:"
swapon --show
exit 0
fi fi
# Vergleichen und ggf. vergrößern # Vergleichen und ggf. vergrößern
@@ -75,11 +139,9 @@ if [ $CURRENT_SWAP_MB -lt $TARGET_MB ]; then
echo "/swapfile war nicht aktiv" echo "/swapfile war nicht aktiv"
fi fi
# Alte Datei löschen wenn vorhanden # Alte Datei löschen
if [ -f /swapfile ]; then
rm -f /swapfile rm -f /swapfile
echo "Alte /swapfile gelöscht" echo "Alte /swapfile gelöscht"
fi
# Schritt 4: Neue /swapfile mit Zielgröße erstellen # Schritt 4: Neue /swapfile mit Zielgröße erstellen
echo "" echo ""
@@ -124,6 +186,7 @@ else
echo "" echo ""
swapon --show swapon --show
fi fi
fi
echo "" echo ""
echo "Fertig!" echo "Fertig!"