#!/usr/bin/env bash # Standalone Besu install for LXC CT - no external source. Run inside container as root. # Usage: NODE_TYPE=sentry|rpc bash install-besu-in-ct-standalone.sh # Or: pct exec VMID -- bash /tmp/install-besu-in-ct-standalone.sh (with NODE_TYPE set in env) set -eo pipefail # Don't use set -u to avoid unbound variable from any sourced content NODE_TYPE="${NODE_TYPE:-sentry}" BESU_VERSION="${BESU_VERSION:-23.10.0}" BESU_USER="${BESU_USER:-besu}" BESU_GROUP="${BESU_GROUP:-besu}" BESU_HOME="/opt/besu" BESU_DATA="/data/besu" BESU_CONFIG="/etc/besu" BESU_LOGS="/var/log/besu" echo "[INFO] Installing Besu $NODE_TYPE (${BESU_VERSION})..." export DEBIAN_FRONTEND=noninteractive export LC_ALL=C export LANG=C echo "[INFO] Installing packages..." # Skip apt if java and wget already present (avoids hang in CTs with slow/locked apt) if command -v java >/dev/null 2>&1 && command -v wget >/dev/null 2>&1; then echo "[INFO] java and wget present, skipping apt." else # Allow update to fail (e.g. command-not-found DB I/O error in CT) apt-get update -qq 2>/dev/null || true apt-get install -y -qq openjdk-17-jdk wget curl jq ca-certificates 2>/dev/null || true fi command -v java >/dev/null 2>&1 || { echo "[ERROR] java not found; run: apt-get install -y openjdk-17-jdk"; exit 1; } command -v wget >/dev/null 2>&1 || { echo "[ERROR] wget not found; run: apt-get install -y wget"; exit 1; } if ! id -u "$BESU_USER" &>/dev/null; then useradd -r -s /bin/bash -d "$BESU_HOME" -m "$BESU_USER" fi mkdir -p "$BESU_HOME" BESU_TAR="${TMPDIR:-/tmp}/besu-${BESU_VERSION}.tar.gz" BESU_DOWNLOAD_URL="https://hyperledger.jfrog.io/hyperledger/besu-binaries/besu/${BESU_VERSION}/besu-${BESU_VERSION}.tar.gz" echo "[INFO] Downloading Besu..." wget -q "$BESU_DOWNLOAD_URL" -O "$BESU_TAR" || { echo "[ERROR] Failed to download Besu"; exit 1; } tar -xzf "$BESU_TAR" -C "$BESU_HOME" --strip-components=1 rm -f "$BESU_TAR" chown -R "$BESU_USER:$BESU_GROUP" "$BESU_HOME" chmod +x "$BESU_HOME/bin/besu" mkdir -p "$BESU_DATA" "$BESU_CONFIG" "$BESU_LOGS" "${BESU_DATA}/tmp" chown -R "$BESU_USER:$BESU_GROUP" "$BESU_DATA" "$BESU_CONFIG" "$BESU_LOGS" if [[ "$NODE_TYPE" == "sentry" ]]; then CONFIG_FILE="config-sentry.toml" SERVICE_NAME="besu-sentry" DESC="Besu Sentry Node" else CONFIG_FILE="config-rpc.toml" SERVICE_NAME="besu-rpc" DESC="Besu RPC Node" fi echo "[INFO] Creating $SERVICE_NAME.service..." cat > /etc/systemd/system/${SERVICE_NAME}.service << EOF [Unit] Description=Hyperledger $DESC After=network.target Wants=network-online.target [Service] Type=simple User=$BESU_USER Group=$BESU_GROUP WorkingDirectory=$BESU_HOME Environment="BESU_OPTS=-Xmx2g -Xms1g -Djava.io.tmpdir=${BESU_DATA}/tmp" ExecStart=$BESU_HOME/bin/besu --config-file=$BESU_CONFIG/$CONFIG_FILE Restart=always RestartSec=10 LimitNOFILE=65536 StandardOutput=journal StandardError=journal SyslogIdentifier=$SERVICE_NAME [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable ${SERVICE_NAME}.service echo "[OK] Besu $NODE_TYPE install complete. Add $BESU_CONFIG/$CONFIG_FILE and genesis.json then: systemctl start $SERVICE_NAME" exit 0