From 858ae5da3900b0181a0464bbb83bf4fd56c96490 Mon Sep 17 00:00:00 2001 From: cimryan Date: Thu, 18 Oct 2018 20:45:48 -0700 Subject: [PATCH 01/16] Fix errors on both the cam and the music filesystems before the archive loop starts. --- windows_archive/archive-teslacam-clips | 87 +--------------- windows_archive/archiveloop | 133 +++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 83 deletions(-) diff --git a/windows_archive/archive-teslacam-clips b/windows_archive/archive-teslacam-clips index c6be29a..8b2d793 100644 --- a/windows_archive/archive-teslacam-clips +++ b/windows_archive/archive-teslacam-clips @@ -1,7 +1,6 @@ #!/bin/bash -eu LOG_FILE=/tmp/archive-teslacam-clips.log -CAM_MOUNT=/mnt/cam ARCHIVE_MOUNT=/mnt/archive function log () { @@ -9,66 +8,6 @@ function log () { echo "$1" >> "$LOG_FILE" } -function retry () { - local attempts=0 - while [ true ] - do - if eval "$@" - then - true - return - fi - if [ "$attempts" -ge 10 ] - then - log "Attempts exhausted." - false - return - fi - log "Sleeping before retry..." - /bin/sleep 1 - attempts=$((attempts + 1)) - log "Retrying..." - done - false - return -} - -function mount_mountpoint () { - local mount_point="$1" - log "Mounting $mount_point..." - - local mounted=true - mount "$mount_point" >> "$LOG_FILE" 2>&1 || mounted=false - if [ "$mounted" = true ] - then - log "Mounted $mount_point." - true - return - else - log "Failed to mount $mount_point." - false - return - fi -} - -function ensure_mountpoint_is_mounted () { - local mount_point="$1" - local mount_exists=true - - findmnt --mountpoint "$mount_point" > /dev/null || mount_exists=false - - if [ "$mount_exists" = true ] - then - log "$mount_point is already mounted." - else - mount_mountpoint "$mount_point" - fi -} - -function ensure_mountpoint_is_mounted_with_retry () { - retry ensure_mountpoint_is_mounted "$1" -} - function move_clips_to_archive () { log "Moving clips to archive..." local move_count=0 @@ -95,42 +34,24 @@ function disconnect_usb_drives_from_host () { log "Disconnected usb from host." } -function fix_errors_on_cam_drive () { - log "Running fsck..." - /sbin/fsck "$CAM_MOUNT" -- -a >> "$LOG_FILE" 2>&1 || echo "" - log "Finished running fsck." -} - function ensure_archive_is_mounted () { log "Ensuring cam archive is mounted..." ensure_mountpoint_is_mounted_with_retry "$ARCHIVE_MOUNT" log "Ensured cam archive is mounted." } -function ensure_cam_drive_is_mounted () { - log "Ensuring cam drive is mounted..." - ensure_mountpoint_is_mounted_with_retry "$CAM_MOUNT" - log "Ensured cam drive is mounted." -} - -function unmount_cam_drive () { - log "Unmounting cam drive..." - umount "$CAM_MOUNT" - log "Unmounted cam drive." -} - log "Starting..." ensure_archive_is_mounted disconnect_usb_drives_from_host -fix_errors_on_cam_drive +ensure_cam_file_is_mounted -ensure_cam_drive_is_mounted +fix_errors_in_cam_file move_clips_to_archive -unmount_cam_drive +unmount_cam_file -connect_usb_drives_to_host \ No newline at end of file +connect_usb_drives_to_host diff --git a/windows_archive/archiveloop b/windows_archive/archiveloop index 09f276d..4dd01c7 100644 --- a/windows_archive/archiveloop +++ b/windows_archive/archiveloop @@ -2,13 +2,29 @@ # Change the value on the right side of the equal sign to the name of the server hosting the archive. ARCHIVE_HOST_NAME=archiveserver + LOGFILE=/tmp/archiveloop.log +export CAM_MOUNT=/mnt/cam +export MUSIC_MOUNT=/mnt/music + function log () { echo "$( date )" >> "$LOGFILE" echo "$1" >> "$LOGFILE" } +function fix_errors_in_mount_point () { + local mount_point="$1" + log "Running fsck on $mount_point..." + /sbin/fsck "$mount_point" -- -a >> "$LOG_FILE" 2>&1 || echo "" + log "Finished fsck on $mount_point." +} + +function fix_errors_in_mounted_files () { + fix_errors_in_mount_point "$CAM_MOUNT" + fix_errors_in_mount_point "$MUSIC_MOUNT" +} + function archive_is_reachable () { local reachable=true ping -q -w 1 -c 1 "$ARCHIVE_HOST_NAME" > /dev/null 2>&1 || reachable=false @@ -39,6 +55,105 @@ function wait_for_archive_to_be_reachable () { done } +function retry () { + local attempts=0 + while [ true ] + do + if eval "$@" + then + true + return + fi + if [ "$attempts" -ge 10 ] + then + log "Attempts exhausted." + false + return + fi + log "Sleeping before retry..." + /bin/sleep 1 + attempts=$((attempts + 1)) + log "Retrying..." + done + false + return +} + +function mount_mountpoint () { + local mount_point="$1" + log "Mounting $mount_point..." + + local mounted=true + mount "$mount_point" >> "$LOG_FILE" 2>&1 || mounted=false + if [ "$mounted" = true ] + then + log "Mounted $mount_point." + true + return + else + log "Failed to mount $mount_point." + false + return + fi +} + +function ensure_mountpoint_is_mounted () { + local mount_point="$1" + local mount_exists=true + + findmnt --mountpoint "$mount_point" > /dev/null || mount_exists=false + + if [ "$mount_exists" = true ] + then + log "$mount_point is already mounted." + else + mount_mountpoint "$mount_point" + fi +} + +function ensure_mountpoint_is_mounted_with_retry () { + retry ensure_mountpoint_is_mounted "$1" +} + +function fix_errors_in_cam_file () { + ensure_cam_file_is_mounted + fix_errors_in_mount_point "$CAM_MOUNT" + unmount_cam_file +} + +function ensure_cam_file_is_mounted () { + log "Ensuring cam file is mounted..." + ensure_mountpoint_is_mounted_with_retry "$CAM_MOUNT" + log "Ensured cam file is mounted." +} + +function ensure_music_file_is_mounted () { + log "Ensuring music backing file is mounted..." + ensure_mountpoint_is_mounted_with_retry "$MUSIC_MOUNT" + log "Ensured cam drive is mounted." +} + +function unmount_mount_point () { + local mount_point="$1" + log "Unmounting $mount_point..." + umount "$mount_point" + log "Unmounted $mount_point." +} + +function unmount_cam_file () { + unmount_mount_point "$CAM_MOUNT" +} + +function unmount_music_file () { + unmount_mount_point "$MUSIC_MOUNT" +} + +function fix_errors_in_music_file () { + ensure_music_file_is_mounted + fix_errors_in_mount_point "$MUSIC_MOUNT" + unmount_music_file +} + function archive_clips () { log "Archiving..." /root/bin/archive-teslacam-clips @@ -58,16 +173,34 @@ function wait_for_archive_to_be_unreachable () { done } +function fix_errors_in_files () { + fix_errors_in_cam_file + fix_errors_in_music_file +} + +export -f fix_errors_in_cam_file +export -f retry +export -f mount_mountpoint +export -f ensure_mountpoint_is_mounted +export -f ensure_mountpoint_is_mounted_with_retry +export -f ensure_cam_file_is_mounted +export -f fix_errors_in_cam_file +export -f unmount_cam_file export -f connect_usb_drives_to_host log "Starting..." if archive_is_reachable then + # archive_clips will fix errors in the cam file + fix_errors_in_music_file + archive_clips wait_for_archive_to_be_unreachable else + fix_errors_in_files + connect_usb_drives_to_host fi From 5788bc4f7b612c24c6e58dd59f8e2331dbd5fc15 Mon Sep 17 00:00:00 2001 From: cimryan Date: Fri, 19 Oct 2018 14:26:10 -0700 Subject: [PATCH 02/16] Verify that the archive is mountable before making changes to the file systems. --- windows_archive/setup-teslausb | 66 ++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 5910eab..ef965ce 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -56,6 +56,38 @@ function check_archive_server_reachable () { echo "The archive server is reachable." } +function write_archive_credentials_to () { + local file_path="$1" + + echo "username=$shareuser" > "$file_path" + echo "password=$sharepassword" >> "$file_path" + +} + +function check_archive_mountable () { + local archive_server_ip_address="$1" + + local test_mount_location="/tmp/archivetestmount" + + if [ ! -e "$test_mount_location" ] + then + mkdir "$test_mount_location" + fi + + local tmp_credentials_file_path="/tmp/teslaCamArchiveCredentials" + + write_archive_credentials_to "$tmp_credentials_file_path" + + local mount_failed=false + mount -t cifs "//$archive_server_ip_address/$sharename" "$test_mount_location" -o "vers=${cifs_version},credentials=${tmp_credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777" || mount_failed=true + + if [ "$mount_failed" = true ] + then + echo "STOP: The archive couldn't be mounted with CIFS version ${cifs_version}. Try specifying a lower number for the CIFS version like this: export cifs_version=2" + exit 1 + fi +} + function check_available_space () { echo "Verifying that there is sufficient space available on the MicroSD card..." @@ -70,6 +102,7 @@ function check_available_space () { echo "There is sufficient space available." } + function get_ancillary_setup_scripts () { pushd /tmp wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh @@ -104,13 +137,23 @@ function create_usb_drive_backing_files () { } function configure_archive () { - echo "Configuring the archive..." - mkdir /mnt/archive - local archive_server_ip_address="$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" - echo "//$archive_server_ip_address/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab + local archive_server_ip_address="$1" - echo "username=$shareuser" > /root/.teslaCamArchiveCredentials - echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials + echo "Configuring the archive..." + + local archive_path="/mnt/archive" + + if [ ! -e "$archive_path" ] + then + mkdir "$archive_path" + fi + + local credentials_file_path="/root/.teslaCamArchiveCredentials" + + write_archive_credentials_to "$credentials_file_path" + + echo "//$archive_server_ip_address/$sharename $archive_path cifs vers=${cifs_version},credentials=${credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab + echo "Configured the archive." } @@ -190,6 +233,11 @@ function make_root_fs_readonly () { echo "Verifying environment variables..." +if [ ! -n "${cifs_version+x}" ] +then + cifs_version=3 +fi + check_variable "archiveserver" check_variable "sharename" check_variable "shareuser" @@ -200,6 +248,10 @@ check_pushover_enabled check_archive_server_reachable +ARCHIVE_SERVER_IP_ADDRESS="$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" + +check_archive_mountable "$ARCHIVE_SERVER_IP_ADDRESS" + check_available_space get_ancillary_setup_scripts @@ -216,7 +268,7 @@ echo "" >> /etc/fstab create_usb_drive_backing_files -configure_archive +configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" configure_rc_local From edc5c47513d3a58e2e23add5f72fad742c682480 Mon Sep 17 00:00:00 2001 From: cimryan Date: Sun, 21 Oct 2018 11:21:25 -0700 Subject: [PATCH 03/16] Fix references to setup-piForHeadlessConfig.sh --- GetShellWithoutMonitorOnLinux.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GetShellWithoutMonitorOnLinux.md b/GetShellWithoutMonitorOnLinux.md index 88d0522..2013114 100644 --- a/GetShellWithoutMonitorOnLinux.md +++ b/GetShellWithoutMonitorOnLinux.md @@ -20,7 +20,7 @@ If you prefer not to run the script, it's also a useful reference for the steps 1. Run the following commands: ``` wget https://raw.githubusercontent.com/cimryan/teslausb/master/mac_linux_archive/setup-piForHeadlessConfig.sh - chmod +x update-rpi-mac-linux.sh + chmod +x setup-piForHeadlessConfig.sh ``` 1. Set your SSID (Wifi network name) and WIFIPASS environment variables. The script will insert them into the `wpa_supplicant.conf` when creating it: @@ -29,7 +29,7 @@ If you prefer not to run the script, it's also a useful reference for the steps export WIFIPASS=your_wifi_password_here ``` 1. Run the script: - `./update-rpi-mac-linux.sh` + `./setup-piForHeadlessConfig.sh` 1. If all goes well, the script will report: `-- Files updated and ready for Wifi and SSH over USB --` 1. Eject the SD card safely, insert into your Pi, and reboot. If the Pi is connected over USB to your host, and/or if the Wifi setup went correctly, you should be able to `ssh pi@raspberrypi.local`. The default password is `raspberry`. From 10ce08485112a06b428d1980638734f5feb2855d Mon Sep 17 00:00:00 2001 From: cimryan Date: Mon, 22 Oct 2018 19:42:54 -0700 Subject: [PATCH 04/16] Eliminate BOM from wpa_supplicant.conf --- windows_archive/setup-piForHeadlessConfig.ps1 | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/windows_archive/setup-piForHeadlessConfig.ps1 b/windows_archive/setup-piForHeadlessConfig.ps1 index 972bbbc..d2a1eea 100644 --- a/windows_archive/setup-piForHeadlessConfig.ps1 +++ b/windows_archive/setup-piForHeadlessConfig.ps1 @@ -44,14 +44,18 @@ if ([System.IO.File]::Exists("$wpaSupplicantConfPath")) { del "$wpaSupplicantConfPath" } -"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev` -update_config=1` -` -network={` - ssid=`"$wifiSSID`"` - psk=`"$wifiPSK`"` - key_mgmt=WPA-PSK` -}` -" | Out-File -FilePath "$wpaSupplicantConfPath" -Encoding utf8 +$wpaSupplicantConfContent=@" +ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev +update_config=1 + +network={ + ssid="$wifiSSID" + psk="$wifiPSK" +} +"@ + +$utf8 = New-Object System.Text.UTF8Encoding $false + +Set-Content -Value $utf8.GetBytes($wpaSupplicantConfContent) -Encoding Byte -Path "$wpaSupplicantConfPath" Write-Verbose "All done." \ No newline at end of file From 71612c46ef3a2b9843810ac2404f1a0931cbd5a0 Mon Sep 17 00:00:00 2001 From: cimryan Date: Mon, 22 Oct 2018 21:48:27 -0700 Subject: [PATCH 05/16] Fix errors. --- windows_archive/archiveloop | 48 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/windows_archive/archiveloop b/windows_archive/archiveloop index 4dd01c7..a7f16b0 100644 --- a/windows_archive/archiveloop +++ b/windows_archive/archiveloop @@ -3,14 +3,14 @@ # Change the value on the right side of the equal sign to the name of the server hosting the archive. ARCHIVE_HOST_NAME=archiveserver -LOGFILE=/tmp/archiveloop.log +LOG_FILE=/tmp/archiveloop.log export CAM_MOUNT=/mnt/cam export MUSIC_MOUNT=/mnt/music function log () { - echo "$( date )" >> "$LOGFILE" - echo "$1" >> "$LOGFILE" + echo "$( date )" >> "$LOG_FILE" + echo "$1" >> "$LOG_FILE" } function fix_errors_in_mount_point () { @@ -51,6 +51,12 @@ function wait_for_archive_to_be_reachable () { log "Archive is reachable." break fi + if [ -e /tmp/archive_is_reachable ] + then + log "Simulating archive is reachable" + rm /tmp/archive_is_reachable + break + fi sleep 1 done } @@ -116,9 +122,7 @@ function ensure_mountpoint_is_mounted_with_retry () { } function fix_errors_in_cam_file () { - ensure_cam_file_is_mounted fix_errors_in_mount_point "$CAM_MOUNT" - unmount_cam_file } function ensure_cam_file_is_mounted () { @@ -136,7 +140,7 @@ function ensure_music_file_is_mounted () { function unmount_mount_point () { local mount_point="$1" log "Unmounting $mount_point..." - umount "$mount_point" + umount "$mount_point" >> "$LOG_FILE" 2>&1 log "Unmounted $mount_point." } @@ -149,9 +153,7 @@ function unmount_music_file () { } function fix_errors_in_music_file () { - ensure_music_file_is_mounted fix_errors_in_mount_point "$MUSIC_MOUNT" - unmount_music_file } function archive_clips () { @@ -169,15 +171,34 @@ function wait_for_archive_to_be_unreachable () { log "Archive is unreachable." break fi + if [ -e /tmp/archive_is_unreachable ] + then + log "Simulating archive being unreachable." + rm /tmp/archive_is_unreachable + break + fi sleep 1 done } -function fix_errors_in_files () { +function mount_and_fix_errors_in_cam_file () { + ensure_cam_file_is_mounted fix_errors_in_cam_file - fix_errors_in_music_file + unmount_cam_file } +function mount_and_fix_errors_in_music_file () { + ensure_music_file_is_mounted + fix_errors_in_music_file + unmount_music_file +} + +function mount_and_fix_errors_in_files () { + mount_and_fix_errors_in_cam_file + mount_and_fix_errors_in_music_file +} + +export -f fix_errors_in_mount_point export -f fix_errors_in_cam_file export -f retry export -f mount_mountpoint @@ -185,6 +206,7 @@ export -f ensure_mountpoint_is_mounted export -f ensure_mountpoint_is_mounted_with_retry export -f ensure_cam_file_is_mounted export -f fix_errors_in_cam_file +export -f unmount_mount_point export -f unmount_cam_file export -f connect_usb_drives_to_host @@ -193,13 +215,13 @@ log "Starting..." if archive_is_reachable then # archive_clips will fix errors in the cam file - fix_errors_in_music_file + mount_and_fix_errors_in_music_file archive_clips wait_for_archive_to_be_unreachable else - fix_errors_in_files + mount_and_fix_errors_in_files connect_usb_drives_to_host fi @@ -211,4 +233,4 @@ do archive_clips wait_for_archive_to_be_unreachable -done +done \ No newline at end of file From 664e47b1c05358cc7c074aed2d936b82a1ec7762 Mon Sep 17 00:00:00 2001 From: Stephen Gordon Date: Tue, 23 Oct 2018 07:47:23 -0400 Subject: [PATCH 06/16] Create the TeslaCam directory as part of the SD card setup --- windows_archive/setup-teslausb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 5910eab..b9da36e 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -103,6 +103,12 @@ function create_usb_drive_backing_files () { /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" } +function create_teslacam_directory () { + mount /mnt/cam + mkdir /mnt/cam/TeslaCam + umount /mnt/cam +} + function configure_archive () { echo "Configuring the archive..." mkdir /mnt/archive @@ -216,6 +222,8 @@ echo "" >> /etc/fstab create_usb_drive_backing_files +create_teslacam_directory + configure_archive configure_rc_local From 7eaead43aef881286ca2c8848058861805841b3f Mon Sep 17 00:00:00 2001 From: Stephen Gordon Date: Tue, 23 Oct 2018 09:13:25 -0400 Subject: [PATCH 07/16] Remove instructions about manually creating the TeslaCam directory --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index c672958..e963075 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,6 @@ After reboot, the Pi hostname will become `teslausb`, so future `ssh` sessions w ### Get the Pi set up for your Tesla. If you set up the Pi with a keyboard and a monitor disconnect it and connect it to a PC. If you're using a cable be sure to use the port labeled "USB" on the circuitboard. 1. Wait for the Pi to show up on the PC as a USB drive. -1. Create a directory named TeslaCam at the root of the drive labeled CAM. 1. Copy any music you'd like to the drive labeled MUSIC. 1. Eject the drives. 1. Unplug the Pi from the PC. From a8e3671a948b7a49e220b11089d7720baab6c1b5 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 06:38:32 -0700 Subject: [PATCH 08/16] Set the branch and repo only if not already set. --- windows_archive/setup-teslausb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index ef965ce..9716c28 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,9 +1,17 @@ #!/bin/bash -eu -REPO=cimryan -BRANCH=master user_enabled_pushover=false +if [[ -z $REPO ]] +then + REPO=cimryan +fi + +f [[ -z $BRANCH ]] +then + BRANCH=master +fi + if ! [ $(id -u) = 0 ] then echo "STOP: Run sudo -i." From b71d7d77d6641df75c4e3babe6dc12cd8708161c Mon Sep 17 00:00:00 2001 From: Stephen Gordon Date: Tue, 23 Oct 2018 09:39:18 -0400 Subject: [PATCH 09/16] More changes to the readme instructions. Connecting the Pi to a PC after setup-teslausb is run is only required when adding music to the Pi --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e963075..2771d94 100644 --- a/README.md +++ b/README.md @@ -106,12 +106,14 @@ Now that you have Wifi up and running, it's time to set up the USB storage and s ``` 1. Run this command: ``` - reboot + shutdown -h now ``` -After reboot, the Pi hostname will become `teslausb`, so future `ssh` sessions will be `ssh pi@teslausb.local`. +On the next boot, the Pi hostname will become `teslausb`, so future `ssh` sessions will be `ssh pi@teslausb.local`. -### Get the Pi set up for your Tesla. +Your Pi is now ready to be plugged into your Tesla. If you want to add music to the Pi, follow the instructions in the next section. + +### (Optional) Add music to the Pi If you set up the Pi with a keyboard and a monitor disconnect it and connect it to a PC. If you're using a cable be sure to use the port labeled "USB" on the circuitboard. 1. Wait for the Pi to show up on the PC as a USB drive. 1. Copy any music you'd like to the drive labeled MUSIC. From 9898fe95eec125ca335cc0c7801d635363a37237 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 06:52:22 -0700 Subject: [PATCH 10/16] Simplify tests for empty variables. Use caps for globals. --- windows_archive/setup-teslausb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 9716c28..8be1acc 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,6 +1,6 @@ #!/bin/bash -eu -user_enabled_pushover=false +USER_ENABLED_PUSHOVER=false if [[ -z $REPO ]] then @@ -28,9 +28,9 @@ function check_variable () { } function check_pushover_enabled () { - if [ ! -z "${pushover_enabled+x}" ] + if [ "$pushover_enabled" = "true" ] then - if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] + if [[ -z $pushover_user_key ]] || [[ -z $pushover_app_key ]] then echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." echo "Define the variables like this:" @@ -42,7 +42,7 @@ function check_pushover_enabled () { echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." exit 1 else - user_enabled_pushover=true + USER_ENABLED_PUSHOVER=true echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials @@ -110,7 +110,6 @@ function check_available_space () { echo "There is sufficient space available." } - function get_ancillary_setup_scripts () { pushd /tmp wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh @@ -189,9 +188,8 @@ function configure_archive_scripts () { echo "Downloaded script to remount filesystems read/write if needed (/root/remountfs_rw)." } - function configure_pushover_scripts() { -if [ ${user_enabled_pushover} = "true" ] +if [ ${USER_ENABLED_PUSHOVER} = "true" ] then pushd /root/bin wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/send-pushover From b6abdfb7262d68c3e0e3d60c3fc072289d699b2c Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 07:27:58 -0700 Subject: [PATCH 11/16] Simplified tests aren't compatible with bash -u. --- windows_archive/setup-teslausb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 8be1acc..52dd5cc 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -2,15 +2,9 @@ USER_ENABLED_PUSHOVER=false -if [[ -z $REPO ]] -then - REPO=cimryan -fi +REPO=${REPO:-cimryan} -f [[ -z $BRANCH ]] -then - BRANCH=master -fi +BRANCH=${BRANCH:-master} if ! [ $(id -u) = 0 ] then @@ -28,9 +22,9 @@ function check_variable () { } function check_pushover_enabled () { - if [ "$pushover_enabled" = "true" ] + if [ ! -z "${pushover_enabled+x}" ] then - if [[ -z $pushover_user_key ]] || [[ -z $pushover_app_key ]] + if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] then echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." echo "Define the variables like this:" From 85372bcb063bd378b93d912cd3dfd001881d6e6c Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 07:48:01 -0700 Subject: [PATCH 12/16] Umount the temp mount. --- windows_archive/setup-teslausb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 52dd5cc..c50b31b 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -88,6 +88,8 @@ function check_archive_mountable () { echo "STOP: The archive couldn't be mounted with CIFS version ${cifs_version}. Try specifying a lower number for the CIFS version like this: export cifs_version=2" exit 1 fi + + umount "$test_mount_location" } function check_available_space () { From 41e9c9f945b6cba110db011bd423560436331702 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 09:17:17 -0700 Subject: [PATCH 13/16] Don't attempt to fix errors in the music file when there is no music file. --- windows_archive/archiveloop | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/windows_archive/archiveloop b/windows_archive/archiveloop index a7f16b0..257eba9 100644 --- a/windows_archive/archiveloop +++ b/windows_archive/archiveloop @@ -188,9 +188,12 @@ function mount_and_fix_errors_in_cam_file () { } function mount_and_fix_errors_in_music_file () { - ensure_music_file_is_mounted - fix_errors_in_music_file - unmount_music_file + if [ -e "$MUSIC_MOUNT" ] + then + ensure_music_file_is_mounted + fix_errors_in_music_file + unmount_music_file + fi } function mount_and_fix_errors_in_files () { @@ -233,4 +236,4 @@ do archive_clips wait_for_archive_to_be_unreachable -done \ No newline at end of file +done From cc89acefce2286862c2e8e24f2c4e6370c7ea5c4 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 16:40:41 -0700 Subject: [PATCH 14/16] Halt is preferred. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2771d94..0b941a4 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ Now that you have Wifi up and running, it's time to set up the USB storage and s ``` 1. Run this command: ``` - shutdown -h now + halt ``` On the next boot, the Pi hostname will become `teslausb`, so future `ssh` sessions will be `ssh pi@teslausb.local`. From 5b75897c7d480df27ef266272c99e63efd499695 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 18:18:35 -0700 Subject: [PATCH 15/16] Move creation of the TeslaCam directory to create-backingfiles.sh The create-backingfiles.sh file already contains the responsibility for defining the mount points, which this step needs. --- windows_archive/create-backingfiles.sh | 8 ++++++++ windows_archive/setup-teslausb | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/windows_archive/create-backingfiles.sh b/windows_archive/create-backingfiles.sh index a406df4..69f19b4 100644 --- a/windows_archive/create-backingfiles.sh +++ b/windows_archive/create-backingfiles.sh @@ -21,6 +21,12 @@ function add_drive () { echo "$filename $mountpoint vfat noauto,users,umask=000 0 0" >> /etc/fstab } +function create_teslacam_directory () { + mount /mnt/cam + mkdir /mnt/cam/TeslaCam + umount /mnt/cam +} + FREE_1K_BLOCKS="$(df --output=avail --block-size=1K /backingfiles/ | tail -n 1)" CAM_DISK_SIZE="$(( $FREE_1K_BLOCKS * $CAM_PERCENT / 100 ))" @@ -36,3 +42,5 @@ then else echo "options g_mass_storage file=$CAM_DISK_FILE_NAME removable=1 ro=0 stall=0 iSerialNumber=123456" > "$G_MASS_STORAGE_CONF_FILE_NAME" fi + +create_teslacam_directory diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 3fe1526..c50b31b 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -139,12 +139,6 @@ function create_usb_drive_backing_files () { /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" } -function create_teslacam_directory () { - mount /mnt/cam - mkdir /mnt/cam/TeslaCam - umount /mnt/cam -} - function configure_archive () { local archive_server_ip_address="$1" @@ -276,8 +270,6 @@ echo "" >> /etc/fstab create_usb_drive_backing_files -create_teslacam_directory - configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" configure_rc_local From a7a9110160505b167f5ef4e601d9efc114942871 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 18:29:26 -0700 Subject: [PATCH 16/16] Just three phases to setting up the Pi, now. --- README.md | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0b941a4..6f3c49e 100644 --- a/README.md +++ b/README.md @@ -58,11 +58,10 @@ Get the IP address of the archive machine. You'll need this later, so write it d ### TODO Other hosting solutions ## Set up the Raspberry Pi -There are four phases to setting up the Pi: +There are three phases to setting up the Pi: 1. Get the OS onto the micro sd card. 1. Get a shell on the Pi. 1. Set up the USB storage functionality. -1. Get the Pi set up for your Tesla. ### Get the OS onto the micro SD card @@ -73,13 +72,17 @@ There are four phases to setting up the Pi: ### Get a shell on the Pi If you used a Windows computer to flash the OS onto the MicroSD card, follow these [Instructions](GetShellWithoutMonitorOnWindows.md). + If you used a Mac or a Linux computer, follow these [Instructions](GetShellWithoutMonitorOnLinux.md). ### Set up the USB storage functionality Now that you have Wifi up and running, it's time to set up the USB storage and scripts that will manage the dashcam and (optionally) music storage. -1. SSH to the Pi and run `sudo -i` +1. SSH to the Pi and run + ``` + sudo -i + ``` 1. Try to ping your archive server from the Pi. In this example the server is named `nautilus`. ``` ping -c 3 nautilus @@ -108,29 +111,30 @@ Now that you have Wifi up and running, it's time to set up the USB storage and s ``` halt ``` +1. Disconnect the Pi from the computer. On the next boot, the Pi hostname will become `teslausb`, so future `ssh` sessions will be `ssh pi@teslausb.local`. -Your Pi is now ready to be plugged into your Tesla. If you want to add music to the Pi, follow the instructions in the next section. +Your Pi is now ready to be plugged into your Tesla. If you want to add music to the Pi, follow the instructions in the next section. -### (Optional) Add music to the Pi -If you set up the Pi with a keyboard and a monitor disconnect it and connect it to a PC. If you're using a cable be sure to use the port labeled "USB" on the circuitboard. -1. Wait for the Pi to show up on the PC as a USB drive. +## (Optional) Add music to the Pi +Connect the Pi to a computer. If you're using a cable be sure to use the port labeled "USB" on the circuitboard. +1. Wait for the Pi to show up on the computer as a USB drive. 1. Copy any music you'd like to the drive labeled MUSIC. 1. Eject the drives. 1. Unplug the Pi from the PC. 1. Plug the Pi into your Tesla. ## Making changes to the system after setup -The setup process configures the Pi with read-only file systems for the operating system but with read-write access through the USB - interface. This means that you'll be able to record dashcam video and add and remove music files but you won't be able to make changes - to files on / or on /boot. This is to protect against corruption of the operating system when the Tesla cuts power to the Pi. +The setup process configures the Pi with read-only file systems for the operating system but with read-write +access through the USB interface. This means that you'll be able to record dashcam video and add and remove +music files but you won't be able to make changes to files on / or on /boot. This is to protect against +corruption of the operating system when the Tesla cuts power to the Pi. To make changes to the system partitions: ``` ssh pi@teslausb. sudo -i -mount / -o remount,rw -mount /boot -o remount,rw +/root/bin/remountfs_rw ``` Then make whatever changes you need to. The next time the system boots the partitions will once again be read-only.