Files
sysmig/migrations/001-create-swap.sh
T
wan f69cb99b3f sysmig: migration runner + initial 4 migrations (swap, daemon trims)
- up/down/status interface, history in /var/lib/sysmig/applied
- 001 swap 2GB + swappiness=10, 002-004 trim unneeded daemons (~56MB)
- google-osconfig-agent intentionally kept
- all messages/comments in ASCII English (console font safety)
2026-08-31 15:49:01 +09:00

37 lines
1.2 KiB
Bash

#!/usr/bin/env bash
# 001-create-swap - Create 2GB swap + fstab entry + vm.swappiness=10
# up: enable swap (skipped if swap already active)
# down: disable swap and remove file/fstab/sysctl entries
set -euo pipefail
case "${1:-}" in
up)
if swapon --show --noheadings | grep -q .; then
echo " swap already active - skipping"
exit 0
fi
if [[ ! -f /swapfile ]]; then
fallocate -l 2G /swapfile || {
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=none
}
chmod 600 /swapfile
mkswap /swapfile >/dev/null
fi
swapon /swapfile
grep -q '^/swapfile' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab
if ! grep -rqs 'vm.swappiness' /etc/sysctl.d/ /etc/sysctl.conf; then
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swap.conf
sysctl -p /etc/sysctl.d/99-swap.conf >/dev/null
fi
echo " swap 2GB enabled + fstab entry + vm.swappiness=10"
;;
down)
swapoff /swapfile
rm -f /swapfile
sed -i '\|^/swapfile|d' /etc/fstab
grep -q '^vm.swappiness=10$' /etc/sysctl.d/99-swap.conf 2>/dev/null \
&& rm -f /etc/sysctl.d/99-swap.conf || true
echo " swap removed"
;;
esac