#!/bin/bash set -e if [[ "$(whoami)" != "root" ]]; then echo "You need to run this script as root." exit 1 fi TARGETDIR="/opt/fan_control" if [ ! -z "$1" ]; then TARGETDIR="$1" fi echo "*** Installing system dependencies..." if [ -x "$(command -v apt-get)" ]; then apt-get update apt-get install -y python3-venv python3-pip lm-sensors ipmitool elif [ -x "$(command -v dnf)" ]; then dnf install -y python3-pip lm-sensors ipmitool else echo "Unsupported package manager. Please install: python3-venv, python3-pip, lm-sensors, ipmitool" exit 1 fi echo "*** Creating folder '$TARGETDIR'..." if [ ! -d "$TARGETDIR" ]; then mkdir -p "$TARGETDIR" fi echo "*** Creating Python3 virtual environment..." if [ -d "$TARGETDIR/venv" ]; then echo "*** Existing venv found, purging it." rm -rf "$TARGETDIR/venv" fi python3 -m venv "$TARGETDIR/venv" echo "*** Installing Python dependencies..." "$TARGETDIR/venv/bin/pip" install --upgrade pip "$TARGETDIR/venv/bin/pip" install PyYAML echo "*** Copying script and configuration..." if [ -f "$TARGETDIR/fan_control.yaml" ]; then echo "*** Backing up existing configuration..." cp "$TARGETDIR/fan_control.yaml" "$TARGETDIR/fan_control.yaml.backup.$(date +%Y%m%d_%H%M%S)" else cp fan_control.yaml.example "$TARGETDIR/fan_control.yaml" echo "*** Please edit $TARGETDIR/fan_control.yaml to configure your temperature thresholds" fi cp fan_control.py "$TARGETDIR/" chmod +x "$TARGETDIR/fan_control.py" echo "*** Creating SystemD service..." cat > /etc/systemd/system/fan-control.service << EOF [Unit] Description=Temperature-based fan speed controller After=multi-user.target Wants=multi-user.target [Service] Type=simple ExecStart=$TARGETDIR/venv/bin/python3 -u $TARGETDIR/fan_control.py Restart=always RestartSec=10 User=root Group=root # Ensure fans return to automatic mode on service stop/failure ExecStopPost=/usr/bin/ipmitool raw 0x30 0x30 0x01 0x01 # Resource limits MemoryLimit=100M CPUQuota=10% # Security hardening NoNewPrivileges=yes ProtectSystem=strict ProtectHome=yes ReadWritePaths=$TARGETDIR PrivateTmp=yes [Install] WantedBy=multi-user.target EOF echo "*** Testing the script..." if ! "$TARGETDIR/venv/bin/python3" -c "import yaml; print('✓ PyYAML working')"; then echo "Error: PyYAML not properly installed" exit 1 fi if ! command -v sensors >/dev/null 2>&1; then echo "Error: sensors command not found. Please install lm-sensors" exit 1 fi if ! sensors | grep -q "Core"; then echo "Warning: No CPU core temperatures detected. You may need to run 'sensors-detect'" echo "Run: sensors-detect and answer YES to safe probes, then reboot" fi echo "*** Enabling and starting service..." systemctl daemon-reload systemctl enable fan-control systemctl start fan-control echo "*** Waiting for service to start..." sleep 3 echo "*** Installation complete! Service status:" systemctl status fan-control --no-pager echo "" echo "*** Quick checks:" echo "- Edit configuration: nano $TARGETDIR/fan_control.yaml" echo "- View logs: journalctl -u fan-control.service -f" echo "- Test manually: $TARGETDIR/venv/bin/python3 $TARGETDIR/fan_control.py" set +e