- Complete installation and uninstallation scripts - Optimized TLP configuration for maximum battery life - PowerTOP analysis and auto-tune functionality - Real-time battery monitoring with detailed stats - ThinkPad-specific optimizations and battery thresholds - Comprehensive documentation and usage guides - Tested on ThinkPad T14 Gen 1 with 13% power reduction
198 lines
4.6 KiB
Bash
Executable File
198 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Battery Life Optimizer - TLP and PowerTOP Installation Script
|
|
# For Debian/Ubuntu-based Linux distributions
|
|
|
|
set -euo pipefail
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Logging function
|
|
log() {
|
|
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
|
|
}
|
|
|
|
success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Check if running as root
|
|
check_root() {
|
|
if [[ $EUID -eq 0 ]]; then
|
|
error "This script should not be run as root!"
|
|
error "It will use sudo when necessary."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Check if system is supported
|
|
check_system() {
|
|
if [[ ! -f /etc/debian_version ]] && [[ ! -f /etc/ubuntu_version ]]; then
|
|
warning "This script is optimized for Debian/Ubuntu systems."
|
|
read -p "Continue anyway? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
exit 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Update package lists
|
|
update_packages() {
|
|
log "Updating package lists..."
|
|
sudo apt update
|
|
success "Package lists updated"
|
|
}
|
|
|
|
# Install TLP
|
|
install_tlp() {
|
|
log "Installing TLP (Advanced Power Management)..."
|
|
|
|
# Check if TLP is already installed
|
|
if command -v tlp &> /dev/null; then
|
|
warning "TLP is already installed"
|
|
return 0
|
|
fi
|
|
|
|
# Install TLP and additional packages
|
|
sudo apt install -y tlp tlp-rdw
|
|
|
|
# For ThinkPads, install additional tools
|
|
if dmidecode -s system-product-name | grep -qi "thinkpad"; then
|
|
log "ThinkPad detected, installing additional tools..."
|
|
sudo apt install -y tp-smapi-dkms acpi-call-dkms
|
|
fi
|
|
|
|
# Enable and start TLP service
|
|
sudo systemctl enable tlp.service
|
|
sudo systemctl start tlp.service
|
|
|
|
success "TLP installed and configured"
|
|
}
|
|
|
|
# Install PowerTOP
|
|
install_powertop() {
|
|
log "Installing PowerTOP..."
|
|
|
|
# Check if PowerTOP is already installed
|
|
if command -v powertop &> /dev/null; then
|
|
warning "PowerTOP is already installed"
|
|
return 0
|
|
fi
|
|
|
|
sudo apt install -y powertop
|
|
|
|
success "PowerTOP installed"
|
|
}
|
|
|
|
# Setup PowerTOP service for automatic tuning
|
|
setup_powertop_service() {
|
|
log "Setting up PowerTOP auto-tune service..."
|
|
|
|
# Create systemd service file for PowerTOP
|
|
sudo tee /etc/systemd/system/powertop.service > /dev/null <<EOF
|
|
[Unit]
|
|
Description=PowerTOP auto tune
|
|
After=multi-user.target
|
|
|
|
[Service]
|
|
Type=oneshot
|
|
ExecStart=/usr/sbin/powertop --auto-tune
|
|
RemainAfterExit=true
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
# Enable the service
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable powertop.service
|
|
|
|
success "PowerTOP auto-tune service configured"
|
|
}
|
|
|
|
# Backup existing TLP configuration
|
|
backup_tlp_config() {
|
|
local tlp_config="/etc/tlp.conf"
|
|
local backup_file="/etc/tlp.conf.backup.$(date +%Y%m%d_%H%M%S)"
|
|
|
|
if [[ -f "$tlp_config" ]]; then
|
|
log "Backing up existing TLP configuration..."
|
|
sudo cp "$tlp_config" "$backup_file"
|
|
success "TLP configuration backed up to $backup_file"
|
|
fi
|
|
}
|
|
|
|
# Apply optimized TLP configuration
|
|
apply_tlp_config() {
|
|
log "Applying optimized TLP configuration..."
|
|
|
|
local config_file="../config/tlp.conf"
|
|
if [[ -f "$config_file" ]]; then
|
|
sudo cp "$config_file" /etc/tlp.conf
|
|
success "TLP configuration applied"
|
|
else
|
|
warning "Optimized TLP config file not found. Using default configuration."
|
|
fi
|
|
}
|
|
|
|
# Install additional power management tools
|
|
install_additional_tools() {
|
|
log "Installing additional power management tools..."
|
|
|
|
sudo apt install -y \
|
|
laptop-mode-tools \
|
|
thermald \
|
|
cpufrequtils \
|
|
smartmontools \
|
|
ethtool
|
|
|
|
success "Additional tools installed"
|
|
}
|
|
|
|
# Main installation function
|
|
main() {
|
|
log "Starting Battery Life Optimizer installation..."
|
|
|
|
check_root
|
|
check_system
|
|
|
|
update_packages
|
|
install_tlp
|
|
install_powertop
|
|
setup_powertop_service
|
|
install_additional_tools
|
|
|
|
backup_tlp_config
|
|
apply_tlp_config
|
|
|
|
# Start TLP
|
|
log "Starting TLP service..."
|
|
sudo tlp start
|
|
|
|
success "Installation completed successfully!"
|
|
echo
|
|
log "Next steps:"
|
|
echo "1. Run 'tlp-stat' to check TLP status"
|
|
echo "2. Run 'sudo powertop' to analyze power consumption"
|
|
echo "3. Reboot your system for all changes to take effect"
|
|
echo "4. Use the monitoring scripts in the scripts/ directory"
|
|
}
|
|
|
|
# Run main function if script is executed directly
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
main "$@"
|
|
fi |