resizeswap.py hinzugefügt
This commit is contained in:
129
resizeswap.py
Normal file
129
resizeswap.py
Normal file
@@ -0,0 +1,129 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Swap-Vergrößerungsskript für Debian (mit temporärem Swap)
|
||||
# Vergrößert /swapfile 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ühr werden"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Temporäre Swap-Datei
|
||||
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)"
|
||||
|
||||
# Aktuelle Swap-Größe prüfen
|
||||
if [ -f /swapfile ]; then
|
||||
CURRENT_SWAP_MB=$(stat -c%s /swapfile | awk '{print int($1/1048576)}')
|
||||
echo "Aktuelle Swap-Größe: ${CURRENT_SWAP_MB} MB"
|
||||
else
|
||||
echo "Warnung: /swapfile existiert nicht, wird neu erstellt"
|
||||
CURRENT_SWAP_MB=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 wenn vorhanden
|
||||
if [ -f /swapfile ]; then
|
||||
rm -f /swapfile
|
||||
echo "Alte /swapfile gelöscht"
|
||||
fi
|
||||
|
||||
# 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
|
||||
|
||||
echo ""
|
||||
echo "Fertig!"
|
||||
Reference in New Issue
Block a user