raspiblitz/home.admin/_bootstrap.sh

368 lines
13 KiB
Bash
Raw Normal View History

#!/bin/bash
# This script runs on every start called by boostrap.service
2018-10-15 22:31:56 +02:00
# It makes sure that the system is configured like the
# default values or as in the config.
# For more details see background_raspiblitzSettings.md
2018-12-10 14:48:02 +01:00
# use to detect multiple starts of service
#uid=$(date +%s)
#echo "started" > /home/admin/${uid}.boot
2018-12-08 22:19:46 +01:00
# 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
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"
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
# 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
# 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
2019-01-21 00:25:32 +01:00
echo "network=${network}" >> $infoFile
echo "chain=${chain}" >> $infoFile
echo "setupStep=${setupStep}" >> $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
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
################################
# 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
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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=nohdd/g" ${infoFile}
sed -i "s/^message=.*/message='Connect the Hard Drive'/g" ${infoFile}
2018-11-27 04:19:57 +01:00
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
2018-12-10 14:48:02 +01:00
# check if the HDD is auto-mounted ( auto-mounted = setup-done)
2018-12-06 17:52:29 +01:00
hddIsAutoMounted=$(sudo cat /etc/fstab | grep -c '/mnt/hdd')
2018-11-27 05:20:31 +01:00
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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=waitsetup/g" ${infoFile}
sed -i "s/^message=.*/message='HDD needs SetUp (1)'/g" ${infoFile}
2018-12-08 22:43:27 +01:00
exit 0
2018-11-27 04:19:57 +01:00
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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=waitsetup/g" ${infoFile}
sed -i "s/^message=.*/message='HDD failed Mounting'/g" ${infoFile}
# no need to unmount the HDD, it failed mounting
2018-12-08 22:43:27 +01:00
exit 0
2018-11-27 04:19:57 +01:00
else
echo "OK - HDD available under /mnt/hdd" >> $logFile
fi
2018-12-06 17:39:06 +01:00
# UPDATE MIGRATION & CONFIG PROVISIONING
2018-11-27 04:19:57 +01:00
# 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
2018-12-06 17:39:06 +01:00
echo "Found existing configuration" >> $logFile
2018-12-11 19:37:32 +01:00
source ${configFile}
# check if config files contains basic: version
if [ ${#raspiBlitzVersion} -eq 0 ]; then
echo "Invalid Config: missing raspiBlitzVersion in (${configFile})!" >> ${logFile}
configExists=0
fi
# check if config files contains basic: network
if [ ${#network} -eq 0 ]; then
echo "Invalid Config: missing network in (${configFile})!" >> ${logFile}
configExists=0
fi
# check if config files contains basic: chain
if [ ${#chain} -eq 0 ]; then
echo "Invalid Config: missing chain in (${configFile})!" >> ${logFile}
configExists=0
fi
2018-12-11 20:12:21 +01:00
if [ ${configExists} -eq 0 ]; then
echo "Moving invalid config to raspiblitz.invalid.conf" >> ${logFile}
sudo mv ${configFile} /mnt/hdd/raspiblitz.invalid.conf
fi
2018-12-11 19:37:32 +01:00
fi
# if config is still valid ...
if [ ${configExists} -eq 1 ]; then
echo "Found valid configuration" >> $logFile
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=recovering/g" ${infoFile}
sed -i "s/^message=.*/message='Starting Recover'/g" ${infoFile}
2018-12-06 17:39:06 +01:00
echo "Calling Data Migration .." >> $logFile
sudo /home/admin/_bootstrap.migration.sh
echo "Calling Provisioning .." >> $logFile
sudo /home/admin/_bootstrap.provision.sh
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=recovered/g" ${infoFile}
sed -i "s/^message=.*/message='Done Recover'/g" ${infoFile}
2018-12-06 17:39:06 +01:00
echo "rebooting" >> $logFile
2018-12-12 16:23:09 +01:00
# set flag that system is freshly recovered and needs setup dialogs
echo "state=recovered" >> /home/admin/raspiblitz.recover.info
2018-12-06 17:39:06 +01:00
# save log file for inspection before reboot
2018-12-10 16:04:48 +01:00
cp $logFile /home/admin/raspiblitz.recover.log
2018-12-06 17:39:06 +01:00
sudo shutdown -r now
2018-12-08 22:43:27 +01:00
exit 0
2018-11-27 04:19:57 +01:00
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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=olddata/g" ${infoFile}
sed -i "s/^message=.*/message='No Auto-Update possible'/g" ${infoFile}
# keep HDD mounted if user wants to copy data
2018-12-08 22:43:27 +01:00
exit 0
2018-11-27 04:19:57 +01:00
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
2019-02-06 11:16:43 +00: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')
2018-12-06 15:42:39 +01:00
# 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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=presync/g" ${infoFile}
sed -i "s/^message=.*/message='starting presync'/g" ${infoFile}
2018-11-27 16:13:59 +01:00
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
2019-02-10 19:09:47 +01:00
# make sure that debug file is clean, so just pre-sync gets analysed on stop
sudo rm /mnt/hdd/bitcoin/debug.log
2018-11-28 23:45:41 +01:00
# starting in background, because this scripts is part of systemd
# so to change systemd needs to happen after delay in seperate process
2018-12-10 13:06:55 +01:00
sudo chown -R bitcoin:bitcoin /mnt/hdd/bitcoin 2>> $logFile
sudo -u bitcoin /usr/local/bin/bitcoind -daemon -conf=/home/admin/assets/bitcoin.conf -pid=/mnt/hdd/bitcoin/bitcoind.pid 2>> $logFile
echo "OK Started bitcoind for presync" >> $logFile
sudo sed -i "s/^message=.*/message='running presync'/g" ${infoFile}
# after admin login, presync will be stopped and HDD unmounted
2018-12-08 22:43:27 +01:00
exit 0
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
2018-12-10 14:48:02 +01:00
sed -i "s/^state=.*/state=waitsetup/g" ${infoFile}
sed -i "s/^message=.*/message='HDD needs SetUp (2)'/g" ${infoFile}
# unmount HDD to be ready for auto-mount during setup
sudo umount -l /mnt/hdd
2018-12-08 22:43:27 +01:00
exit 0
2018-11-27 04:19:57 +01:00
2018-12-11 00:53:07 +01:00
fi # END - no automount
#####################################
# UPDATE HDD CONFIG FILE (if exists)
2018-12-11 13:15:15 +01:00
# needs to be done before starting LND
# so that environment info is fresh
2018-12-11 00:53:07 +01:00
#####################################
echo "Check if HDD contains configuration .." >> $logFile
configExists=$(ls ${configFile} | grep -c '.conf')
if [ ${configExists} -eq 1 ]; then
# load values
echo "load and update publicIP" >> $logFile
source ${configFile}
# update public IP on boot
# wait otherwise looking for publicIP fails
sleep 5
2018-12-11 13:15:15 +01:00
freshPublicIP=$(curl -s http://v4.ipv6-test.com/api/myip.php)
if [ ${#freshPublicIP} -eq 0 ]; then
# prevent having no publicIP set at all and LND getting stuck
# https://github.com/rootzoll/raspiblitz/issues/312#issuecomment-462675101
if [ ${#publicIP} -eq 0 ]; then
localIP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
echo "WARNING: No publicIP information at all - working with placeholder: ${localIP}" >> $logFile
freshPublicIP="${localIP}"
fi
fi
2019-01-21 21:40:48 +01:00
if [ ${#freshPublicIP} -eq 0 ]; then
echo "WARNING: Was not able to determine external IP on startup." >> $logFile
2018-12-11 00:53:07 +01:00
else
2019-01-21 21:40:48 +01:00
publicIPValueExists=$( sudo cat ${configFile} | grep -c 'publicIP=' )
if [ ${publicIPValueExists} -gt 1 ]; then
# remove one
echo "more then one publiIp entry - removing one" >> $logFile
sed -i "s/^publicIP=.*//g" ${configFile}
publicIPValueExists=$( sudo cat ${configFile} | grep -c 'publicIP=' )
fi
if [ ${publicIPValueExists} -eq 0 ]; then
echo "create value (${freshPublicIP})" >> $logFile
echo "publicIP=${freshPublicIP}" >> $configFile
else
echo "update value (${freshPublicIP})" >> $logFile
sed -i "s/^publicIP=.*/publicIP=${freshPublicIP}/g" ${configFile}
fi
2018-12-11 00:53:07 +01:00
fi
2018-10-16 12:50:36 +02:00
fi
2019-01-09 21:10:35 +01:00
#################################
# 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
2018-12-12 16:23:09 +01:00
################################
# DETECT FRESHLY RECOVERED SD
################################
recoveredInfoExists=$(ls /home/admin/raspiblitz.recover.info | grep -c '.info')
2018-12-12 18:37:13 +01:00
if [ ${recoveredInfoExists} -eq 1 ]; then
2018-12-12 16:23:09 +01:00
sed -i "s/^state=.*/state=recovered/g" ${infoFile}
sed -i "s/^message=.*/message='login to finish'/g" ${infoFile}
exit 0
fi
################################
2018-12-11 00:53:07 +01:00
# SD INFOFILE BASICS
################################
# state info
2018-12-12 18:53:01 +01:00
sed -i "s/^state=.*/state=ready/g" ${infoFile}
2018-12-06 18:36:34 +01:00
sed -i "s/^message=.*/message='waiting login'/g" ${infoFile}
# determine network and chain from system
# check for BITCOIN
loaded=$(sudo systemctl status bitcoind | grep -c 'loaded')
if [ ${loaded} -gt 0 ]; then
2019-02-02 23:27:13 +01:00
sed -i "s/^network=.*/network=bitcoin/g" ${infoFile}
source /mnt/hdd/bitcoin/bitcoin.conf
if [ ${testnet} -gt 0 ]; then
sed -i "s/^chain=.*/chain=test/g" ${infoFile}
else
sed -i "s/^chain=.*/chain=main/g" ${infoFile}
fi
fi
# check for LITECOIN
loaded=$(sudo systemctl status litecoind | grep -c 'loaded')
if [ ${loaded} -gt 0 ]; then
2019-02-02 23:27:13 +01:00
sed -i "s/^network=.*/network=litecoin/g" ${infoFile}
sed -i "s/^chain=.*/chain=main/g" ${infoFile}
fi
2018-12-11 00:53:07 +01:00
echo "DONE BOOTSTRAP" >> $logFile
2018-12-06 14:36:02 +01:00
exit 0