Swap — Ajouter ou redimensionner un swapfile

Crée ou redimensionne un swapfile Linux de la taille voulue et le rend persistant dans /etc/fstab. Variable SWAP_SIZE pour choisir la taille (défaut : 2G).

sysadminswapperformancelinux
$ curl scripts.ysavary.fr/swap-add | bash

Swap — Ajouter ou redimensionner un swapfile

Crée un swapfile à la taille souhaitée et le monte immédiatement. Persistant au reboot via /etc/fstab.

Variable configurable :

Recommandations :

bash
#!/bin/bash
set -euo pipefail

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info()  { echo -e "${GREEN}[INFO]${NC}  $*"; }
warn()  { echo -e "${YELLOW}[WARN]${NC}  $*"; }
error() { echo -e "${RED}[ERR]${NC}   $*"; exit 1; }

[[ $EUID -ne 0 ]] && error "Ce script doit être exécuté en root."

SWAP_SIZE="${SWAP_SIZE:-2G}"
SWAP_FILE="/swapfile"

# ── Infos actuelles ───────────────────────────────────────────────────────────
RAM_MB=$(awk '/MemTotal/ {print int($2/1024)}' /proc/meminfo)
info "RAM détectée : ${RAM_MB} Mo"

CURRENT_SWAP=$(swapon --show 2>/dev/null || true)
if [[ -n "$CURRENT_SWAP" ]]; then
    warn "Swap actuellement actif :"
    swapon --show
    echo ""
fi

# ── Désactivation de l'ancien swapfile si existant ───────────────────────────
if [[ -f "$SWAP_FILE" ]]; then
    warn "Swapfile existant détecté, remplacement par $SWAP_SIZE..."
    swapoff "$SWAP_FILE" 2>/dev/null || true
    rm -f "$SWAP_FILE"
fi

# ── Création du swapfile ──────────────────────────────────────────────────────
info "=== Création du swapfile ($SWAP_SIZE) ==="

# Convertit la taille en Mo pour fallocate
case "${SWAP_SIZE^^}" in
    *G) SIZE_MB=$(( ${SWAP_SIZE//[Gg]/} * 1024 )) ;;
    *M) SIZE_MB=${SWAP_SIZE//[Mm]/} ;;
    *)  error "Format invalide. Utilise 2G, 512M, etc." ;;
esac

[[ $SIZE_MB -lt 256 ]] && error "Taille minimale : 256M"
[[ $SIZE_MB -gt 32768 ]] && warn "Swapfile > 32G, es-tu sûr ?"

if command -v fallocate &>/dev/null; then
    fallocate -l "${SWAP_SIZE}" "$SWAP_FILE"
else
    dd if=/dev/zero of="$SWAP_FILE" bs=1M count="$SIZE_MB" status=progress
fi

chmod 600 "$SWAP_FILE"
mkswap "$SWAP_FILE"
swapon "$SWAP_FILE"

# ── Persistance fstab ─────────────────────────────────────────────────────────
info "=== Persistance dans /etc/fstab ==="
if grep -q "^$SWAP_FILE" /etc/fstab 2>/dev/null; then
    sed -i "s|^$SWAP_FILE.*|$SWAP_FILE none swap sw 0 0|" /etc/fstab
else
    echo "$SWAP_FILE none swap sw 0 0" >> /etc/fstab
fi

# ── Optimisation swappiness ───────────────────────────────────────────────────
CURRENT_SWAPPINESS=$(cat /proc/sys/vm/swappiness)
if [[ $CURRENT_SWAPPINESS -gt 30 ]]; then
    warn "swappiness actuel : $CURRENT_SWAPPINESS → passage à 10 (recommandé serveur)"
    sysctl vm.swappiness=10 > /dev/null
    grep -q 'vm.swappiness' /etc/sysctl.conf \
        && sed -i 's/vm.swappiness.*/vm.swappiness=10/' /etc/sysctl.conf \
        || echo 'vm.swappiness=10' >> /etc/sysctl.conf
fi

echo ""
info "=== Swap configuré ==="
echo ""
swapon --show
echo ""
free -h
echo ""