phoebus_os/installer/lib/hardware.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

128 lines
4.1 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Read-only hardware probing.
#
# Nothing in this file modifies the system: it only reads /proc, /sys and the
# output of read-only inventory tools. Results feed the installation plan and
# the kernel/profile decisions.
hw_cpu_vendor() {
awk -F': ' '/^vendor_id/{print $2; exit}' /proc/cpuinfo 2>/dev/null
}
hw_cpu_model() {
awk -F': ' '/^model name/{print $2; exit}' /proc/cpuinfo 2>/dev/null
}
hw_cpu_cores() {
local n
n="$(nproc --all 2>/dev/null || echo 1)"
printf '%s' "$n"
}
hw_cpu_threads() {
local n
n="$(grep -c '^processor' /proc/cpuinfo 2>/dev/null || echo 1)"
printf '%s' "$n"
}
hw_cpu_has() {
grep -qw "$1" /proc/cpuinfo 2>/dev/null
}
# hw_cpu_virt - print a human readable virtualisation capability summary
hw_cpu_virt() {
local out=()
hw_cpu_has vmx && out+=("Intel VT-x")
hw_cpu_has svm && out+=("AMD-V")
hw_cpu_has avx2 && out+=("AVX2")
hw_cpu_has avx512f && out+=("AVX-512")
hw_cpu_has aes && out+=("AES-NI")
((${#out[@]})) && printf '%s' "${out[*]}"
}
hw_mem_total_mib() {
awk '/^MemTotal:/{printf "%d", $2/1024; exit}' /proc/meminfo 2>/dev/null
}
hw_firmware_mode() {
if [[ -d /sys/firmware/efi ]]; then printf 'UEFI'; else printf 'BIOS'; fi
}
_is_laptop() {
local p
for p in /sys/class/power_supply/BAT*; do
[[ -e "$p" ]] && return 0
done
[[ -d /sys/firmware/acpi/tables ]] && \
grep -qi 'laptop' /sys/class/dmi/id/chassis_type 2>/dev/null
return 1
}
# _hw_lspci_filter REGEX - matching lspci lines, if lspci is available
_hw_lspci_filter() {
command -v lspci >/dev/null 2>&1 || return 0
lspci -nn 2>/dev/null | grep -iE "$1" || true
}
_hw_lsusb_filter() {
command -v lsusb >/dev/null 2>&1 || return 0
lsusb 2>/dev/null | grep -iE "$1" || true
}
hw_gpus() { _hw_lspci_filter 'VGA compatible controller|3D controller|Display controller'; }
hw_network_hw() { _hw_lspci_filter 'Ethernet controller|Network controller|Wireless'; }
hw_bluetooth() { { _hw_lspci_filter 'Bluetooth'; _hw_lsusb_filter 'Bluetooth'; } | sort -u; }
hw_audio() { _hw_lspci_filter 'Audio device'; }
hw_usb() { _hw_lspci_filter 'USB controller'; }
hw_storage_hw() { _hw_lspci_filter 'SATA controller|NVMe|RAID bus controller'; }
# hw_gpu_vendors - space separated vendor tags, used for driver policy
hw_gpu_vendors() {
local gpus vendors=()
gpus="$(hw_gpus)"
grep -qi 'nvidia' <<<"$gpus" && vendors+=("nvidia")
grep -qi 'amd\|advanced micro devices\|ati technologies' <<<"$gpus" && vendors+=("amd")
grep -qi 'intel' <<<"$gpus" && vendors+=("intel")
printf '%s' "${vendors[*]:-unknown}"
}
hw_input_devices() {
local count=0
[[ -r /proc/bus/input/devices ]] && \
count="$(grep -c '^N: Name=' /proc/bus/input/devices 2>/dev/null)"
printf '%s' "$count"
}
# hw_report - human readable report for the TUI / hardware screen
hw_report() {
local virt
virt="$(hw_cpu_virt)"
printf 'Firmware: %s\n' "$(hw_firmware_mode)"
printf 'CPU: %s\n' "$(hw_cpu_model)"
printf 'CPU cores: %s threads / %s logical CPUs\n' \
"$(hw_cpu_threads)" "$(hw_cpu_cores)"
printf 'CPU features: %s\n' "${virt:-none detected}"
printf 'Memory: %s MiB\n' "$(hw_mem_total_mib)"
printf 'Machine type: %s\n' "$(_is_laptop && echo laptop || echo desktop/server)"
printf 'Input devices: %s\n' "$(hw_input_devices)"
printf '\n'
printf 'GPUs:\n%s\n\n' "$(hw_gpus | sed 's/^/ /' | grep . || echo ' (none detected)')"
printf 'Storage controllers:\n%s\n\n' "$(hw_storage_hw | sed 's/^/ /' | grep . || echo ' (none detected)')"
printf 'Network:\n%s\n\n' "$(hw_network_hw | sed 's/^/ /' | grep . || echo ' (none detected)')"
printf 'Bluetooth:\n%s\n\n' "$(hw_bluetooth | sed 's/^/ /' | grep . || echo ' (none detected)')"
printf 'Audio:\n%s\n\n' "$(hw_audio | sed 's/^/ /' | grep . || echo ' (none detected)')"
printf 'USB controllers:\n%s\n' "$(hw_usb | sed 's/^/ /' | grep . || echo ' (none detected)')"
}
# hw_recommended_kernel_profile - conservative recommendation
hw_recommended_kernel_profile() {
local mem
mem="$(hw_mem_total_mib)"
if [[ -n "$mem" ]] && ((mem < 2048)); then
printf 'safe'
else
printf 'optimized'
fi
}