From 146e27fd13c53ca256735cbf1d635ab0d81aeeff Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 23 Jan 2024 19:00:51 +0000 Subject: [PATCH 1/3] install: initial pass at disk setup --- install/docker/setup-scrypted-nvr-volume.sh | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 install/docker/setup-scrypted-nvr-volume.sh diff --git a/install/docker/setup-scrypted-nvr-volume.sh b/install/docker/setup-scrypted-nvr-volume.sh new file mode 100644 index 000000000..7286d0765 --- /dev/null +++ b/install/docker/setup-scrypted-nvr-volume.sh @@ -0,0 +1,97 @@ + +if [ -z "$SERVICE_USER" ] +then + echo "Scrypted SERVICE_USER environment variable was not specified. NVR Storage can not be configured." + exit 0 +fi + +if [ "$USER" != "root" ] +then + echo "$USER" + echo "This script must be run as sudo or root." + exit 1 +fi + +USER_HOME=$(eval echo ~$SERVICE_USER) +SCRYPTED_HOME=$USER_HOME/.scrypted +DOCKER_COMPOSE_YML=$SCRYPTED_HOME/docker-compose.yml + +if [ ! -f "$DOCKER_COMPOSE_YML" ] +then + echo "$DOCKER_COMPOSE_YML not found. Install Scrypted first." + exit 1 +fi + + +function readyn() { + while true; do + read -p "$1 (y/n) " yn + case $yn in + [Yy]* ) break;; + [Nn]* ) break;; + * ) echo "Please answer yes or no. (y/n)";; + esac + done +} + +if [ -z "$1" ] +then + lsblk + echo "" + echo "Please run the script with an existing mount path or the 'disk' device to format (e.g. sdx)." + exit 1 +fi + +BLOCK_DEVICE="/dev/$1" +if [ -b "$BLOCK_DEVICE" ] +then + readyn "Format $BLOCK_DEVICE?" + if [ "$yn" == "n" ] + then + exit 1 + fi + + umount "$BLOCK_DEVICE"1 2> /dev/null + umount "$BLOCK_DEVICE"2 2> /dev/null + umount /mnt/scrypted-nvr 2> /dev/null + + set -e + parted "$BLOCK_DEVICE" --script mklabel gpt + parted -a optimal "$BLOCK_DEVICE" mkpart scrypted-nvr "0%" "100%" + set +e + + sync + mkfs -F -t ext4 "$BLOCK_DEVICE"1 + sync + + for attr in $(blkid | grep "$BLOCK_DEVICE") + do + e=$(echo $attr | grep =) + if [ ! -z "$e" ] + then + # echo "$e" + export "$e" + fi + done + if [ -z "$UUID" ] + then + echo "Error parsing disk UUID." + exit 1 + fi + + echo "UUID: $UUID" + set -e + if [ ! -f "/etc/fstab.scrypted-bak" ] + then + cp /etc/fstab /etc/fstab.scrypted-bak + fi + grep -v "scrypted-nvr" /etc/fstab > /tmp/fstab && cp /tmp/fstab /etc/fstab + # ensure newline + sed -i -e '$a\' /etc/fstab + mkdir -p /mnt/scrypted-nvr + echo "PARTLABEL=scrypted-nvr /mnt/scrypted-nvr ext4 defaults 0 0" >> /etc/fstab + mount -a + set +e +else + DIR=$1 +fi From 4e2f3bf2c79dd5c2fa592ed67a2bd43f04936844 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 23 Jan 2024 19:40:16 +0000 Subject: [PATCH 2/3] docker: finish drive setup script --- install/docker/setup-scrypted-nvr-volume.sh | 49 ++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/install/docker/setup-scrypted-nvr-volume.sh b/install/docker/setup-scrypted-nvr-volume.sh index 7286d0765..97601a273 100644 --- a/install/docker/setup-scrypted-nvr-volume.sh +++ b/install/docker/setup-scrypted-nvr-volume.sh @@ -22,6 +22,22 @@ then exit 1 fi +NVR_MOUNT_LINE=$(cat "$DOCKER_COMPOSE_YML" | grep :/nvr) +if [ -z "$NVR_MOUNT_LINE" ] +then + echo "Unexpected contents in $DOCKER_COMPOSE_YML. Rerun the Scrypted docker compose installer." + exit 1 +fi + +function backup() { + BACKUP_FILE="$1".scrypted-bak + if [ ! -f "$BACKUP_FILE" ] + then + cp "$1" "$BACKUP_FILE" + fi +} + +backup "$DOCKER_COMPOSE_YML" function readyn() { while true; do @@ -42,6 +58,15 @@ then exit 1 fi +function stopscrypted() { + cd "$SCRYPTED_HOME" + echo "" + echo "Stopping the Scrypted container. If there are any errors during disk setup, Scrypted will need to be manually restarted with:" + echo "cd $SCRYPTED_HOME && docker compose up -d" + echo "" + docker compose down +} + BLOCK_DEVICE="/dev/$1" if [ -b "$BLOCK_DEVICE" ] then @@ -51,6 +76,8 @@ then exit 1 fi + stopscrypted + umount "$BLOCK_DEVICE"1 2> /dev/null umount "$BLOCK_DEVICE"2 2> /dev/null umount /mnt/scrypted-nvr 2> /dev/null @@ -64,12 +91,12 @@ then mkfs -F -t ext4 "$BLOCK_DEVICE"1 sync + # parse/evaluate blkid line as env vars for attr in $(blkid | grep "$BLOCK_DEVICE") do e=$(echo $attr | grep =) if [ ! -z "$e" ] then - # echo "$e" export "$e" fi done @@ -81,10 +108,7 @@ then echo "UUID: $UUID" set -e - if [ ! -f "/etc/fstab.scrypted-bak" ] - then - cp /etc/fstab /etc/fstab.scrypted-bak - fi + backup "/etc/fstab" grep -v "scrypted-nvr" /etc/fstab > /tmp/fstab && cp /tmp/fstab /etc/fstab # ensure newline sed -i -e '$a\' /etc/fstab @@ -92,6 +116,19 @@ then echo "PARTLABEL=scrypted-nvr /mnt/scrypted-nvr ext4 defaults 0 0" >> /etc/fstab mount -a set +e + + DIR="/mnt/scrypted-nvr" else - DIR=$1 + stopscrypted + + DIR="$1" fi + +ESCAPED_DIR=$(echo "$DIR" | sed s/\\//\\\\\\//g) + +set -e +sed -i s/'^.*:\/nvr'/" - $ESCAPED_DIR:\/nvr"/ "$DOCKER_COMPOSE_YML" +sed -i s/'^.*SCRYPTED_NVR_VOLUME.*$'/" - SCRYPTED_NVR_VOLUME=\/nvr"/ "$DOCKER_COMPOSE_YML" +set +e + +cd "$SCRYPTED_HOME" && docker compose up -d From fa266e9dd1f771a47df1769553da405db01a2077 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 23 Jan 2024 20:10:02 +0000 Subject: [PATCH 3/3] docker: validate the storage directory --- install/docker/setup-scrypted-nvr-volume.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/install/docker/setup-scrypted-nvr-volume.sh b/install/docker/setup-scrypted-nvr-volume.sh index 97601a273..4d9f577d7 100644 --- a/install/docker/setup-scrypted-nvr-volume.sh +++ b/install/docker/setup-scrypted-nvr-volume.sh @@ -119,6 +119,12 @@ then DIR="/mnt/scrypted-nvr" else + if [ ! -d "$1" ] + then + echo "$1 is not a valid directory." + exit 1 + fi + stopscrypted DIR="$1"