-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathoptimize.sh
More file actions
199 lines (176 loc) · 5.22 KB
/
Copy pathoptimize.sh
File metadata and controls
199 lines (176 loc) · 5.22 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# Pi Optimizations - Safe reversible optimizations
# Usage: ./optimize.sh [--dry-run|--undo]
DRY_RUN=false
UNDO=false
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=true
shift
;;
--undo)
UNDO=true
shift
;;
esac
done
if [ "$DRY_RUN" = true ]; then
echo "🔍 DRY RUN MODE - No changes will be made"
echo "=========================================="
echo ""
elif [ "$UNDO" = true ]; then
echo "↩️ UNDO MODE - Reverting optimizations"
echo "========================================"
echo ""
fi
echo "⚡ Pi Optimizations"
echo "==================="
echo ""
# Check for sudo
if [ "$DRY_RUN" = false ] && [ "$UNDO" = false ]; then
if [ "$EUID" -ne 0 ]; then
echo "⚠️ This script requires sudo privileges"
echo ""
read -p "Continue with sudo? [y/N] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
SUDO="sudo"
else
SUDO=""
fi
else
SUDO="sudo"
fi
# Show current state
echo "Current State:"
echo "--------------"
echo " Bluetooth: $(systemctl is-enabled bluetooth.service)"
echo " ModemManager: $(systemctl is-enabled ModemManager.service)"
echo " Avahi: $(systemctl is-enabled avahi-daemon.service)"
echo " Swappiness: $(cat /proc/sys/vm/swappiness)"
echo ""
# RAM savings estimate
RAM_SAVED_MB=0
if [ "$(systemctl is-enabled bluetooth.service)" = "enabled" ]; then
RAM_SAVED_MB=$((RAM_SAVED_MB + 50))
fi
if [ "$(systemctl is-enabled ModemManager.service)" = "enabled" ]; then
RAM_SAVED_MB=$((RAM_SAVED_MB + 30))
fi
if [ "$(systemctl is-enabled avahi-daemon.service)" = "enabled" ]; then
RAM_SAVED_MB=$((RAM_SAVED_MB + 20))
fi
echo "Potential RAM savings: ~${RAM_SAVED_MB}MB"
echo ""
# Exit early in dry-run mode
if [ "$DRY_RUN" = true ]; then
echo "🔍 Dry run complete. These optimizations would be applied:"
echo ""
echo "Changes:"
if [ "$(systemctl is-enabled bluetooth.service)" = "enabled" ]; then
echo " ❌ Disable Bluetooth service"
fi
if [ "$(systemctl is-enabled ModemManager.service)" = "enabled" ]; then
echo " ❌ Disable ModemManager"
fi
if [ "$(systemctl is-enabled avahi-daemon.service)" = "enabled" ]; then
echo " ❌ Disable Avahi"
fi
echo " ⚙️ Set swappiness to 10 (current: $(cat /proc/sys/vm/swappiness))"
echo ""
echo "Run without --dry-run to apply."
exit 0
fi
# Undo mode
if [ "$UNDO" = true ]; then
echo "Reverting optimizations..."
echo ""
if [ "$(systemctl is-enabled bluetooth.service)" = "disabled" ]; then
echo "Enabling Bluetooth..."
$SUDO systemctl enable bluetooth.service
echo "✅ Bluetooth enabled"
fi
if [ "$(systemctl is-enabled ModemManager.service)" = "disabled" ]; then
echo "Enabling ModemManager..."
$SUDO systemctl enable ModemManager.service
echo "✅ ModemManager enabled"
fi
if [ "$(systemctl is-enabled avahi-daemon.service)" = "disabled" ]; then
echo "Enabling Avahi..."
$SUDO systemctl enable avahi-daemon.service
echo "✅ Avahi enabled"
fi
echo "Restoring swappiness to 60..."
$SUDO sysctl vm.swappiness=60
echo "vm.swappiness=60" | $SUDO tee /etc/sysctl.d/99-swappiness.conf > /dev/null
echo "✅ Swappiness restored to 60"
echo ""
echo "↩️ Undo complete! Restart required for full effect."
exit 0
fi
# Apply optimizations
echo "Applying optimizations..."
echo ""
# 1. Disable Bluetooth
if [ "$(systemctl is-enabled bluetooth.service)" = "enabled" ]; then
echo "1. Disabling Bluetooth..."
$SUDO systemctl disable bluetooth.service
echo " ✅ Bluetooth disabled (~50MB RAM saved)"
else
echo "1. Bluetooth already disabled - skipping"
fi
# 2. Disable ModemManager
if [ "$(systemctl is-enabled ModemManager.service)" = "enabled" ]; then
echo ""
echo "2. Disabling ModemManager..."
$SUDO systemctl disable ModemManager.service
echo " ✅ ModemManager disabled (~30MB RAM saved)"
else
echo ""
echo "2. ModemManager already disabled - skipping"
fi
# 3. Disable Avahi
if [ "$(systemctl is-enabled avahi-daemon.service)" = "enabled" ]; then
echo ""
echo "3. Disabling Avahi..."
$SUDO systemctl disable avahi-daemon.service
echo " ✅ Avahi disabled (~20MB RAM saved)"
else
echo ""
echo "3. Avahi already disabled - skipping"
fi
# 4. Set swappiness to 10
echo ""
echo "4. Setting swappiness to 10..."
CURRENT_SWAPPINESS=$(cat /proc/sys/vm/swappiness)
if [ "$CURRENT_SWAPPINESS" -ne 10 ]; then
$SUDO sysctl vm.swappiness=10
echo "vm.swappiness=10" | $SUDO tee /etc/sysctl.d/99-swappiness.conf > /dev/null
echo " ✅ Swappiness set to 10 (was $CURRENT_SWAPPINESS)"
else
echo " Swappiness already 10 - skipping"
fi
# Summary
echo ""
echo "✅ Optimizations applied!"
echo ""
echo "Changes made:"
echo "-------------"
if [ "$(systemctl is-enabled bluetooth.service)" = "disabled" ]; then
echo " ❌ Bluetooth disabled"
fi
if [ "$(systemctl is-enabled ModemManager.service)" = "disabled" ]; then
echo " ❌ ModemManager disabled"
fi
if [ "$(systemctl is-enabled avahi-daemon.service)" = "disabled" ]; then
echo " ❌ Avahi disabled"
fi
echo " ⚙️ Swappiness set to 10"
echo ""
echo "RAM saved: ~${RAM_SAVED_MB}MB"
echo ""
echo "📝 To undo these changes, run: ./optimize.sh --undo"
echo "🔄 Restart recommended for full effect (run: ./skill.sh reboot)"