Files
debianito-post-install/modules/utils.sh
T
2026-05-14 21:52:17 -05:00

260 lines
7.4 KiB
Bash

#!/usr/bin/env bash
# Common utility functions for the post-install script
# ------------------
# Global variables
# ------------------
CPU_SUMMARY=""
RAM_SUMMARY=""
GPU_TYPE=""
GPU_DESC=""
GPU_VERSION=""
INTEL_GPU_DEVICE_ID=""
NVIDIA_GPU_DEVICE_ID=""
KERNEL_VERSION=""
WIFI_CHIPSET=""
# --------------------------
# Pre-flight checks
# --------------------------
check_root() {
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Do not run this script as root. Use a normal user with sudo.${NC}"
exit 1
fi
}
check_sudo() {
if ! sudo -v; then
echo -e "${RED}This script requires sudo privileges.${NC}"
exit 1
fi
}
# --------------------------------
# Time sync before network ops
# --------------------------------
sync_system_time() {
if command -v timedatectl &> /dev/null; then
local ntp_active
ntp_active=$(timedatectl show --property=NTP --value 2>/dev/null || echo "no")
if [ "$ntp_active" != "yes" ]; then
echo -e "${YELLOW}NTP not active. Attempting to enable time sync...${NC}"
sudo timedatectl set-ntp true 2>/dev/null || true
sleep 2
fi
elif command -v hwclock &> /dev/null && command -v ntpd &> /dev/null; then
sudo hwclock --hctosys 2>/dev/null || true
fi
}
# --------------------------------
# Debian version detection
# --------------------------------
detect_debian_version() {
if ! command -v lsb_release &> /dev/null; then
if [ -f /etc/os-release ]; then
DEBIAN_CODENAME=$(grep -oP 'VERSION_CODENAME=\K\w+' /etc/os-release 2>/dev/null || echo "")
fi
if [ -z "$DEBIAN_CODENAME" ]; then
echo -e "${YELLOW}Installing lsb-release...${NC}"
sync_system_time
sudo apt update -qq 2>/dev/null && sudo apt install -y -qq lsb-release
fi
fi
if [ -z "$DEBIAN_CODENAME" ]; then
DEBIAN_CODENAME=$(lsb_release -cs 2>/dev/null || echo "")
fi
case "$DEBIAN_CODENAME" in
bookworm) DEBIAN_VERSION="12" ;;
trixie) DEBIAN_VERSION="13" ;;
*)
echo -e "${RED}Unsupported Debian version: '$DEBIAN_CODENAME'. Only 12 (bookworm) and 13 (trixie) are supported.${NC}"
exit 1
;;
esac
}
# ----------------------------------
# CPU and RAM info (cosmetic)
# ----------------------------------
detect_cpu_ram() {
CPU_SUMMARY=$(grep -m1 'model name' /proc/cpuinfo | sed 's/.*: //')
RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
RAM_GB=$(awk -v kb="$RAM_KB" 'BEGIN { printf "%.2f", kb / 1048576 }')
RAM_SUMMARY="${RAM_GB} GB"
}
get_cpu_summary() {
echo "$CPU_SUMMARY"
}
get_ram_summary() {
echo "$RAM_SUMMARY"
}
# ----------------------------------
# Package installed check
# ----------------------------------
is_installed() {
dpkg -l "$1" 2>/dev/null | grep -q '^ii'
}
# ----------------------------------
# Package version lookup
# ----------------------------------
pkg_versions() {
local result=""
for pkg in "$@"; do
local ver
ver=$(apt-cache policy "$pkg" 2>/dev/null | awk 'NR==3 {print $2; exit}')
if [ -n "$ver" ] && [ "$ver" != "(none)" ]; then
result+=" - ${pkg} ${ver}\n"
else
result+=" - ${pkg}\n"
fi
done
echo -e "$result"
}
# ----------------------------------
# Kernel version
# ----------------------------------
detect_kernel() {
KERNEL_VERSION=$(uname -r)
}
# ----------------------------------
# GPU detection
# ----------------------------------
detect_gpu() {
local gpu_line
gpu_line=$(lspci -nn | grep -E "VGA|3D" | head -n1) || true
if [ -z "$gpu_line" ]; then
GPU_TYPE="unknown"
GPU_DESC="No GPU detected"
return
fi
GPU_DESC=$(echo "$gpu_line" | sed -E 's/.*: //; s/ *\(rev.*//')
if echo "$gpu_line" | grep -qi "AMD"; then
GPU_TYPE="amd"
elif echo "$gpu_line" | grep -qi "Intel"; then
GPU_TYPE="intel"
INTEL_GPU_DEVICE_ID=$(echo "$gpu_line" | grep -oP '8086:\K[0-9a-fA-F]+' | head -n1)
if [ -n "$INTEL_GPU_DEVICE_ID" ]; then
INTEL_GPU_DEVICE_ID="0x${INTEL_GPU_DEVICE_ID,,}"
fi
elif echo "$gpu_line" | grep -qi "NVIDIA"; then
GPU_TYPE="nvidia"
NVIDIA_GPU_DEVICE_ID=$(echo "$gpu_line" | grep -oP '10de:\K[0-9a-fA-F]+' | head -n1)
else
GPU_TYPE="unknown"
fi
if [ "$GPU_TYPE" = "nvidia" ]; then
local nv_ver
nv_ver=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -1)
if [ -z "$nv_ver" ]; then
nv_ver=$(dpkg -l nvidia-driver 2>/dev/null | awk '/^ii/ {print $3}' | sed 's/-.*//')
fi
[ -n "$nv_ver" ] && GPU_VERSION="NVIDIA $nv_ver"
fi
if [ -z "$GPU_VERSION" ]; then
local mesa_ver
mesa_ver=$(dpkg -l libgl1-mesa-dri 2>/dev/null | awk '/^ii/ {print $3; exit}' | sed 's/-.*//')
[ -n "$mesa_ver" ] && GPU_VERSION="Mesa $mesa_ver"
fi
}
get_gpu_summary() {
if [ -n "$GPU_DESC" ]; then
echo "$GPU_DESC"
else
echo "Unknown/Rare"
fi
}
# -------------------------------------
# Network adapter detection
# -------------------------------------
WIFI_CHIPSET=""
WIFI_DESC=""
ETH_DESC=""
detect_network() {
local eth_line
eth_line=$(lspci -nn | grep -i 'Ethernet controller' | head -n1) || true
if [ -n "$eth_line" ]; then
ETH_DESC=$(echo "$eth_line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//')
fi
local wifi_line
wifi_line=$(lspci -nn | grep -i 'Network controller' | head -n1) || true
if [ -n "$wifi_line" ]; then
WIFI_CHIPSET="$wifi_line"
WIFI_DESC=$(echo "$wifi_line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//')
fi
}
# ---------------------------------------
# Intel HD Graphics generation detection
# ---------------------------------------
# Returns "gen7-" if Device ID < 0x1600, else "gen8+"
get_intel_generation() {
if [ -z "$INTEL_GPU_DEVICE_ID" ]; then
# fallback: assume gen8+
echo "gen8+"
return
fi
local dev_int
dev_int=$(printf "%d" "$INTEL_GPU_DEVICE_ID")
if [ "$dev_int" -lt 5632 ]; then # 0x1600 = 5632
echo "gen7-"
else
echo "gen8+"
fi
}
# ----------------------------------------------------------------------
# Check if backports repository is enabled (active line without #)
# ----------------------------------------------------------------------
is_backports_enabled() {
# Check classic sources.list
if [ -f /etc/apt/sources.list ]; then
if grep -Eq '^[^#]*[ \t]+bookworm-backports[ \t]+' /etc/apt/sources.list 2>/dev/null; then
echo true
return
fi
if grep -Eq '^[^#]*[ \t]+trixie-backports[ \t]+' /etc/apt/sources.list 2>/dev/null; then
echo true
return
fi
fi
# Check deb822 .sources files
if [ -d /etc/apt/sources.list.d ]; then
if grep -qr 'Suites:.*-backports' /etc/apt/sources.list.d/*.sources 2>/dev/null; then
echo true
return
fi
fi
echo false
}
get_backports_kernel_version() {
local ver
ver=$(apt-cache policy linux-image-amd64 2>/dev/null | \
grep -E '^[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+.*~bpo' | head -n1 | awk '{print $1}')
if [ -n "$ver" ]; then
echo "$ver"
else
echo "unknown"
fi
}