2018-10-15 14:43:09 +02:00
#!/bin/bash
2019-02-03 20:50:03 +00:00
# This script runs on every start called by boostrap.service
2021-08-04 00:18:30 +02:00
# see logs with --> tail -n 100 /home/admin/raspiblitz.log
2018-10-15 14:43:09 +02:00
2019-12-11 13:47:21 +01:00
################################
2019-12-12 14:51:21 +01:00
# BASIC SETTINGS
2019-12-11 13:47:21 +01:00
################################
2018-10-15 14:43:09 +02:00
# load codeVersion
source /home/admin/_version.info
2018-11-27 04:19:57 +01:00
# CONFIGFILE - configuration of RaspiBlitz
# used by fresh SD image to recover configuration
# and delivers basic config info for scripts
configFile = "/mnt/hdd/raspiblitz.conf"
# LOGFILE - store debug logs of bootstrap
# resets on every start
logFile = "/home/admin/raspiblitz.log"
# INFOFILE - state data from bootstrap
# used by display and later setup steps
infoFile = "/home/admin/raspiblitz.info"
2021-08-04 00:18:30 +02:00
# SETUPFILE
# this key/value file contains the state during the setup process
setupFile = "/var/cache/raspiblitz/temp/raspiblitz.setup"
2020-07-22 21:20:11 +02:00
2021-08-04 00:18:30 +02:00
# Init boostrap log file
2018-11-27 04:19:57 +01:00
echo " Writing logs to: ${ logFile } "
echo "" > $logFile
echo "***********************************************" >> $logFile
echo " Running RaspiBlitz Bootstrap ${ codeVersion } " >> $logFile
date >> $logFile
echo "***********************************************" >> $logFile
2018-10-15 14:43:09 +02:00
2021-12-14 23:34:35 +01:00
# make sure SSH server is configured & running
sudo /home/admin/config.scripts/blitz.ssh.sh checkrepair >> ${ logFile }
2019-01-21 00:25:32 +01:00
2021-08-04 00:18:30 +02:00
################################
# INIT raspiblitz.info
################################
2021-12-14 23:34:35 +01:00
# raspiblitz.info contains the persisted system state
# that either given by build or has to survive a reboot
echo "## INIT raspiblitz.info" >> $logFile
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
# set default values for raspiblitz.info (that are not set by build_sdcard.sh)
setupPhase = 'boot'
setupStep = 0
fsexpanded = 0
fundRecovery = 0
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
# load already persisted valued (overwriting defaults if exist)
source ${ infoFile } 2>/dev/null
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
# write fresh raspiblitz.info file
echo " baseimage= ${ baseimage } " > $infoFile
2021-04-03 12:24:50 +02:00
echo " cpu= ${ cpu } " >> $infoFile
2021-04-08 00:02:57 +02:00
echo " displayClass= ${ displayClass } " >> $infoFile
echo " displayType= ${ displayType } " >> $infoFile
2021-08-04 00:18:30 +02:00
echo " setupPhase= ${ setupPhase } " >> $infoFile
2021-12-14 23:34:35 +01:00
echo " setupStep= ${ setupStep } " >> $infoFile
2021-04-08 18:04:16 +02:00
echo " fundRecovery= ${ fundRecovery } " >> $infoFile
2021-12-14 23:34:35 +01:00
echo " fsexpanded= ${ fsexpanded } " >> $infoFile
echo "state=starting" >> $infoFile
2021-08-24 19:35:11 +02:00
sudo chmod 664 ${ infoFile }
2018-10-15 22:31:56 +02:00
2021-12-14 23:34:35 +01:00
# write content of raspiblitz.info to logs
cat $infoFile >> $logFile
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
#########################
# INIT RaspiBlitz Cache
#########################
echo "## INIT RaspiBlitz Cache ... wait background.scan.service to finsih first scan loop" >> $logFile
systemscan_runtime = ""
while [ " ${ systemscan_runtime } " = = "" ]
do
sleep 1
source <( /home/admin/_cache.sh get systemscan_runtime)
echo " - waiting for background.scan.service --> systemscan_runtime( ${ systemscan_runtime } ) " >> $logFile
done
# make sure latest info file is imported
/home/admin/_cache.sh import $infoFile
# setting basic status info
/home/admin/_cache.sh set state "starting"
/home/admin/_cache.sh set message "bootstrap"
# try to load config values if available (config overwrites info)
source ${ configFile } 2>/dev/null
# monitor LAN connection fast to display local IP changes
/home/admin/_cache.sh focus internet_localip 0
######################################
# CHECK SD CARD STATE
2021-08-20 23:23:37 +02:00
2021-09-05 14:34:30 +02:00
# when a file 'stop' is on the sd card boot partition - stop for manual provision
flagExists = $( sudo ls /boot/stop | grep -c 'stop' )
if [ " ${ flagExists } " = = "1" ] ; then
2021-12-14 23:34:35 +01:00
# remove flag
2021-09-05 14:34:30 +02:00
sudo rm /boot/stop
2021-12-14 23:34:35 +01:00
# set state info
/home/admin/_cache.sh set state "stop"
/home/admin/_cache.sh set message "stopped for manual provision"
# log info
2021-09-08 08:46:12 +01:00
echo "INFO: 'bootstrap stopped - run release after manual provison'" >> ${ logFile }
2021-09-05 14:34:30 +02:00
exit 0
fi
2021-08-04 00:18:30 +02:00
# when the provision did not ran thru without error (ask user for fresh sd card)
provisionFlagExists = $( sudo ls /home/admin/provision.flag | grep -c 'provision.flag' )
if [ " ${ provisionFlagExists } " = = "1" ] ; then
sudo systemctl stop ${ network } d 2>/dev/null
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "inconsistentsystem"
/home/admin/_cache.sh set message "provision did not ran thru"
2021-09-08 08:37:27 +01:00
echo "FAIL: 'provision did not ran thru' - need fresh sd card!" >> ${ logFile }
2021-08-04 00:18:30 +02:00
exit 1
fi
2021-12-14 23:34:35 +01:00
################################
# BOOT LOGO
################################
# display 3 secs logo - try to kickstart LCD
# see https://github.com/rootzoll/raspiblitz/issues/195#issuecomment-469918692
# see https://github.com/rootzoll/raspiblitz/issues/647
# see https://github.com/rootzoll/raspiblitz/pull/1580
randnum = $( shuf -i 0-7 -n 1)
/home/admin/config.scripts/blitz.display.sh image /home/admin/raspiblitz/pictures/startlogo${ randnum } .png
sleep 5
/home/admin/config.scripts/blitz.display.sh hide
################################
# CLEANING BOOT SYSTEM
################################
# Emergency cleaning logs when over 1GB (to prevent SD card filling up)
# see https://github.com/rootzoll/raspiblitz/issues/418#issuecomment-472180944
echo "*** Checking Log Size ***"
logsMegaByte = $( sudo du -c -m /var/log | grep "total" | awk '{print $1;}' )
if [ ${ logsMegaByte } -gt 1000 ] ; then
echo "WARN !! Logs /var/log in are bigger then 1GB" >> $logFile
# dont delete directories - can make services crash
sudo rm /var/log/*
sudo service rsyslog restart
/home/admin/_cache.sh set message "WARNING: /var/log/ >1GB"
echo "WARN !! Logs in /var/log in were bigger then 1GB and got emergency delete to prevent fillup." >> $logFile
echo "If you see this in the logs please report to the GitHub issues, so LOG config needs to be optimized." >> $logFile
sleep 10
else
echo " OK - logs are at ${ logsMegaByte } MB - within safety limit " >> $logFile
fi
echo ""
# get the state of data drive
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
################################
# WAIT LOOP: HDD CONNECTED
################################
echo "Waiting for HDD/SSD ..." >> $logFile
sudo ls -la /etc/ssh >> $logFile
until [ ${ isMounted } -eq 1 ] || [ ${# hddCandidate } -gt 0 ]
do
# recheck HDD/SSD
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
echo " isMounted: $isMounted " >> $logFile
echo " hddCandidate: $hddCandidate " >> $logFile
# in case of HDD analyse ERROR
if [ " ${ hddError } " != "" ] ; then
echo " FAIL - error on HDD analysis: ${ hddError } " >> $logFile
/home/admin/_cache.sh set state "errorHDD"
/home/admin/_cache.sh set message " ${ hddError } "
elif [ " ${ isMounted } " = = "0" ] && [ " ${ hddCandidate } " = = "" ] ; then
/home/admin/_cache.sh set state "noHDD"
/home/admin/_cache.sh set message ">=1TB"
fi
# wait for next check
sleep 2
done
echo " HDD/SSD connected: ${ $hddCandidate } " >> $logFile
# write info for LCD
/home/admin/_cache.sh set state "system-init"
/home/admin/_cache.sh set message "please wait"
2021-08-04 00:18:30 +02:00
######################################
# SECTION FOR POSSIBLE REBOOT ACTIONS
systemInitReboot = 0
################################
# FORCED SWITCH TO HDMI
# if a file called 'hdmi' gets
# placed onto the boot part of
# the sd card - switch to hdmi
################################
forceHDMIoutput = $( sudo ls /boot/hdmi* 2>/dev/null | grep -c hdmi)
if [ ${ forceHDMIoutput } -eq 1 ] ; then
# delete that file (to prevent loop)
sudo rm /boot/hdmi*
# switch to HDMI what will trigger reboot
echo "HDMI switch found ... activating HDMI display output & reboot" >> $logFile
sudo /home/admin/config.scripts/blitz.display.sh set-display hdmi >> $logFile
systemInitReboot = 1
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set message "HDMI"
2021-08-04 00:18:30 +02:00
else
echo "No HDMI switch found. " >> $logFile
fi
################################
# FS EXPAND
2021-12-08 09:21:12 +01:00
# extend sd card to maximum capacity
2021-08-04 00:18:30 +02:00
################################
2021-08-20 00:20:16 +02:00
2021-08-04 00:18:30 +02:00
source <( sudo /home/admin/config.scripts/blitz.bootdrive.sh status)
if [ " ${ needsExpansion } " = = "1" ] && [ " ${ fsexpanded } " = = "0" ] ; then
echo "FSEXPAND needed ... starting process" >> $logFile
sudo /home/admin/config.scripts/blitz.bootdrive.sh status >> $logFile
sudo /home/admin/config.scripts/blitz.bootdrive.sh fsexpand >> $logFile
systemInitReboot = 1
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set message "FSEXPAND"
2021-08-04 00:18:30 +02:00
elif [ " ${ tooSmall } " = = "1" ] ; then
echo "!!! FAIL !!!!!!!!!!!!!!!!!!!!" >> $logFile
echo "SDCARD TOO SMALL 16G minimum" >> $logFile
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "sdtoosmall"
2021-08-04 00:18:30 +02:00
echo "System stopped. Please cut power." >> $logFile
sleep 6000
sudo shutdown -r now
slepp 100
exit 1
else
echo " No FS EXPAND needed. needsExpansion( ${ needsExpansion } ) fsexpanded( ${ fsexpanded } ) " >> $logFile
fi
2021-12-14 23:34:35 +01:00
# now that HDD/SSD is connected ... if relevant data from a previous RaspiBlitz was available
# /var/cache/raspiblitz/hdd-inspect exists with copy of config data to init system with
# NOTE: /var/cache/raspiblitz/hdd-inspect will not exist when HDD/SSD is already regulary mounted
####################################
# WIFI RESTORE from HDD works with
# mem copy from datadrive inspection
####################################
# check if there is a WIFI configuration to backup or restore
if [ -f "/var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf" ] ; then
echo "WIFI RESTORE from /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf" >> $logFile
/home/admin/config.scripts/internet.wifi.sh backup-restore >> $logFile
else
echo "No WIFI RESTORE because no /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf" >> $logFile
fi
################################
# SSH SERVER CERTS RESTORE
# if backup is available on HDD/SSD
################################
if [ -d "/var/cache/raspiblitz/hdd-inspect/ssh" ] ; then
# INIT OLD SSH HOST KEYS on Update/Recovery to prevent "Unknown Host" on ssh client
echo "SSH SERVER CERTS RESTORE activating old SSH host keys" >> $logFile
/home/admin/config.scripts/blitz.ssh.sh restore /var/cache/raspiblitz/hdd-inspect >> $logFile
else
echo "No SSH SERVER CERTS RESTORE because no /var/cache/raspiblitz/hdd-inspect/ssh" >> $logFile
fi
2021-08-20 00:20:16 +02:00
################################
# SSH SERVER CERTS RESET
# if a file called 'ssh.reset' gets
# placed onto the boot part of
# the sd card - delete old ssh data
################################
sshReset = $( sudo ls /boot/ssh.reset* 2>/dev/null | grep -c reset)
if [ ${ sshReset } -eq 1 ] ; then
# delete that file (to prevent loop)
2021-12-14 23:34:35 +01:00
rm /boot/ssh.reset* >> $logFile
2021-08-20 00:20:16 +02:00
# delete ssh certs
echo "SSHRESET switch found ... stopping SSH and deleting old certs" >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/blitz.ssh.sh renew >> $logFile
/home/admin/config.scripts/blitz.ssh.sh backup >> $logFile
2021-08-20 00:20:16 +02:00
systemInitReboot = 1
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set message "SSHRESET"
2021-08-20 00:20:16 +02:00
else
echo "No SSHRESET switch found. " >> $logFile
fi
2021-12-14 23:34:35 +01:00
##################################
# DISPLAY RESTORE (if needed)
##################################
if [ -f "/var/cache/raspiblitz/hdd-inspect/raspiblitz.conf" ] ; then
echo "check that display class in raspiblitz.conf from HDD is different from as it is ow in raspiblitz.info ..." >> $logFile
# get display class value from raspiblitz.info
source <( cat ${ infoFile } | grep "^displayClass=" )
infoFileDisplayClass = " ${ displayClass } "
echo " infoFileDisplayClass( ${ infoFileDisplayClass } ) " >> $logFile
# get display class value from raspiblitz.conf
source <( cat /var/cache/raspiblitz/hdd-inspect/raspiblitz.conf | grep "^displayClass=" )
confFileDisplayClass = " ${ displayClass } "
echo " confFileDisplayClass( ${ confFileDisplayClass } ) " >> $logFile
# check if values are different and need to change
if [ " ${ confFileDisplayClass } " != "" ] && [ " ${ infoFileDisplayClass } " != " ${ displayClass } " ] ; then
echo " DISPLAY RESTORE - need to update displayClass from ( ${ infoFileDisplayClass } ) to ( ${ confFileDisplayClass } )' " >> ${ logFile }
/home/admin/config.scripts/blitz.display.sh set-display ${ confFileDisplayClass } >> ${ logFile }
systemInitReboot = 1
else
echo "No DISPLAY RESTORE because no need to change" >> $logFile
fi
else
echo "No DISPLAY RESTORE because no /var/cache/raspiblitz/hdd-inspect/raspiblitz.conf" >> $logFile
fi
2021-08-04 00:18:30 +02:00
################################
2021-12-14 23:34:35 +01:00
# UASP FIX
2021-08-04 00:18:30 +02:00
################################
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set message "checking HDD"
2021-08-04 00:18:30 +02:00
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh uasp-fix)
if [ " ${ neededReboot } " = = "1" ] ; then
2021-12-14 23:34:35 +01:00
echo "UASP FIX applied ... reboot needed." >> $logFile
2021-08-04 00:18:30 +02:00
systemInitReboot = 1
else
2021-12-14 23:34:35 +01:00
echo "No UASP FIX needed" >> $logFile
2021-08-04 00:18:30 +02:00
fi
######################################
# CHECK IF REBOOT IS NEEDED
# from actions above
if [ " ${ systemInitReboot } " = = "1" ] ; then
2021-09-09 01:19:09 +02:00
sudo cp ${ logFile } /home/admin/raspiblitz.systeminit.log
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "reboot"
2021-08-19 00:18:59 +02:00
sleep 8
2021-04-13 13:55:12 +02:00
sudo shutdown -r now
sleep 100
2020-03-18 14:07:27 -07:00
exit 0
fi
2021-08-04 00:18:30 +02:00
###################################
# WAIT LOOP: LOCALNET / INTERNET
# after HDD > can contain WIFI conf
###################################
gotLocalIP = 0
until [ ${ gotLocalIP } -eq 1 ]
2019-12-12 14:51:21 +01:00
do
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
# get latest network info directly
source <( /home/admin/config.scripts/internet.sh status online)
2021-08-04 00:18:30 +02:00
# check state of network
if [ ${ dhcp } -eq 0 ] ; then
# display user waiting for DHCP
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "noDHCP"
/home/admin/_cache.sh set message "Waiting for DHCP"
2021-08-04 00:18:30 +02:00
elif [ ${# localip } -eq 0 ] ; then
if [ ${ configWifiExists } -eq 0 ] ; then
# display user to connect LAN
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "noIP-LAN"
/home/admin/_cache.sh set message "Connect the LAN/WAN"
2021-08-04 00:18:30 +02:00
else
# display user that wifi settings are not working
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "noIP-WIFI"
/home/admin/_cache.sh set message "WIFI Settings not working"
2021-08-04 00:18:30 +02:00
fi
elif [ ${ online } -eq 0 ] ; then
# display user that wifi settings are not working
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "noInternet"
/home/admin/_cache.sh set message "No connection to Internet"
2021-08-04 00:18:30 +02:00
else
gotLocalIP = 1
2019-12-12 14:51:21 +01:00
fi
2021-08-04 00:18:30 +02:00
sleep 1
2019-12-12 14:51:21 +01:00
done
2019-12-12 16:06:07 +01:00
# write info for LCD
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "inspect-hdd"
/home/admin/_cache.sh set message "please wait"
2019-12-12 14:51:21 +01:00
# get fresh info about data drive to continue
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2021-03-26 23:26:29 +01:00
2021-09-09 11:33:31 +01:00
echo " isMounted: $isMounted " >> $logFile
2018-12-10 14:48:02 +01:00
# check if the HDD is auto-mounted ( auto-mounted = setup-done)
2021-08-04 00:18:30 +02:00
echo " HDD already part of system: $isMounted " >> $logFile
2019-12-12 14:51:21 +01:00
2021-08-04 00:18:30 +02:00
############################
############################
# WHEN SETUP IS NEEDED
############################
2018-11-27 04:19:57 +01:00
2021-08-04 00:18:30 +02:00
if [ ${ isMounted } -eq 0 ] ; then
2018-11-27 04:19:57 +01:00
2021-09-04 21:21:03 +02:00
# temp mount the HDD
2021-09-05 15:05:45 +02:00
echo " Temp mounting (1) data drive ( $hddCandidate ) " >> $logFile
2021-09-04 21:21:03 +02:00
if [ " ${ hddFormat } " != "btrfs" ] ; then
2021-09-30 21:32:53 +02:00
source <( /home/admin/config.scripts/blitz.datadrive.sh tempmount ${ hddPartitionCandidate } )
2021-09-04 21:21:03 +02:00
else
2021-09-30 21:32:53 +02:00
source <( /home/admin/config.scripts/blitz.datadrive.sh tempmount ${ hddCandidate } )
2021-09-04 21:21:03 +02:00
fi
2021-09-05 15:05:45 +02:00
echo " Temp mounting (1) result: ${ isMounted } " >> $logFile
2021-09-04 21:21:03 +02:00
2021-08-04 00:18:30 +02:00
# write data needed for setup process into raspiblitz.info
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set hddCandidate " ${ hddCandidate } "
/home/admin/_cache.sh set hddGigaBytes " ${ hddGigaBytes } "
/home/admin/_cache.sh set hddBlocksBitcoin " ${ hddBlocksBitcoin } "
/home/admin/_cache.sh set hddBlocksLitecoin " ${ hddBlocksLitecoin } "
/home/admin/_cache.sh set hddGotMigrationData " ${ hddGotMigrationData } "
2021-08-04 00:18:30 +02:00
echo ""
echo "HDD is there but not AutoMounted yet - Waiting for user Setup/Update" >> $logFile
2021-09-30 20:37:00 +02:00
# add some debug info to logfile
echo "# df " >> ${ logFile }
df >> ${ logFile }
echo "# lsblk -o NAME,FSTYPE,LABEL " >> ${ logFile }
lsblk -o NAME,FSTYPE,LABEL >> ${ logFile }
2021-09-30 21:32:53 +02:00
echo "# /home/admin/config.scripts/blitz.datadrive.sh status"
/home/admin/config.scripts/blitz.datadrive.sh status >> ${ logFile }
2021-09-30 20:37:00 +02:00
2021-08-04 00:18:30 +02:00
# determine correct setup phase
infoMessage = "Please Login for Setup"
setupPhase = "setup"
if [ " ${ hddGotMigrationData } " != "" ] ; then
infoMessage = "Please Login for Migration"
setupPhase = "migration"
elif [ " ${ hddRaspiData } " = = "1" ] ; then
2021-08-20 00:33:20 +02:00
2021-08-04 00:18:30 +02:00
# determine if this is a recovery or an update
2021-09-07 14:12:51 +01:00
# TODO: improve version/update detection later
2021-08-04 00:18:30 +02:00
isRecovery = $( echo " ${ hddRaspiVersion } " | grep -c " ${ codeVersion } " )
if [ " ${ isRecovery } " = = "1" ] ; then
infoMessage = "Please Login for Recovery"
setupPhase = "recovery"
else
infoMessage = "Please Login for Update"
setupPhase = "update"
fi
2021-08-20 00:33:20 +02:00
2018-11-27 04:19:57 +01:00
fi
2021-08-04 00:18:30 +02:00
# signal "WAIT LOOP: SETUP" to LCD, SSH & WEBAPI
echo " Displaying Info Message: ${ infoMessage } " >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "waitsetup"
/home/admin/_cache.sh set message " ${ infoMessage } "
/home/admin/_cache.sh set setupPhase " ${ setupPhase } "
2021-08-04 00:18:30 +02:00
#############################################
# WAIT LOOP: USER SETUP/UPDATE/MIGRATION
# until SSH or WEBUI setup data is available
#############################################
2021-09-30 20:37:00 +02:00
echo "## WAIT LOOP: USER SETUP/UPDATE/MIGRATION" >> ${ logFile }
2021-08-04 00:18:30 +02:00
until [ " ${ state } " = = "waitprovision" ]
do
# get fresh info about data drive (in case the hdd gets disconnected)
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
if [ " ${ hddCandidate } " = = "" ] ; then
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "lost-hdd" "Lost HDD connection .. triggering reboot." "happened during WAIT LOOP: USER SETUP/UPDATE/MIGRATION" ${ logFile }
2021-08-19 00:18:59 +02:00
sleep 8
2021-08-18 21:24:18 +02:00
sudo shutdown -r now
sleep 100
exit 0
fi
2021-12-14 23:34:35 +01:00
# detect if network get deconnected again (call directly instead of cache)
2021-08-18 21:24:18 +02:00
# --> "removing network cable" can be used as signal to shutdown clean on test startup
2021-12-14 23:34:35 +01:00
source <( /home/admin/config.scripts/internet.sh status local )
2021-08-18 21:24:18 +02:00
if [ " ${ localip } " = = "" ] ; then
sed -i "s/^state=.*/state=errorNetwork/g" ${ infoFile }
2021-08-19 00:18:59 +02:00
sleep 8
2021-08-18 21:36:22 +02:00
sudo shutdown now
2021-08-04 00:18:30 +02:00
sleep 100
exit 0
fi
# give the loop a little bed time
sleep 4
2021-12-14 23:34:35 +01:00
# check for updated state value from SSH-UI or WEB-UI for loop
source <( /home/admin/_cache.sh get state)
2021-08-04 00:18:30 +02:00
done
#############################################
# PROVISION PROCESS
#############################################
# refresh data from info file
2021-12-14 23:34:35 +01:00
source <( /home/admin/_cache.sh get state setupPhase)
2021-09-30 20:37:00 +02:00
echo " # PROVISION PROCESS with setupPhase( ${ setupPhase } ) " >> ${ logFile }
2021-08-04 00:18:30 +02:00
# mark system on sd card as in setup process
echo "the provision process was started but did not finish yet" > /home/admin/provision.flag
2021-09-30 20:37:00 +02:00
# make sure HDD is mounted (could be freshly formatted by user on last loop)
2021-09-29 13:07:24 +02:00
source <( /home/admin/config.scripts/blitz.datadrive.sh status)
2021-09-30 20:37:00 +02:00
echo " Temp mounting (2) data drive ( $hddCandidate ) " >> ${ logFile }
2021-09-05 15:05:45 +02:00
if [ " ${ hddFormat } " != "btrfs" ] ; then
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh tempmount ${ hddPartitionCandidate } )
else
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh tempmount ${ hddCandidate } )
fi
2021-09-30 20:37:00 +02:00
echo " Temp mounting (2) result: ${ isMounted } " >> ${ logFile }
2021-09-05 15:05:45 +02:00
2021-09-29 13:07:24 +02:00
# check that HDD was temp mounted
if [ " ${ isMounted } " != "1" ] ; then
sed -i "s/^state=.*/state=errorHDD/g" ${ infoFile }
sed -i "s/^message=.*/message='Was not able to mount HDD (2)'/g" ${ infoFile }
exit 1
fi
2019-12-12 14:51:21 +01:00
# make sure all links between directories/drives are correct
2021-09-30 20:37:00 +02:00
echo "Refreshing links between directories/drives .." >> ${ logFile }
2019-12-12 14:51:21 +01:00
sudo /home/admin/config.scripts/blitz.datadrive.sh link
2021-08-04 00:18:30 +02:00
# copy over the raspiblitz.conf created from setup to HDD
configExists = $( ls /mnt/hdd/raspiblitz.conf 2>/dev/null | grep -c "raspiblitz.conf" )
if [ " ${ configExists } " != "1" ] ; then
2021-08-24 19:35:11 +02:00
sudo cp /var/cache/raspiblitz/temp/raspiblitz.conf ${ configFile }
2021-08-04 00:18:30 +02:00
fi
2021-12-14 23:34:35 +01:00
# enable tor service
sudo /home/admin/config.scripts/tor.install.sh enable >> ${ logFile }
2021-08-04 00:18:30 +02:00
2021-12-14 23:34:35 +01:00
# kick-off provision process
/home/admin/_cache.sh set state "provision"
/home/admin/_cache.sh set message "Starting Provision"
2021-09-30 20:37:00 +02:00
# add some debug info to logfile
echo "# df " >> ${ logFile }
df >> ${ logFile }
echo "# lsblk -o NAME,FSTYPE,LABEL " >> ${ logFile }
lsblk -o NAME,FSTYPE,LABEL >> ${ logFile }
2021-10-01 17:09:49 +02:00
# if migrationFile was uploaded - now import it
echo " # migrationFile( ${ migrationFile } ) " >> ${ logFile }
if [ " ${ migrationFile } " != "" ] ; then
2021-12-14 23:34:35 +01:00
# unpack
2021-10-01 17:09:49 +02:00
sed -i "s/^message=.*/message='Unpacking Migration Data'/g" ${ infoFile }
2021-12-14 23:34:35 +01:00
source <( /home/admin/config.scripts/blitz.migration.sh import " ${ migrationFile } " )
# check for errors
if [ " ${ error } " != "" ] ; then
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "migration-import-error" "blitz.migration.sh import exited with error" " /home/admin/config.scripts/blitz.migration.sh import ${ migrationFile } --> ${ error } " ${ logFile }
exit 1
fi
# signal recovery provision phase
2021-10-01 23:17:36 +02:00
setupPhase = "recovery"
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set setupPhase " ${ setupPhase } "
2021-10-01 17:09:49 +02:00
fi
2021-12-14 23:34:35 +01:00
# load fresh config data
echo " # Sourcing ${ configFile } " >> ${ logFile }
cat ${ configFile } >> ${ logFile }
source ${ configFile }
# load fresh setup data
echo " # Sourcing ${ setupFile } " >> ${ logFile }
cat ${ setupFile } >> ${ logFile }
source ${ setupFile }
# make sure basic info is in raspiblitz.info
/home/admin/_cache.sh set network " ${ network } "
/home/admin/_cache.sh set chain " ${ chain } "
/home/admin/_cache.sh set lightning " ${ lightning } "
2021-08-04 00:18:30 +02:00
###################################
# Set Password A (in all cases)
if [ " ${ passwordA } " = = "" ] ; then
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "missing-passworda" " missing passwordA in ( ${ setupFile } ) " "" ${ logFile }
2021-08-04 00:18:30 +02:00
exit 1
fi
2021-09-30 20:37:00 +02:00
echo "# setting PASSWORD A" >> ${ logFile }
2021-08-04 00:18:30 +02:00
sudo /home/admin/config.scripts/blitz.setpassword.sh a " ${ passwordA } " >> ${ logFile }
# if setup - run provision setup first
if [ " ${ setupPhase } " = = "setup" ] ; then
echo "Calling _provision.setup.sh for basic setup tasks .." >> $logFile
2021-11-30 11:58:06 +00:00
echo "Follow in a new terminal with: 'tail -f raspiblitz.provision-setup.log'" >> $logFile
2021-08-04 00:18:30 +02:00
sed -i "s/^message=.*/message='Provision Setup'/g" ${ infoFile }
2021-09-29 14:12:04 +02:00
/home/admin/_provision.setup.sh
2021-08-26 11:28:56 +02:00
errorState = $?
2021-09-09 01:19:09 +02:00
sudo cat /home/admin/raspiblitz.provision-setup.log
2021-08-26 11:28:56 +02:00
if [ " $errorState " != "0" ] ; then
2021-12-14 23:34:35 +01:00
# only trigger an error message if the script hasnt itself triggered an error message already
source <( /home/admin/_cache.sh get state)
if [ " ${ state } " != "error" ] ; then
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "provision-setup-exit" " unknown or syntax error on ( ${ errorState } ) _provision.setup.sh " "" ${ logFile }
fi
2021-08-04 00:18:30 +02:00
exit 1
2018-12-11 19:37:32 +01:00
fi
2021-08-04 00:18:30 +02:00
fi
2021-10-01 23:17:36 +02:00
# if migration from other nodes - run the migration provision first
2021-08-04 00:18:30 +02:00
if [ " ${ setupPhase } " = = "migration" ] ; then
echo "Calling _provision.migration.sh for possible migrations .." >> $logFile
sed -i "s/^message=.*/message='Provision migration'/g" ${ infoFile }
2021-09-29 14:12:04 +02:00
/home/admin/_provision.migration.sh
2021-08-26 11:28:56 +02:00
errorState = $?
2021-09-29 14:12:04 +02:00
cat /home/admin/raspiblitz.provision-migration.log
2021-08-26 11:28:56 +02:00
if [ " $errorState " != "0" ] ; then
2021-12-14 23:34:35 +01:00
# only trigger an error message if the script hasnt itself triggered an error message already
source <( /home/admin/_cache.sh get state)
if [ " ${ state } " != "error" ] ; then
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "provision-migration-exit" " unknown or syntax error on ( ${ errorState } ) _provision.migration.sh " "" ${ logFile }
fi
2021-08-04 00:18:30 +02:00
exit 1
2018-12-11 19:37:32 +01:00
fi
2021-08-04 00:18:30 +02:00
fi
2021-09-09 00:12:46 +02:00
# if update/recovery/migration-followup
2021-08-04 00:18:30 +02:00
if [ " ${ setupPhase } " = = "update" ] || [ " ${ setupPhase } " = = "recovery" ] || [ " ${ setupPhase } " = = "migration" ] ; then
echo "Calling _provision.update.sh .." >> $logFile
sed -i "s/^message=.*/message='Provision Update/Recovery/Migration'/g" ${ infoFile }
2021-09-29 14:12:04 +02:00
/home/admin/_provision.update.sh
2021-08-26 11:28:56 +02:00
errorState = $?
2021-09-29 14:12:04 +02:00
cat /home/admin/raspiblitz.provision-update.log
2021-08-26 11:28:56 +02:00
if [ " $errorState " != "0" ] ; then
2021-12-14 23:34:35 +01:00
# only trigger an error message if the script hasnt itself triggered an error message already
source <( /home/admin/_cache.sh get state)
if [ " ${ state } " != "error" ] ; then
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "provision-update-exit" " unknown or syntax error on ( ${ errorState } ) _provision.update.sh " "" ${ logFile }
fi
2021-08-04 00:18:30 +02:00
exit 1
2018-12-11 20:12:21 +01:00
fi
2018-12-11 19:37:32 +01:00
fi
2019-12-11 01:25:18 +01:00
2021-08-04 00:18:30 +02:00
# finalize provisioning
echo " Calling _bootstrap.provision.sh for general system provisioning ( ${ setupPhase } ) .. " >> $logFile
sed -i "s/^message=.*/message='Provision Basics'/g" ${ infoFile }
2021-09-29 14:11:50 +02:00
/home/admin/_provision_.sh
2021-08-26 11:28:56 +02:00
errorState = $?
if [ " $errorState " != "0" ] ; then
2021-12-14 23:34:35 +01:00
# only trigger an error message if the script hasnt itself triggered an error message already
source <( /home/admin/_cache.sh get state)
if [ " ${ state } " != "error" ] ; then
/home/admin/config.scripts/blitz.error.sh _bootstrap.sh "provision-exit" " unknown or syntax error on ( ${ errorState } ) _provision_.sh " "" ${ logFile }
fi
2021-08-04 00:18:30 +02:00
exit 1
2018-11-27 04:19:57 +01:00
fi
2021-08-26 13:14:00 +02:00
# everyone can read the config but it can only be
# edited/written by root ot admin user (part of group sudo)
2021-12-14 23:34:35 +01:00
chown root:sudo ${ configFile }
chmod 664 ${ configFile }
2021-08-26 13:14:00 +02:00
2021-09-12 17:45:57 +02:00
# delete provision in progress flag
2021-12-14 23:34:35 +01:00
rm /home/admin/provision.flag
2021-09-12 17:45:57 +02:00
2021-08-04 00:18:30 +02:00
# mark provision process done
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set message "Provision Done"
2019-12-12 14:51:21 +01:00
2021-08-04 00:18:30 +02:00
# wait until syncProgress is available (neeed for final dialogs)
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "waitsync"
btc_default_ready = "0"
while [ " ${ btc_default_ready } " != "1" ]
2021-08-04 00:18:30 +02:00
do
2021-12-14 23:34:35 +01:00
source <( /home/admin/_cache.sh get btc_default_ready)
echo " # waitsync loop ... btc_default_ready( ${ btc_default_ready } ) " >> $logFile
2021-08-04 00:18:30 +02:00
sleep 2
done
2020-07-22 21:20:11 +02:00
2021-08-04 00:18:30 +02:00
###################################################
2021-10-05 12:27:52 +02:00
# HANDOVER TO FINAL SETUP CONTROLLER
2021-08-04 00:18:30 +02:00
###################################################
2020-07-22 21:20:11 +02:00
2021-10-05 12:27:52 +02:00
echo "# HANDOVER TO FINAL SETUP CONTROLLER ..." >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set state "waitfinal"
/home/admin/_cache.sh set message "Setup Done"
2020-10-29 23:27:30 +01:00
2021-10-05 12:27:52 +02:00
# system has to wait before reboot to present like seed words and other info/options to user
echo "BOOTSTRAP EXIT ... waiting for final setup controller to initiate final reboot." >> $logFile
exit 1
2021-08-26 17:41:53 +02:00
2021-08-04 00:18:30 +02:00
else
############################
############################
# NORMAL START BOOTSTRAP (not executed after setup)
# Blockchain & Lightning not running
############################
#################################
# FIX BLOCKCHAINDATA OWNER (just in case)
# https://github.com/rootzoll/raspiblitz/issues/239#issuecomment-450887567
#################################
2021-12-14 23:34:35 +01:00
chown bitcoin:bitcoin -R /mnt/hdd/bitcoin 2>/dev/null
2021-08-04 00:18:30 +02:00
#################################
# FIX BLOCKING FILES (just in case)
# https://github.com/rootzoll/raspiblitz/issues/1901#issue-774279088
# https://github.com/rootzoll/raspiblitz/issues/1836#issue-755342375
2021-12-14 23:34:35 +01:00
rm -f /mnt/hdd/bitcoin/bitcoind.pid 2>/dev/null
rm -f /mnt/hdd/bitcoin/.lock 2>/dev/null
2021-08-04 00:18:30 +02:00
################################
# DELETE LOG & LOCK FILES
################################
# LND and Blockchain Errors will be still in systemd journals
# /mnt/hdd/bitcoin/debug.log
2021-12-14 23:34:35 +01:00
rm /mnt/hdd/${ network } /debug.log 2>/dev/null
2021-08-04 00:18:30 +02:00
# /mnt/hdd/lnd/logs/bitcoin/mainnet/lnd.log
2021-12-14 23:34:35 +01:00
rm /mnt/hdd/lnd/logs/${ network } /${ chain } net/lnd.log 2>/dev/null
2021-08-04 00:18:30 +02:00
# https://github.com/rootzoll/raspiblitz/issues/1700
2021-12-14 23:34:35 +01:00
rm /mnt/storage/app-storage/electrs/db/mainnet/LOCK 2>/dev/null
2018-12-11 00:53:07 +01:00
2018-10-16 12:50:36 +02:00
fi
2021-08-04 00:18:30 +02:00
##############################
##############################
# BOOSTRAP IN EVERY SITUATION
##############################
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set setupPhase "starting"
2021-08-04 00:18:30 +02:00
# load data from config file fresh
echo "load configfile data" >> $logFile
source ${ configFile }
2021-12-14 23:34:35 +01:00
# if a WIFI config exists backup to HDD
source <( /home/admin/config.scripts/internet.sh status)
if [ ${ configWifiExists } -eq 1 ] ; then
echo "Making Backup Copy of WIFI config to HDD" >> $logFile
cp /etc/wpa_supplicant/wpa_supplicant.conf /mnt/hdd/app-data/wpa_supplicant.conf
2021-09-09 12:51:50 +02:00
fi
2021-12-14 23:34:35 +01:00
# make sure users have latest credentials (if lnd is on)
if [ " ${ lightning } " = = "lnd" ] || [ " ${ lnd } " = = "on" ] ; then
2020-05-03 23:58:34 +02:00
echo "running LND users credentials update" >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/lnd.credentials.sh sync >> $logFile
2019-04-25 15:01:53 +02:00
else
2021-08-27 03:59:21 -04:00
echo "skipping LND credentials sync" >> $logFile
2019-04-25 15:01:53 +02:00
fi
2020-07-04 01:00:15 +02:00
################################
# MOUNT BACKUP DRIVE
# if "localBackupDeviceUUID" is set in
# raspiblitz.conf mount it on boot
################################
2020-07-04 01:57:12 +02:00
echo " Checking if additional backup device is configured .. ( ${ localBackupDeviceUUID } ) " >> $logFile
2020-07-12 11:03:44 +01:00
if [ " ${ localBackupDeviceUUID } " != "" ] && [ " ${ localBackupDeviceUUID } " != "off" ] ; then
2020-07-04 01:57:12 +02:00
echo " Yes - Mounting BackupDrive: ${ localBackupDeviceUUID } " >> $logFile
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/blitz.backupdevice.sh mount >> $logFile
2020-07-04 01:17:22 +02:00
else
2020-07-04 01:47:24 +02:00
echo "No additional backup device was configured." >> $logFile
2020-07-04 01:00:15 +02:00
fi
2019-12-12 14:51:21 +01:00
#####################################
# CLEAN HDD TEMP
#####################################
echo "CLEANING TEMP DRIVE/FOLDER" >> $logFile
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh clean temp)
if [ ${# error } -gt 0 ] ; then
echo " FAIL: ${ error } " >> $logFile
else
echo "OK: Temp cleaned" >> $logFile
fi
2021-08-04 00:18:30 +02:00
###############################
# RAID data check (BRTFS)
###############################
# see https://github.com/rootzoll/raspiblitz/issues/360#issuecomment-467698260
if [ ${ isRaid } -eq 1 ] ; then
echo "TRIGGERING BTRFS RAID DATA CHECK ..."
echo "Check status with: sudo btrfs scrub status /mnt/hdd/"
2021-12-14 23:34:35 +01:00
btrfs scrub start /mnt/hdd/
fi
####################
# FORCE UASP FLAG
####################
# if uasp.force flag was set on sd card - now move into raspiblitz.conf
if [ -f "/boot/uasp.force" ] ; then
/home/admin/config.scripts/blitz.conf.sh set forceUasp "on"
echo "DONE forceUasp=on recorded in raspiblitz.conf" >> $logFile
2021-08-04 00:18:30 +02:00
fi
2020-05-26 19:07:40 +02:00
######################################
# PREPARE SUBSCRIPTIONS DATA DIRECTORY
######################################
2021-08-27 03:59:21 -04:00
if [ -d "/mnt/hdd/app-data/subscriptions" ] ; then
2020-05-26 19:07:40 +02:00
echo "OK: subscription data directory exists"
2021-12-07 21:59:37 +01:00
sudo chown admin:admin /mnt/hdd/app-data/subscriptions
2020-05-26 19:07:40 +02:00
else
echo "CREATE: subscription data directory"
2021-12-14 23:34:35 +01:00
mkdir /mnt/hdd/app-data/subscriptions
chown admin:admin /mnt/hdd/app-data/subscriptions
2020-05-26 19:07:40 +02:00
fi
2020-06-17 22:20:06 +02:00
# make sure that bitcoin service is active
sudo systemctl enable ${ network } d
2021-08-24 20:28:32 +02:00
# make sure setup/provision is marked as done
2021-12-14 23:34:35 +01:00
/home/admin/_cache.sh set setupPhase "done"
/home/admin/_cache.sh set state "ready"
/home/admin/_cache.sh set message "Node Running"
# relax systemscan on certain values
/home/admin/_cache.sh focus internet_localip -1
# if node is stil in inital blockchain download
source <( /home/admin/_cache.sh get btc_default_sync_initialblockdownload)
if [ " ${ btc_default_sync_initialblockdownload } " = = "1" ] ; then
echo "Node is still in IBD .. refresh btc_default_sync_progress faster" >> $logFile
/home/admin/_cache.sh focus btc_default_sync_progress 0
fi
2021-08-04 00:18:30 +02:00
2019-05-29 15:50:37 +01:00
echo "DONE BOOTSTRAP" >> $logFile
exit 0