raspiblitz/home.admin/_bootstrap.sh

228 lines
7.5 KiB
Bash
Raw Normal View History

#!/bin/bash
2018-10-15 22:31:56 +02:00
# This script runs on every start calles by boostrap.service
# It makes sure that the system is configured like the
# default values or as in the config.
# For more details see background_raspiblitzSettings.md
# load codeVersion
source /home/admin/_version.info
2018-11-27 04:19:57 +01:00
################################
# FILES TO WORK WITH
################################
# 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"
echo "Writing logs to: ${logFile}"
echo "" > $logFile
echo "***********************************************" >> $logFile
echo "Running RaspiBlitz Bootstrap ${codeVersion}" >> $logFile
date >> $logFile
echo "***********************************************" >> $logFile
2018-11-27 04:19:57 +01:00
echo "Resetting the InfoFile: ${infoFile}"
echo "state=starting" > $infoFile
2018-11-27 05:58:53 +01:00
sudo chmod 777 ${infoFile}
2018-10-15 22:31:56 +02:00
################################
# AFTER BOOT SCRIPT
# when a process needs to
# execute stuff after a reboot
2018-11-27 04:19:57 +01:00
# it should in file
2018-10-16 12:50:36 +02:00
# /home/admin/setup.sh
2018-10-15 22:31:56 +02:00
################################
# check for after boot script
2018-10-16 12:50:36 +02:00
afterSetupScriptExists=$(ls /home/admin/setup.sh 2>/dev/null | grep -c setup.sh)
2018-10-15 22:31:56 +02:00
if [ ${afterSetupScriptExists} -eq 1 ]; then
echo "*** SETUP SCRIPT DETECTED ***"
# echo out script to journal logs
2018-10-16 12:50:36 +02:00
sudo cat /home/admin/setup.sh
2018-10-15 22:31:56 +02:00
# execute the after boot script
2018-10-16 12:50:36 +02:00
sudo /home/admin/setup.sh
2018-10-15 22:31:56 +02:00
# delete the after boot script
2018-10-16 12:50:36 +02:00
sudo rm /home/admin/setup.sh
2018-10-15 22:31:56 +02:00
# reboot again
echo "DONE wait 6 secs ... one more reboot needed ... "
sudo shutdown -r now
sleep 100
fi
################################
# PUBLIC IP
# for LND on startup
################################
printf "PUBLICIP=$(curl -vv ipinfo.io/ip 2> /run/publicip.log)\n" > /run/publicip;
chmod 774 /run/publicip
2018-10-16 12:50:36 +02:00
################################
2018-11-27 04:19:57 +01:00
# HDD CHECK & PRE-INIT
2018-10-16 12:50:36 +02:00
################################
2018-11-27 04:19:57 +01:00
# waiting for HDD to connect
hddExists=$(lsblk | grep -c sda1)
while [ ${hddExists} -eq 0 ]
do
# display will ask user to connect a HDD
echo "state=nohdd" > $infoFile
echo "message='Connect the Hard Drive'" >> $infoFile
sleep 5
2018-11-27 04:38:37 +01:00
# retry to find HDD
hddExists=$(lsblk | grep -c sda1)
2018-11-27 04:19:57 +01:00
done
# check if the HDD is auto-mounted
2018-11-27 05:20:31 +01:00
hddIsAutoMounted=$(lsblk | grep -c '/mnt/hdd')
if [ ${hddIsAutoMounted} -eq 0 ]; then
2018-11-27 04:19:57 +01:00
echo "HDD is there but not AutoMounted yet." >> $logFile
echo "Analysing the situation ..." >> $logFile
# detect for correct device name (the biggest partition)
hddDeviceName="sda1"
hddSecondPartitionExists=$(lsblk | grep -c sda2)
if [ ${hddSecondPartitionExists} -eq 1 ]; then
echo "HDD has a second partition - choosing the bigger one ..." >> $logFile
# get both with size
size1=$(lsblk -o NAME,SIZE -b | grep "sda1" | awk '{ print substr( $0, 12, length($0)-2 ) }' | xargs)
echo "sda1(${size1})" >> $logFile
size2=$(lsblk -o NAME,SIZE -b | grep "sda2" | awk '{ print substr( $0, 12, length($0)-2 ) }' | xargs)
echo "sda2(${size2})" >> $logFile
# chosse to run with the bigger one
if [ ${size2} -gt ${size1} ]; then
echo "sda2 is BIGGER - run with this one" >> $logFile
hddDeviceName="sda2"
else
echo "sda1 is BIGGER - run with this one" >> $logFile
hddDeviceName="sda1"
fi
fi
# check if HDD is formatted EXT4
2018-11-27 04:55:04 +01:00
hddExt4=$(lsblk -o NAME,FSTYPE -b /dev/${hddDeviceName} | grep -c "ext4")
2018-11-27 04:19:57 +01:00
if [ ${hddExt4} -eq 0 ]; then
echo "HDD is NOT formatted in ext4." >> $logFile
# stop the bootstrap here ...
# display will ask user to run setup
echo "state=waitsetup" > $infoFile
echo "message='HDD needs SetUp (1)'" >> $infoFile
exit 1
fi
# temp-mount the HDD
echo "temp-mounting the HDD .." >> $logFile
sudo mkdir /mnt/hdd
sudo mount -t ext4 /dev/${hddDeviceName} /mnt/hdd
2018-11-27 05:42:44 +01:00
mountOK=$(lsblk | grep -c '/mnt/hdd')
2018-11-27 04:19:57 +01:00
if [ ${mountOK} -eq 0 ]; then
echo "FAIL - not able to temp-mount HDD" >> $logFile
echo "state=waitsetup" > $infoFile
echo "message='HDD failed Mounting'" >> $infoFile
# no need to unmount the HDD, it failed mounting
2018-11-27 04:19:57 +01:00
exit 1
else
echo "OK - HDD available under /mnt/hdd" >> $logFile
fi
# check if HDD contains already a configuration
echo "Check if HDD contains already a configuration .." >> $logFile
2018-11-27 18:33:14 +01:00
configExists=$(ls ${configFile} | grep -c '.conf')
2018-11-27 04:19:57 +01:00
if [ ${configExists} -eq 1 ]; then
// TODO: Migration and Recover
echo "Found existing configuration - TODO migration and recover!" >> $logFile
echo "state=recovering" > $infoFile
echo "message='TODO: migration and recover'" >> $infoFile
# unmountig the HDD at the end of the process
sudo umount -l /mnt/hdd
2018-11-27 04:19:57 +01:00
exit 1
else
echo "OK - No config file found: ${configFile}" >> $logFile
fi
# check if HDD cointains existing LND data (old RaspiBlitz Version)
echo "Check if HDD contains existing LND data .." >> $logFile
2018-11-27 18:33:14 +01:00
lndDataExists=$(ls /mnt/hdd/lnd/lnd.conf | grep -c '.conf')
2018-11-27 04:19:57 +01:00
if [ ${lndDataExists} -eq 1 ]; then
echo "Found existing LND data - old RaspiBlitz?" >> $logFile
echo "state=olddata" > $infoFile
echo "message='No Auto-Update possible'" >> $infoFile
# keep HDD mounted if user wants to copy data
2018-11-27 04:19:57 +01:00
exit 1
else
echo "OK - No LND data found" >> $logFile
fi
2018-12-06 15:42:39 +01:00
# check if HDD contains pre-loaded blockchain data
2018-11-27 04:19:57 +01:00
echo "Check if HDD contains pre-loaded blockchain data .." >> $logFile
2018-12-06 15:42:39 +01:00
litecoinDataExists=$(ls /mnt/hdd/litecoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat')
bitcoinDataExists=$(ls /mnt/hdd/bitcoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat')
# check if node can go into presync (only for bitcoin)
if [ ${bitcoinDataExists} -eq 1 ]; then
2018-11-27 15:38:55 +01:00
2018-11-27 16:13:59 +01:00
# update info file
echo "state=presync" > $infoFile
echo "message='starting pre-sync'" >> $infoFile
2018-11-27 15:38:55 +01:00
# activating presync
# so that on a hackathon you can just connect a RaspiBlitz
# to the network and have it up-to-date for setting up
2018-11-27 18:24:09 +01:00
echo "Found pre-loaded blockchain" >> $logFile
# check if pre-sync was already activated on last power-on
2018-11-28 23:29:37 +01:00
#presyncActive=$(systemctl status bitcoind | grep -c 'could not be found')
2018-11-28 23:45:41 +01:00
echo "starting pre-sync in background" >> $logFile
# starting in background, because this scripts is part of systemd
# so to change systemd needs to happen after delay in seperate process
/home/admin/_bootstrap.presync.sh &
echo "done" >> $logFile
2018-11-27 16:13:59 +01:00
# after admin login, presync will be stoped and HDD unmounted
2018-11-27 04:19:57 +01:00
exit 1
2018-11-27 15:38:55 +01:00
2018-11-27 04:19:57 +01:00
else
2018-12-06 15:42:39 +01:00
echo "OK - No bitcoin blockchain data found" >> $logFile
2018-11-27 04:19:57 +01:00
fi
2018-10-16 12:50:36 +02:00
2018-11-27 04:19:57 +01:00
# if it got until here: HDD is empty ext4
echo "Waiting for SetUp." >> $logFile
echo "state=waitsetup" > $infoFile
echo "message='HDD needs SetUp (2)'" >> $infoFile
# unmount HDD to be ready for auto-mount during setup
sudo umount -l /mnt/hdd
2018-10-16 12:50:36 +02:00
exit 1
2018-11-27 04:19:57 +01:00
2018-10-16 12:50:36 +02:00
fi
################################
2018-12-06 14:36:02 +01:00
# INFOFILE BASICS
################################
2018-12-06 14:36:02 +01:00
# init network and chain values if needed with defaults
valueExists=$(sudo cat /home/admin/raspiblitz.info 2>/dev/null | grep -c "network=")
if [ ${valueExists} -eq 0 ]; then
echo "network=bitcoin" >> /home/admin/raspiblitz.info
2018-11-27 04:19:57 +01:00
fi
2018-12-06 14:36:02 +01:00
valueExists=$(sudo cat /home/admin/raspiblitz.info 2>/dev/null | grep -c "chain=")
if [ ${valueExists} -eq 0 ]; then
echo "chain=main" >> /home/admin/raspiblitz.info
fi
2018-12-06 14:36:02 +01:00
# EXIT on BOOTSTRAP HERE AT THE MOMENT
echo "DONE BOOTSTRAP (before any configs etc)" >> $logFile
echo "state=ready" > $infoFile
exit 0