forked from cimryan/teslausb
-
Notifications
You must be signed in to change notification settings - Fork 400
Add NFS Archiving Support #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6b1e285
Add NFS reachability check & Create File
Scottmg1 c2f6cbf
Add NFS configuration script & Create File
Scottmg1 6e4f2fe
Create connect-archive.sh
Scottmg1 4673303
Add NFS disconnect script & Create File
Scottmg1 8cb1223
Add NFS archive loop script & Create File
Scottmg1 15db9cd
Add NFS music copy script
Scottmg1 60a7ff0
Add NFS to the config check
Scottmg1 1488fd8
Add NFS to the module selector
Scottmg1 64333a9
Enable Music Copying for NFS
Scottmg1 00f7c76
Add NFS support to main loop
Scottmg1 deac0df
Add NFS to diagnostics tool
Scottmg1 6b86111
Document NFS variables
Scottmg1 e28c312
Update teslausb_setup_variables.conf.sample
Scottmg1 d01160a
Update verify-and-configure-archive.sh
Scottmg1 505fb6b
Update archiveloop
Scottmg1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| function connectionmonitor { | ||
| while true | ||
| do | ||
| for _ in {1..5} | ||
| do | ||
| if timeout 6 /root/bin/archive-is-reachable.sh "$ARCHIVE_SERVER" | ||
| then | ||
| sleep 5 | ||
| continue 2 | ||
| fi | ||
| sleep 1 | ||
| done | ||
| log "connection dead, killing archive-clips" | ||
| killall rsync || true | ||
| sleep 2 | ||
| killall -9 rsync || true | ||
| kill -9 "$1" || true | ||
| return | ||
| done | ||
| } | ||
|
|
||
| connectionmonitor $$ & | ||
|
|
||
| rsynctmp=".teslausbtmp" | ||
| rm -rf "$ARCHIVE_MOUNT/${rsynctmp:?}" || true | ||
| mkdir -p "$ARCHIVE_MOUNT/$rsynctmp" | ||
|
|
||
| rm -f /tmp/archive-rsync-cmd.log /tmp/archive-error.log | ||
|
|
||
| while [ -n "${1+x}" ] | ||
| do | ||
| # Using --no-o --no-g to prevent permission errors on NFS root squashed shares | ||
| if ! (rsync -avhRL --no-o --no-g --remove-source-files --temp-dir="$rsynctmp" --no-perms --omit-dir-times --stats \ | ||
| --log-file=/tmp/archive-rsync-cmd.log --ignore-missing-args \ | ||
| --files-from="$2" "$1/" "$ARCHIVE_MOUNT" &> /tmp/rsynclog || [[ "$?" = "24" ]] ) | ||
| then | ||
| cat /tmp/archive-rsync-cmd.log /tmp/rsynclog > /tmp/archive-error.log | ||
| exit 1 | ||
| fi | ||
| shift 2 | ||
| done | ||
|
|
||
| rm -rf "$ARCHIVE_MOUNT/${rsynctmp:?}" || true | ||
| kill %1 || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| ARCHIVE_HOST_NAME="$1" | ||
| nc -z -w 5 "$ARCHIVE_HOST_NAME" 2049 > /dev/null 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| function mount_if_set() { | ||
| local mount_point=$1 | ||
| [ -z "$mount_point" ] || ensure_mountpoint_is_mounted_with_retry "$mount_point" | ||
| } | ||
|
|
||
| mount_if_set "${ARCHIVE_MOUNT:-}" | ||
| mount_if_set "${MUSIC_ARCHIVE_MOUNT:-}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| ensure_music_file_is_mounted | ||
| /root/bin/copy-music.sh | ||
| trim_free_space "$MUSIC_MOUNT" | ||
| unmount_music_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| unmount_if_set() { | ||
| local mount_point=$1 | ||
| if [ -n "$mount_point" ] | ||
| then | ||
| if findmnt --mountpoint "$mount_point" > /dev/null | ||
| then | ||
| if timeout 10 umount -f -l "$mount_point" >> "$LOG_FILE" 2>&1 | ||
| then | ||
| log "Unmounted $mount_point." | ||
| else | ||
| log "Failed to unmount $mount_point." | ||
| fi | ||
| else | ||
| log "$mount_point already unmounted." | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| unmount_if_set "${ARCHIVE_MOUNT:-}" & | ||
| unmount_if_set "${MUSIC_ARCHIVE_MOUNT:-}" & |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| #!/bin/bash -eu | ||
|
|
||
| function log_progress () { | ||
| if declare -F setup_progress > /dev/null | ||
| then | ||
| setup_progress "verify-and-configure-archive: $*" | ||
| return | ||
| fi | ||
| echo "verify-and-configure-archive: $1" | ||
| } | ||
|
|
||
| function check_archive_server_reachable () { | ||
| log_progress "Verifying that the archive server $ARCHIVE_SERVER is reachable..." | ||
| local serverunreachable=false | ||
| local default_interface | ||
| default_interface=$(route | grep "^default" | awk '{print $NF}') | ||
|
|
||
| # Check NFS Port 2049 | ||
| hping3 -c 1 -S -p 2049 "$ARCHIVE_SERVER" 1>/dev/null 2>&1 || | ||
| hping3 -c 1 -S -p 2049 -I "$default_interface" "$ARCHIVE_SERVER" 1>/dev/null 2>&1 || | ||
| serverunreachable=true | ||
|
|
||
| if [ "$serverunreachable" = true ] | ||
| then | ||
| log_progress "STOP: The archive server $ARCHIVE_SERVER is unreachable on port 2049. Try specifying its IP address instead." | ||
| exit 1 | ||
| fi | ||
|
|
||
| log_progress "The archive server is reachable." | ||
| } | ||
|
|
||
| function check_archive_mountable () { | ||
| local test_mount_location="/tmp/archivetestmount" | ||
| local share_path="$1" | ||
| local mode="$2" | ||
|
|
||
| log_progress "Verifying that the archive share is mountable..." | ||
|
|
||
| if [ ! -e "$test_mount_location" ] | ||
| then | ||
| mkdir "$test_mount_location" | ||
| fi | ||
|
|
||
| local mounted=false | ||
|
|
||
| # NFS Mount Command | ||
| # Forced vers=3 for wider NAS compatibility (Unifi, Synology, etc) | ||
| # proto=tcp and nolock help with stability over wifi | ||
| local commandline="mount -t nfs '$ARCHIVE_SERVER:$share_path' '$test_mount_location' -o 'rw,noauto,nolock,proto=tcp,vers=3'" | ||
|
|
||
| log_progress "Trying NFS mount command-line:" | ||
| log_progress "$commandline" | ||
|
|
||
| if eval "$commandline" | ||
| then | ||
| mounted=true | ||
| fi | ||
|
|
||
| if [ "$mounted" = false ] | ||
| then | ||
| log_progress "STOP: unable to mount archive share via NFS" | ||
| exit 1 | ||
| else | ||
| log_progress "The archive share is mountable." | ||
| if [ "$mode" = "rw" ] | ||
| then | ||
| if ! touch "$test_mount_location/testfile" | ||
| then | ||
| log_progress "STOP: archive share is not writeable. Check permissions on NAS." | ||
| umount "$test_mount_location" | ||
| exit 1 | ||
| fi | ||
| rm "$test_mount_location/testfile" | ||
| fi | ||
| fi | ||
|
|
||
| umount "$test_mount_location" | ||
| } | ||
|
|
||
| function install_required_packages () { | ||
| log_progress "Installing/updating required packages if needed" | ||
| apt-get -y --force-yes install hping3 nfs-common | ||
| if ! command -v nc > /dev/null | ||
| then | ||
| apt-get -y --force-yes install netcat || apt-get -y --force-yes install netcat-openbsd | ||
| fi | ||
| log_progress "Done" | ||
| } | ||
|
|
||
| install_required_packages | ||
|
|
||
| check_archive_server_reachable | ||
|
|
||
| if [ -e /backingfiles/cam_disk.bin ] | ||
| then | ||
| check_archive_mountable "$SHARE_NAME" rw | ||
| fi | ||
|
|
||
| if [ -n "${MUSIC_SHARE_NAME:+x}" ] | ||
| then | ||
| if [ "$MUSIC_SIZE" = "0" ] | ||
| then | ||
| log_progress "STOP: MUSIC_SHARE_NAME specified but no music drive size specified" | ||
| exit 1 | ||
| fi | ||
| check_archive_mountable "$MUSIC_SHARE_NAME" ro | ||
| fi | ||
|
|
||
| function configure_archive () { | ||
| log_progress "Configuring the archive..." | ||
|
|
||
| local archive_path="/mnt/archive" | ||
| local music_archive_path="/mnt/musicarchive" | ||
|
|
||
| if [ ! -e "$archive_path" ] && [ -e /backingfiles/cam_disk.bin ] | ||
| then | ||
| mkdir "$archive_path" | ||
| fi | ||
|
|
||
| # Clean up old credentials if they exist (not needed for NFS) | ||
Scottmg1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sed -i "/^.*\.teslaCamArchiveCredentials.*$/ d" /etc/fstab | ||
| # Remove existing NFS entries to prevent duplicates | ||
| sed -i "/^.* nfs .*$/ d" /etc/fstab | ||
|
|
||
| if [ -e /backingfiles/cam_disk.bin ] | ||
| then | ||
| echo "$ARCHIVE_SERVER:$SHARE_NAME $archive_path nfs rw,noauto,nolock,proto=tcp,vers=3 0 0" >> /etc/fstab | ||
| elif [ -d "$archive_path" ] | ||
| then | ||
| rmdir "$archive_path" || log_progress "failed to remove $archive_path" | ||
| fi | ||
|
|
||
| if [ -n "${MUSIC_SHARE_NAME:+x}" ] | ||
| then | ||
| if [ ! -e "$music_archive_path" ] | ||
| then | ||
| mkdir "$music_archive_path" | ||
| fi | ||
| echo "$ARCHIVE_SERVER:$MUSIC_SHARE_NAME $music_archive_path nfs ro,noauto,nolock,proto=tcp,vers=3 0 0" >> /etc/fstab | ||
| elif [ -d "$music_archive_path" ] | ||
| then | ||
| rmdir "$music_archive_path" || log_progress "failed to remove $music_archive_path" | ||
| fi | ||
| log_progress "Configured the archive." | ||
| } | ||
|
|
||
| configure_archive | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.