From f0e60cfdf876396a32667928e9552f98fe056098 Mon Sep 17 00:00:00 2001 From: rootzoll Date: Tue, 27 Nov 2018 04:19:57 +0100 Subject: [PATCH] boostrap HDD precheck & config scripts --- home.admin/00infoLCD.sh | 12 +- home.admin/00mainMenu.sh | 53 ++++ home.admin/00settingsMenuServices.sh | 25 +- home.admin/_bootstrap.sh | 274 ++++++++++++++++----- home.admin/config.scripts/lnd.autopilot.sh | 41 +++ 5 files changed, 338 insertions(+), 67 deletions(-) create mode 100644 home.admin/config.scripts/lnd.autopilot.sh diff --git a/home.admin/00infoLCD.sh b/home.admin/00infoLCD.sh index 1aec463..786a514 100755 --- a/home.admin/00infoLCD.sh +++ b/home.admin/00infoLCD.sh @@ -55,13 +55,23 @@ while : # before initial setup if [ ${setupStep} -eq 0 ]; then + state="0" + message="Welcome" + + # check data from _bootstrap.sh that was running on device setup + bootstrapInfoExists=$(ls /home/admin/raspiblitz.info | grep -c '.info') + if [ ${bootstrapInfoExists} -eq 1 ]; then + # load the data from the info file - overwrite state & message + sudo -u admin source /home/admin/raspiblitz.info + fi + # setup process has not started yet l1="Login to your RaspiBlitz with:\n" l2="ssh admin@${localip}\n" l3="Use password: raspiblitz\n" boxwidth=$((${#localip} + 24)) sleep 3 - dialog --backtitle "RaspiBlitz ${localip} - Welcome (${setupStep})" --infobox "$l1$l2$l3" 5 ${boxwidth} + dialog --backtitle "RaspiBlitz (${state}) - ${message}" --infobox "$l1$l2$l3" 6 ${boxwidth} sleep 5 # during basic setup diff --git a/home.admin/00mainMenu.sh b/home.admin/00mainMenu.sh index a87988b..f048524 100755 --- a/home.admin/00mainMenu.sh +++ b/home.admin/00mainMenu.sh @@ -1,5 +1,58 @@ #!/bin/bash +# check data from _bootstrap.sh that was running on device setup +bootstrapInfoExists=$(ls /home/admin/raspiblitz.info | grep -c '.info') +if [ ${bootstrapInfoExists} -eq 1 ]; then + + # load the data from the info file + source /home/admin/raspiblitz.info + echo "Found raspiblitz.info from bootstrap - processing ..." + + # if pre-sync is running - stop it + if [ "${state}" = "presync" ]; then + echo "TODO: Stop pre-sync ... press key to continue" + read key + # update info file + state=waitsetup + echo "state=waitsetup" > $infoFile + echo "message='Pre-Sync Stopped'" >> $infoFile + echo "device=${device}" >> $infoFile + fi + + # wait until boostrap process is done + keepWaiting=1 + while [ ${keepWaiting} -eq 1 ] + do + + # 1) when bootstrap on configured device + if [ "${state}" = "ready" ]; then + echo "detected bootstrap ready" + keepWaiting=0 + + # 2) when bootstrap on a fresh sd card + elif [ "${state}" = "waitsetup" ]; then + echo "detected bootstrap waitinmg for setup" + + # unmount the temporary mount + sudo umount -l /mnt/hdd + + # update info file - that setup started + echo "state=setup" > $infoFile + echo "message='SetUp Started'" >> $infoFile + echo "device=${device}" >> $infoFile + keepWaiting=0 + + # 3) when bootstap is still running + else + # wait 2 sevs and check again + sleep 2 + keepWaiting=1 + fi + + done + +fi + ## default menu settings HEIGHT=13 WIDTH=64 diff --git a/home.admin/00settingsMenuServices.sh b/home.admin/00settingsMenuServices.sh index a8e68e9..f1711b2 100644 --- a/home.admin/00settingsMenuServices.sh +++ b/home.admin/00settingsMenuServices.sh @@ -26,14 +26,19 @@ fi # AUTOPILOT process choice choice="off"; check=$(echo "${CHOICES}" | grep -c "1") if [ ${check} -eq 1 ]; then choice="on"; fi -sudo sed -i "s/^autoPilot=.*/autoPilot=${choice}/g" /mnt/hdd/raspiblitz.conf - -# confirm reboot to activate new settings with bootstrap.service -dialog --backtitle "Rebooting" --yesno "To activate the settings a reboot is needed." 6 52 -if [ $? -eq 0 ];then - echo "Starting Reboot .." - sudo shutdown -r now -else - echo "No Reboot - changes stored, but maybe not active." - sleep 3 +if [ "${autoPilot}" != "${choice}" ]; then + echo "Autopilot Setting changed" + echo "Stopping Service" + sudo systemctl stop lnd + echo "Changing raspiblitz.conf" + sudo sed -i "s/^autoPilot=.*/autoPilot=${choice}/g" /mnt/hdd/raspiblitz.conf + echo "Executing change" + sudo /home/admin/config.scripts/lnd.autopilot.sh ${choice} + echo "Restarting Service" + echo "You may need to unlock after restart ..." + sudo systemctl start lnd + echo "Giving LND 120 seconds to get ready ..." + sleep 120 +else + echo "Autopilot Setting unchanged." fi \ No newline at end of file diff --git a/home.admin/_bootstrap.sh b/home.admin/_bootstrap.sh index 591b60b..0481a14 100644 --- a/home.admin/_bootstrap.sh +++ b/home.admin/_bootstrap.sh @@ -8,19 +8,38 @@ # load codeVersion source /home/admin/_version.info -logfile="/home/admin/raspiblitz.log" -echo "Writing logs to: ${logfile}" -echo "" > $logfile -echo "***********************************************" >> $logfile -echo "Running RaspiBlitz Bootstrap ${codeVersion}" >> $logfile -date >> $logfile -echo "***********************************************" >> $logfile +################################ +# 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 +echo "Resetting the InfoFile: ${infoFile}" +echo "state=starting" > $infoFile ################################ # AFTER BOOT SCRIPT # when a process needs to # execute stuff after a reboot +# it should in file # /home/admin/setup.sh ################################ @@ -50,16 +69,121 @@ chmod 774 /run/publicip ################################ -# HDD CHECK / INIT -# for the very first setup +# HDD CHECK & PRE-INIT ################################ + +# 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 + done + +# check if the HDD is auto-mounted +hddIsAutoMounted=$(ls -la /mnt/hdd 2>/dev/null) +if [ ${#hddIsAutoMounted} -eq 0 ]; then + + 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 + hddExt4=$(df -T /dev/${hddDeviceName} | grep -c "ext4") + 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 + echo "device=${hddDeviceName}" >> $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 + mountOK=$(df | grep -c /mnt/hdd) + if [ ${mountOK} -eq 0 ]; then + echo "FAIL - not able to temp-mount HDD" >> $logFile + echo "state=waitsetup" > $infoFile + echo "message='HDD failed Mounting'" >> $infoFile + echo "device=${hddDeviceName}" >> $infoFile + 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 + configExists=$(ls ${configFile} 2>/dev/null | grep -c '.conf') + 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 + echo "device=${hddDeviceName}" >> $infoFile + 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 + lndDataExists=$(ls /mnt/hdd/lnd/lnd.conf 2>/dev/null | grep -c '.conf') + if [ ${lndDataExists} -eq 1 ]; then + echo "Found existing LND data - old RaspiBlitz?" >> $logFile + echo "state=olddata" > $infoFile + echo "message='No Auto-Update possible'" >> $infoFile + echo "device=${hddDeviceName}" >> $infoFile + exit 1 + else + echo "OK - No LND data found" >> $logFile + fi + + # check if HDD contains pre-loaded blockchain data (just bitcoin for now) + echo "Check if HDD contains pre-loaded blockchain data .." >> $logFile + blockchainDataExists=$(ls /mnt/hdd/bitcoin 2>/dev/null) + if [ ${#blockchainDataExist} -gt 0 ]; then + // TODO: Pre-Sync Blockchain + echo "Found pre-loaded blockchain - TODO start pre-sync!" >> $logFile + echo "state=presync" > $infoFile + echo "message='TODO: start pre-sync'" >> $infoFile + echo "device=${hddDeviceName}" >> $infoFile + exit 1 + else + echo "OK - No blockchain data found" >> $logFile + fi -# check if the HDD is mounted -hddAvailable=$(ls -la /mnt/hdd 2>/dev/null) -if [ ${#hddAvailable} -eq 0 ]; then - echo "HDD is NOT available" >> $logfile - echo "TODO: Try to mount." + # 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 + echo "device=${hddDeviceName}" >> $infoFile exit 1 + fi ################################ @@ -67,74 +191,112 @@ fi ################################ # check if there is a config file -configFile="/mnt/hdd/raspiblitz.conf" configExists=$(ls ${configFile} 2>/dev/null | grep -c '.conf') if [ ${configExists} -eq 0 ]; then # create new config - echo "creating config file: ${configFile}" >> $logfile + echo "creating config file: ${configFile}" >> $logFile echo "# RASPIBLITZ CONFIG FILE" > $configFile echo "raspiBlitzVersion='${version}'" >> $configFile sudo chmod 777 ${configFile} + # the rest will be set under DEFAULT VALUES else # load & check config version source $configFile - if [ "${raspiBlitzVersion}" != "${raspiBlitzVersion}" ]; then - echo "detected version change ... starting migration script" >> $logfile + echo "codeVersion(${codeVersion})" >> $logFile + echo "configVersion(${raspiBlitzVersion})" >> $logFile + if [ "${raspiBlitzVersion}" != "${codeVersion}" ]; then + echo "detected version change ... starting migration script" >> $logFile /home/admin/_migrateVersion.sh fi fi -################################ +################################## # DEFAULT VALUES -################################ +# check which are not set and add +################################## -# AUTOPILOT -# autoPilot=off|on -if [ ${#autoPilot} -eq 0 ]; then - echo "autoPilot=off" >> $configFile +# COIN NETWORK +# network=bitcoin|litecoin +if [ ${#network} -eq 0 ]; then + oldNetworkConfigExists=$(sudo ls /home/admin/.network | grep -c '.network') + if [ ${oldNetworkConfigExists} -eq 1 ]; then + network=`sudo cat /home/admin/.network` + echo "importing old network value: ${network}" >> $logFile + echo "network=${network}" >> $configFile + else + echo "network=" >> $configFile + fi fi -# after all default values written to config - reload config -source $configFile - - -################################ -# AUTOPILOT -################################ - -echo "" >> $logfile -echo "** AUTOPILOT" >> $logfile - -# check if LND is installed -lndExists=$(ls /mnt/hdd/lnd/lnd.conf 2>/dev/null | grep -c '.conf') -if [ ${lndExists} -eq 1 ]; then - - # check if autopilot is active in LND config - lndAutopilot=$( grep -c "autopilot.active=1" /mnt/hdd/lnd/lnd.conf ) - echo "confAutopilot(${autoPilot})" >> $logfile - echo "lndAutopilot(${lndAutopilot})" >> $logfile - - # switch on - if [ ${lndAutopilot} -eq 0 ] && [ "${autoPilot}" = "on" ]; then - echo "switching the LND autopilot ON" >> $logfile - sudo sed -i "s/^autopilot.active=.*/autopilot.active=1/g" /mnt/hdd/lnd/lnd.conf +# RUNNING CHAIN +# chain=test|main +if [ ${#chain} -eq 0 ]; then + networkConfigExists=$(sudo ls /mnt/hdd/${network}/${network}.conf 2>/dev/null | grep -c '.conf') + if [ ${networkConfigExists} -eq 1 ]; then + source /mnt/hdd/${network}/${network}.conf + if [ ${testnet} -eq 1 ]; then + echo "detecting mainchain" >> $logF + ile + echo "chain=main" >> $configFile + else + echo "detecting testnet" >> $logF + ile + echo "chain=test" >> $configFile + fi + else + echo "chain=" >> $configFile fi +fi - # switch off - if [ ${lndAutopilot} -eq 1 ] && [ "${autoPilot}" = "off" ]; then - echo "switching the LND autopilot OFF" >> $logfile - sudo sed -i "s/^autopilot.active=.*/autopilot.active=0/g" /mnt/hdd/lnd/lnd.conf +# HOSTNAME +# hostname=ONEWORDSTRING +if [ ${#setupStep} -eq 0 ]; then + oldValueExists=$(sudo ls /home/admin/.hostname | grep -c '.hostname') + if [ ${oldValueExists} -eq 1 ]; then + oldValue=`sudo cat /home/admin/.hostname` + echo "importing old hostname: ${oldValue}" >> $logFile + echo "hostname=${oldValue}" >> $configFile + else + echo "hostname=" >> $configFile fi +fi -else +# SETUP STEP +# setupStep=0-100 +if [ ${#setupStep} -eq 0 ]; then + oldValueExists=$(sudo ls /home/admin/.setup | grep -c '.setup') + if [ ${oldValueExists} -eq 1 ]; then + oldValue=`sudo cat /home/admin/.setup` + echo "importing old setup value: ${oldValue}" >> $logFile + echo "setupStep=${oldValue}" >> $configFile + else + echo "setupStep=0" >> $configFile + fi +fi - echo "WARNING: /mnt/hdd/lnd/lnd.conf does not exists. Setup needs to run properly first!" >> $logfile +# AUTOPILOT +# autoPilot=off|on +if [ ${#autoPilot} -eq 0 ]; then + echo "autoPilot=off" >> $configFile +fi +# AUTO NAT DISCOVERY +# autoNatDiscovery=off|on +if [ ${#autoNatDiscovery} -eq 0 ]; then + echo "autoNatDiscovery=off" >> $configFile fi -echo "" >> $logfile -echo "DONE BOOTSTRAP" >> $logfile \ No newline at end of file +################################## +# CHECK CONFIG CONSISTENCY +################################## + +# after all default values written to config - reload config +source $configFile +echo "" >> $logFile + +echo "DONE BOOTSTRAP" >> $logFile +echo "state=ready" > $infoFile \ No newline at end of file diff --git a/home.admin/config.scripts/lnd.autopilot.sh b/home.admin/config.scripts/lnd.autopilot.sh new file mode 100644 index 0000000..2c867d3 --- /dev/null +++ b/home.admin/config.scripts/lnd.autopilot.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +if [ $# -eq 0 ]; then + echo "small config script to switch the LND auto pilot on or off" + echo "lnd.autopilot.sh [1|0]" + exit 1 +fi + +# check lnd.conf exits +lndConfExists=$(sudo ls /mnt/hdd/lnd/lnd.conf | grep -c 'lnd.conf') +if [ ${lndConfExists} -eq 0 ]; then + echo "FAIL - /mnt/hdd/lnd/lnd.conf not found" + exit 1 +fi + +# check if "autopilot.active" exists +valueExists=$(sudo cat /mnt/hdd/lnd/lnd.conf | grep -c 'autopilot.active=') +if [ ${valueExists} -eq 0 ]; then + echo "Adding autopilot config defaults to /mnt/hdd/lnd/lnd.conf" + sudo sed -i '$ a [autopilot]' /mnt/hdd/lnd/lnd.conf + sudo sed -i '$ a autopilot.active=0' /mnt/hdd/lnd/lnd.conf + sudo sed -i '$ a autopilot.allocation=0.6' /mnt/hdd/lnd/lnd.conf + sudo sed -i '$ a autopilot.maxchannels=5' /mnt/hdd/lnd/lnd.conf +fi + +# switch on +if [ $1 -eq 1 ] || [ "$1" = "on" ]; then + echo "switching the LND autopilot ON" + sudo sed -i "s/^autopilot.active=.*/autopilot.active=1/g" /mnt/hdd/lnd/lnd.conf + exit 0 +fi + +# switch off +if [ $1 -eq 0 ] || [ "$1" = "off" ]; then + echo "switching the LND autopilot OFF" + sudo sed -i "s/^autopilot.active=.*/autopilot.active=0/g" /mnt/hdd/lnd/lnd.conf + exit 0 +fi + +echo "FAIL - Unknown Paramter $1" +exit 1 \ No newline at end of file