From 4e889be7d880144111aab0ae8c479091d9c21388 Mon Sep 17 00:00:00 2001 From: /rootzoll Date: Mon, 31 Jul 2023 23:00:25 +0200 Subject: [PATCH] #4003 bitcoin.check.sh to set debug log path (#4051) * #4003 add bitcoin.check.sh to set debug file * add debug * change delimiter * set network on bitcoind service --- home.admin/assets/bitcoin.conf | 3 + home.admin/assets/bitcoind.service | 58 -------------- home.admin/config.scripts/bitcoin.check.sh | 82 ++++++++++++++++++++ home.admin/config.scripts/bitcoin.install.sh | 23 +++--- 4 files changed, 95 insertions(+), 71 deletions(-) delete mode 100644 home.admin/assets/bitcoind.service create mode 100755 home.admin/config.scripts/bitcoin.check.sh diff --git a/home.admin/assets/bitcoin.conf b/home.admin/assets/bitcoin.conf index 045ac3ce3..d4a134afe 100755 --- a/home.admin/assets/bitcoin.conf +++ b/home.admin/assets/bitcoin.conf @@ -11,6 +11,9 @@ txindex=0 disablewallet=1 peerbloomfilters=1 datadir=/mnt/hdd/bitcoin +main.debuglogfile=/mnt/hdd/bitcoin/debug.log +test.debuglogfile=/mnt/hdd/bitcoin/testnet3/debug.log +signet.debuglogfile=/mnt/hdd/bitcoin/signet/debug.log # Connection settings rpcuser=raspibolt diff --git a/home.admin/assets/bitcoind.service b/home.admin/assets/bitcoind.service deleted file mode 100644 index 5062983f8..000000000 --- a/home.admin/assets/bitcoind.service +++ /dev/null @@ -1,58 +0,0 @@ -# RaspiBlitz: systemd unit for bitcoind -# based on https://github.com/bitcoin/bitcoin/blob/master/contrib/init/bitcoind.service -[Unit] -Description=Bitcoin daemon - -After=network-online.target -Wants=network-online.target - -# for use with sendmail alert -#OnFailure=systemd-sendmail@%n - -[Service] -Environment='MALLOC_ARENA_MAX=1' -ExecStartPre=-/home/admin/config.scripts/blitz.systemd.sh log blockchain STARTED -ExecStart=/usr/local/bin/bitcoind -daemonwait \ - -conf=/mnt/hdd/bitcoin/bitcoin.conf \ - -datadir=/mnt/hdd/bitcoin \ - -debuglogfile=/mnt/hdd/bitcoin/debug.log - -# Make sure the config directory is readable by the service user -PermissionsStartOnly=true -ExecStartPre=/bin/chgrp bitcoin /mnt/hdd/bitcoin - -# Process management -#################### -Type=forking -Restart=on-failure -TimeoutStartSec=infinity -TimeoutStopSec=600 - -# Directory creation and permissions -#################################### -# Run as bitcoin:bitcoin -User=bitcoin -Group=bitcoin - -StandardOutput=null -StandardError=journal - -# Hardening measures -#################### -# Provide a private /tmp and /var/tmp. -PrivateTmp=true -# Mount /usr, /boot/ and /etc read-only for the process. -ProtectSystem=full -# Deny access to /home, /root and /run/user -ProtectHome=true -# Disallow the process and all of its children to gain -# new privileges through execve(). -NoNewPrivileges=true -# Use a new /dev namespace only populated with API pseudo devices -# such as /dev/null, /dev/zero and /dev/random. -PrivateDevices=true -# Deny the creation of writable and executable memory mappings. -MemoryDenyWriteExecute=true - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/home.admin/config.scripts/bitcoin.check.sh b/home.admin/config.scripts/bitcoin.check.sh new file mode 100755 index 000000000..c9e5ed6e0 --- /dev/null +++ b/home.admin/config.scripts/bitcoin.check.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# command info +if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then + echo "# bitcoin.check.sh prestart [mainnet|testnet|signet]" + exit 1 +fi + +###################################################################### +# PRESTART +# is executed by systemd bitcoind services everytime before bitcoin is started +# so it tries to make sure the config is in valid shape +###################################################################### + +# check/repair lnd config before starting +if [ "$1" == "prestart" ]; then + + echo "### RUNNING bitcoin.check.sh prestart" + + # check correct user + if [ "$USER" != "bitcoin" ]; then + echo "# FAIL: run as user 'bitcoin'" + exit 1 + fi + + # check correct parameter + if [ "$2" != "mainnet" ] && [ "$2" != "testnet" ] && [ "$2" != "signet" ]; then + echo "# FAIL: missing/wrong parameter" + exit 1 + fi + + CHAIN="$2" + + ##### DIRECTORY PERMISSIONS ##### + + /bin/chgrp bitcoin /mnt/hdd/bitcoin + + ##### CLEAN UP ##### + + # all lines with just spaces to empty lines + sed -i 's/^[[:space:]]*$//g' /mnt/hdd/bitcoin/bitcoin.conf + # all double empty lines to single empty lines + sed -i '/^$/N;/^\n$/D' /mnt/hdd/bitcoin/bitcoin.conf + + ##### CHECK/SET CONFIG VALUES ##### + + # correct debug log path + if [ "${CHAIN}" == "mainnet" ]; then + bitcoinlog_entry="main.debuglogfile" + bitcoinlog_path="/mnt/hdd/bitcoin/debug.log" + elif [ "${CHAIN}" == "testnet" ]; then + bitcoinlog_entry="test.debuglogfile" + bitcoinlog_path="/mnt/hdd/bitcoin/testnet3/debug.log" + elif [ "${CHAIN}" == "signet" ]; then + bitcoinlog_entry="signet.debuglogfile" + bitcoinlog_path="/mnt/hdd/bitcoin/signet/debug.log" + fi + + # make sure entry exists + echo "# make sure entry(${bitcoinlog_entry}) exists" + extryExists=$(grep -c "^${bitcoinlog_entry}=" /mnt/hdd/bitcoin/bitcoin.conf) + if [ "${extryExists}" == "0" ]; then + echo "${bitcoinlog_entry}=${bitcoinlog_path}" >> /mnt/hdd/bitcoin/bitcoin.conf + fi + + # make sure entry has the correct value + echo "# make sure entry(${bitcoinlog_entry}) has the correct value(${bitcoinlog_path})" + sed -i "s|^${bitcoinlog_entry}=.*|${bitcoinlog_entry}=${bitcoinlog_path}|g" /mnt/hdd/bitcoin/bitcoin.conf + + ##### STATISTICS ##### + + # count startings + if [ "${CHAIN}" == "mainnet" ]; then + /home/admin/config.scripts/blitz.systemd.sh log blockchain STARTED + fi + + echo "# OK PRESTART DONE" + +else + echo "# FAIL: parameter not known - run with -h for help" + exit 1 +fi diff --git a/home.admin/config.scripts/bitcoin.install.sh b/home.admin/config.scripts/bitcoin.install.sh index ed1f0c31d..563b94315 100644 --- a/home.admin/config.scripts/bitcoin.install.sh +++ b/home.admin/config.scripts/bitcoin.install.sh @@ -241,12 +241,13 @@ signet.addnode=nsgyo7begau4yecc46ljfecaykyzszcseapxmtu6adrfagfrrzrlngyd.onion:38 removeParallelService - if [ ${CHAIN} = mainnet ];then - sudo cp /home/admin/assets/bitcoind.service /etc/systemd/system/bitcoind.service - else - # /etc/systemd/system/${prefix}bitcoind.service - # based on https://github.com/bitcoin/bitcoin/blob/master/contrib/init/bitcoind.service - echo " + # /etc/systemd/system/${prefix}bitcoind.service + # based on https://github.com/bitcoin/bitcoin/blob/master/contrib/init/bitcoind.service + chainparameter="" + if [ "${CHAIN}" != "mainnet" ]; then + chainparameter="-${CHAIN}" + fi + echo " [Unit] Description=Bitcoin daemon on ${CHAIN} @@ -255,15 +256,12 @@ Wants=network-online.target [Service] Environment='MALLOC_ARENA_MAX=1' -ExecStart=/usr/local/bin/bitcoind -${CHAIN} \\ +ExecStartPre=-/home/admin/config.scripts/bitcoin.check.sh prestart ${CHAIN} +ExecStart=/usr/local/bin/bitcoind ${chainparameter} \\ -daemonwait \\ -conf=/mnt/hdd/bitcoin/bitcoin.conf \\ - -datadir=/mnt/hdd/bitcoin \\ - -debuglogfile=${bitcoinlogpath} - -# Make sure the config directory is readable by the service user + -datadir=/mnt/hdd/bitcoin PermissionsStartOnly=true -ExecStartPre=/bin/chgrp bitcoin /mnt/hdd/bitcoin # Process management #################### @@ -301,7 +299,6 @@ MemoryDenyWriteExecute=true [Install] WantedBy=multi-user.target " | sudo tee /etc/systemd/system/${prefix}bitcoind.service - fi sudo systemctl daemon-reload sudo systemctl enable ${prefix}bitcoind echo "# OK - the bitcoin daemon on ${CHAIN} service is now enabled"