2021-05-23 23:38:12 -05:00
#!/bin/bash
2021-07-15 21:08:14 +02:00
# this provision file is just executed on fresh setups
# not on recoveries or updates
2021-05-23 23:38:12 -05:00
# LOGFILE - store debug logs of bootstrap
logFile = "/home/admin/raspiblitz.log"
# INFOFILE - state data from bootstrap
infoFile = "/home/admin/raspiblitz.info"
source ${ infoFile }
# SETUPFILE - setup data of RaspiBlitz
setupFile = "/var/cache/raspiblitz/temp/raspiblitz.setup"
source ${ setupFile }
# CONFIGFILE - configuration of RaspiBlitz
configFile = "/mnt/hdd/raspiblitz.conf"
source ${ configFile }
2021-05-24 13:09:51 -05:00
# log header
echo "" >> ${ logFile }
echo "###################################" >> ${ logFile }
echo "# _provision.setup.sh" >> ${ logFile }
echo "###################################" >> ${ logFile }
2021-05-23 23:38:12 -05:00
sudo sed -i "s/^message=.*/message='Provision Setup'/g" ${ infoFile }
###################################
# Preserve SSH keys
# just copy dont link anymore
# see: https://github.com/rootzoll/raspiblitz/issues/1798
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='SSH Keys'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
# link ssh directory from SD card to HDD
echo "# --> SSH key settings" >> ${ logFile }
echo "# copying SSH pub keys to HDD" >> ${ logFile }
sudo cp -r /etc/ssh /mnt/hdd/ssh >> ${ logFile }
echo "# OK" >> ${ logFile }
###################################
# Prepare Blockchain Service
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='Blockchain Setup'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
if [ " ${ network } " = = "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='config: missing network'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo " FAIL: missing network in ( ${ setupFile } )! " >> ${ logFile }
exit 1
fi
if [ " ${ chain } " = = "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='config: missing chain'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo " FAIL: missing chain in ( ${ setupFile } )! " >> ${ logFile }
exit 1
fi
# make sure choosen blockchain service is installed
if [ " ${ network } " != "bitcoin" ] ; then
# TODO also ... check if /home/admin/selfsync.flag is needed on other chains
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i " s/^message=.*/message='TODO: install ${ network } '/g " ${ infoFile }
echo " FAIL see ${ logFile } "
echo " TODO: make sure ${ network } is installed! " >> ${ logFile }
exit 1
fi
# copy configs files and directories
echo ""
echo " *** Prepare ${ network } *** " >> ${ logFile }
sudo -u bitcoin mkdir /mnt/hdd/${ network } 2>/dev/null
sudo -u bitcoin mkdir /mnt/hdd/${ network } /blocks 2>/dev/null
sudo -u bitcoin mkdir /mnt/hdd/${ network } /chainstate 2>/dev/null
sudo cp /home/admin/assets/${ network } .conf /mnt/hdd/${ network } /${ network } .conf
sudo mkdir /home/admin/.${ network } 2>/dev/null
sudo cp /home/admin/assets/${ network } .conf /home/admin/.${ network } /${ network } .conf
2021-05-26 11:07:35 -05:00
# set password B as RPC password
echo "SETTING PASSWORD B" >> ${ logFile }
sudo /home/admin/config.scripts/blitz.setpassword.sh b " ${ passwordB } " >> ${ logFile }
2021-05-23 23:38:12 -05:00
# optimize RAM for blockchain validation (bitcoin only)
if [ " ${ network } " = = "bitcoin" ] && [ " ${ hddBlocksBitcoin } " = = "0" ] ; then
echo "*** Optimizing RAM for Sync ***" >> ${ logFile }
kbSizeRAM = $( cat /proc/meminfo | grep "MemTotal" | sed 's/[^0-9]*//g' )
echo "dont forget to reduce dbcache once IBD is done" > " /mnt/hdd/ ${ network } /blocks/selfsync.flag "
# RP4 4GB
if [ ${ kbSizeRAM } -gt 3500000 ] ; then
echo " Detected RAM >=4GB --> optimizing ${ network } .conf " >> ${ logFile }
sudo sed -i "s/^dbcache=.*/dbcache=3072/g" /mnt/hdd/${ network } /${ network } .conf
# RP4 2GB
elif [ ${ kbSizeRAM } -gt 1500000 ] ; then
echo " Detected RAM >=2GB --> optimizing ${ network } .conf " >> ${ logFile }
sudo sed -i "s/^dbcache=.*/dbcache=1536/g" /mnt/hdd/${ network } /${ network } .conf
#RP3/4 1GB
else
echo " Detected RAM <=1GB --> optimizing ${ network } .conf " >> ${ logFile }
sudo sed -i "s/^dbcache=.*/dbcache=512/g" /mnt/hdd/${ network } /${ network } .conf
fi
fi
# start network service
echo ""
echo " *** Start ${ network } *** " >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='Blockchain Testrun'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
echo "- This can take a while .." >> ${ logFile }
sudo cp /home/admin/assets/${ network } d.service /etc/systemd/system/${ network } d.service
#sudo chmod +x /etc/systemd/system/${network}d.service
sudo systemctl daemon-reload >> ${ logFile }
sudo systemctl enable ${ network } d.service >> ${ logFile }
sudo systemctl start ${ network } d.service >> ${ logFile }
# check if bitcoin has started
bitcoinRunning = 0
loopcount = 0
while [ ${ bitcoinRunning } -eq 0 ]
do
>& 2 echo " # ( ${ loopcount } /200) checking if ${ network } d is running ... " >> ${ logFile }
2021-05-25 14:12:26 -05:00
bitcoinRunning = $( sudo -u bitcoin ${ network } -cli getblockchaininfo 2>/dev/null | grep "initialblockdownload" -c)
2021-05-23 23:38:12 -05:00
sleep 2
sync
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 200 ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i " s/^message=.*/message='setup: failed ${ network } '/g " ${ infoFile }
echo " FAIL: setup: failed ${ network } " >> ${ logFile }
exit 1
fi
done
2021-05-25 14:30:29 -05:00
echo " OK ${ network } startup successfull " >> ${ logFile }
2021-05-23 23:38:12 -05:00
###################################
# Prepare Lightning
2021-05-25 14:46:20 -05:00
echo " Prepare Lightning ( ${ lightning } ) " >> ${ logFile }
2021-05-23 23:38:12 -05:00
2021-07-16 21:50:26 +02:00
if [ " ${ lightning } " != "lnd" ] ; then
2021-07-15 21:08:14 +02:00
###################################
2021-07-16 20:16:39 +02:00
# Remove LND from systemd
2021-07-16 21:50:26 +02:00
echo "Remove LND" >> ${ logFile }
2021-07-15 21:08:14 +02:00
sudo sed -i "s/^message=.*/message='Deactivate Lightning'/g" ${ infoFile }
sudo systemctl disable lnd
2021-07-16 12:19:14 +02:00
sudo rm /etc/systemd/system/lnd.service
sudo systemctl daemon-reload
2021-07-15 22:05:53 +02:00
fi
2021-07-15 21:08:14 +02:00
2021-05-23 23:38:12 -05:00
if [ " ${ lightning } " = = "lnd" ] ; then
###################################
# LND
2021-07-16 21:50:26 +02:00
echo "############## Setup LND" >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Setup'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
if [ " ${ passwordC } " = = "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='config: missing passwordC'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo " FAIL: missing passwordC in ( ${ setupFile } )! " >> ${ logFile }
exit 1
fi
# if user uploaded an LND rescue file
if [ " ${ lndrescue } " != "" ] ; then
echo " Restore LND data from uploaded rescue file ${ lndrescue } ... " >> ${ logFile }
source <( sudo /home/admin/config.scripts/lnd.backup.sh lnd-import ${ lndrescue } )
if [ " ${ error } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd import backup failed'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd import backup failed" >> ${ logFile }
echo " ${ error } " >> ${ logFile }
exit 1
fi
else
# preparing new LND config
echo "Creating new LND config ..." >> ${ logFile }
sudo -u bitcoin mkdir /mnt/hdd/lnd 2> /dev/null
sudo cp /home/admin/assets/lnd.${ network } .conf /mnt/hdd/lnd/lnd.conf
sudo chown bitcoin:bitcoin /mnt/hdd/lnd/lnd.conf
2021-07-15 21:08:14 +02:00
sudo /home/admin/config.scripts/lnd.chain.sh on mainnet
2021-05-23 23:38:12 -05:00
sudo /home/admin/config.scripts/lnd.setname.sh ${ hostname }
fi
# check if now a config exists
configLinkedCorrectly = $( sudo ls sudo ls /home/bitcoin/.lnd/lnd.conf | grep -c "lnd.conf" )
if [ " ${ configLinkedCorrectly } " != "1" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd conf link broken'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd conf link broken" >> ${ logFile }
exit 1
fi
# Init LND service & start
echo "*** Init LND Service & Start ***" >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Testrun'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
# just in case
sudo systemctl stop lnd 2>/dev/null
sudo systemctl disable lnd 2>/dev/null
# make sure lnd gets started after blockchain service
sed -i " 5s/.*/Wants= ${ network } d.service/ " /home/admin/assets/lnd.service >> ${ logFile }
sed -i " 6s/.*/After= ${ network } d.service/ " /home/admin/assets/lnd.service >> ${ logFile }
sudo cp /home/admin/assets/lnd.service /etc/systemd/system/lnd.service >> ${ logFile }
2021-05-26 10:47:35 -05:00
# make sure LND starts with Tor by default
2021-05-23 23:38:12 -05:00
sudo /home/admin/config.scripts/internet.tor.sh lndconf-on >> ${ logFile }
# start lnd up
echo "Starting LND Service ..." >> ${ logFile }
sudo systemctl enable lnd >> ${ logFile }
sudo systemctl start lnd >> ${ logFile }
# check that lnd started
lndRunning = 0
loopcount = 0
while [ ${ lndRunning } -eq 0 ]
do
lndRunning = $( sudo systemctl status lnd.service | grep -c running)
if [ ${ lndRunning } -eq 0 ] ; then
date +%s >> ${ logFile }
echo "LND not ready yet ... waiting another 60 seconds." >> ${ logFile }
sleep 10
fi
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 100 ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: failed lnd start'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: failed lnd start" >> ${ logFile }
exit 1
fi
done
echo "OK - LND is running" ${ logFile }
sleep 10
# Check LND health/fails (to be extended)
tlsExists = $( sudo ls /mnt/hdd/lnd/tls.cert 2>/dev/null | grep -c "tls.cert" )
if [ ${ tlsExists } -eq 0 ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: missing lnd tls'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: missing lnd tls" >> ${ logFile }
exit 1
fi
# import static channel backup if was uploaded
if [ " ${ staticchannelbackup } " != "" ] ; then
echo " Preparing static channel backup file ${ staticchannelbackup } ... " >> ${ logFile }
source <( sudo /home/admin/config.scripts/lnd.backup.sh scb-import ${ staticchannelbackup } )
if [ " ${ error } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd import SCB failed'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd import SCB failed" >> ${ logFile }
echo " ${ error } " >> ${ logFile }
exit 1
fi
fi
# WALLET --> SEED + SCB
2021-05-25 14:46:20 -05:00
if [ " ${ seedWords } " != "" ] && [ " ${ staticchannelbackup } " != "" ] ; then
2021-05-23 23:38:12 -05:00
2021-07-03 01:55:44 +02:00
echo "WALLET --> SEED + SCB " >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Wallet (SEED & SCB)'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
sudo /home/admin/config.scripts/lnd.initwallet.py scb ${ passwordC } " ${ seedWords } " " ${ staticchannelbackup } " ${ seedPassword }
if [ " ${ err } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd wallet SCB failed'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd wallet SCB failed" >> ${ logFile }
echo " ${ err } " >> ${ logFile }
echo " ${ errMore } " >> ${ logFile }
exit 1
fi
# WALLET --> SEED
2021-05-25 14:46:20 -05:00
elif [ " ${ seedWords } " != "" ] ; then
2021-05-23 23:38:12 -05:00
2021-07-03 01:55:44 +02:00
echo "WALLET --> SEED" >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Wallet (SEED)'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
sudo /home/admin/config.scripts/lnd.initwallet.py seed ${ passwordC } " ${ seedWords } " ${ seedPassword }
if [ " ${ err } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd wallet SEED failed'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd wallet SEED failed" >> ${ logFile }
echo " ${ err } " >> ${ logFile }
echo " ${ errMore } " >> ${ logFile }
exit 1
fi
# WALLET --> NEW
else
2021-07-03 01:55:44 +02:00
echo "WALLET --> NEW" >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Wallet (NEW)'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
source <( sudo /home/admin/config.scripts/lnd.initwallet.py new ${ passwordC } )
if [ " ${ err } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd wallet SEED failed'/g" ${ infoFile }
echo " FAIL see ${ logFile } "
echo "FAIL: setup: lnd wallet SEED failed" >> ${ logFile }
echo " ${ err } " >> ${ logFile }
echo " ${ errMore } " >> ${ logFile }
exit 1
fi
# write created seedwords into SETUPFILE to be displayed to user on final setup later
2021-05-25 18:08:45 -05:00
echo " seedwordsNEW=' ${ seedwords } ' " >> ${ setupFile }
2021-05-25 16:08:48 -05:00
echo " seedwords6x4NEW=' ${ seedwords6x4 } ' " >> ${ setupFile }
2021-05-23 23:38:12 -05:00
fi
# sync macaroons & TLS to other users
echo "*** Copy LND Macaroons to user admin ***" >> ${ logFile }
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='LND Credentials'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
# check if macaroon exists now - if not fail
macaroonExists = $( sudo -u bitcoin ls -la /home/bitcoin/.lnd/data/chain/${ network } /${ chain } net/admin.macaroon 2>/dev/null | grep -c admin.macaroon)
if [ ${ macaroonExists } -eq 0 ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd no macaroons'/g" ${ infoFile }
echo "FAIL: setup: lnd no macaroons" >> ${ logFile }
exit 1
fi
# now sync macaroons & TLS zo other users
2021-05-25 14:50:52 -05:00
sudo /home/admin/config.scripts/lnd.credentials.sh sync >> ${ logFile }
2021-05-23 23:38:12 -05:00
# make a final lnd check
source <( /home/admin/config.scripts/lnd.check.sh basic-setup)
if [ " ${ err } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: lnd wallet SEED failed'/g" ${ infoFile }
echo "FAIL: setup: lnd wallet SEED failed" >> ${ logFile }
echo " ${ err } " >> ${ logFile }
exit 1
fi
fi
if [ " ${ lightning } " = = "cln" ] ; then
###################################
# c-lightning
2021-07-17 00:54:31 +02:00
echo "############## c-lightning" >> ${ logFile }
sudo sed -i "s/^message=.*/message='C-Lightning Install'/g" ${ infoFile }
sudo /home/admin/config.scripts/cln.install.sh on mainnet >> ${ logFile }
2021-07-16 20:16:39 +02:00
sudo sed -i "s/^message=.*/message='C-Lightning Setup'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
2021-07-17 00:54:31 +02:00
# OLD WALLET FROM CLIGHTNING RESCUE
if [ " ${ lndrescue } " != "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: implement clnrescue'/g" ${ infoFile }
echo "FAIL: setup: implement clnrescue" >> ${ logFile }
exit 1
# OLD WALLET FROM SEEDWORDS
else [ " ${ seedwords } " != "" ] ; then
source <( sudo /home/admin/config.scripts/cln.hsmtool.sh seed-force mainnet " ${ seedwords } " )
# NEW WALLET
else
# sudo /home/admin/config.scripts/cln.hsmtool.sh seed-force mainnet "dad march erode large digital fun lift squirrel zebra order label inquiry distance tube predict benefit skin insect mistake bullet solar ostrich shiver road"
# generate new wallet
source <( sudo /home/admin/config.scripts/cln.hsmtool.sh new-force mainnet)
# check if got new seedwords
if [ " ${ seedwords } " = = "" ] || [ " ${ seedwords6x4 } " = = "" ] ; then
sed -i "s/^state=.*/state=error/g" ${ infoFile }
sed -i "s/^message=.*/message='setup: no cln seedwords'/g" ${ infoFile }
echo "FAIL: setup: no cln seedwords" >> ${ logFile }
exit 1
fi
# write created seedwords into SETUPFILE to be displayed to user on final setup later
echo " seedwordsNEW=' ${ seedwords } ' " >> ${ setupFile }
echo " seedwords6x4NEW=' ${ seedwords6x4 } ' " >> ${ setupFile }
fi
2021-07-15 21:08:14 +02:00
2021-07-17 00:54:31 +02:00
# TODO setPasswordC --> for any new wallet encryption
2021-05-26 13:22:29 -05:00
2021-05-23 23:38:12 -05:00
fi
2021-05-25 13:28:27 -05:00
sudo sed -i "s/^message=.*/message='Provision Setup Finish'/g" ${ infoFile }
2021-05-23 23:38:12 -05:00
echo "END Setup" >> ${ logFile }
2021-07-16 21:50:26 +02:00
exit 0