Swap: skip when already active, btrfs-aware creation (fallocate fails on btrfs)

This commit is contained in:
Gavin Nugent 2026-06-06 11:02:55 +01:00
parent 18e97e75ff
commit 67f10da503

View file

@ -335,15 +335,23 @@ fi
step "Swap file" step "Swap file"
if [ ! -f /swapfile ]; then if swapon --show --noheadings 2>/dev/null | grep -q .; then
fallocate -l 2G /swapfile warn "Swap already active — skipping:"
chmod 600 /swapfile swapon --show
mkswap /swapfile elif [ -f /swapfile ]; then
warn "Swap file already exists — skipping"
else
# fallocate swapfiles fail on btrfs (needs NOCOW) — use the proper tool
if [ "$(findmnt -n -o FSTYPE /)" = "btrfs" ]; then
btrfs filesystem mkswapfile --size 2g /swapfile
else
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=none
chmod 600 /swapfile
mkswap /swapfile
fi
swapon /swapfile swapon /swapfile
grep -q '/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab grep -q '/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
log "2GB swap created and enabled" log "2GB swap created and enabled"
else
warn "Swap file already exists — skipping"
fi fi
# ============================================================================= # =============================================================================