mirror of
https://github.com/hanzoskill/pi-admin.git
synced 2026-10-30 21:05:40 +00:00
199 lines
5.2 KiB
Bash
199 lines
5.2 KiB
Bash
#!/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)" |