diff --git a/README.md b/README.md index e95413c..dec63a5 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,11 @@ Required: * A Micro SD card, at least 8 GB in size, and an adapter (if necessary) to connect the card to your computer. * A mechanism to connect the Pi to the Tesla. Either: * A USB A/Micro B cable: [Adafruit](https://www.adafruit.com/product/898) or [Amazon](https://www.amazon.com/gp/product/B013G4EAEI/), or - * A USB A Add-on Board if you want to plug your Pi into your Tesla like a USB drive instead of using a cable. [Amazon](https://www.amazon.com/gp/product/B07BK2BR6C/) + * A USB A Add-on Board if you want to plug your Pi into your Tesla like a USB drive instead of using a cable. [Amazon](https://www.amazon.com/gp/product/B07BK2BR6C/), or + * A PCB kit if you want the lowest profile possible and you're able to solder. [Sparkfun](https://www.sparkfun.com/products/14526) Optional: -* A case. The "Official" case: [Adafruit](https://www.adafruit.com/product/2885) or [Amazon](https://www.amazon.com/gp/product/B06Y593MHV). There are many others to choose from. Note that the official case won't work with the USB A Add on board. +* A case. The "Official" case: [Adafruit](https://www.adafruit.com/product/3446) or [Amazon](https://www.amazon.com/gp/product/B06Y593MHV). There are many others to choose from. Note that the official case won't work with the USB A Add-on board or the PCB kit. * USB Splitter if you don't want to lose a front USB port. [The Onvian Splitter](https://www.amazon.com/gp/product/B01KX4TKH6) has been reported working by multiple people on reddit. ### Software @@ -93,7 +94,9 @@ On the next boot, the Pi hostname will become `teslausb`, so future `ssh` sessio 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 +## Optional: Add music to the Pi +> Note: If you set `campercent` to `100` then skip this step. + 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. @@ -101,7 +104,7 @@ Connect the Pi to a computer. If you're using a cable be sure to use the port la 1. Unplug the Pi from the computer. 1. Plug the Pi into your Tesla. -## Making changes to the system after setup +## Optional: 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 diff --git a/doc/OneStepSetup.md b/doc/OneStepSetup.md index 5cfe7ed..c97ccc6 100644 --- a/doc/OneStepSetup.md +++ b/doc/OneStepSetup.md @@ -42,7 +42,7 @@ A sample conf file is located in the `boot` folder on the SD card. | Stage (number of flashes) | Activity | |---|---| -| 2 | Verify setup variables | +| 2 | Verify the requested configuration is creatable | | 3 | Grab scripts to start/continue setup | | 4 | Create partition and files to store camera clips/music) | | 5 | Setup completed; remounting filesystems as read-only and rebooting | diff --git a/run/cifs_archive/configure-archive.sh b/run/cifs_archive/configure-archive.sh index eff994f..a9b7fb5 100644 --- a/run/cifs_archive/configure-archive.sh +++ b/run/cifs_archive/configure-archive.sh @@ -12,15 +12,16 @@ function configure_archive () { mkdir "$archive_path" fi - local credentials_file_path="/root/.teslaCamArchiveCredentials" + local cifs_version="${cifs_version:-3.0}" - /root/bin/write-archive-credentials-to.sh "$credentials_file_path" + local credentials_file_path="/root/.teslaCamArchiveCredentials" + /root/bin/write-archive-configs-to.sh "$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." } -ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )" +ARCHIVE_SERVER_IP_ADDRESS="$( $INSTALL_DIR/lookup-ip-address.sh "$archiveserver" )" configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" \ No newline at end of file diff --git a/run/cifs_archive/verify-archive-configuration.sh b/run/cifs_archive/verify-archive-configuration.sh index 5bbc131..bf8b9f2 100644 --- a/run/cifs_archive/verify-archive-configuration.sh +++ b/run/cifs_archive/verify-archive-configuration.sh @@ -24,10 +24,11 @@ function check_archive_mountable () { mkdir "$test_mount_location" fi + local cifs_version="${cifs_version:-3.0}" + local tmp_credentials_file_path="/tmp/teslaCamArchiveCredentials" - - /root/bin/write-archive-credentials-to.sh "$tmp_credentials_file_path" - + /root/bin/write-archive-configs-to.sh "$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 @@ -42,8 +43,7 @@ function check_archive_mountable () { umount "$test_mount_location" } +ARCHIVE_SERVER_IP_ADDRESS="$( $INSTALL_DIR/lookup-ip-address.sh "$archiveserver" )" + check_archive_server_reachable - -ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )" - check_archive_mountable "$ARCHIVE_SERVER_IP_ADDRESS" diff --git a/run/cifs_archive/write-archive-credentials-to.sh b/run/cifs_archive/write-archive-configs-to.sh similarity index 61% rename from run/cifs_archive/write-archive-credentials-to.sh rename to run/cifs_archive/write-archive-configs-to.sh index edce256..082af81 100644 --- a/run/cifs_archive/write-archive-credentials-to.sh +++ b/run/cifs_archive/write-archive-configs-to.sh @@ -3,4 +3,4 @@ FILE_PATH="$1" echo "username=$shareuser" > "$FILE_PATH" -echo "password=$sharepassword" >> "$FILE_PATH" +echo "password=$sharepassword" >> "$FILE_PATH" \ No newline at end of file diff --git a/run/get-archiveserver-ip-address.sh b/run/get-archiveserver-ip-address.sh deleted file mode 100644 index 8a3936e..0000000 --- a/run/get-archiveserver-ip-address.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -eu - -echo "$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" \ No newline at end of file diff --git a/run/lookup-ip-address.sh b/run/lookup-ip-address.sh new file mode 100644 index 0000000..41b8f5a --- /dev/null +++ b/run/lookup-ip-address.sh @@ -0,0 +1,3 @@ +#!/bin/bash -eu + +echo "$(ping -c 1 -w 1 $1 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" \ No newline at end of file diff --git a/run/rclone_archive/configure-archive.sh b/run/rclone_archive/configure-archive.sh index 13e291b..c4e307f 100644 --- a/run/rclone_archive/configure-archive.sh +++ b/run/rclone_archive/configure-archive.sh @@ -1,10 +1,19 @@ #!/bin/bash -eu function configure_archive () { - echo "Configuring the archive for Rclone..." + echo "Configuring rclone archive..." - echo "drive=$RCLONE_DRIVE" > /root/.teslaCamRcloneConfig - echo "path=$RCLONE_PATH" >> /root/.teslaCamRcloneConfig + local config_file_path="/root/.teslaCamRcloneConfig" + /root/bin/write-archive-configs-to.sh "$config_file_path" + + if [ ! -L "/root/.config/rclone" ] && [ -e "/root/.config/rclone" ] + then + echo "Moving rclone configs into /mutable" + mv /root/.config/rclone /mutable/configs + ln -s /mutable/configs/rclone /root/.config/rclone + fi + + echo "Done" } configure_archive \ No newline at end of file diff --git a/run/rclone_archive/verify-archive-configuration.sh b/run/rclone_archive/verify-archive-configuration.sh index 95dbbf8..38c5303 100644 --- a/run/rclone_archive/verify-archive-configuration.sh +++ b/run/rclone_archive/verify-archive-configuration.sh @@ -1 +1,18 @@ -#!/bin/bash -eu \ No newline at end of file +#!/bin/bash -eu + +function verify_configuration () { + echo "Verifying rlcone configuration..." + if ! [ -e "/root/.config/rclone/rclone.conf" ] + then + echo "STOP: rclone config was not found. did you configure rclone correctly?" + exit 1 + fi + + if ! rclone lsd "$RCLONE_DRIVE": | grep -q "$RCLONE_PATH" + then + echo "STOP: Could not find the $RCLONE_DRIVE:$RCLONE_PATH" + exit 1 + fi +} + +verify_configuration \ No newline at end of file diff --git a/run/rclone_archive/write-archive-configs-to.sh b/run/rclone_archive/write-archive-configs-to.sh new file mode 100644 index 0000000..b8ccef6 --- /dev/null +++ b/run/rclone_archive/write-archive-configs-to.sh @@ -0,0 +1,6 @@ +#!/bin/bash -eu + +FILE_PATH="$1" + +echo "drive=$RCLONE_DRIVE" > "$FILE_PATH" +echo "path=$RCLONE_PATH" >> "$FILE_PATH" \ No newline at end of file diff --git a/run/rsync_archive/configure-archive.sh b/run/rsync_archive/configure-archive.sh index 4fcb84c..bb77faa 100644 --- a/run/rsync_archive/configure-archive.sh +++ b/run/rsync_archive/configure-archive.sh @@ -1,16 +1,10 @@ #!/bin/bash -eu function configure_archive () { - local archive_server_ip_address="$1" + echo "Configuring the rsync archive..." - echo "Configuring the archive..." - - echo "Configuring for Rsync..." - echo "user=$RSYNC_USER" > /root/.teslaCamRsyncConfig - echo "server=$RSYNC_SERVER" >> /root/.teslaCamRsyncConfig - echo "path=$RSYNC_PATH" >> /root/.teslaCamRsyncConfig + local config_file_path="/root/.teslaCamRsyncConfig" + /root/bin/write-archive-configs-to.sh "$config_file_path" } -ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )" - -configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" \ No newline at end of file +configure_archive \ No newline at end of file diff --git a/run/rsync_archive/write-archive-configs-to.sh b/run/rsync_archive/write-archive-configs-to.sh new file mode 100644 index 0000000..ae9a1f3 --- /dev/null +++ b/run/rsync_archive/write-archive-configs-to.sh @@ -0,0 +1,7 @@ +#!/bin/bash -eu + +FILE_PATH="$1" + +echo "user=$RSYNC_USER" > "$FILE_PATH" +echo "server=$RSYNC_SERVER" >> "$FILE_PATH" +echo "path=$RSYNC_PATH" >> "$FILE_PATH" \ No newline at end of file diff --git a/setup/pi/configure.sh b/setup/pi/configure.sh new file mode 100644 index 0000000..cc6874c --- /dev/null +++ b/setup/pi/configure.sh @@ -0,0 +1,197 @@ +#!/bin/bash -eu + +REPO=${REPO:-cimryan} +BRANCH=${BRANCH:-master} + +export INSTALL_DIR=${INSTALL_DIR:-/root/bin} + +function check_variable () { + local var_name="$1" + if [ -z "${!var_name+x}" ] + then + echo "STOP: Define the variable $var_name like this: export $var_name=value" + exit 1 + fi +} + +function get_script () { + local local_path="$1" + local name="$2" + local remote_path="${3:-}" + + echo "Starting download for $local_path/$name" + curl -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" + chmod +x "$local_path/$name" + echo "Done" +} + +function install_rc_local () { + local install_home="$1" + + if grep -q archiveloop /etc/rc.local + then + echo "Skipping rc.local installation" + return + fi + + echo "Configuring /etc/rc.local to run the archive scripts at startup..." + echo "#!/bin/bash -eu" > ~/rc.local + echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local + echo "install_home=\"${install_home}\"" >> ~/rc.local + cat << 'EOF' >> ~/rc.local +LOGFILE=/tmp/rc.local.log + +function log () { + echo "$( date )" >> "$LOGFILE" + echo "$1" >> "$LOGFILE" +} + +log "Launching archival script..." +"$install_home"/archiveloop "$archiveserver" & +log "All done" +exit 0 +EOF + + cat ~/rc.local > /etc/rc.local + rm ~/rc.local + echo "Installed rc.local." +} + +function check_archive_configs () { + echo -n "Checking archive configs: " + + RSYNC_ENABLE="${RSYNC_ENABLE:-false}" + RCLONE_ENABLE="${RCLONE_ENABLE:-false}" + if [ "$RSYNC_ENABLE" = true ] && [ "$RCLONE_ENABLE" = true ] + then + echo "STOP: Can't enable rsync and rclone at the same time" + exit 1 + fi + + if [ "$RSYNC_ENABLE" = true ] + then + check_variable "RSYNC_USER" + check_variable "RSYNC_SERVER" + check_variable "RSYNC_PATH" + export archiveserver="$RSYNC_SERVER" + + elif [ "$RCLONE_ENABLE" = true ] + then + check_variable "RCLONE_DRIVE" + check_variable "RCLONE_PATH" + export archiveserver="8.8.8.8" # since it's a cloud hosted drive we'll just set this to google dns + else + # default to cifs + check_variable "sharename" + check_variable "shareuser" + check_variable "sharepassword" + check_variable "archiveserver" + fi + + echo "done" +} + +function get_archive_module () { + + if [ "$RSYNC_ENABLE" = true ] + then + archive_module="run/rsync_archive" + elif [ "$RCLONE_ENABLE" = true ] + then + archive_module="run/rclone_archive" + else + archive_module="run/cifs_archive" + fi + + echo $archive_module +} + +function install_archive_scripts () { + local install_path="$1" + local archive_module="$2" + + echo "Installing base archive scripts into $install_path" + get_script $install_path archiveloop run + get_script $install_path remountfs_rw run + get_script $install_path lookup-ip-address.sh run + + echo "Installing archive module scripts" + get_script $install_path verify-archive-configuration.sh $archive_module + get_script $install_path configure-archive.sh $archive_module + get_script $install_path archive-clips.sh $archive_module + get_script $install_path connect-archive.sh $archive_module + get_script $install_path disconnect-archive.sh $archive_module + get_script $install_path write-archive-configs-to.sh $archive_module +} + + +function check_pushover_configuration () { + if [ ! -z "${pushover_enabled+x}" ] + then + 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:" + echo "export pushover_user_key=put_your_userkey_here" + echo "export pushover_app_key=put_your_appkey_here" + exit 1 + elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] + then + echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." + exit 1 + fi + fi +} + +function configure_pushover () { + if [ ! -z "${pushover_enabled+x}" ] + then + echo "Enabling pushover" + 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 + else + echo "Pushover not configured." + fi +} + +function check_and_configure_pushover () { + check_pushover_configuration + + configure_pushover +} + +function install_pushover_scripts() { + local install_path="$1" + get_script $install_path send-pushover run +} + +if ! [ $(id -u) = 0 ] +then + echo "STOP: Run sudo -i." + exit 1 +fi + +if [ ! -e "$INSTALL_DIR" ] +then + mkdir "$INSTALL_DIR" +fi + +echo "Getting files from $REPO:$BRANCH" + +check_and_configure_pushover +install_pushover_scripts "$INSTALL_DIR" + +check_archive_configs + +archive_module="$( get_archive_module )" +echo "Using archive module: $archive_module" + +install_archive_scripts $INSTALL_DIR $archive_module +"$INSTALL_DIR"/verify-archive-configuration.sh +"$INSTALL_DIR"/configure-archive.sh + +install_rc_local "$INSTALL_DIR" + + + diff --git a/setup/pi/make-root-fs-readonly.sh b/setup/pi/make-root-fs-readonly.sh index fdf2ba6..4ac638b 100644 --- a/setup/pi/make-root-fs-readonly.sh +++ b/setup/pi/make-root-fs-readonly.sh @@ -25,7 +25,7 @@ append_cmdline_txt_param ro # Move fake-hwclock.data to /mutable directory so it can be updated if ! findmnt --mountpoint /mutable then - echo "Mounting the multable partition..." + echo "Mounting the mutable partition..." mount /mutable echo "Mounted." fi @@ -33,25 +33,19 @@ if [ ! -e "/mutable/etc" ] then mkdir -p /mutable/etc fi -if [ -e "/etc/fake-hwclock.data" ] + +if [ ! -L "/etc/fake-hwclock.data" ] && [ -e "/etc/fake-hwclock.data" ] then echo "Moving fake-hwclock data" - cp /etc/fake-hwclock.data /mutable/etc/fake-hwclock.data - rm /etc/fake-hwclock.data + mv /etc/fake-hwclock.data /mutable/etc/fake-hwclock.data ln -s /mutable/etc/fake-hwclock.data /etc/fake-hwclock.data fi -# Move rclone configs if it exists so we can write to it +# Create a configs directory for others to use if [ ! -e "/mutable/configs" ] then mkdir -p /mutable/configs fi -if [ -e "/root/.config/rclone/rclone.conf" ] -then - echo "Moving rclone configs" - mv /root/.config/rclone /mutable/configs - ln -s /mutable/configs/rclone /root/.config/rclone -fi # Move /var/spool to /tmp rm -rf /var/spool @@ -61,8 +55,7 @@ ln -s /tmp /var/spool sed -i "s/spool\s*0755/spool 1777/g" /usr/lib/tmpfiles.d/var.conf >/dev/null # Move dhcpd.resolv.conf to tmpfs -touch /tmp/dhcpcd.resolv.conf -rm /etc/resolv.conf +mv /etc/resolv.conf /tmp/dhcpcd.resolv.conf ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf # Update /etc/fstab diff --git a/setup/pi/setup-teslausb b/setup/pi/setup-teslausb index 4cdf3f2..c478e5b 100644 --- a/setup/pi/setup-teslausb +++ b/setup/pi/setup-teslausb @@ -1,10 +1,10 @@ #!/bin/bash -eu -USER_ENABLED_PUSHOVER=${USER_ENABLED_PUSHOVER:-false} REPO=${REPO:-cimryan} BRANCH=${BRANCH:-master} HEADLESS_SETUP=${HEADLESS_SETUP:-false} USE_LED_FOR_SETUP_PROGRESS=true +CONFIGURE_ARCHIVING=${CONFIGURE_ARCHIVING:-true} if ! [ $(id -u) = 0 ] then @@ -88,29 +88,6 @@ function check_variable () { fi } -function check_pushover_enabled () { - if [ ! -z "${pushover_enabled+x}" ] - then - if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] - then - setup_progress "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." - setup_progress "Define the variables like this:" - setup_progress "export pushover_user_key=put_your_userkey_here" - setup_progress "export pushover_app_key=put_your_appkey_here" - exit 1 - elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] - then - setup_progress "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 - 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 - fi - fi -} - function check_available_space () { setup_progress "Verifying that there is sufficient space available on the MicroSD card..." @@ -125,12 +102,18 @@ function check_available_space () { setup_progress "There is sufficient space available." } +function verify_configuration () { + get_script /tmp verify-configuration.sh setup/pi + + /tmp/verify-configuration.sh +} + function get_script () { local local_path="$1" local name="$2" local remote_path="${3:-}" - curl -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" + curl --fail -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" # wget -O "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" chmod +x "$local_path/$name" setup_progress "Downloaded $local_path/$name ..." @@ -140,6 +123,7 @@ function get_ancillary_setup_scripts () { get_script /tmp create-backingfiles-partition.sh setup/pi get_script /tmp create-backingfiles.sh setup/pi get_script /tmp make-root-fs-readonly.sh setup/pi + get_script /root configure.sh setup/pi } function fix_cmdline_txt_modules_load () @@ -185,64 +169,6 @@ function create_usb_drive_backing_files () { fi } -function configure_archive_scripts () { - setup_progress "Configuring the archive scripts..." - - get_script /root/bin archiveloop run - - if [ $RSYNC_ENABLE = true ] - then - get_script /root/bin archive-clips.sh run/rsync_archive - get_script /root/bin connect-archive.sh run/rsync_archive - get_script /root/bin disconnect-archive.sh run/rsync_archive - elif [ $RCLONE_ENABLE = true ] - then - get_script /root/bin archive-clips.sh run/rclone_archive - get_script /root/bin connect-archive.sh run/rclone_archive - get_script /root/bin disconnect-archive.sh run/rclone_archive - else - get_script /root/bin archive-clips.sh run/cifs_archive - get_script /root/bin connect-archive.sh run/cifs_archive - get_script /root/bin disconnect-archive.sh run/cifs_archive - fi - - get_script /root/bin remountfs_rw run - - setup_progress "Configured the archive scripts." -} - -function configure_pushover_scripts() { - get_script /root/bin send-pushover run -} - -function configure_rc_local () { - if grep -q archiveloop /etc/rc.local - then - return - fi - - setup_progress "Configuring /etc/rc.local to run the archive scripts at startup..." - echo "#!/bin/bash -eu" > ~/rc.local - echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local -cat << 'EOF' >> ~/rc.local -LOGFILE=/tmp/rc.local.log - -function log () { - echo "$( date )" >> "$LOGFILE" - echo "$1" >> "$LOGFILE" -} - -log "Launching archival script..." -/root/bin/archiveloop "$archiveserver" & -log "All done" -exit 0 -EOF - - cat ~/rc.local > /etc/rc.local - rm ~/rc.local - setup_progress "Configured rc.local." -} - function configure_hostname () { # Headless image already has hostname set if [ ! $HEADLESS_SETUP = "true" ] @@ -252,10 +178,12 @@ function configure_hostname () { local new_host_name="teslausb" cp /etc/hosts ~ sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts + rm ~/hosts cp /etc/hostname ~ sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname setup_progress "Configured the hostname." + rm ~/hostname fi } @@ -263,83 +191,30 @@ function make_root_fs_readonly () { /tmp/make-root-fs-readonly.sh } +export -f setup_progress +export HEADLESS_SETUP + headless_setup_populate_variables # If USE_LED_FOR_SETUP_PROGRESS = true. setup_led_off -# Flash for stage 2 headless (verify variables) +# Flash for stage 2 headless (verify requested configuration) headless_setup_progress_flash 1 -setup_progress "Verifying environment variables..." +setup_progress "Verifying that the requested configuration is valid..." -RSYNC_ENABLE="${RSYNC_ENABLE:-false}" -RCLONE_ENABLE="${RCLONE_ENABLE:-false}" - -if [ "$RSYNC_ENABLE" = true ] -then - check_variable "RSYNC_USER" - check_variable "RSYNC_SERVER" - export archiveserver="$RSYNC_SERVER" - check_variable "RSYNC_PATH" -elif [ "$RCLONE_ENABLE" = true ] -then - check_variable "RCLONE_DRIVE" - check_variable "RCLONE_PATH" - # since it's a cloud hosted drive we'll just set this to google dns - export archiveserver="8.8.8.8" -else # Else for now, TODO allow both for more redundancy? - check_variable "sharename" - check_variable "shareuser" - check_variable "sharepassword" - - export cifs_version="${cifs_version:-3.0}" -fi - -check_variable "archiveserver" -check_variable "campercent" - -check_pushover_enabled +verify_configuration # Flash for Stage 3 headless (grab scripts) headless_setup_progress_flash 2 setup_progress "Downloading additional setup scripts." -if [ ! -e /root/bin ] -then - mkdir /root/bin -fi - -if [ "$RSYNC_ENABLE" = true ] -then - get_script /root/bin verify-archive-configuration.sh run/rsync_archive - get_script /root/bin configure-archive.sh run/rsync_archive -elif [ "$RCLONE_ENABLE" = true ] -then - get_script /root/bin verify-archive-configuration.sh run/rclone_archive - get_script /root/bin configure-archive.sh run/rclone_archive -else - get_script /root/bin verify-archive-configuration.sh run/cifs_archive - get_script /root/bin configure-archive.sh run/cifs_archive - get_script /root/bin write-archive-credentials-to.sh run/cifs_archive -fi - -get_script /root/bin get-archiveserver-ip-address.sh run - -/root/bin/verify-archive-configuration.sh - -check_available_space - get_ancillary_setup_scripts pushd ~ - -configure_archive_scripts - -configure_pushover_scripts - fix_cmdline_txt_modules_load echo "" >> /etc/fstab @@ -349,10 +224,6 @@ headless_setup_progress_flash 3 create_usb_drive_backing_files -/root/bin/configure-archive.sh - -configure_rc_local - configure_hostname # Flash for stage 5 headless (Mark success, FS readonly) @@ -360,6 +231,11 @@ headless_setup_progress_flash 4 headless_setup_mark_setup_success +if [ "$CONFIGURE_ARCHIVING" = true ] +then + /root/configure.sh +fi + make_root_fs_readonly # If USE_LED_FOR_SETUP_PROGRESS = true. diff --git a/setup/pi/verify-configuration.sh b/setup/pi/verify-configuration.sh new file mode 100644 index 0000000..6ee926c --- /dev/null +++ b/setup/pi/verify-configuration.sh @@ -0,0 +1,28 @@ +#!/bin/bash -eu + +function check_variable () { + local var_name="$1" + if [ -z "${!var_name+x}" ] + then + setup_progress "STOP: Define the variable $var_name like this: export $var_name=value" + exit 1 + fi +} + +function check_available_space () { + setup_progress "Verifying that there is sufficient space available on the MicroSD card..." + + local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )" + + if [ "$available_space" -lt 4294967296 ] + then + setup_progress "STOP: The MicroSD card is too small." + exit 1 + fi + + setup_progress "There is sufficient space available." +} + +check_variable "campercent" + +check_available_space