raspiblitz/home.admin/_bootstrap.sh

742 lines
25 KiB
Bash
Raw Normal View History

#!/bin/bash
# This script runs on every start called by boostrap.service
2021-05-20 22:52:31 -05:00
# see logs with --> tail -n 100 /home/admin/raspiblitz.log
2019-12-11 13:47:21 +01:00
################################
# BASIC SETTINGS
2019-12-11 13:47:21 +01: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
2019-02-05 10:33:34 +00:00
# make raspiblitz.conf if not there
sudo touch /mnt/hdd/raspiblitz.conf
2018-11-27 04:19:57 +01:00
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-05-21 11:52:20 -05:00
# SETUPFILE
# this key/value file contains the state during the setup process
setupFile="/var/cache/raspiblitz/temp/raspiblitz.setup"
2021-05-03 23:54:55 +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
2019-01-21 00:25:32 +01:00
# set default values for raspiblitz.info
network=""
chain=""
setupStep=0
2021-05-04 16:40:23 +02:00
setupPhase='boot'
fsexpanded=0
2021-04-08 00:02:57 +02:00
# see https://github.com/rootzoll/raspiblitz/issues/1265#issuecomment-813369284
displayClass="lcd"
displayType=""
fundRecovery=0
2019-01-21 00:25:32 +01:00
2021-05-03 23:54:55 +02:00
################################
# INIT raspiblitz.info
################################
2019-01-21 00:25:32 +01:00
# try to load old values if available (overwrites defaults)
2019-01-21 00:49:30 +01:00
source ${infoFile} 2>/dev/null
2019-01-21 00:25:32 +01:00
2021-04-08 00:02:57 +02:00
# try to load config values if available (config overwrites info)
source ${configFile} 2>/dev/null
2021-05-04 01:05:50 +02:00
# get first basic network info
source <(/home/admin/config.scripts/internet.sh status)
2021-05-18 21:05:35 -05:00
# get basic hardware info
2021-06-20 20:59:05 +02:00
source <(/home/admin/config.scripts/.sh status)
# get basic dns info
source <(sudo /home/admin/config.scripts/internet.dns.sh test nodialog)
2021-05-18 21:05:35 -05:00
2019-01-21 00:25:32 +01:00
# resetting info file
2018-11-27 04:19:57 +01:00
echo "Resetting the InfoFile: ${infoFile}"
echo "state=starting" > $infoFile
2018-12-12 16:12:30 +01:00
echo "message=" >> $infoFile
echo "baseimage=${baseimage}" >> $infoFile
echo "cpu=${cpu}" >> $infoFile
2021-05-18 21:05:35 -05:00
echo "board=${board}" >> $infoFile
echo "ramMB=${ramMB}" >> $infoFile
2019-01-21 00:25:32 +01:00
echo "network=${network}" >> $infoFile
echo "chain=${chain}" >> $infoFile
2021-05-04 01:05:50 +02:00
echo "localip='${localip}'" >> $infoFile
echo "online='${online}'" >> $infoFile
2021-06-20 20:59:05 +02:00
echo "dnsworking=${dnsworking}" >> $infoFile
echo "fsexpanded=${fsexpanded}" >> $infoFile
2021-04-08 00:02:57 +02:00
echo "displayClass=${displayClass}" >> $infoFile
echo "displayType=${displayType}" >> $infoFile
2019-01-21 00:25:32 +01:00
echo "setupStep=${setupStep}" >> $infoFile
2021-05-04 16:40:23 +02:00
echo "setupPhase=${setupPhase}" >> $infoFile
echo "fundRecovery=${fundRecovery}" >> $infoFile
if [ "${setupStep}" != "100" ]; then
echo "hostname=${hostname}" >> $infoFile
fi
2018-11-27 05:58:53 +01:00
sudo chmod 777 ${infoFile}
2018-10-15 22:31:56 +02:00
2021-05-25 13:55:58 -05:00
######################################
# CHECK SD CARD INCONSISTENT STATE
# 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
2021-06-21 17:09:03 +02:00
sudo systemctl stop ${network}d 2>/dev/null
sudo systemctl stop lnd 2>/dev/null
2021-05-25 13:55:58 -05:00
sed -i "s/^state=.*/state=inconsistentsystem/g" ${infoFile}
sed -i "s/^message=.*/message='provision did not ran thru'/g" ${infoFile}
echo "FAIL: 'provision did not ran thru - need fresh sd card!" >> ${logFile}
exit 1
fi
2021-05-03 23:54:55 +02:00
######################################
# SECTION FOR POSSIBLE REBOOT ACTIONS
systemInitReboot=0
2019-04-23 16:22:42 +02:00
2021-05-03 23:54:55 +02:00
################################
# 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
else
2021-05-03 23:54:55 +02:00
echo "No HDMI switch found. " >> $logFile
fi
2019-03-13 16:04:09 +01:00
2021-05-03 23:54:55 +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
################################
2021-05-03 23:54:55 +02:00
sshReset=$(sudo ls /boot/ssh.reset* 2>/dev/null | grep -c reset)
if [ ${sshReset} -eq 1 ]; then
# delete that file (to prevent loop)
sudo rm /boot/ssh.reset* >> $logFile
# delete ssh certs
echo "SSHRESET switch found ... stopping SSH and deleting old certs" >> $logFile
sudo systemctl stop sshd >> $logFile
sudo rm /mnt/hdd/ssh/ssh_host* >> $logFile
sudo ssh-keygen -A >> $logFile
systemInitReboot=1
else
echo "No SSHRESET switch found. " >> $logFile
fi
2021-05-26 09:21:42 -05:00
################################
# BACKGROUND TASK RUN FROM BEGINNING
# on 1.7 sd card build background task runs after boostrap
# but bootstrap already needs background task running now
# REMOVE ON v1.8 release #2328
################################
backgroundNeedsEdit=$(sudo cat /etc/systemd/system/background.service 2>/dev/null | grep -c 'Wants=bootstrap.service')
if [ ${backgroundNeedsEdit} -eq 1 ]; then
echo "BACKGROUND EDIT needed ..." >> $logFile
sudo sed -i "s/^Wants=.*/Wants=network.target/g" /etc/systemd/system/background.service
sudo sed -i "s/^After=.*/After=network.target/g" /etc/systemd/system/background.service
systemInitReboot=1
else
echo "BACKGROUND EDIT already done. " >> $logFile
fi
2021-05-03 23:54:55 +02:00
################################
# FS EXPAND
# if a file called 'ssh.reset' gets
# placed onto the boot part of
# the sd card - delete old ssh data
################################
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-05-04 01:35:48 +02:00
elif [ "${tooSmall}" == "1" ]; then
echo "!!! FAIL !!!!!!!!!!!!!!!!!!!!" >> $logFile
echo "SDCARD TOO SMALL 16G minimum" >> $logFile
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >> $logFile
sed -i "s/^state=.*/state=sdtoosmall/g" ${infoFile}
echo "System stopped. Please cut power." >> $logFile
sleep 6000
sudo shutdown -r now
slepp 100
exit 1
2021-05-03 23:54:55 +02:00
else
echo "No FS EXPAND needed. needsExpansion(${needsExpansion}) fsexpanded(${fsexpanded})" >> $logFile
fi
################################
# UASP FIX - first try
# if HDD is connected on start
################################
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh uasp-fix)
if [ "${neededReboot}" == "1" ]; then
echo "UASP FIX applied (1st-try) ... reboot needed." >> $logFile
systemInitReboot=1
else
echo "No UASP FIX needed (1st-try)." >> $logFile
fi
######################################
# CHECK IF REBOOT IS NEEDED
# from actions above
if [ "${systemInitReboot}" == "1" ]; then
sudo cp ${logFile} ${logFile}.systeminit
sudo sed -i "s/^state=.*/state=reboot/g" ${infoFile}
sudo shutdown -r now
sleep 100
exit 0
fi
################################
# 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)
2021-04-08 00:02:57 +02:00
/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
2019-01-15 02:37:14 +01:00
################################
# GENERATE UNIQUE SSH PUB KEYS
# on first boot up
################################
numberOfPubKeys=$(sudo ls /etc/ssh/ | grep -c 'ssh_host_')
if [ ${numberOfPubKeys} -eq 0 ]; then
echo "*** Generating new SSH PubKeys" >> $logFile
sudo dpkg-reconfigure openssh-server
echo "OK" >> $logFile
fi
2018-10-15 22:31:56 +02:00
################################
2021-05-03 23:54:55 +02:00
# CLEANING BOOT SYSTEM
2018-10-15 22:31:56 +02:00
################################
2021-05-03 23:54:55 +02:00
# resetting start count files
echo "SYSTEMD RESTART LOG: blockchain (bitcoind/litecoind)" > /home/admin/systemd.blockchain.log
echo "SYSTEMD RESTART LOG: lightning (LND)" > /home/admin/systemd.lightning.log
sudo chmod 777 /home/admin/systemd.blockchain.log
sudo chmod 777 /home/admin/systemd.lightning.log
2020-01-24 21:46:51 +01:00
2021-05-03 23:54:55 +02:00
# 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"
echo "ACTION --> DELETED ALL LOGS"
if [ -d "/var/log/nginx" ]; then
nginxLog=1
echo "/var/log/nginx is present"
fi
sudo rm -r /var/log/*
if [ $nginxLog == 1 ]; then
sudo mkdir /var/log/nginx
echo "Recreated /var/log/nginx"
fi
sleep 3
echo "WARN !! Logs in /var/log in were bigger then 1GB and got emergency delete to prevent fillup."
echo "If you see this in the logs please report to the GitHub issues, so LOG config needs to hbe optimized."
else
2021-05-03 23:54:55 +02:00
echo "OK - logs are at ${logsMegaByte} MB - within safety limit"
2020-01-24 21:46:51 +01:00
fi
2021-05-03 23:54:55 +02:00
echo ""
2020-01-24 21:46:51 +01:00
2021-05-03 23:54:55 +02:00
###############################
# WAIT FOR ALL SERVICES
2020-03-18 14:07:27 -07:00
2021-05-03 23:54:55 +02:00
# get the state of data drive
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2020-03-18 14:07:27 -07:00
2018-10-16 12:50:36 +02:00
################################
2021-05-04 01:05:50 +02:00
# WAIT LOOP: HDD CONNECTED
2018-10-16 12:50:36 +02:00
################################
2021-05-03 23:54:55 +02:00
2021-05-04 01:44:03 +02:00
echo "Waiting for HDD/SSD ..." >> $logFile
until [ ${isMounted} -eq 1 ] || [ ${#hddCandidate} -gt 0 ]
do
2021-05-04 01:44:03 +02:00
# recheck HDD/SSD
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2021-05-04 01:44:03 +02:00
echo "isMounted: $isMounted"
echo "hddCandidate: $hddCandidate"
2021-05-04 15:24:38 +02:00
# in case of HDD analyse ERROR
2021-05-05 02:08:22 +02:00
if [ "${hddError}" != "" ]; then
2021-05-04 15:24:38 +02:00
echo "FAIL - error on HDD analysis: ${hddError}" >> $logFile
sed -i "s/^state=.*/state=errorHDD/g" ${infoFile}
sed -i "s/^message=.*/message='${hddError}'/g" ${infoFile}
2021-05-05 02:08:22 +02:00
elif [ "${isMounted}" == "0" ] && [ "${hddCandidate}" == "" ]; then
2019-12-11 13:47:21 +01:00
sed -i "s/^state=.*/state=noHDD/g" ${infoFile}
2021-05-24 13:41:58 -05:00
sed -i "s/^message=.*/message='>=1TB'/g" ${infoFile}
fi
2021-05-04 01:44:03 +02:00
# wait for next check
sleep 2
2021-05-04 01:44:03 +02:00
done
2021-05-04 01:44:03 +02:00
echo "HDD/SSD connected: ${$hddCandidate}" >> $logFile
2019-12-12 16:06:07 +01:00
2021-05-05 02:32:13 +02:00
# write info for LCD
sed -i "s/^state=.*/state=system-init/g" ${infoFile}
sed -i "s/^message=.*/message='please wait'/g" ${infoFile}
2021-05-03 23:54:55 +02:00
####################################
# WIFI RESTORE from HDD works with
# mem copy from datadrive inspection
####################################
# check if there is a WIFI configuration to backup or restore
/home/admin/config.scripts/internet.wifi.sh backup-restore >> $logFile
################################
# UASP FIX - second try
# when HDD gets connected later
################################
2021-05-05 02:32:13 +02:00
sed -i "s/^message=.*/message='checking HDD'/g" ${infoFile}
2021-05-03 23:54:55 +02:00
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh uasp-fix)
if [ "${neededReboot}" == "1" ]; then
2021-05-05 01:30:39 +02:00
echo "UASP FIX applied (2nd-try) ... reboot needed." >> $logFile
2021-05-03 23:54:55 +02:00
sudo cp ${logFile} ${logFile}.uasp
sudo sed -i "s/^state=.*/state=reboot/g" ${infoFile}
sudo shutdown -r now
sleep 100
exit 0
else
2021-05-05 01:30:39 +02:00
echo "No UASP FIX needed (2nd-try)." >> $logFile
2021-05-03 23:54:55 +02:00
fi
2021-05-04 01:05:50 +02:00
###################################
# WAIT LOOP: LOCALNET / INTERNET
# after HDD > can contain WIFI conf
###################################
2021-05-03 23:54:55 +02:00
gotLocalIP=0
until [ ${gotLocalIP} -eq 1 ]
do
2021-05-04 01:05:50 +02:00
2021-05-04 01:44:03 +02:00
# get latest network info & update raspiblitz.info
2021-05-03 23:54:55 +02:00
source <(/home/admin/config.scripts/internet.sh status)
2021-05-04 01:05:50 +02:00
sed -i "s/^localip=.*/localip='${localip}'/g" ${infoFile}
# check state of network
2021-05-03 23:54:55 +02:00
if [ ${dhcp} -eq 0 ]; then
# display user waiting for DHCP
2021-05-13 14:31:15 +02:00
sed -i "s/^state=.*/state=noDHCP/g" ${infoFile}
2021-05-03 23:54:55 +02:00
sed -i "s/^message=.*/message='Waiting for DHCP'/g" ${infoFile}
elif [ ${#localip} -eq 0 ]; then
if [ ${configWifiExists} -eq 0 ]; then
# display user to connect LAN
2021-05-13 14:31:15 +02:00
sed -i "s/^state=.*/state=noIP-LAN/g" ${infoFile}
2021-05-03 23:54:55 +02:00
sed -i "s/^message=.*/message='Connect the LAN/WAN'/g" ${infoFile}
else
# display user that wifi settings are not working
2021-05-13 14:31:15 +02:00
sed -i "s/^state=.*/state=noIP-WIFI/g" ${infoFile}
2021-05-03 23:54:55 +02:00
sed -i "s/^message=.*/message='WIFI Settings not working'/g" ${infoFile}
fi
elif [ ${online} -eq 0 ]; then
# display user that wifi settings are not working
sed -i "s/^state=.*/state=noInternet/g" ${infoFile}
sed -i "s/^message=.*/message='No connection to Internet'/g" ${infoFile}
else
gotLocalIP=1
fi
sleep 1
done
2019-12-12 16:06:07 +01:00
# write info for LCD
2021-05-05 02:32:13 +02:00
sed -i "s/^state=.*/state=inspect-hdd/g" ${infoFile}
2019-12-12 16:06:07 +01:00
sed -i "s/^message=.*/message='please wait'/g" ${infoFile}
# get fresh info about data drive to continue
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2018-11-27 04:19:57 +01:00
2018-12-10 14:48:02 +01:00
# check if the HDD is auto-mounted ( auto-mounted = setup-done)
2021-05-05 01:33:34 +02:00
echo "HDD already part of system: $isMounted" >> $logFile
2021-05-20 22:52:31 -05:00
############################
############################
# WHEN SETUP IS NEEDED
############################
if [ ${isMounted} -eq 0 ]; then
2021-05-21 11:52:20 -05:00
# write data needed for setup process into raspiblitz.info
2021-06-21 00:17:24 +02:00
echo "hddCandidate='${hddCandidate}'" >> ${infoFile}
2021-05-22 11:48:46 -05:00
echo "hddBlocksBitcoin=${hddBlocksBitcoin}" >> ${infoFile}
echo "hddBlocksLitecoin=${hddBlocksLitecoin}" >> ${infoFile}
echo "hddGotMigrationData=${hddGotMigrationData}" >> ${infoFile}
echo ""
2021-05-21 11:52:20 -05:00
2021-05-04 15:24:38 +02:00
echo "HDD is there but not AutoMounted yet - Waiting for user Setup/Update" >> $logFile
2018-11-27 04:19:57 +01:00
2021-05-21 11:52:20 -05:00
# determine correct setup phase
2021-05-04 15:24:38 +02:00
infoMessage="Please Login for Setup"
2021-05-04 16:40:23 +02:00
setupPhase="setup"
2021-05-21 11:52:20 -05:00
if [ "${hddGotMigrationData}" != "" ]; then
2021-05-04 15:24:38 +02:00
infoMessage="Please Login for Migration"
2021-05-04 16:40:23 +02:00
setupPhase="migration"
2021-05-21 11:52:20 -05:00
elif [ "${hddRaspiData}" == "1" ]; then
# determine if this is a recovery or an update
# TODO: improve version/update detetion later
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
2018-11-27 04:19:57 +01:00
fi
2021-05-04 16:40:23 +02:00
# signal "WAIT LOOP: SETUP" to LCD, SSH & WEBAPI
2021-05-04 15:24:38 +02:00
echo "Displaying Info Message: ${infoMessage}" >> $logFile
sed -i "s/^state=.*/state=waitsetup/g" ${infoFile}
sed -i "s/^message=.*/message='${infoMessage}'/g" ${infoFile}
2021-05-04 16:40:23 +02:00
sed -i "s/^setupPhase=.*/setupPhase='${setupPhase}'/g" ${infoFile}
2021-05-04 15:24:38 +02:00
#############################################
# WAIT LOOP: USER SETUP/UPDATE/MIGRATION
# until SSH or WEBUI setup data is available
#############################################
2021-05-24 13:21:26 -05:00
echo "## WAIT LOOP: USER SETUP/UPDATE/MIGRATION" >> $logFile
2021-05-20 21:13:35 -05:00
until [ "${state}" == "waitprovision" ]
2021-05-04 15:24:38 +02:00
do
2021-05-05 03:11:29 +02:00
# get fresh info about data drive (in case the hdd gets disconnected)
2021-05-05 01:56:43 +02:00
source <(sudo /home/admin/config.scripts/blitz.datadrive.sh status)
2021-05-05 03:04:42 +02:00
if [ "${hddCandidate}" == "" ]; then
2021-05-05 02:05:47 +02:00
echo "!!! WARNING !!! Lost HDD connection .. triggering reboot, to restart system-init." >> $logFile
2021-05-05 01:56:43 +02:00
sed -i "s/^state=.*/state=errorHDD/g" ${infoFile}
sed -i "s/^message=.*/message='lost HDD - rebooting'/g" ${infoFile}
2021-05-05 02:05:47 +02:00
sudo cp ${logFile} ${logFile}.error
2021-05-05 03:16:40 +02:00
sleep 6
2021-05-05 01:56:43 +02:00
sudo shutdown -r now
sleep 100
exit 0
fi
2021-05-05 10:42:53 +02:00
# give the loop a little bed time
sleep 4
2021-05-20 21:13:35 -05:00
# check info file for updated values
# especially the state for checking loop
source ${infoFile}
2021-05-20 22:52:31 -05:00
done
2021-05-05 02:32:13 +02:00
2021-05-23 10:08:58 -05:00
#############################################
# PROVISION PROCESS
#############################################
2021-05-23 23:38:12 -05:00
# refresh data from info file
source ${infoFile}
2021-05-24 13:21:26 -05:00
echo "# PROVISION PROCESS with setupPhase(${setupPhase})"
2021-05-23 23:38:12 -05:00
2021-05-25 13:55:58 -05: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
# temp mount the HDD
echo "Temp mounting data drive ($hddCandidate)" >> $logFile
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
# make sure all links between directories/drives are correct
echo "Refreshing links between directories/drives .." >> $logFile
sudo /home/admin/config.scripts/blitz.datadrive.sh link
2021-05-23 11:21:56 -05:00
# copy over the raspiblitz.conf created from setup to HDD
sudo cp /var/cache/raspiblitz/temp/raspiblitz.conf /mnt/hdd/raspiblitz.conf
2021-05-20 22:52:31 -05:00
# kick-off provision process
2021-05-21 11:52:20 -05:00
sed -i "s/^state=.*/state=provision/g" ${infoFile}
2021-05-20 22:52:31 -05:00
sed -i "s/^message=.*/message='Starting Provision'/g" ${infoFile}
2021-05-24 13:21:26 -05:00
2021-05-23 23:38:12 -05:00
# if setup - run provision setup first
if [ "${setupPhase}" == "setup" ]; then
echo "Calling _bootstrap.setup.sh for basic setup tasks .." >> $logFile
sudo /home/admin/_provision.setup.sh
if [ "$?" != "0" ]; then
2021-05-25 14:46:20 -05:00
echo "EXIT BECAUSE OF ERROR STATE ($?)" >> $logFile
2021-05-25 14:47:55 -05:00
echo "This can also happen if _provision.setup.sh has syntax errros" >> $logFile
2021-05-23 23:38:12 -05:00
exit 1
fi
fi
# if update - run provision update migration first
if [ "${setupPhase}" == "update" ]; then
echo "Calling _bootstrap.update.sh for possible update migrations .." >> $logFile
sudo /home/admin/_provision.update.sh
if [ "$?" != "0" ]; then
2021-05-25 14:46:20 -05:00
echo "EXIT BECAUSE OF ERROR STATE ($?)" >> $logFile
2021-05-25 14:47:55 -05:00
echo "This can also happen if _provision.update.sh has syntax errros" >> $logFile
2021-05-23 23:38:12 -05:00
exit 1
fi
fi
# if update - run provision update migration first
if [ "${setupPhase}" == "migration" ]; then
echo "Calling _bootstrap.migration.sh for possible update migrations .." >> $logFile
sudo /home/admin/_provision.migration.sh
if [ "$?" != "0" ]; then
2021-05-25 14:47:55 -05:00
echo "EXIT BECAUSE OF ERROR STATE ($?)" >> $logFile
echo "This can also happen if _provision.migration.sh has syntax errros" >> $logFile
2021-05-23 23:38:12 -05:00
exit 1
fi
fi
2021-05-24 14:15:53 -05:00
echo "Calling _bootstrap.provision.sh for general system provisioning (${setupPhase}) .." >> $logFile
2021-05-23 23:38:12 -05:00
sudo /home/admin/_provision_.sh
if [ "$?" != "0" ]; then
echo "EXIT BECAUSE OF ERROR STATE" >> $logFile
exit 1
2021-05-23 11:37:34 -05:00
fi
2021-05-20 22:52:31 -05:00
2021-05-23 10:08:58 -05:00
###################################################
2021-05-25 17:29:57 -05:00
# WAIT LOOP: AFTER FRESH SETUP, MIGRATION
2021-05-23 10:08:58 -05:00
# successfull update & recover can skip this
###################################################
2018-11-27 04:19:57 +01:00
2021-05-25 17:29:57 -05:00
if [ "${setupPhase}" == "setup" ] || [ "${setupPhase}" == "migration" ]; then
2021-05-25 22:10:57 -05:00
echo "# Gather info on blockchain sync ..." >> $logFile
source <(sudo /home/admin/config.scripts/blitz.statusscan.sh)
if [ "${syncProgress}" == "" ]; then syncProgress="0"; fi
syncProgress=$(echo "${syncProgress}" | cut -d "." -f1)
echo "syncProgress=${syncProgress}" >> ${infoFile}
2021-05-25 17:29:57 -05:00
echo "# Go into WAIT LOOP for final setup dialog ..." >> $logFile
sed -i "s/^state=.*/state=waitfinal/g" ${infoFile}
sed -i "s/^message=.*/message='Setup Done'/g" ${infoFile}
else
echo "# Skip WAIT LOOP boot directly into main menu ..." >> $logFile
sed -i "s/^state=.*/state=finalready/g" ${infoFile}
sed -i "s/^message=.*/message='Setup Done'/g" ${infoFile}
fi
source ${infoFile}
until [ "${state}" == "finalready" ]
2021-05-23 10:08:58 -05:00
do
# TODO: DETECT WHEN USER SETUP IS DONE
echo "TODO: DETECT WHEN USER FINAL DIALOG IS DONE" >> $logFile
2021-05-23 23:38:12 -05:00
# offer option to COPY BLOCKHCAIN (see 50copyHDD.sh)
2021-05-23 10:08:58 -05:00
# handle possible errors
# show seed words
# get latest network info & update raspiblitz.info (in case network changes)
source <(/home/admin/config.scripts/internet.sh status)
sed -i "s/^localip=.*/localip='${localip}'/g" ${infoFile}
2018-11-27 04:19:57 +01:00
2021-05-23 10:08:58 -05:00
# give the loop a little bed time
sleep 4
# check info file for updated values
# especially the state for checking loop
source ${infoFile}
done
########################################
# AFTER FINAL SETUP TASKS
2021-05-25 17:29:57 -05:00
# make sure for future starts that blockchain service gets started after boostrap
sed -i "s/^Wants=.*/Wants=bootstrap.service/g" /etc/systemd/system/${network}d.service
sed -i "s/^After=.*/After=network.target/g" /etc/systemd/system/${network}d.service
# delete provision in progress flag
sudo rm /home/admin/provision.flag
2021-05-25 18:27:15 -05:00
# delete setup data from RAM
sudo rm ${setupFile}
2021-05-25 17:29:57 -05:00
# signal that setup phas is over
sed -i "s/^setupPhase=.*/setupPhase='done'/g" ${infoFile}
2021-05-20 22:52:31 -05:00
2021-05-25 17:29:57 -05:00
else
############################
############################
# NORMAL START BOOTSTRAP (not executed after setup)
# Blockchain & Lightning not running
############################
######################################################################
# MAKE SURE LND RPC/REST ports are standard & open to all connections
######################################################################
sudo sed -i "s/^rpclisten=.*/rpclisten=0.0.0.0:10009/g" /mnt/hdd/lnd/lnd.conf
sudo sed -i "s/^restlisten=.*/restlisten=0.0.0.0:8080/g" /mnt/hdd/lnd/lnd.conf
#################################
# FIX BLOCKCHAINDATA OWNER (just in case)
# https://github.com/rootzoll/raspiblitz/issues/239#issuecomment-450887567
#################################
sudo chown bitcoin:bitcoin -R /mnt/hdd/bitcoin 2>/dev/null
#################################
# FIX BLOCKING FILES (just in case)
# https://github.com/rootzoll/raspiblitz/issues/1901#issue-774279088
# https://github.com/rootzoll/raspiblitz/issues/1836#issue-755342375
sudo rm -f /mnt/hdd/bitcoin/bitcoind.pid 2>/dev/null
sudo rm -f /mnt/hdd/bitcoin/.lock 2>/dev/null
################################
# DELETE LOG & LOCK FILES
################################
# LND and Blockchain Errors will be still in systemd journals
# /mnt/hdd/bitcoin/debug.log
sudo rm /mnt/hdd/${network}/debug.log 2>/dev/null
# /mnt/hdd/lnd/logs/bitcoin/mainnet/lnd.log
sudo rm /mnt/hdd/lnd/logs/${network}/${chain}net/lnd.log 2>/dev/null
# https://github.com/rootzoll/raspiblitz/issues/1700
sudo rm /mnt/storage/app-storage/electrs/db/mainnet/LOCK 2>/dev/null
2021-05-20 22:52:31 -05:00
fi
2021-05-25 17:29:57 -05:00
##############################
##############################
# BOOSTRAP IN EVERY SITUATION
##############################
2021-05-04 16:40:23 +02:00
sed -i "s/^setupPhase=.*/setupPhase='starting'/g" ${infoFile}
2020-07-22 21:20:11 +02:00
# if a WIFI config exists backup to HDD
configWifiExists=$(sudo cat /etc/wpa_supplicant/wpa_supplicant.conf 2>/dev/null| grep -c "network=")
if [ ${configWifiExists} -eq 1 ]; then
2020-07-22 21:20:11 +02:00
echo "Making Backup Copy of WIFI config to HDD" >> $logFile
sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /mnt/hdd/app-data/wpa_supplicant.conf
2020-07-22 21:20:11 +02:00
fi
2021-05-20 22:52:31 -05:00
# make sure lndAddress & lndPort exist in cofigfile
valueExists=$(cat ${configFile} | grep -c 'lndPort=')
if [ ${valueExists} -eq 0 ]; then
lndPort=$(sudo cat /mnt/hdd/lnd/lnd.conf | grep "^listen=*" | cut -f2 -d':')
if [ ${#lndPort} -eq 0 ]; then
lndPort="9735"
2019-04-03 02:58:10 +01:00
fi
2021-05-20 22:52:31 -05:00
echo "lndPort='${lndPort}'" >> ${configFile}
fi
valueExists=$(cat ${configFile} | grep -c 'lndAddress=')
if [ ${valueExists} -eq 0 ]; then
echo "lndAddress=''" >> ${configFile}
fi
2021-05-20 22:52:31 -05:00
# load data from config file fresh
echo "load configfile data" >> $logFile
source ${configFile}
2018-12-11 00:53:07 +01:00
2021-05-20 22:52:31 -05:00
# update public IP on boot - set to domain if available
/home/admin/config.scripts/internet.sh update-publicip ${lndAddress}
2018-10-16 12:50:36 +02:00
#################################
# MAKE SURE USERS HAVE LATEST LND CREDENTIALS
#################################
source ${configFile}
if [ ${#network} -gt 0 ] && [ ${#chain} -gt 0 ]; then
2019-04-27 23:04:20 +02:00
echo "running LND users credentials update" >> $logFile
sudo /home/admin/config.scripts/lnd.credentials.sh sync >> $logFile
2020-02-10 20:44:41 +01:00
else
echo "skipping LND credientials sync" >> $logFile
fi
################################
# MOUNT BACKUP DRIVE
# if "localBackupDeviceUUID" is set in
# raspiblitz.conf mount it on boot
################################
source ${configFile}
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
2020-07-04 13:00:35 +02:00
sudo /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
fi
#####################################
# 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-05-03 23:54:55 +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/"
sudo btrfs scrub start /mnt/hdd/
fi
2020-05-26 19:07:40 +02:00
######################################
# PREPARE SUBSCRIPTIONS DATA DIRECTORY
######################################
if [ -d "/mnt/hdd/app-data/subscrptions" ]; then
echo "OK: subscription data directory exists"
else
echo "CREATE: subscription data directory"
sudo mkdir /mnt/hdd/app-data/subscriptions
sudo chown admin:admin /mnt/hdd/app-data/subscriptions
fi
# mark that node is ready now
sed -i "s/^state=.*/state=ready/g" ${infoFile}
sed -i "s/^message=.*/message='Node Running'/g" ${infoFile}
2020-06-17 22:20:06 +02:00
# make sure that bitcoin service is active
sudo systemctl enable ${network}d
2021-05-05 01:01:11 +02:00
sed -i "s/^setupPhase=.*/setupPhase='done'/g" ${infoFile}
2021-05-25 18:28:37 -05:00
sed -i "s/^state=.*/state=ready/g" ${infoFile}
2021-05-04 16:40:23 +02:00
2019-05-29 15:50:37 +01:00
echo "DONE BOOTSTRAP" >> $logFile
exit 0