-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtune-pi.sh
More file actions
executable file
·163 lines (142 loc) · 7.55 KB
/
tune-pi.sh
File metadata and controls
executable file
·163 lines (142 loc) · 7.55 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
# =============================================================================
# tune-pi.sh — OS-level tuning for Raspberry Pi slideshow kiosk
#
# Usage: sudo ./tune-pi.sh
# Safe to re-run. Reboot after to apply boot config changes.
# =============================================================================
set -euo pipefail
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info() { echo -e "${GREEN}[tune]${NC} $*"; }
warn() { echo -e "${YELLOW}[warn]${NC} $*"; }
[[ "$(id -u)" -eq 0 ]] || { echo "Run with sudo: sudo ./tune-pi.sh"; exit 1; }
# ── Detect boot config path ───────────────────────────────────────────────────
if [[ -f /boot/firmware/config.txt ]]; then
BOOT_CFG=/boot/firmware/config.txt
BOOT_CMD=/boot/firmware/cmdline.txt
else
BOOT_CFG=/boot/config.txt
BOOT_CMD=/boot/cmdline.txt
fi
info "Boot config: ${BOOT_CFG}"
set_boot() {
local key="$1" val="$2"
if grep -qE "^#?${key}=" "${BOOT_CFG}"; then
sed -i "s|^#\?${key}=.*|${key}=${val}|" "${BOOT_CFG}"
else
echo "${key}=${val}" >> "${BOOT_CFG}"
fi
}
# ── 1. GPU memory ─────────────────────────────────────────────────────────────
# More VRAM for smoother canvas/CSS rendering. Default is 76MB — too low.
info "Setting GPU memory to 256MB..."
set_boot gpu_mem 256
# ── 2. VC4 GPU driver ──────────────────────────────────────────────────────
# Use FKMS (not full KMS) for Pi 3 compatibility. Remove vc4-kms-v3d if present.
info "Enabling VC4 GPU driver (FKMS)..."
if grep -q "^dtoverlay=vc4-kms-v3d" "${BOOT_CFG}"; then
sed -i 's/^dtoverlay=vc4-kms-v3d/# dtoverlay=vc4-kms-v3d # disabled for Pi 3/' "${BOOT_CFG}"
info " Disabled vc4-kms-v3d (conflicts with fkms)"
fi
# Also remove disable_fw_kms_setup (only needed for full KMS)
if grep -q "^disable_fw_kms_setup=" "${BOOT_CFG}"; then
sed -i 's/^disable_fw_kms_setup=/#disable_fw_kms_setup=/' "${BOOT_CFG}"
info " Disabled disable_fw_kms_setup"
fi
if ! grep -q "^dtoverlay=vc4-fkms-v3d" "${BOOT_CFG}"; then
echo "dtoverlay=vc4-fkms-v3d" >> "${BOOT_CFG}"
info " Added vc4-fkms-v3d overlay"
else
info " vc4-fkms-v3d overlay already present"
fi
# ── 3. HDMI & display ─────────────────────────────────────────────────────────
info "Configuring HDMI..."
set_boot disable_overscan 1 # Remove black border
set_boot hdmi_force_hotplug 1 # Always output HDMI even without monitor at boot
# Disable console blanking (screen would go black after 10 min otherwise)
if ! grep -q "consoleblank=0" "${BOOT_CMD}"; then
sed -i 's/$/ consoleblank=0/' "${BOOT_CMD}"
info "Disabled console blanking"
fi
# ── 4. Disable unused services ────────────────────────────────────────────────
info "Disabling unused services..."
DISABLE_SERVICES=(
bluetooth
hciuart # Bluetooth UART
triggerhappy # Hot-key daemon
cups # Printing
cups-browsed
ModemManager # Mobile modems
)
for svc in "${DISABLE_SERVICES[@]}"; do
if systemctl list-unit-files "${svc}.service" &>/dev/null 2>&1; then
systemctl disable --now "${svc}" 2>/dev/null && info " Disabled: ${svc}" || true
fi
done
# ── 5. CPU performance governor ───────────────────────────────────────────────
info "Locking CPU governor to 'performance'..."
cat > /etc/systemd/system/cpu-performance.service <<'EOF'
[Unit]
Description=Set CPU governor to performance
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'for f in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo performance > "$f"; done'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now cpu-performance
info " CPU governor locked to performance"
# ── 6. Reduce swappiness ──────────────────────────────────────────────────────
# Don't disable swap entirely, but tell the kernel to avoid it aggressively.
# Swapping to SD card mid-animation causes visible stutters.
info "Setting swappiness to 10..."
echo "vm.swappiness=10" > /etc/sysctl.d/99-kiosk.conf
sysctl -w vm.swappiness=10 > /dev/null
# ── 7. zram compressed swap ─────────────────────────────────────────────────
# 1GB RAM is tight. zram gives ~50% more effective memory without SD card thrash.
info "Setting up zram swap..."
if ! command -v zramctl &>/dev/null; then
apt-get install -y zram-tools 2>/dev/null || true
fi
cat > /etc/systemd/system/zram-swap.service <<'ZEOF'
[Unit]
Description=Configure zram swap
After=local-fs.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c '\
modprobe zram && \
zramctl /dev/zram0 --algorithm lz4 --size $(( $(grep MemTotal /proc/meminfo | awk "{print \\$2}") / 2 ))K && \
mkswap /dev/zram0 && \
swapon -p 100 /dev/zram0'
ExecStop=/bin/bash -c 'swapoff /dev/zram0 && zramctl --reset /dev/zram0'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
ZEOF
systemctl daemon-reload
systemctl enable zram-swap
info " zram swap service installed"
# Disable SD card swap — zram replaces it
if systemctl is-enabled dphys-swapfile 2>/dev/null; then
systemctl disable --now dphys-swapfile 2>/dev/null || true
info " Disabled dphys-swapfile (SD card swap)"
fi
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Done! Reboot to apply boot config changes. ║${NC}"
echo -e "${GREEN}╠══════════════════════════════════════════════════════╣${NC}"
echo -e "${GREEN}║ ✓ GPU memory → 256 MB ║${NC}"
echo -e "${GREEN}║ ✓ VC4 GPU driver enabled ║${NC}"
echo -e "${GREEN}║ ✓ HDMI force hotplug + no overscan ║${NC}"
echo -e "${GREEN}║ ✓ Console blanking disabled ║${NC}"
echo -e "${GREEN}║ ✓ 7 background services disabled ║${NC}"
echo -e "${GREEN}║ ✓ CPU governor → performance ║${NC}"
echo -e "${GREEN}║ ✓ Swappiness → 10 (avoids SD card thrash) ║${NC}"
echo -e "${GREEN}║ ✓ zram swap (compressed RAM) ║${NC}"
echo -e "${GREEN}║ ✓ SD card swap disabled ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════╝${NC}"