raspiblitz/home.admin/config.scripts/bitcoin.update.sh

268 lines
8.8 KiB
Bash
Raw Normal View History

2019-12-04 08:13:21 +00:00
#!/bin/bash
2021-04-19 15:46:44 +01:00
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "Interim optional Bitcoin Core updates between RaspiBlitz releases."
echo "bitcoin.update.sh [info|tested|reckless|custom]"
echo "info -> get actual state and possible actions"
echo "tested -> only do a tested update by the RaspiBlitz team"
echo "reckless -> the update was not tested by the RaspiBlitz team"
echo "custom -> update to a chosen version"
echo " the binary will be checked by signature and checksum in all cases"
echo
exit 1
2021-04-19 15:46:44 +01:00
fi
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
source /home/admin/raspiblitz.info
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# 1. parameter [info|tested|reckless]
mode="$1"
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# RECOMMENDED UPDATE BY RASPIBLITZ TEAM
# comment will be shown as "BEWARE Info" when option is choosen (can be multiple lines)
2021-08-26 20:27:42 +02:00
bitcoinVersion="" # example: 0.21.0 .. keep empty if no newer version as sd card build is available
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# needed to check code signing
laanwjPGP="01EA5486DE18A882D4C2684590C8019E36C2E964"
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# GATHER DATA
# setting download directory
downloadDir="/home/admin/download"
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# detect CPU architecture & fitting download link
if [ $(uname -m | grep -c 'arm') -eq 1 ] ; then
2019-12-04 08:13:21 +00:00
bitcoinOSversion="arm-linux-gnueabihf"
fi
2021-04-19 15:46:44 +01:00
if [ $(uname -m | grep -c 'aarch64') -eq 1 ] ; then
2019-12-04 08:13:21 +00:00
bitcoinOSversion="aarch64-linux-gnu"
fi
2021-04-19 15:46:44 +01:00
if [ $(uname -m | grep -c 'x86_64') -eq 1 ] ; then
2019-12-04 08:13:21 +00:00
bitcoinOSversion="x86_64-linux-gnu"
fi
2021-04-19 15:46:44 +01:00
# installed version
installedVersion=$(sudo -u bitcoin bitcoind --version | head -n1| cut -d" " -f4|cut -c 2-)
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# test if the installed version already the tested/recommended update version
bitcoinUpdateInstalled=$(echo "${installedVersion}" | grep -c "${bitcoinVersion}")
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# get latest release from GitHub releases
gitHubLatestReleaseJSON="$(curl -s https://api.github.com/repos/bitcoin/bitcoin/releases | jq '.[0]')"
bitcoinLatestVersion=$(echo "${gitHubLatestReleaseJSON}"|jq -r '.tag_name'|cut -c 2-)
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
# INFO
function displayInfo() {
echo "# basic data"
echo "installedVersion='${installedVersion}'"
echo "bitcoinOSversion='${bitcoinOSversion}'"
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
echo "# the tested/recommended update option"
echo "bitcoinUpdateInstalled='${bitcoinUpdateInstalled}'"
echo "bitcoinVersion='${bitcoinVersion}'"
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
echo "# reckless update option (latest Bitcoin Core release from GitHub)"
echo "bitcoinLatestVersion='${bitcoinLatestVersion}'"
}
if [ "${mode}" = "info" ]; then
displayInfo
2019-12-04 08:13:21 +00:00
exit 1
fi
2021-04-19 15:46:44 +01:00
# tested
if [ "${mode}" = "tested" ]; then
echo "# bitcoin.update.sh tested"
# check for optional second parameter: forced update version
# --> only does the tested update if its the given version
# this is needed for recovery/update.
fixedBitcoinVersion="$2"
if [ ${#fixedBitcoinVersion} -gt 0 ]; then
echo "# checking for fixed version update: askedFor(${bitcoinVersion}) available(${bitcoinVersion})"
if [ "${fixedBitcoinVersion}" != "${bitcoinVersion}" ]; then
echo "# warn='required update version does not match'"
echo "# this is normal when the recovery script of a new RaspiBlitz version checks for an old update - just ignore"
exit 1
else
echo "# OK - update version is matching"
fi
fi
2021-04-19 23:14:46 +01:00
pathVersion=${bitcoinVersion}
2021-04-19 15:46:44 +01:00
elif [ "${mode}" = "reckless" ]; then
# RECKLESS
# this mode is just for people running test and development nodes - its not recommended
# for production nodes. In a update/recovery scenario it will not install a fixed version
# it will always pick the latest release from the github
echo "# bitcoin.update.sh reckless"
bitcoinVersion=${bitcoinLatestVersion}
2021-04-19 23:14:46 +01:00
pathVersion=${bitcoinVersion}
2021-04-19 15:46:44 +01:00
elif [ "${mode}" = "custom" ]; then
2021-04-19 23:14:46 +01:00
clear
2021-04-19 15:46:44 +01:00
echo
echo "# Update Bitcoin Core to a chosen version."
echo
echo "# Input the version you would like to install and press ENTER."
2021-04-19 23:14:46 +01:00
echo "# Examples:"
echo "0.21.1rc1"
2021-04-19 15:46:44 +01:00
echo "0.21.0"
echo
read bitcoinVersion
2021-04-19 23:14:46 +01:00
if [ $(echo ${bitcoinVersion} | grep -c "rc") -gt 0 ];then
cutVersion=$(echo ${bitcoinVersion} | awk -F"r" '{print $1}')
rcVersion=$(echo ${bitcoinVersion} | awk -F"r" '{print $2}')
pathVersion=${cutVersion}/test.r${rcVersion}
else
pathVersion=${bitcoinVersion}
fi
2021-04-19 15:46:44 +01:00
if curl --output /dev/null --silent --head --fail \
2021-04-19 23:14:46 +01:00
https://bitcoincore.org/bin/bitcoin-core-${pathVersion}/SHA256SUMS.asc; then
echo "# OK version exists at https://bitcoincore.org/bin/bitcoin-core-${pathVersion}"
echo "# Press ENTER to proceed to install Bitcoin Core $bitcoinVersion or CTRL+C to abort."
2021-04-19 15:46:44 +01:00
read key
else
echo "# FAIL $bitcoinVersion does not exist"
echo
echo "# Press ENTER to return to the main menu"
read key
exit 0
fi
fi
# JOINED INSTALL
if [ "${mode}" = "tested" ]||[ "${mode}" = "reckless" ]||[ "${mode}" = "custom" ]; then
displayInfo
if [ $installedVersion = $bitcoinVersion ];then
echo "# installedVersion = bitcoinVersion"
echo "# exiting script"
exit 0
fi
echo
echo "# clean & change into download directory"
sudo rm -r ${downloadDir}/*
cd "${downloadDir}" || exit 1
echo
# download, check and import signer key
sudo -u admin wget https://bitcoin.org/laanwj-releases.asc
if [ ! -f "./laanwj-releases.asc" ]
then
echo "# !!! FAIL !!! Download laanwj-releases.asc not success."
exit 1
fi
gpg --import-options show-only --import ./laanwj-releases.asc
fingerprint=$(gpg ./laanwj-releases.asc 2>/dev/null | grep -c "${laanwjPGP}")
if [ ${fingerprint} -eq 0 ]; then
2021-04-19 15:46:44 +01:00
echo
echo "# !!! BUILD WARNING --> Bitcoin PGP author not as expected"
echo "# Should contain laanwjPGP: ${laanwjPGP}"
echo "# PRESS ENTER to TAKE THE RISK if you think all is OK"
read key
fi
gpg --import ./laanwj-releases.asc
# download signed binary sha256 hash sum file and check
2021-04-19 23:14:46 +01:00
sudo -u admin wget https://bitcoincore.org/bin/bitcoin-core-${pathVersion}/SHA256SUMS.asc
2021-04-19 15:46:44 +01:00
verifyResult=$(gpg --verify SHA256SUMS.asc 2>&1)
goodSignature=$(echo ${verifyResult} | grep 'Good signature' -c)
echo "goodSignature(${goodSignature})"
correctKey=$(echo ${verifyResult} | grep "using RSA key ${laanwjPGP: -16}" -c)
echo "correctKey(${correctKey})"
if [ ${correctKey} -lt 1 ] || [ ${goodSignature} -lt 1 ]; then
echo
echo "# !!! BUILD FAILED --> PGP Verify not OK / signature(${goodSignature}) verify(${correctKey})"
exit 1
else
echo
echo "# OK --> BITCOIN MANIFEST IS CORRECT"
echo
fi
echo "# Downloading Bitcoin Core v${bitcoinVersion} for ${bitcoinOSversion} ..."
2021-04-19 15:46:44 +01:00
binaryName="bitcoin-${bitcoinVersion}-${bitcoinOSversion}.tar.gz"
2021-04-19 23:14:46 +01:00
sudo -u admin wget https://bitcoincore.org/bin/bitcoin-core-${pathVersion}/${binaryName}
2021-04-19 15:46:44 +01:00
if [ ! -f "./${binaryName}" ]
then
2021-04-19 23:14:46 +01:00
echo "# !!! FAIL !!! Downloading BITCOIN BINARY did not succeed."
exit 1
2021-04-19 15:46:44 +01:00
fi
2021-04-19 23:14:46 +01:00
echo "# Checking binary checksum ..."
checksumTest=$(sha256sum -c --ignore-missing SHA256SUMS.asc ${binaryName} 2>/dev/null \
| grep -c "${binaryName}: OK")
if [ "${checksumTest}" -eq 0 ]; then
# get the sha256 value for the corresponding platform from signed hash sum file
bitcoinSHA256=$(grep -i "$bitcoinOSversion" SHA256SUMS.asc | cut -d " " -f1)
echo "!!! FAIL !!! Downloaded BITCOIN BINARY CHECKSUM:"
echo "$(sha256sum ${binaryName})"
echo "NOT matching SHA256 checksum:"
echo "${bitcoinSHA256}"
2021-04-19 15:46:44 +01:00
exit 1
else
echo
echo "# OK --> VERIFIED BITCOIN CHECKSUM IS CORRECT"
2021-04-19 15:46:44 +01:00
echo
fi
fi
if [ "${mode}" = "tested" ]||[ "${mode}" = "custom" ]; then
bitcoinInterimsUpdateNew="${bitcoinVersion}"
elif [ "${mode}" = "reckless" ]; then
bitcoinInterimsUpdateNew="reckless"
fi
# JOINED INSTALL
2021-04-19 15:46:44 +01:00
if [ "${mode}" = "tested" ]||[ "${mode}" = "reckless" ]||[ "${mode}" = "custom" ];then
# install
2021-04-19 23:14:46 +01:00
echo "# Stopping bitcoind and lnd ..."
2021-04-19 15:46:44 +01:00
sudo systemctl stop lnd
sudo systemctl stop bitcoind
echo
echo "# Installing Bitcoin Core v${bitcoinVersion}"
sudo -u admin tar -xvf ${binaryName}
sudo install -m 0755 -o root -g root -t /usr/local/bin/ bitcoin-${bitcoinVersion}/bin/*
sleep 3
installed=$(sudo -u admin bitcoind --version | grep "${bitcoinVersion}" -c)
if [ ${installed} -lt 1 ]; then
echo
echo "# !!! BUILD FAILED --> Was not able to install bitcoind version(${bitcoinVersion})"
exit 1
fi
echo "# flag update in raspiblitz config"
source /mnt/hdd/raspiblitz.conf
if [ ${#bitcoinInterimsUpdate} -eq 0 ]; then
echo "bitcoinInterimsUpdate='${bitcoinInterimsUpdateNew}'" >> /mnt/hdd/raspiblitz.conf
else
sudo sed -i "s/^bitcoinInterimsUpdate=.*/bitcoinInterimsUpdate='${bitcoinInterimsUpdateNew}'/g" /mnt/hdd/raspiblitz.conf
fi
2021-04-19 23:14:46 +01:00
echo "# OK Bitcoin Core ${bitcoinVersion} is installed"
2021-04-19 15:46:44 +01:00
if [ "${state}" == "ready" ]; then
echo
echo "# Starting ..."
2021-04-19 15:46:44 +01:00
sudo systemctl start bitcoind
sleep 10
2021-04-19 23:14:46 +01:00
echo
sudo systemctl start lnd
echo "# Starting LND ..."
sleep 10
2021-04-19 23:14:46 +01:00
echo
echo "# Press ENTER to proceed to unlock the LND wallet ..."
2021-04-19 23:14:46 +01:00
read key
sudo /home/admin/config.scripts/lnd.unlock.sh
2021-04-19 15:46:44 +01:00
fi
2021-04-19 23:14:46 +01:00
exit 0
2019-12-04 08:13:21 +00:00
2021-04-19 15:46:44 +01:00
else
echo "# error='parameter not known'"
exit 1
fi