#!/usr/bin/env bash # Create a Debian 13 LXC container on a Proxmox VE host and install Keystone in it. # Run it as root in the Proxmox host's shell: # # bash -c "$(curl -fsSL https://raw.githubusercontent.com/seeARMS/keystone/main/scripts/proxmox-lxc.sh)" # # It downloads scripts/install.sh, asks for your UniFi Protect controller's IP, # optional vision model, and which optional features to enable, creates the # container (2 cores, 4–6 GiB RAM, 16 GiB disk), and runs install.sh inside it. # To update Keystone later, run install.sh again inside the container. # # Optional environment variables (for non-interactive installs): # KEYSTONE_CTID Container ID (default: the next free ID) # KEYSTONE_STORAGE Storage for the root disk (default: local-lvm, else first active) # KEYSTONE_BRIDGE Network bridge (default: vmbr0) # KEYSTONE_VLAN VLAN tag (default: none) # KEYSTONE_IP Static IPv4 address in CIDR form, e.g. 192.0.2.50/24 (default: DHCP) # KEYSTONE_GATEWAY Gateway for a static address # KEYSTONE_MEMORY Container RAM in MiB (default: 4096, or 6144 if faces or AI Port enabled) # KEYSTONE_CONTROLLER_IP UniFi Protect controller IPv4 address # KEYSTONE_VISION_PROVIDER ollama | openai-compatible | openai | anthropic | disabled # KEYSTONE_VISION_BASE_URL Vision server URL (empty disables event descriptions) # KEYSTONE_VISION_MODEL Vision model name # KEYSTONE_VISION_API_KEY API key (required for openai/anthropic; optional for openai-compatible) # KEYSTONE_OLLAMA_URL Legacy fallback for KEYSTONE_VISION_BASE_URL when using Ollama # KEYSTONE_STT_ENABLED true/false — Speech-to-Text transcripts (interactive default: Y) # KEYSTONE_LPR_ENABLED true/false — License plate recognition (interactive default: Y) # KEYSTONE_ENHANCE_ENABLED true/false — Enhance Face with GFPGAN (interactive default: Y) # KEYSTONE_FACES_ENABLED true/false — Face recognition, InsightFace non-commercial (default: N) # KEYSTONE_AIPORT_ENABLED true/false — Virtual AI Port for ONVIF / legacy cameras (default: N) # KEYSTONE_WEB_BIND_HOST Dashboard listen IP (default: 127.0.0.1; set 0.0.0.0 for LAN access) # KEYSTONE_ACCEL cpu | intel | openvino (alias for intel) | auto (detect Intel GPU) # Unset: offer Intel if detected interactively, otherwise use CPU. # KEYSTONE_REF Git tag or branch of Keystone passed to install.sh # KEYSTONE_INSTALL_URL Override URL for scripts/install.sh (default: main) # KEYSTONE_INSTALL_SCRIPT Local path to scripts/install.sh instead of downloading set -euo pipefail INSTALL_URL=${KEYSTONE_INSTALL_URL:-https://raw.githubusercontent.com/seeARMS/keystone/main/scripts/install.sh} say() { printf '\n\033[1m==> %s\033[0m\n' "$*"; } die() { printf '\nError: %s\n' "$*" >&2; exit 1; } first_storage() { # first_storage CONTENT PREFERRED: an active storage for CONTENT local all all=$(pvesm status --content "$1" | awk 'NR > 1 && $3 == "active" {print $1}') grep -x "$2" <<<"$all" || head -n1 <<<"$all" } container_group_gid() { local group=$1 entry if ! entry=$(pct exec "$ctid" -- getent group "$group"); then pct exec "$ctid" -- groupadd --force --system "$group" || return entry=$(pct exec "$ctid" -- getent group "$group") || return fi cut -d: -f3 <<<"$entry" } configure_intel_passthrough() { local render=$1 render_gid card_gid card card_path="" # Pick the card belonging to this render node on multi-GPU hosts. for card in /sys/class/drm/"${render##*/}"/device/drm/card[0-9]*; do if [ -e "/dev/dri/${card##*/}" ]; then card_path="/dev/dri/${card##*/}" break fi done # Look up GIDs inside the running Debian guest, then configure while stopped. render_gid=$(container_group_gid render) if [ -n "$card_path" ]; then card_gid=$(container_group_gid video) fi pct shutdown "$ctid" --timeout 30 if ! pct set "$ctid" --dev0 "$render,gid=$render_gid,mode=0660"; then printf 'Could not pass %s through to CT %s with pct set --dev0.\n' "$render" "$ctid" >&2 printf 'Keystone will install with Intel support and can fall back to CPU/software decoding.\n' >&2 printf 'Upgrade Proxmox VE for Device Passthrough or configure an unprivileged LXC device mapping for %s with guest render GID %s and mode 0660.\n' "$render" "$render_gid" >&2 printf 'On supported versions, stop the container and run: pct set %s --dev0 %s,gid=%s,mode=0660\n' "$ctid" "$render" "$render_gid" >&2 elif [ -n "$card_path" ]; then pct set "$ctid" --dev1 "$card_path,gid=$card_gid,mode=0660" || printf 'Could not pass through optional node %s. Continuing with %s.\n' "$card_path" "$render" >&2 fi pct start "$ctid" } command -v pct >/dev/null || die "Run this on a Proxmox VE host. Inside a container or VM, use install.sh instead." [ "$(id -u)" -eq 0 ] || die "Run this as root." case "$INSTALL_URL" in http://* | https://*) ;; *) die "Invalid KEYSTONE_INSTALL_URL: '$INSTALL_URL'." ;; esac ctid=${KEYSTONE_CTID:-$(pvesh get /cluster/nextid)} storage=${KEYSTONE_STORAGE:-$(first_storage rootdir local-lvm)} template_storage=$(first_storage vztmpl local) [ -n "$storage" ] || die "No active storage holds containers. Set KEYSTONE_STORAGE." [ -n "$template_storage" ] || die "No active storage holds container templates." installer="" setup_env="" created_ct=0 cleanup() { rm -f "${installer:-}" "${setup_env:-}" if [ "$created_ct" = 1 ]; then pct exec "$ctid" -- rm -f /root/.keystone-setup.env >/dev/null 2>&1 || true fi } trap cleanup EXIT installer=$(mktemp) if [ -n "${KEYSTONE_INSTALL_SCRIPT:-}" ]; then cp "$KEYSTONE_INSTALL_SCRIPT" "$installer" else curl -fsSL "$INSTALL_URL" -o "$installer" fi # Verify the downloaded installer supports library mode before sourcing it on the host. grep -q 'KEYSTONE_INSTALL_LIB_ONLY' "$installer" || die "Downloaded install.sh does not match this proxmox-lxc.sh version. Set KEYSTONE_INSTALL_URL or KEYSTONE_INSTALL_SCRIPT to a matching install.sh." # Reuse install.sh's shared setup wizard so prompts, defaults, and validation stay in one place. KEYSTONE_INSTALL_LIB_ONLY=1 # shellcheck source=scripts/install.sh source "$installer" unset KEYSTONE_INSTALL_LIB_ONLY declare -F keystone_resolve_accel >/dev/null || die "Downloaded install.sh does not support KEYSTONE_ACCEL. Use a matching install.sh." keystone_collect_setup python3 default_memory=4096 if [ "$faces_enabled" = true ] || [ "$aiport_enabled" = true ]; then default_memory=6144 fi memory=${KEYSTONE_MEMORY:-$default_memory} case "$memory" in '' | *[!0-9]*) die "KEYSTONE_MEMORY must be a positive integer in MiB (got '$memory')." ;; esac say "Downloading the Debian 13 template" pveam update >/dev/null template=$(pveam available --section system | awk '{print $2}' | grep -E "^debian-13-standard_.*_$(dpkg --print-architecture)\.tar\.zst$" | sort -V | tail -n1) [ -n "$template" ] || die "Proxmox's template list has no Debian 13 image." pveam list "$template_storage" | grep -q "$template" || pveam download "$template_storage" "$template" >/dev/null say "Creating container $ctid on $storage (${memory} MiB RAM)" net="name=eth0,bridge=${KEYSTONE_BRIDGE:-vmbr0},ip=${KEYSTONE_IP:-dhcp}" net+="${KEYSTONE_GATEWAY:+,gw=$KEYSTONE_GATEWAY}${KEYSTONE_VLAN:+,tag=$KEYSTONE_VLAN}" pct create "$ctid" "$template_storage:vztmpl/$template" \ --hostname keystone --unprivileged 1 --features nesting=1 --onboot 1 \ --cores 2 --memory "$memory" --swap 512 --rootfs "$storage:16" --net0 "$net" \ --tags keystone --description "Keystone, a virtual AI Key and AI Port for UniFi Protect: https://github.com/seeARMS/keystone" >/dev/null created_ct=1 pct start "$ctid" if [ "$accel_mode" = intel ]; then if [ -n "$accel_render_node" ]; then say "Mapping the Intel GPU into container $ctid" configure_intel_passthrough "$accel_render_node" else printf 'No Intel GPU render node detected on the Proxmox host. Enable the host GPU driver and configure device passthrough for GPU acceleration.\n' >&2 fi fi for _ in $(seq 30); do pct exec "$ctid" -- getent hosts deb.debian.org >/dev/null 2>&1 && break sleep 2 done pct exec "$ctid" -- getent hosts deb.debian.org >/dev/null 2>&1 || die "Container $ctid has no network. Check its bridge, VLAN and DHCP, then run install.sh inside it." say "Installing Keystone in container $ctid" pct push "$ctid" "$installer" /root/keystone-install.sh --perms 0755 setup_env=$(umask 077 && mktemp) { printf 'export KEYSTONE_CONTROLLER_IP=%q\n' "$controller" printf 'export KEYSTONE_VISION_PROVIDER=%q\n' "$vision_provider" printf 'export KEYSTONE_VISION_BASE_URL=%q\n' "$vision_url" printf 'export KEYSTONE_VISION_MODEL=%q\n' "$vision_model" printf 'export KEYSTONE_VISION_API_KEY=%q\n' "$vision_api_key" printf 'export KEYSTONE_STT_ENABLED=%q\n' "$stt_enabled" printf 'export KEYSTONE_LPR_ENABLED=%q\n' "$lpr_enabled" printf 'export KEYSTONE_ENHANCE_ENABLED=%q\n' "$enhance_enabled" printf 'export KEYSTONE_FACES_ENABLED=%q\n' "$faces_enabled" printf 'export KEYSTONE_AIPORT_ENABLED=%q\n' "$aiport_enabled" printf 'export KEYSTONE_ACCEL=%q\n' "$accel_mode" if [ "$accel_mode" = intel ] && [ -n "$accel_render_node" ]; then printf 'export KEYSTONE_ACCEL_FFMPEG_DEVICE=%q\n' "$accel_render_node" fi if [ -n "${web_bind_host:-}" ]; then printf 'export KEYSTONE_WEB_BIND_HOST=%q\n' "$web_bind_host" fi if [ -n "${KEYSTONE_REF:-}" ]; then printf 'export KEYSTONE_REF=%q\n' "$KEYSTONE_REF" fi } >"$setup_env" pct push "$ctid" "$setup_env" /root/.keystone-setup.env --perms 0600 rm -f "$setup_env" "$installer" setup_env="" installer="" pct exec "$ctid" -- env KEYSTONE_SETUP_ENV=/root/.keystone-setup.env bash /root/keystone-install.sh echo "To update Keystone later, open the container (pct enter $ctid) and run the install.sh one-liner from the README."