-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathincrease_swap.sh
More file actions
executable file
·34 lines (27 loc) · 968 Bytes
/
increase_swap.sh
File metadata and controls
executable file
·34 lines (27 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env bash
# enlarge-swap.sh — grow the Raspberry Pi swapfile (dphys-swapfile)
set -euo pipefail
SIZE_MB="${1:-2048}" # default 2 GiB; run: sudo ./enlarge-swap.sh 3072 for 3 GiB
need_root() { [ "$(id -u)" -eq 0 ] && return; echo "Run as root: sudo $0 $*"; exit 1; }
need_root "$@"
echo "▶︎ ensuring dphys-swapfile is present"
if ! command -v dphys-swapfile >/dev/null 2>&1; then
apt-get update -qq
apt-get install -y dphys-swapfile
fi
echo "▶︎ turning off current swap"
systemctl stop dphys-swapfile 2>/dev/null || true
dphys-swapfile swapoff 2>/dev/null || true
echo "▶︎ setting CONF_SWAPSIZE=$SIZE_MB"
conf=/etc/dphys-swapfile
if grep -q '^CONF_SWAPSIZE=' "$conf"; then
sed -i "s/^CONF_SWAPSIZE=.*/CONF_SWAPSIZE=$SIZE_MB/" "$conf"
else
echo "CONF_SWAPSIZE=$SIZE_MB" >> "$conf"
fi
echo "▶︎ regenerating swapfile"
dphys-swapfile setup
systemctl start dphys-swapfile
echo "▶︎ done:"
free -h
swapon --show