resizeswap.py aktualisiert
This commit is contained in:
103
resizeswap.py
103
resizeswap.py
@@ -1,17 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Swap-Vergrößerungsskript für Debian (mit temporärem Swap)
|
||||
# Vergrößert /swapfile auf RAM + 2GB falls nötig
|
||||
|
||||
# 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ühr werden"
|
||||
echo "Fehler: Dieses Skript muss als root ausgeführt werden"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Temporäre Swap-Datei
|
||||
# Temporäre Swap-Datei (nur bei Vergrößerung)
|
||||
TEMP_SWAP="/swapfile.tmp"
|
||||
TEMP_SWAP_SIZE_MB=10240 # 10 GB
|
||||
|
||||
@@ -25,17 +23,83 @@ echo "Erkannter RAM: ${RAM_MB} MB"
|
||||
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
|
||||
# Prüfen ob überhaupt Swap aktiv ist
|
||||
SWAP_ACTIVE=$(swapon --show | wc -l)
|
||||
|
||||
# Vergleichen und ggf. vergrößern
|
||||
if [ $CURRENT_SWAP_MB -lt $TARGET_MB ]; then
|
||||
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..."
|
||||
|
||||
@@ -75,11 +139,9 @@ if [ $CURRENT_SWAP_MB -lt $TARGET_MB ]; then
|
||||
echo "/swapfile war nicht aktiv"
|
||||
fi
|
||||
|
||||
# Alte Datei löschen wenn vorhanden
|
||||
if [ -f /swapfile ]; then
|
||||
# Alte Datei löschen
|
||||
rm -f /swapfile
|
||||
echo "Alte /swapfile gelöscht"
|
||||
fi
|
||||
|
||||
# Schritt 4: Neue /swapfile mit Zielgröße erstellen
|
||||
echo ""
|
||||
@@ -116,13 +178,14 @@ if [ $CURRENT_SWAP_MB -lt $TARGET_MB ]; then
|
||||
swapon --show
|
||||
echo ""
|
||||
free -h
|
||||
else
|
||||
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 ""
|
||||
|
||||
Reference in New Issue
Block a user