From 2915165bd51e19c808434c96254ccb01801a8429 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Mon, 23 Jan 2023 22:19:30 -0300 Subject: [PATCH] Migrate db path to /config --- .gitignore | 3 +- Makefile | 2 +- docker-compose.yml | 2 +- docker/install_deps.sh | 5 +++ .../frigate-prepare/dependencies.d/base | 0 .../s6-overlay/s6-rc.d/frigate-prepare/down | 1 + .../s6-overlay/s6-rc.d/frigate-prepare/finish | 26 ++++++++++++++ .../s6-overlay/s6-rc.d/frigate-prepare/run | 36 +++++++++++++++++++ .../s6-overlay/s6-rc.d/frigate-prepare/type | 1 + .../etc/s6-overlay/s6-rc.d/frigate-prepare/up | 1 + .../frigate/dependencies.d/frigate-prepare | 0 .../etc/s6-overlay/s6-rc.d/frigate/finish | 2 +- .../etc/s6-overlay/s6-rc.d/go2rtc/finish | 2 +- .../etc/s6-overlay/s6-rc.d/log-prepare/finish | 26 ++++++++++++++ .../etc/s6-overlay/s6-rc.d/nginx/finish | 2 +- docs/docs/configuration/advanced.md | 2 +- .../configuration/hardware_acceleration.md | 2 +- docs/docs/configuration/index.md | 4 +-- docs/docs/frigate/installation.md | 36 +++---------------- docs/docs/troubleshooting/faqs.md | 2 +- 20 files changed, 113 insertions(+), 42 deletions(-) create mode 100644 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/dependencies.d/base create mode 100644 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/down create mode 100755 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/finish create mode 100755 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/run create mode 100644 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/type create mode 100644 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/up create mode 100644 docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/dependencies.d/frigate-prepare create mode 100755 docker/rootfs/etc/s6-overlay/s6-rc.d/log-prepare/finish diff --git a/.gitignore b/.gitignore index 200107ae5..33ec9ee24 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ debug .vscode/* !.vscode/launch.json -config/config.yml +config/* +!config/*.example models *.mp4 *.ts diff --git a/Makefile b/Makefile index 506b11247..49281b550 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ push: build docker buildx build --push --platform linux/amd64 --target=frigate-tensorrt --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH)-tensorrt . run: local - docker run --rm --publish=5000:5000 --volume=${PWD}/config/config.yml:/config/config.yml frigate:latest + docker run --rm --publish=5000:5000 --volume=${PWD}/config:/config frigate:latest run_tests: local docker run --rm --workdir=/opt/frigate --entrypoint= frigate:latest python3 -u -m unittest diff --git a/docker-compose.yml b/docker-compose.yml index e050840ab..cf3613484 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,7 +27,7 @@ services: - .:/workspace/frigate:cached - ./web/dist:/opt/frigate/web:cached - /etc/localtime:/etc/localtime:ro - - ./config/config.yml:/config/config.yml:ro + - ./config:/config - ./debug:/media/frigate # Create the trt-models folder using the documented method of generating TRT models # - ./debug/trt-models:/trt-models diff --git a/docker/install_deps.sh b/docker/install_deps.sh index 422752292..cdcb228fa 100755 --- a/docker/install_deps.sh +++ b/docker/install_deps.sh @@ -86,3 +86,8 @@ apt-get purge gnupg apt-transport-https wget xz-utils -y apt-get clean autoclean -y apt-get autoremove --purge -y rm -rf /var/lib/apt/lists/* + +# Install yq, for frigate-prepare and go2rtc echo source +curl -fsSL \ + "https://github.com/mikefarah/yq/releases/download/v4.30.8/yq_linux_$(dpkg --print-architecture)" \ + --output /usr/local/bin/yq diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/dependencies.d/base b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/dependencies.d/base new file mode 100644 index 000000000..e69de29bb diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/down b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/down new file mode 100644 index 000000000..c67fd290f --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/down @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/frigate-prepare/finish diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/finish b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/finish new file mode 100755 index 000000000..6d13bce11 --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/finish @@ -0,0 +1,26 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Take down the S6 supervision tree when the service exits + +set -o errexit -o nounset -o pipefail + +declare exit_code_container +exit_code_container=$(cat /run/s6-linux-init-container-results/exitcode) +readonly exit_code_container +readonly exit_code_service="${1}" +readonly exit_code_signal="${2}" +readonly service="frigate-prepare" + +echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 + +if [[ "${exit_code_service}" -eq 256 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode + fi +elif [[ "${exit_code_service}" -ne 0 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode + fi +fi + +exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/run b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/run new file mode 100755 index 000000000..9280cc742 --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/run @@ -0,0 +1,36 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +set -o errexit -o nounset -o pipefail + +config_file="${CONFIG_FILE:-"/config/config.yml"}" + +if [[ ! -f "${config_file}" ]]; then + config_file="${config_file//.yml/.yaml}" + if [[ ! -f "${config_file}" ]]; then + echo "[ERROR] Frigate config file not found" >&2 + exit 1 + fi +fi + +readonly config_file + +# Use yq to check if database.path is set +user_db_path=$(yq eval '.database.path' "${config_file}") +readonly user_db_path + +if [[ "${user_db_path}" == "null" ]]; then + readonly previous_db_path="/media/frigate/frigate.db" + readonly new_db_dir="/config" + if [[ -f "${previous_db_path}" ]]; then + if mountpoint --quiet "${new_db_dir}"; then + # /config is a mount point, move the db + echo "[INFO] Moving db from '${previous_db_path}' to the '${new_db_dir}' dir..." >&2 + # Move all files that starts with frigate.db to the new directory + mv -vf "${previous_db_path}"* "${new_db_dir}" + else + echo "[ERROR] Trying to migrate the db path from '${previous_db_path}' to the '${new_db_dir}' dir, but '${new_db_dir}' is not a mountpoint, please mount the '${new_db_dir}' dir" >&2 + exit 1 + fi + fi +fi diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/type b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/type new file mode 100644 index 000000000..bdd22a185 --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/type @@ -0,0 +1 @@ +oneshot diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/up b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/up new file mode 100644 index 000000000..397b0fbf4 --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate-prepare/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/frigate-prepare/run diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/dependencies.d/frigate-prepare b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/dependencies.d/frigate-prepare new file mode 100644 index 000000000..e69de29bb diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/finish b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/finish index d83c1a6e6..86f5ea33f 100755 --- a/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/finish +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/frigate/finish @@ -11,7 +11,7 @@ readonly exit_code_service="${1}" readonly exit_code_signal="${2}" readonly service="Frigate" -echo "Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 +echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 if [[ "${exit_code_service}" -eq 256 ]]; then if [[ "${exit_code_container}" -eq 0 ]]; then diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/finish b/docker/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/finish index 26b34294a..c5fa99114 100755 --- a/docker/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/finish +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/go2rtc/finish @@ -11,7 +11,7 @@ readonly exit_code_service="${1}" readonly exit_code_signal="${2}" readonly service="go2rtc" -echo "Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 +echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 if [[ "${exit_code_service}" -eq 256 ]]; then if [[ "${exit_code_container}" -eq 0 ]]; then diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/log-prepare/finish b/docker/rootfs/etc/s6-overlay/s6-rc.d/log-prepare/finish new file mode 100755 index 000000000..045914a77 --- /dev/null +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/log-prepare/finish @@ -0,0 +1,26 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Take down the S6 supervision tree when the service exits + +set -o errexit -o nounset -o pipefail + +declare exit_code_container +exit_code_container=$(cat /run/s6-linux-init-container-results/exitcode) +readonly exit_code_container +readonly exit_code_service="${1}" +readonly exit_code_signal="${2}" +readonly service="log-prepare" + +echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 + +if [[ "${exit_code_service}" -eq 256 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode + fi +elif [[ "${exit_code_service}" -ne 0 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode + fi +fi + +exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/s6-overlay/s6-rc.d/nginx/finish b/docker/rootfs/etc/s6-overlay/s6-rc.d/nginx/finish index 01c37d192..c28725f62 100755 --- a/docker/rootfs/etc/s6-overlay/s6-rc.d/nginx/finish +++ b/docker/rootfs/etc/s6-overlay/s6-rc.d/nginx/finish @@ -11,7 +11,7 @@ readonly exit_code_service="${1}" readonly exit_code_signal="${2}" readonly service="NGINX" -echo "Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 +echo "[INFO] Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2 if [[ "${exit_code_service}" -eq 256 ]]; then if [[ "${exit_code_container}" -eq 0 ]]; then diff --git a/docs/docs/configuration/advanced.md b/docs/docs/configuration/advanced.md index 7cedfce3c..818c4b9cd 100644 --- a/docs/docs/configuration/advanced.md +++ b/docs/docs/configuration/advanced.md @@ -42,7 +42,7 @@ environment_vars: ### `database` -Event and recording information is managed in a sqlite database at `/media/frigate/frigate.db`. If that database is deleted, recordings will be orphaned and will need to be cleaned up manually. They also won't show up in the Media Browser within Home Assistant. +Event and recording information is managed in a sqlite database at `/config/frigate.db`. If that database is deleted, recordings will be orphaned and will need to be cleaned up manually. They also won't show up in the Media Browser within Home Assistant. If you are storing your database on a network share (SMB, NFS, etc), you may get a `database is locked` error message on startup. You can customize the location of the database in the config if necessary. diff --git a/docs/docs/configuration/hardware_acceleration.md b/docs/docs/configuration/hardware_acceleration.md index 3d1dcff30..7bf2123a9 100644 --- a/docs/docs/configuration/hardware_acceleration.md +++ b/docs/docs/configuration/hardware_acceleration.md @@ -21,7 +21,7 @@ ffmpeg: ffmpeg: hwaccel_args: preset-vaapi ``` -**NOTICE**: With some of the processors, like the J4125, the default driver `iHD` doesn't seem to work correctly for hardware acceleration. You may need to change the driver to `i965` by adding the following environment variable `LIBVA_DRIVER_NAME=i965` to your docker-compose file or [in the frigate.yml for HA OS users](advanced.md#environment_vars). +**NOTICE**: With some of the processors, like the J4125, the default driver `iHD` doesn't seem to work correctly for hardware acceleration. You may need to change the driver to `i965` by adding the following environment variable `LIBVA_DRIVER_NAME=i965` to your docker-compose file or [in the `frigate.yaml` for HA OS users](advanced.md#environment_vars). ### Intel-based CPUs (>=10th Generation) via Quicksync diff --git a/docs/docs/configuration/index.md b/docs/docs/configuration/index.md index b026744a2..f3cc880e3 100644 --- a/docs/docs/configuration/index.md +++ b/docs/docs/configuration/index.md @@ -3,7 +3,7 @@ id: index title: Configuration File --- -For Home Assistant Addon installations, the config file needs to be in the root of your Home Assistant config directory (same location as `configuration.yaml`) and named `frigate.yml`. +For Home Assistant Addon installations, the config file needs to be in the root of your Home Assistant config directory (same location as `configuration.yaml`) and named `frigate.yaml`. For all other installation types, the config file should be mapped to `/config/config.yml` inside the container. @@ -87,7 +87,7 @@ detectors: # Optional: Database configuration database: # The path to store the SQLite DB (default: shown below) - path: /media/frigate/frigate.db + path: /config/frigate.db # Optional: model modifications model: diff --git a/docs/docs/frigate/installation.md b/docs/docs/frigate/installation.md index f5afc37c9..bcaeb4f49 100644 --- a/docs/docs/frigate/installation.md +++ b/docs/docs/frigate/installation.md @@ -21,12 +21,11 @@ Windows is not officially supported, but some users have had success getting it Frigate uses the following locations for read/write operations in the container. Docker volume mappings can be used to map these to any location on your host machine. +- `/config`: Used to store the Frigate config file and sqlite database. You will also see a few files alongside the database file while Frigate is running. - `/media/frigate/clips`: Used for snapshot storage. In the future, it will likely be renamed from `clips` to `snapshots`. The file structure here cannot be modified and isn't intended to be browsed or managed manually. - `/media/frigate/recordings`: Internal system storage for recording segments. The file structure here cannot be modified and isn't intended to be browsed or managed manually. -- `/media/frigate/frigate.db`: Default location for the sqlite database. You will also see several files alongside this file while Frigate is running. If moving the database location (often needed when using a network drive at `/media/frigate`), it is recommended to mount a volume with docker at `/db` and change the storage location of the database to `/db/frigate.db` in the config file. - `/tmp/cache`: Cache location for recording segments. Initial recordings are written here before being checked and converted to mp4 and moved to the recordings folder. - `/dev/shm`: It is not recommended to modify this directory or map it with docker. This is the location for raw decoded frames in shared memory and it's size is impacted by the `shm-size` calculations below. -- `/config/config.yml`: Default location of the config file. #### Common docker compose storage configurations @@ -38,7 +37,7 @@ services: frigate: ... volumes: - - /path/to/your/config.yml:/config/config.yml + - /path/to/your/config:/config - /path/to/your/storage:/media/frigate - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear target: /tmp/cache @@ -47,31 +46,6 @@ services: ... ``` -Writing to a network drive with database on a local drive: - -```yaml -version: "3.9" -services: - frigate: - ... - volumes: - - /path/to/your/config.yml:/config/config.yml - - /path/to/network/storage:/media/frigate - - /path/to/local/disk:/db - - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear - target: /tmp/cache - tmpfs: - size: 1000000000 - ... -``` - -frigate.yml - -```yaml -database: - path: /db/frigate.db -``` - ### Calculating required shm-size Frigate utilizes shared memory to store frames during processing. The default `shm-size` provided by Docker is **64MB**. @@ -123,7 +97,7 @@ services: - /dev/dri/renderD128 # for intel hwaccel, needs to be updated for your hardware volumes: - /etc/localtime:/etc/localtime:ro - - /path/to/your/config.yml:/config/config.yml + - /path/to/your/config:/config - /path/to/your/storage:/media/frigate - type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear target: /tmp/cache @@ -149,7 +123,7 @@ docker run -d \ --device /dev/dri/renderD128 \ --shm-size=64m \ -v /path/to/your/storage:/media/frigate \ - -v /path/to/your/config.yml:/config/config.yml \ + -v /path/to/your/config:/config \ -v /etc/localtime:/etc/localtime:ro \ -e FRIGATE_RTSP_PASSWORD='password' \ -p 5000:5000 \ @@ -179,7 +153,7 @@ HassOS users can install via the addon repository. 2. Add https://github.com/blakeblackshear/frigate-hass-addons 3. Install your desired Frigate NVR Addon and navigate to it's page 4. Setup your network configuration in the `Configuration` tab -5. (not for proxy addon) Create the file `frigate.yml` in your `config` directory with your detailed Frigate configuration +5. (not for proxy addon) Create the file `frigate.yaml` in your `config` directory with your detailed Frigate configuration 6. Start the addon container 7. (not for proxy addon) If you are using hardware acceleration for ffmpeg, you may need to disable "Protection mode" diff --git a/docs/docs/troubleshooting/faqs.md b/docs/docs/troubleshooting/faqs.md index 15ce9bc12..a3a703ec3 100644 --- a/docs/docs/troubleshooting/faqs.md +++ b/docs/docs/troubleshooting/faqs.md @@ -11,7 +11,7 @@ This error message is due to a shm-size that is too small. Try updating your shm By default, Frigate removes audio from recordings to reduce the likelihood of failing for invalid data. If you would like to include audio, you need to set a [FFmpeg preset](/configuration/ffmpeg_presets) that supports audio: -```yaml title="frigate.yml" +```yaml ffmpeg: output_args: record: preset-record-generic-audio-aac