#!/usr/bin/env bash # # fanctl — control helper for ugreen-fancontrol.sh on TrueNAS SCALE # # fanctl start start the fan loop # fanctl stop stop it and force chip auto mode # fanctl restart stop then start # fanctl status running state, pwm mode, last log # fanctl temps one-shot temps + RPM # fanctl diagnose modules, hwmon, writability # fanctl log follow the log # set -euo pipefail HERE="$(cd "$(dirname "$(readlink -f "$0" 2>/dev/null || echo "$0")")" && pwd)" FAN_SCRIPT="${FAN_SCRIPT:-$HERE/ugreen-fancontrol.sh}" PIDFILE="/var/run/ugreen-fancontrol.pid" LOGFILE="/var/log/ugreen-fancontrol.log" if [ "$(id -u)" -ne 0 ]; then exec sudo "$0" "$@"; fi hwmon_name() { local n="" [ -r "$1/name" ] && n="$(tr -d '\n' < "$1/name" 2>/dev/null || true)" echo "$n" } find_it87_hwmon() { local d name for d in /sys/class/hwmon/hwmon*; do [ -d "$d" ] || continue name="$(hwmon_name "$d")" case "$name" in it8613|it87|it8622|it8623) [ -e "$d/pwm2" ] && [ -e "$d/pwm3" ] && { echo "$d"; return 0; } ;; esac done return 1 } load_modules() { modprobe coretemp 2>/dev/null || true modprobe drivetemp 2>/dev/null || true find_it87_hwmon >/dev/null && return 0 local id for id in "" 0x8613 0x8622 0x8623; do if [ -n "$id" ]; then modprobe it87 ignore_resource_conflict=1 force_id="$id" 2>/dev/null || true else modprobe it87 ignore_resource_conflict=1 2>/dev/null || true fi sleep 0.4 find_it87_hwmon >/dev/null && return 0 rmmod it87 2>/dev/null || true done return 1 } restore_auto() { local hw hw="$(find_it87_hwmon || true)" [ -n "$hw" ] || return 0 printf '2' > "$hw/pwm2_enable" 2>/dev/null || true printf '2' > "$hw/pwm3_enable" 2>/dev/null || true } running_pid() { if [ -f "$PIDFILE" ]; then local p p="$(tr -d '\n' < "$PIDFILE" 2>/dev/null || true)" if [ -n "${p:-}" ] && kill -0 "$p" 2>/dev/null; then echo "$p" return 0 fi fi local q q="$(pgrep -f "/ugreen-fancontrol.sh" | head -n1 || true)" [ -n "${q:-}" ] && { echo "$q"; return 0; } return 1 } milli_to_c() { local m="$1" case "$m" in ''|*[!0-9]*) echo "?"; return ;; esac echo $(( m / 1000 )) } cmd_start() { if pid="$(running_pid)"; then echo "already running (pid $pid)" return 0 fi [ -x "$FAN_SCRIPT" ] || { echo "ERROR: $FAN_SCRIPT missing or not executable" >&2; exit 1; } load_modules || echo "WARN: it87 not loaded yet — daemon will retry / fail in the log" mkdir -p "$(dirname "$PIDFILE")" "$(dirname "$LOGFILE")" touch "$LOGFILE" nohup "$FAN_SCRIPT" >> "$LOGFILE" 2>&1 & echo "$!" > "$PIDFILE" sleep 1 if pid="$(running_pid)"; then echo "started (pid $pid)" echo "log: $LOGFILE" else echo "ERROR: failed to start — last log lines:" >&2 tail -n 15 "$LOGFILE" 2>/dev/null || true exit 1 fi } cmd_stop() { if pid="$(running_pid)"; then kill -TERM "$pid" 2>/dev/null || true local i=0 while [ "$i" -lt 20 ]; do kill -0 "$pid" 2>/dev/null || break sleep 0.2 i=$(( i + 1 )) done if kill -0 "$pid" 2>/dev/null; then echo "WARN: process still alive after TERM — leaving it (not using kill -9)" else echo "stopped (pid $pid)" fi else echo "not running" fi restore_auto echo "fans set to chip auto mode" rm -f "$PIDFILE" } cmd_status() { if pid="$(running_pid)"; then echo "STATUS : running (pid $pid)" else echo "STATUS : stopped" fi echo "SCRIPT : $FAN_SCRIPT" local hw hw="$(find_it87_hwmon || true)" if [ -n "$hw" ]; then echo "it87 : $hw ($(hwmon_name "$hw"))" local c en pwm mode for c in 2 3; do en="$(tr -d '\n' < "$hw/pwm${c}_enable" 2>/dev/null || echo '?')" pwm="$(tr -d '\n' < "$hw/pwm${c}" 2>/dev/null || echo '?')" case "$en" in 1) mode="manual" ;; 2) mode="auto" ;; *) mode="mode${en}" ;; esac echo "pwm$c : $mode value=$pwm" done else echo "it87 : not found" fi if [ -f "$LOGFILE" ]; then echo "--- last log lines ---" tail -n 6 "$LOGFILE" fi } cmd_temps() { load_modules >/dev/null || true local d name t f max="" echo "--- temps ---" for d in /sys/class/hwmon/hwmon*; do [ -d "$d" ] || continue name="$(hwmon_name "$d")" case "$name" in coretemp) t="$(milli_to_c "$(tr -d '\n' < "$d/temp1_input" 2>/dev/null || echo)")" echo "CPU (coretemp temp1) : ${t} C" ;; drivetemp) t="$(milli_to_c "$(tr -d '\n' < "$d/temp1_input" 2>/dev/null || echo)")" echo " $name $(basename "$d")/temp1 : ${t} C" case "$t" in *[0-9]*) if [ -z "$max" ] || [ "$t" -gt "$max" ]; then max="$t"; fi ;; esac ;; nvme) t="$(milli_to_c "$(tr -d '\n' < "$d/temp1_input" 2>/dev/null || echo)")" echo " $name $(basename "$d")/temp1 (composite) : ${t} C" case "$t" in *[0-9]*) if [ -z "$max" ] || [ "$t" -gt "$max" ]; then max="$t"; fi ;; esac for f in "$d"/temp2_input "$d"/temp3_input; do [ -r "$f" ] || continue echo " (ignored $(basename "$f") : $(milli_to_c "$(tr -d '\n' < "$f")") C)" done ;; esac done echo "Drives hottest : ${max:-?} C" local hw hw="$(find_it87_hwmon || true)" if [ -n "$hw" ]; then echo "--- fans ---" echo "fan2 (CPU / pwm2) : $(tr -d '\n' < "$hw/fan2_input" 2>/dev/null || echo '?') RPM pwm=$(tr -d '\n' < "$hw/pwm2" 2>/dev/null || echo '?')" echo "fan3 (140mm / pwm3) : $(tr -d '\n' < "$hw/fan3_input" 2>/dev/null || echo '?') RPM pwm=$(tr -d '\n' < "$hw/pwm3" 2>/dev/null || echo '?')" else echo "it87 not loaded — cannot read RPM" fi } cmd_diagnose() { echo "=== modules ===" lsmod | grep -E 'it87|coretemp|drivetemp' || echo "(none of it87/coretemp/drivetemp loaded)" echo echo "=== trying load ===" if load_modules; then echo "it87 present: $(find_it87_hwmon)" else echo "FAILED to load it87. On TrueNAS you need the out-of-tree it87 module" echo "from https://github.com/0n1cOn3/UGREEN-Fan-Control" fi echo echo "=== hwmon ===" local d for d in /sys/class/hwmon/hwmon*; do [ -d "$d" ] || continue printf '%s name=%s' "$d" "$(hwmon_name "$d")" [ -e "$d/pwm2" ] && printf ' pwm2=%s' "$(tr -d '\n' < "$d/pwm2" 2>/dev/null || echo)" [ -e "$d/pwm3" ] && printf ' pwm3=%s' "$(tr -d '\n' < "$d/pwm3" 2>/dev/null || echo)" echo done echo echo "=== writability ===" local hw hw="$(find_it87_hwmon || true)" if [ -n "$hw" ]; then for f in pwm2 pwm3 pwm2_enable pwm3_enable; do if [ -w "$hw/$f" ]; then echo "writable $hw/$f" else echo "NOT writable $hw/$f" fi done fi } cmd_log() { tail -f "$LOGFILE"; } case "${1:-status}" in start) cmd_start ;; stop) cmd_stop ;; restart) cmd_stop; sleep 1; cmd_start ;; status) cmd_status ;; temps) cmd_temps ;; diagnose|diag) cmd_diagnose ;; log) cmd_log ;; *) echo "usage: fanctl {start|stop|restart|status|temps|diagnose|log}" >&2; exit 2 ;; esac