192 lines
6.0 KiB
Bash
192 lines
6.0 KiB
Bash
#!/bin/bash
|
|
# Swap-Vergrößerungsskript für Debian (mit intelligentem Swap-Management)
|
|
# Legt /swapfile an oder vergrößert es auf RAM + 2GB falls nötig
|
|
set -e # Bei Fehler abbrechen
|
|
|
|
# Prüfe auf Root-Rechte
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Fehler: Dieses Skript muss als root ausgeführt werden"
|
|
exit 1
|
|
fi
|
|
|
|
# Temporäre Swap-Datei (nur bei Vergrößerung)
|
|
TEMP_SWAP="/swapfile.tmp"
|
|
TEMP_SWAP_SIZE_MB=10240 # 10 GB
|
|
|
|
echo "=== Swap-Anpassung wird gestartet ==="
|
|
|
|
# RAM-Größe in MB ermitteln
|
|
RAM_MB=$(free -m | awk '/^Mem:/{print $2}')
|
|
echo "Erkannter RAM: ${RAM_MB} MB"
|
|
|
|
# Zielgröße berechnen (RAM + 2GB)
|
|
TARGET_MB=$((RAM_MB + 2048))
|
|
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
|
|
if [ -f /swapfile ]; then
|
|
CURRENT_SWAP_MB=$(stat -c%s /swapfile | awk '{print int($1/1048576)}')
|
|
echo ""
|
|
echo "Aktuelle /swapfile-Größe: ${CURRENT_SWAP_MB} MB"
|
|
else
|
|
echo ""
|
|
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
|
|
|
|
# Vergleichen und ggf. vergrößern
|
|
if [ $CURRENT_SWAP_MB -lt $TARGET_MB ]; then
|
|
echo ""
|
|
echo "Swap wird vergrößert von ${CURRENT_SWAP_MB} MB auf ${TARGET_MB} MB..."
|
|
|
|
# Freien Speicherplatz prüfen
|
|
AVAILABLE_MB=$(df -BM /swapfile 2>/dev/null | awk 'NR==2 {print $4}' | sed 's/M//' || df -BM / | awk 'NR==2 {print $4}' | sed 's/M//')
|
|
REQUIRED_MB=$((TARGET_MB + TEMP_SWAP_SIZE_MB))
|
|
echo "Verfügbarer Speicher: ${AVAILABLE_MB} MB"
|
|
echo "Benötigt (temporär): ${REQUIRED_MB} MB"
|
|
|
|
if [ "$AVAILABLE_MB" -lt "$REQUIRED_MB" ]; then
|
|
echo "FEHLER: Nicht genug freier Speicherplatz!"
|
|
echo "Benötigt: ${REQUIRED_MB} MB, Verfügbar: ${AVAILABLE_MB} MB"
|
|
exit 1
|
|
fi
|
|
|
|
# Schritt 1: Temporären Swap erstellen
|
|
echo ""
|
|
echo "[1/6] Erstelle temporären Swap (${TEMP_SWAP_SIZE_MB} MB)..."
|
|
dd if=/dev/zero of=$TEMP_SWAP bs=1M count=$TEMP_SWAP_SIZE_MB status=progress
|
|
chmod 600 $TEMP_SWAP
|
|
mkswap $TEMP_SWAP
|
|
|
|
# Schritt 2: Temporären Swap aktivieren
|
|
echo ""
|
|
echo "[2/6] Aktiviere temporären Swap..."
|
|
swapon $TEMP_SWAP
|
|
echo "Aktueller Swap-Status:"
|
|
swapon --show
|
|
|
|
# Schritt 3: Alten /swapfile deaktivieren
|
|
echo ""
|
|
echo "[3/6] Deaktiviere alten /swapfile..."
|
|
if swapon --show | grep -q "^/swapfile"; then
|
|
swapoff /swapfile
|
|
echo "/swapfile erfolgreich deaktiviert"
|
|
else
|
|
echo "/swapfile war nicht aktiv"
|
|
fi
|
|
|
|
# Alte Datei löschen
|
|
rm -f /swapfile
|
|
echo "Alte /swapfile gelöscht"
|
|
|
|
# Schritt 4: Neue /swapfile mit Zielgröße erstellen
|
|
echo ""
|
|
echo "[4/6] Erstelle neue /swapfile mit ${TARGET_MB} MB..."
|
|
dd if=/dev/zero of=/swapfile bs=1M count=$TARGET_MB status=progress
|
|
chmod 600 /swapfile
|
|
mkswap /swapfile
|
|
|
|
# Schritt 5: Neue /swapfile aktivieren
|
|
echo ""
|
|
echo "[5/6] Aktiviere neue /swapfile..."
|
|
swapon /swapfile
|
|
echo "Swap-Status mit beiden Dateien:"
|
|
swapon --show
|
|
|
|
# Schritt 6: Temporären Swap deaktivieren und löschen
|
|
echo ""
|
|
echo "[6/6] Entferne temporären Swap..."
|
|
swapoff $TEMP_SWAP
|
|
rm -f $TEMP_SWAP
|
|
echo "Temporärer Swap entfernt"
|
|
|
|
# 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 vergrößert und aktiviert! ==="
|
|
echo ""
|
|
echo "Finaler Swap-Status:"
|
|
swapon --show
|
|
echo ""
|
|
free -h
|
|
else
|
|
echo ""
|
|
echo "=== Swap ist bereits groß genug ==="
|
|
echo "Aktuelle Größe (${CURRENT_SWAP_MB} MB) >= Zielgröße (${TARGET_MB} MB)"
|
|
echo "Keine Aktion erforderlich."
|
|
echo ""
|
|
swapon --show
|
|
fi
|
|
fi
|
|
|
|
echo ""
|
|
echo "Fertig!" |