Files
frigate/install_frigate.sh
2025-12-15 18:20:40 -08:00

174 lines
5.4 KiB
Bash

#!/bin/bash
# ==========================================
# Frigate Installer for Existing Podman LXC
# ==========================================
set -e
# --- Configuration Defaults ---
CT_ID_DEFAULT="110"
# --- Colors ---
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== Frigate Installer (Existing Podman LXC) ===${NC}"
# 1. Select Existing Container
read -p "Enter Target Container ID [${CT_ID_DEFAULT}]: " CT_ID
CT_ID=${CT_ID:-$CT_ID_DEFAULT}
# Check if ID exists
if ! pct status $CT_ID &>/dev/null; then
echo -e "${RED}Error: Container $CT_ID does not exist.${NC}"
exit 1
fi
echo -e "${GREEN}Found Container $CT_ID.${NC}"
# 2. Check LXC Configuration (Nesting & Keyctl)
# Podman requires nesting=1. We check the config file on the host.
CONFIG_FILE="/etc/pve/lxc/${CT_ID}.conf"
NEEDS_RESTART=false
if ! grep -q "features:.*nesting=1" "$CONFIG_FILE"; then
echo -e "${YELLOW}Warning: 'nesting' feature is missing (required for Podman).${NC}"
read -p "Enable nesting and reboot container? (y/n) [y]: " ENABLE_NESTING
ENABLE_NESTING=${ENABLE_NESTING:-y}
if [[ "$ENABLE_NESTING" =~ ^[Yy]$ ]]; then
# Append or modify features
if grep -q "^features:" "$CONFIG_FILE"; then
sed -i 's/^features: /features: nesting=1,/' "$CONFIG_FILE"
else
echo "features: nesting=1" >> "$CONFIG_FILE"
fi
echo -e "${GREEN}Nesting enabled.${NC}"
NEEDS_RESTART=true
fi
fi
# 3. Hardware Acceleration Check (Intel iGPU)
IGPU_DETECTED=false
if [ -e /dev/dri/renderD128 ]; then
# Check if already passed through
if grep -q "dev/dri/renderD128" "$CONFIG_FILE"; then
echo -e "${GREEN}iGPU passthrough detected in config.${NC}"
IGPU_DETECTED=true
else
echo -e "${YELLOW}Intel iGPU detected on host, but not passed to LXC.${NC}"
read -p "Pass-through iGPU to this container? (y/n) [y]: " PASSTHROUGH_CONFIRM
PASSTHROUGH_CONFIRM=${PASSTHROUGH_CONFIRM:-y}
if [[ "$PASSTHROUGH_CONFIRM" =~ ^[Yy]$ ]]; then
echo -e "${GREEN}Adding iGPU passthrough to config...${NC}"
cat <<EOF >> "$CONFIG_FILE"
# Hardware Acceleration Pass-through
lxc.cgroup2.devices.allow: c 226:128 rwm
lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file
EOF
chmod 666 /dev/dri/renderD128
IGPU_DETECTED=true
NEEDS_RESTART=true
fi
fi
fi
# 4. Handle Restart if Config Changed
if [ "$NEEDS_RESTART" = true ]; then
echo -e "${YELLOW}Container configuration changed. Rebooting container...${NC}"
pct stop $CT_ID && pct start $CT_ID
echo -e "${YELLOW}Waiting for container to come back online...${NC}"
sleep 5
# Wait for network
while ! pct exec $CT_ID -- ping -c 1 8.8.8.8 &> /dev/null; do
sleep 1
done
fi
# Ensure container is running if we didn't reboot
if [ "$(pct status $CT_ID | awk '{print $2}')" != "running" ]; then
echo -e "${GREEN}Starting Container...${NC}"
pct start $CT_ID
sleep 5
fi
# 5. Install Podman inside LXC
echo -e "${GREEN}Checking/Installing Podman inside Container...${NC}"
pct exec $CT_ID -- bash -c "apt-get update"
pct exec $CT_ID -- bash -c "apt-get install -y podman podman-compose"
# 6. Configure Frigate Directories
echo -e "${GREEN}Setting up Frigate Configuration...${NC}"
pct exec $CT_ID -- mkdir -p /opt/frigate/config
pct exec $CT_ID -- mkdir -p /opt/frigate/storage
# Create minimal config.yml
pct exec $CT_ID -- bash -c "cat > /opt/frigate/config/config.yml <<EOF
mqtt:
enabled: False
cameras:
test_camera:
ffmpeg:
inputs:
- path: /media/frigate/clips/test.mp4
roles:
- detect
- rtmp
detect:
enabled: False
EOF"
# 7. Create Docker Compose (Podman Compatible)
# Note: Podman handles SHM differently, but podman-compose tries to mimic docker behavior.
# We set privileged: true to ensure easy access to /dev/dri if passed through.
RENDER_LINE=""
if [ "$IGPU_DETECTED" = true ]; then
RENDER_LINE=" - /dev/dri/renderD128:/dev/dri/renderD128"
fi
pct exec $CT_ID -- bash -c "cat > /opt/frigate/docker-compose.yml <<EOF
version: \"3.9\"
services:
frigate:
container_name: frigate
privileged: true
restart: unless-stopped
image: ghcr.io/blakeblackshear/frigate:stable
shm_size: \"128mb\"
devices:
$RENDER_LINE
# - /dev/bus/usb:/dev/bus/usb
volumes:
- /etc/localtime:/etc/localtime:ro
- /opt/frigate/config:/config
- /opt/frigate/storage:/media/frigate
- type: tmpfs
target: /tmp/cache
tmpfs:
size: 1000000000
ports:
- \"5000:5000\"
- \"8554:8554\"
- \"8555:8555/tcp\"
- \"8555:8555/udp\"
EOF"
# 8. Start Frigate using Podman Compose
echo -e "${GREEN}Starting Frigate with Podman...${NC}"
# We use nohup or detach so the script doesn't hang waiting for output
pct exec $CT_ID -- bash -c "cd /opt/frigate && podman-compose up -d"
# 9. Finish
IP_ADDR=$(pct exec $CT_ID -- ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
echo -e "=========================================="
echo -e "${GREEN}Installation Complete!${NC}"
echo -e "Target Container: ${CT_ID}"
echo -e "Frigate URL: ${YELLOW}http://${IP_ADDR}:5000${NC}"
echo -e "Engine: Podman (managed via podman-compose)"
echo -e "=========================================="