-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddSwapMemoryToDietPI.sh
More file actions
72 lines (63 loc) · 1.69 KB
/
AddSwapMemoryToDietPI.sh
File metadata and controls
72 lines (63 loc) · 1.69 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Smart Swap Setup for DietPi
# Allows creating 4GB or 8GB swap file safely
# --- CONFIG ---
SWAP_FILE="/swapfile"
SWAP_SIZE_GB=""
# --- FUNCTIONS ---
function check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)."
exit 1
fi
}
function ask_size() {
echo "Select swap size:"
echo "1) 4 GB"
echo "2) 8 GB"
read -rp "Enter choice [1 or 2]: " choice
case "$choice" in
1) SWAP_SIZE_GB=4 ;;
2) SWAP_SIZE_GB=8 ;;
*) echo "Invalid choice."; exit 1 ;;
esac
}
function create_swap() {
if [ -f "$SWAP_FILE" ]; then
echo "Swap file already exists at $SWAP_FILE. Removing..."
swapoff "$SWAP_FILE"
rm -f "$SWAP_FILE"
fi
echo "Creating $SWAP_SIZE_GB GB swap file..."
fallocate -l "${SWAP_SIZE_GB}G" "$SWAP_FILE" || \
dd if=/dev/zero of="$SWAP_FILE" bs=1M count=$((SWAP_SIZE_GB*1024))
chmod 600 "$SWAP_FILE"
mkswap "$SWAP_FILE"
}
function enable_swap() {
swapon "$SWAP_FILE"
echo "$SWAP_FILE none swap sw 0 0" >> /etc/fstab
}
function tune_swappiness() {
read -rp "Set swappiness? (default 10 recommended) [y/n]: " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
read -rp "Enter swappiness value (1-100) [default 10]: " sw
SW_VAL=${sw:-10}
sysctl vm.swappiness=$SW_VAL
grep -q "^vm.swappiness" /etc/sysctl.conf && \
sed -i "s/^vm.swappiness=.*/vm.swappiness=$SW_VAL/" /etc/sysctl.conf || \
echo "vm.swappiness=$SW_VAL" >> /etc/sysctl.conf
fi
}
function show_status() {
echo "Swap setup complete!"
swapon --show
free -h
}
# --- MAIN ---
check_root
ask_size
create_swap
enable_swap
tune_swappiness
show_status