#!/usr/bin/env bash # Hardware report for Omarchy. It sends nothing unless you say yes. # # On Omarchy: asks, then sends a public hardware report. Anywhere else it # says so and sends nothing. # Served as /delete: rebuilds this machine's hash, then asks whether to delete # the report the site holds for it. set -euo pipefail OMAHCL_URL=${OMAHCL_URL:-https://omahcl.com} # CollectsController fills this in for /collect and /delete. Anything but "delete" # collects. OMAHCL_ACTION=${OMAHCL_ACTION:-delete} OMAHCL_SYS_ROOT=${OMAHCL_SYS_ROOT:-} OMAHCL_ARCH=${OMAHCL_ARCH:-} OMAHCL_KERNEL=${OMAHCL_KERNEL:-} OMAHCL_DRY_RUN=${OMAHCL_DRY_RUN:-} OMAHCL_SUBMIT=${OMAHCL_SUBMIT:-} OMAHCL_SKIP_CMDS=${OMAHCL_SKIP_CMDS:-} SYS=${OMAHCL_SYS_ROOT:-/} SYS=${SYS%/} [[ -n $SYS ]] || SYS=/ root() { local part=${1#/} if [[ $SYS == / ]]; then printf '/%s\n' "$part" else printf '%s/%s\n' "$SYS" "$part" fi } read_strip() { local path=$1 [[ -r $path ]] || { printf ''; return; } tr -d '\r' <"$path" | tr '\n\t' ' ' | head -c 8192 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' } clean() { local text=$1 local limit=${2:-240} text=${text//$'\r'/ } text=${text//$'\n'/ } text=${text//$'\t'/ } text=$(printf '%s' "$text" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') local lowered lowered=$(printf '%s' "$text" | tr '[:upper:]' '[:lower:]') case $lowered in "not specified" | unknown | none | "to be filled by o.e.m." | "default string" | "system product name" | "system manufacturer") printf '' return ;; esac printf '%s' "${text:0:limit}" } json_escape() { local s=$1 s=${s//\\/\\\\} s=${s//\"/\\\"} s=${s//$'\n'/ } s=${s//$'\r'/ } s=${s//$'\t'/ } printf '%s' "$s" } os_field() { local key=$1 local file file=$(root etc/os-release) [[ -r $file ]] || { printf ''; return; } local line line=$(grep -E "^${key}=" "$file" | head -n 1 || true) [[ -n $line ]] || { printf ''; return; } line=${line#*=} line=${line#\"} line=${line%\"} printf '%s' "$line" } dmi() { local name=$1 local lower=${name,,} if [[ $lower == *serial* || $lower == product_uuid || $lower == chassis_serial ]]; then printf '' return fi clean "$(read_strip "$(root "sys/class/dmi/id/$name")")" } cpu_field() { local key=$1 local file file=$(root proc/cpuinfo) [[ -r $file ]] || { printf ''; return; } local line line=$(grep -E "^${key}[[:space:]]*:" "$file" | head -n 1 || true) [[ -n $line ]] || { printf ''; return; } clean "${line#*:}" } cpu_cores() { local file file=$(root proc/cpuinfo) [[ -r $file ]] || { printf '1'; return; } local count count=$(grep -c '^processor' "$file" || true) [[ ${count:-0} -gt 0 ]] || count=1 printf '%s' "$count" } # Device tree strings, one per line. They are NUL-terminated, and `compatible` # holds several. Apple Silicon under Asahi Linux has a device tree and no DMI. dt_strings() { local file file=$(root "proc/device-tree/$1") [[ -r $file ]] || return 0 tr '\0' '\n' <"$file" | head -c 1024 } # The chip named by the SoC id in `compatible` ("apple,t8112"). The ids and names # are from the apple/*.dts files in the Linux tree. apple_chip() { local soc soc=$(dt_strings compatible | sed -n 's/^apple,\(t[0-9]\{4\}\)$/\1/p' | head -n 1) case $soc in t8103) printf 'Apple M1' ;; t6000) printf 'Apple M1 Pro' ;; t6001) printf 'Apple M1 Max' ;; t6002) printf 'Apple M1 Ultra' ;; t8112) printf 'Apple M2' ;; t6020) printf 'Apple M2 Pro' ;; t6021) printf 'Apple M2 Max' ;; t6022) printf 'Apple M2 Ultra' ;; t8122) printf 'Apple M3' ;; t6030) printf 'Apple M3 Pro' ;; t6031 | t6034) printf 'Apple M3 Max' ;; t6032) printf 'Apple M3 Ultra' ;; t8132) printf 'Apple M4' ;; t8140) printf 'Apple A18 Pro' ;; *) printf '' ;; esac } # SMBIOS chassis type for a Mac, which has no DMI to say. apple_chassis() { case $1 in *MacBook*) printf '10' ;; *iMac*) printf '13' ;; *"Mac mini"*) printf '35' ;; *"Mac Studio"*) printf '3' ;; *"Mac Pro"*) printf '7' ;; *) printf '' ;; esac } # Core names from lscpu, for Arm machines whose /proc/cpuinfo has no `model name`. lscpu_models() { if [[ -n ${OMAHCL_SKIP_CMDS:-} ]] || ! command -v lscpu >/dev/null 2>&1; then return 0 fi # util-linux indents each cluster's name under its Vendor ID, and prints "-" for # a part it cannot name. LC_ALL=C lscpu 2>/dev/null | sed -n 's/^[[:space:]]*Model name:[[:space:]]*//p' | awk '$0 != "-" && !seen[$0]++' | paste -sd'+' | sed 's/+/ + /g' } cpu_arch() { if [[ $SYS == / ]]; then uname -m else printf '%s' "${OMAHCL_ARCH:-unknown}" fi } memory_bytes() { local file file=$(root proc/meminfo) [[ -r $file ]] || { printf '0'; return; } local kb kb=$(awk '/^MemTotal:/ { print $2; exit }' "$file") if [[ -z ${kb:-} ]]; then printf '0' return fi printf '%s' $((kb * 1024)) } sha256_hex() { sha256sum | awk '{ print $1 }' } instance_digest() { local vendor=$1 product=$2 cpu_model=$3 # This domain string predates the current name. Never rename it: that would # give every machine a new instance_digest and split its history. local domain=omarchy-market-instance-v1 local machine_id machine_id=$(read_strip "$(root etc/machine-id)") printf '%s\0%s\0%s\0%s\0%s' "$domain" "$machine_id" "$vendor" "$product" "$cpu_model" | sha256_hex } host_kernel() { if [[ $SYS == / ]]; then uname -s else printf '%s' "${OMAHCL_KERNEL:-Linux}" fi } bool_dir() { if [[ -d $1 ]]; then printf 'true' else printf 'false' fi } # on, off, or unknown (the variable is missing or unreadable). secure_boot_state() { local path path=$(root sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c) if [[ ! -r $path ]]; then printf 'unknown' return fi local byte byte=$(dd if="$path" bs=1 skip=4 count=1 2>/dev/null | od -An -tu1 | tr -d ' \n') if [[ ${byte:-0} == 1 ]]; then printf 'on' else printf 'off' fi } gpus_json() { if [[ -n ${OMAHCL_SKIP_CMDS:-} ]] || ! command -v lspci >/dev/null 2>&1; then printf '[]' return fi local json='[' n=0 line class vendor name while IFS= read -r line; do case $line in *'"VGA compatible controller"'* | *'"3D controller"'*) ;; *) continue ;; esac class=$(printf '%s' "$line" | grep -o '"[^"]*"' | sed -n '1s/"//gp') vendor=$(printf '%s' "$line" | grep -o '"[^"]*"' | sed -n '2s/"//gp') name=$(printf '%s' "$line" | grep -o '"[^"]*"' | sed -n '3s/"//gp') vendor=$(clean "$vendor" 120) name=$(clean "$name" 160) if [[ $n -gt 0 ]]; then json+=',' fi json+=$(printf '{"vendor":"%s","name":"%s"}' "$(json_escape "$vendor")" "$(json_escape "$name")") n=$((n + 1)) [[ $n -ge 8 ]] && break done < <(lspci -mm 2>/dev/null || true) json+=']' printf '%s' "$json" } # Omarchy's release channel is the path segment of its package repository. omarchy_channel() { local file channel file=$(root etc/pacman.conf) [[ -r $file ]] || { printf 'stable'; return; } channel=$(sed -n 's#^[[:space:]]*Server[[:space:]]*=[[:space:]]*https\{0,1\}://pkgs\.omarchy\.org/\([^/]*\)/.*#\1#p' "$file" | head -n 1) channel=$(clean "$channel" 40) [[ -n $channel ]] || channel=stable printf '%s' "$channel" } # A Mac running Asahi Linux, whose firmware may give no DMI or only part of it: the # device tree model is "Apple MacBook Air (13-inch, M2, 2022)". gather_apple_silicon() { local model model=$(clean "$(dt_strings model | head -n 1)") [[ $model == "Apple "* ]] || return 0 F_vendor=Apple F_product=${model#Apple } F_chassis=$(apple_chassis "$F_product") } # Fills the F_* globals from /proc, /sys and /etc. Never reads serial numbers. gather_facts() { F_vendor=$(dmi sys_vendor) F_product=$(dmi product_name) F_family=$(dmi product_family) F_chassis=$(dmi chassis_type) F_cpu_model=$(cpu_field "model name") F_cpu_vendor=$(cpu_field vendor_id) # The instance digest hashes this and never the Arm names below, so a machine # keeps its digest when its CPU gets a proper name. F_digest_cpu=${F_cpu_model:-Unknown CPU} F_arch=$(clean "$(cpu_arch)" 40) F_cores=$(cpu_cores) F_mem=$(memory_bytes) F_uefi=$(bool_dir "$(root sys/firmware/efi)") F_tpm=$(bool_dir "$(root sys/class/tpm)") F_sb=$(secure_boot_state) F_gpus=$(gpus_json) F_chip=$(apple_chip) if [[ -n $F_chip ]]; then gather_apple_silicon fi if [[ -z $F_cpu_model ]]; then if [[ -n $F_chip ]]; then F_cpu_model=$F_chip [[ -n $F_cpu_vendor ]] || F_cpu_vendor=Apple else F_cpu_model=$(clean "$(lscpu_models)") fi fi if [[ -n $F_chip && $F_gpus == '[]' ]]; then F_gpus=$(printf '[{"vendor":"Apple","name":"%s GPU"}]' "$(json_escape "$F_chip")") fi [[ -n $F_cpu_model ]] || F_cpu_model="Unknown CPU" [[ -n $F_arch ]] || F_arch=unknown } hardware_json() { local sb=false [[ $F_sb == on ]] && sb=true printf '{"vendor":"%s","product":"%s","family":"%s","chassis":"%s","cpu":{"model":"%s","vendor":"%s","arch":"%s","cores":%s},"memory_bytes":%s,"gpus":%s,"uefi":%s,"tpm":{"present":%s},"secure_boot":{"enabled":%s}}' \ "$(json_escape "$F_vendor")" \ "$(json_escape "$F_product")" \ "$(json_escape "$F_family")" \ "$(json_escape "$F_chassis")" \ "$(json_escape "$F_cpu_model")" \ "$(json_escape "$F_cpu_vendor")" \ "$(json_escape "$F_arch")" \ "$F_cores" \ "$F_mem" \ "$F_gpus" \ "$F_uefi" \ "$F_tpm" \ "$sb" } omarchy_payload() { local version channel digest version=$(clean "$(os_field VERSION_ID)" 80) [[ -n $version ]] || version=$(clean "$(os_field BUILD_ID)" 80) [[ -n $version ]] || version=unknown channel=$(omarchy_channel) digest=$(instance_digest "$F_vendor" "$F_product" "$F_digest_cpu") printf '{"omarchy":{"version":"%s","channel":"%s"},"hardware":%s,"instance_digest":"%s"}' \ "$(json_escape "$version")" "$(json_escape "$channel")" "$(hardware_json)" "$digest" } delete_payload() { printf '{"instance_digest":"%s"}' "$(instance_digest "$F_vendor" "$F_product" "$F_digest_cpu")" } print_machine() { echo echo "${F_vendor:-Unknown vendor} ${F_product:-unknown model}" echo "$F_cpu_model, $(((F_mem + 536870912) / 1073741824)) GB memory, $F_arch" } # Shows what a send shares and the privacy policy, then asks. Every send needs an # explicit yes, from the prompt or OMAHCL_SUBMIT; no terminal means no. confirm_submit() { echo echo "A yes publishes your machine's model page and its Omarchy Verified card." echo "Nothing has been sent yet. If you answer yes, this sends:" echo " - vendor, model, product family, chassis type, CPU, memory size and GPU names" echo " - whether the machine uses UEFI, has a TPM and has Secure Boot on" echo " - the Omarchy version and release channel" echo " - a one-way hash of the machine-id, model and CPU, so running this again" echo " updates one entry instead of adding another" echo "It never sends serial numbers, MAC addresses, your hostname or username." echo "The report is public." echo "The server stores no IP address with the report. It keeps per-IP request" echo "counts for about an hour to limit abuse. Database backups keep those" echo "counts for up to a day after that." echo "Privacy policy: ${OMAHCL_URL%/}/privacy" ask_yes "Send it? [y/N] " "share it" } # The only way anything gets sent: an explicit yes at a terminal, or # OMAHCL_SUBMIT=yes. No terminal means no. ask_yes() { local prompt=$1 what=$2 case ${OMAHCL_SUBMIT:-} in yes | y | 1) return 0 ;; no | n | 0) return 1 ;; esac if ! ( : /dev/null; then echo "Run this in a terminal to choose whether to $what, or set OMAHCL_SUBMIT=yes." >&2 return 1 fi local answer= echo read -r -p "$prompt" answer &2 exit 1 } local nonce nonce=$(printf '%s' "$body" | sed -n 's/.*"nonce"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1) if [[ -z $nonce ]]; then echo "Omarchy HCL did not return a challenge nonce." >&2 exit 1 fi printf '%s' "$nonce" } submit() { local body=$1 path=$2 what=$3 local response if ! response=$(curl -fsS -A "omahcl-collect/2" --max-time 30 \ -H "Content-Type: application/json" \ -d "$body" \ "${OMAHCL_URL%/}/$path"); then echo "Omarchy HCL rejected the $what." >&2 exit 1 fi printf '%s' "$response" } json_string_field() { printf '%s' "$1" | sed -n "s/.*\"$2\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" | head -n 1 } json_true_field() { printf '%s' "$1" | grep -Eq "\"$2\"[[:space:]]*:[[:space:]]*true" } json_number_field() { printf '%s' "$1" | sed -n "s/.*\"$2\"[[:space:]]*:[[:space:]]*\\([0-9][0-9]*\\).*/\\1/p" | head -n 1 } refuse_identifiers() { if printf '%s' "$1" | grep -Eqi '"[^"]*(serial|mac|hostname|username)[^"]*"[[:space:]]*:'; then echo "Refusing to submit identifier fields." >&2 exit 1 fi } # Prints the payload for a dry run and returns 0, or returns 1 for a real send. # Callers use it as a condition, so it must never be the place that fails. dry_run_print() { [[ -n ${OMAHCL_DRY_RUN:-} ]] || return 1 printf '%s\n' "$1" } # Plain statement, never inside a condition, so set -e and the explicit exits apply. post_payload() { local body=$1 path=${2:-reports} what=${3:-report} nonce nonce=$(fetch_challenge) || exit 1 body=$(printf '%s' "$body" | sed 's/}$//') body+=$(printf ',"nonce":"%s"}' "$(json_escape "$nonce")") RESULT=$(submit "$body" "$path" "$what") || exit 1 } run_omarchy() { gather_facts if [[ -z $F_vendor || -z $F_product ]]; then echo "Could not read machine vendor and product from DMI or the device tree." >&2 exit 1 fi local payload payload=$(omarchy_payload) refuse_identifiers "$payload" if dry_run_print "$payload"; then return 0 fi print_machine if ! confirm_submit; then echo "Nothing was sent." return 0 fi post_payload "$payload" echo echo "Submitted a hardware report." print_card_link } # Closes the loop with the link people paste. Whether the model is new is said here # and nowhere on the card. A hidden report has no card, so it gets no link. print_card_link() { local card_url model_name card_url=$(json_string_field "$RESULT" card_url) if [[ -z $card_url ]]; then return 0 fi model_name=$(json_string_field "$RESULT" model_name | sed 's/\\u0026/\&/g; s/\\u003c//g') if json_true_field "$RESULT" first_of_model; then echo "You are the first report of the ${model_name:-model}." echo "$card_url" else echo "This model is on the list. Card for this CPU:" echo "$card_url" fi } run_delete() { gather_facts if [[ -z $F_vendor || -z $F_product ]]; then echo "Could not read machine vendor and product from DMI or the device tree, so this machine's report cannot be found." >&2 exit 1 fi local payload payload=$(delete_payload) if dry_run_print "$payload"; then return 0 fi print_machine if ! confirm_delete; then echo "Nothing was sent." return 0 fi post_payload "$payload" deletions "deletion request" echo local deleted deleted=$(json_number_field "$RESULT" deleted) if [[ ${deleted:-0} -gt 0 ]]; then echo "Deleted ${deleted} report(s) for this machine." else echo "No report matched this machine. Reports are matched on the machine-id, vendor," echo "model and CPU, so a reinstall or a changed model or CPU string, or a report" echo "sent before the hash dropped DMI serials, will not match." fi } main() { if [[ $OMAHCL_URL == __* ]]; then echo "Set OMAHCL_URL, or download this script from Omarchy HCL so the URL is filled in." >&2 exit 1 fi local kernel kernel=$(host_kernel) case $kernel in Darwin) if [[ $OMAHCL_ACTION == delete ]]; then echo "This is macOS, where this script cannot read your hardware, so it never sent a report to delete." return 0 fi echo "This is macOS. This script only reports machines running Omarchy, so it sent nothing." >&2 exit 2 ;; Linux) ;; *) echo "This script reads /proc and /sys, so it only runs on Linux ($kernel found)." >&2 exit 2 ;; esac if [[ ! -r $(root proc/cpuinfo) || ! -d $(root sys) ]]; then echo "Could not read /proc and /sys. Run this on the machine itself, not inside a restricted container." >&2 exit 2 fi if grep -qi microsoft "$(root proc/version)" 2>/dev/null; then echo "This is Windows Subsystem for Linux. It does not show your real hardware." >&2 echo "Boot a Linux live USB on the machine and run this again." >&2 exit 2 fi if [[ $OMAHCL_ACTION == delete ]]; then run_delete elif [[ $(os_field ID) == omarchy ]]; then run_omarchy else echo "This machine is not running Omarchy. This script only reports machines running" >&2 echo "Omarchy, so it sent nothing." >&2 exit 2 fi } main