From 3a03382075e2f72306fec01d9d67262fd1c297d6 Mon Sep 17 00:00:00 2001 From: Richard Goodwin Date: Fri, 26 Oct 2018 07:27:26 -0500 Subject: [PATCH] Reworking main setup script Main setup script rework to support headless --- setup/pi/setup-teslausb | 102 +++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 32 deletions(-) diff --git a/setup/pi/setup-teslausb b/setup/pi/setup-teslausb index 6e64ee8..635a4ca 100644 --- a/setup/pi/setup-teslausb +++ b/setup/pi/setup-teslausb @@ -1,22 +1,56 @@ #!/bin/bash -eu USER_ENABLED_PUSHOVER=false - +SETUP_LOGFILE=/boot/teslausb-headless-setup.log REPO=${REPO:-cimryan} - BRANCH=${BRANCH:-master} +HEADLESS_SETUP=${HEADLESS_SETUP:-false} +REPO=rtgoodwin +BRANCH=headless-patch + if ! [ $(id -u) = 0 ] then - echo "STOP: Run sudo -i." + setup_progress "STOP: Run sudo -i." exit 1 fi +function setup_progress () { + if [ $HEADLESS_SETUP = "true" ] + echo "$( date ) : $1" >> "$SETUP_LOGFILE" + else + echo $1 + fi +} + + +function headless_mark_setup_failed () { + setup_log "ERROR: Setup Failed." + touch /boot/TESLAUSB_SETUP_FAILED +} + +function headless_mark_setup_success () { + if [ -e /boot/TESLAUSB_SETUP_FAILED ] + then + rm /boot/TESLAUSB_SETUP_FAILED + fi + rm /boot/TESLAUSB_SETUP_STARTED + touch /boot/TESLAUSB_SETUP_FINISHED + setup_log "Main setup completed. Remounting file systems read only." +} + +function headless_progress_flash () { + if [ $HEADLESS_SETUP = "true" ] + then + /etc/stage_flash $1 + fi +} + 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" + setup_progress "STOP: Define the variable $var_name like this: export $var_name=value" exit 1 fi } @@ -26,36 +60,36 @@ function check_pushover_enabled () { 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" + 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 - echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." + 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 + setup_progress "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials + setup_progress "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials + setup_progress "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials fi fi } function check_available_space () { - echo "Verifying that there is sufficient space available on the MicroSD card..." + 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 - echo "STOP: The MicroSD card is too small." + setup_progress "STOP: The MicroSD card is too small." exit 1 fi - echo "There is sufficient space available." + setup_progress "There is sufficient space available." } function get_script () { @@ -75,11 +109,11 @@ function get_ancillary_setup_scripts () { function fix_cmdline_txt_modules_load () { - echo "Fixing the modules-load parameter in /boot/cmdline.txt..." + setup_progress "Fixing the modules-load parameter in /boot/cmdline.txt..." cp /boot/cmdline.txt ~ cat ~/cmdline.txt | sed 's/ modules-load=dwc2,g_ether/ modules-load=dwc2/' > /boot/cmdline.txt rm ~/cmdline.txt - echo "Fixed cmdline.txt." + setup_progress "Fixed cmdline.txt." } BACKINGFILES_MOUNTPOINT=/backingfiles @@ -97,9 +131,9 @@ function create_usb_drive_backing_files () { if ! findmnt --mountpoint $BACKINGFILES_MOUNTPOINT then - echo "Mounting the partition for the backing files..." + setup_progress "Mounting the partition for the backing files..." mount $BACKINGFILES_MOUNTPOINT - echo "Mounted the partition for the backing files." + setup_progress "Mounted the partition for the backing files." fi if [ ! -e $BACKINGFILES_MOUNTPOINT/*.bin ] @@ -109,7 +143,7 @@ function create_usb_drive_backing_files () { } function configure_archive_scripts () { - echo "Configuring the archive scripts..." + setup_progress "Configuring the archive scripts..." get_script /root/bin archiveloop run @@ -126,7 +160,7 @@ function configure_archive_scripts () { get_script /root/bin remountfs_rw run - echo "Configured the archive scripts." + setup_progress "Configured the archive scripts." } function configure_pushover_scripts() { @@ -139,7 +173,7 @@ function configure_rc_local () { return fi - echo "Configuring /etc/rc.local to run the archive scripts at startup..." + 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 @@ -158,26 +192,30 @@ EOF cat ~/rc.local > /etc/rc.local rm ~/rc.local - echo "Configured rc.local." + setup_progress "Configured rc.local." } function configure_hostname () { - echo "Configuring the hostname..." + # Headless image already has hostname set + if [ ! $HEADLESS_SETUP = "true" ] + then + setup_progress "Configuring the hostname..." - local new_host_name="teslausb" - cp /etc/hosts ~ - sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts + local new_host_name="teslausb" + cp /etc/hosts ~ + sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts - cp /etc/hostname ~ - sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname - echo "Configured the hostname." + cp /etc/hostname ~ + sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname + setup_progress "Configured the hostname." + fi } function make_root_fs_readonly () { /tmp/make-root-fs-readonly.sh } -echo "Verifying environment variables..." +setup_progress "Verifying environment variables..." RSYNC_ENABLE="${RSYNC_ENABLE:-false}" @@ -240,7 +278,7 @@ create_usb_drive_backing_files configure_rc_local configure_hostname - +headless_mark_setup_success make_root_fs_readonly echo "All done."