From f376cce807de9f1f9c793dd244f85ce0a5591d81 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 27 Jan 2024 17:43:29 +0100 Subject: [PATCH] Update entrypoint.sh - Add comments - Move argument compiler from initialize() into compile_args() - Add compile_flags() function - Add internal_error() function - Move several functions - Move log cleaning into own function - Support for Aikar's flags (better variant) - Support for G1GC and ZGC garbage collectors - Support for manipulating memory limits --- entrypoint.sh | 160 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 136 insertions(+), 24 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index efe2216..0206d67 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,6 +2,33 @@ # Disables warning about echo not expanding escape sequences # shellcheck disable=SC2028 +# Used for internal errors +function internal_error() { + echo "-> INTERNAL ERROR: ${*}" + exit 99 +} + +# Automatically accepts the End User License Agreement +function accept_eula() { + echo "eula=true" > /data/eula.txt +} + +# Cleans /logs and /crash-reports +function clean_logs() { + if [ "${MCSD_CLEAN_LOGS}" == "true" ]; then + rm -rf "/data/logs" "/data/crash-reports" + fi +} + +# Used by trap, shuts the server down +function kill_server() { + if [ -z "${CHILDPID}" ]; then + internal_error "\$CHILDPID is unset" + fi + kill -SIGINT "${CHILDPID}" +} + +# Print the MCSD banner function print_banner() { echo " ____ ____ ____ __" echo " /'\\_/\`\\/\\ _\`\\ /\\ _\`\\ /\\ _\`\\ /\\ \\" @@ -12,18 +39,23 @@ function print_banner() { echo " \\/_/ \\/_/\\/___/ \\/_____/\\/____/ \\/_/ \\/__/ \\/____/ \\/_/ \\/___/ \\/___/ \\/____/ \\/_/\\/_/\\/____/ \\/_/" } +# Initialize MCSD function initialize() { echo ":: Initializing MCSD" - export "ARGS=--nogui " - if [ -z "${MCSD_ENFORCE_FRESH_CACHES}" ]; then - export "ARGS=${ARGS}--eraseCache " + # Server arguments + if [ ! "${MCSD_ENFORCE_FRESH_CACHES}" == "true" ] && [ ! "${MCSD_ENFORCE_FRESH_CACHES}" == "false" ]; then + echo ":: Warning: No/Invalid value supplied to \$MCSD_ENFORCE_FRESH_CACHES, defaulting to false" + export "MCSD_ENFORCE_FRESH_CACHES=false" fi - if [ -z "${MCSD_FORCE_UPGRADE}" ]; then - export "ARGS=${ARGS}--forceUpgrade " + if [ ! "${MCSD_FORCE_UPGRADE}" == "true" ] && [ ! "${MCSD_FORCE_UPGRADE}" == "false" ]; then + echo ":: Warning: No/Invalid value supplied to \$MCSD_FORCE_UPGRADE, defaulting to true" + export "MCSD_FORCE_UPGRADE=true" fi - if [ -z "${MCSD_SAFEMODE}" ]; then - export "ARGS=${ARGS}--safeMode " + if [ ! "${MCSD_SAFEMODE}" == "true" ] && [ ! "${MCSD_SAFEMODE}" == "false" ]; then + echo ":: Warning: No/Invalid value supplied to \$MCSD_SAFEMODE, defaulting to false" + export "MCSD_SAFEMODE=false" fi + # MCSD functionality if [ ! "${MCSD_AUTO_RESTART}" == "true" ] && [ ! "${MCSD_AUTO_RESTART}" == "false" ]; then echo ":: Warning: No/Invalid value supplied to \$MCSD_AUTO_RESTART, defaulting to false" export "MCSD_AUTO_RESTART=false" @@ -32,12 +64,32 @@ function initialize() { echo ":: Warning: No/Invalid value supplied to \$MCSD_CLEAN_LOGS, defaulting to false" export "MCSD_CLEAN_LOGS=false" fi + # Memory limits + if [ -n "${MCSD_MEMORY_MIN}" ]; then + echo ":: Error: No memory limit supplied to \$MCSD_MEMORY_MIN" + exit 1 + fi + if [ -n "${MCSD_MEMORY_MAX}" ]; then + echo ":: Error: No memory limit supplied to \$MCSD_MEMORY_MAX" + exit 1 + fi + # Java version if [ ! "${MCSD_JAVA_VERSION}" == "17" ] && [ ! "${MCSD_JAVA_VERSION}" == "11" ] && [ ! "${MCSD_JAVA_VERSION}" == "8" ]; then echo ":: Warning: No/Invalid java version supplied to \$MCSD_JAVA_VERSION, defaulting to java version 17" export "MCSD_JAVA_VERSION=17" fi + # Java optimization + if [ ! "${MCSD_OPTIMIZED_FLAGS}" == "true" ] && [ ! "${MCSD_OPTIMIZED_FLAGS}" == "false" ]; then + echo ":: Warning: No/Invalid value supplied to \$MCSD_OPTIMIZED_FLAGS, defaulting to true" + export "MCSD_OPTIMIZED_FLAGS=true" + fi + if [ ! "${MCSD_GARBAGECOLLECTOR}" == "G1GC" ] && [ ! "${MCSD_GARBAGECOLLECTOR}" == "ZGC" ]; then + echo ":: Warning: No/Invalid value supplied to \$MCSD_GARBAGECOLLECTOR, defaulting to G1GC" + export "MCSD_GARBAGECOLLECTOR=G1GC" + fi } +# Check for directories and files function check() { echo ":: Checking environment" if [ ! -d "/data" ]; then @@ -50,27 +102,96 @@ function check() { fi } -function accept_eula() { - echo "eula=true" > /data/eula.txt +# Compile server arguments +function compile_args() { + echo ":: Compiling server arguments" + export "ARGS=--nogui" + case "${MCSD_ENFORCE_FRESH_CACHES}" in + "true") + export "ARGS=${ARGS} --eraseCache" + ;; + "false") ;; + *) + internal_error "\$MCSD_ENFORCE_FRESH_CACHES has invalid value" + ;; + esac + case "${MCSD_FORCE_UPGRADE}" in + "true") + export "ARGS=${ARGS} --forceUpgrade" + ;; + "false") ;; + *) + internal_error "\$MCSD_FORCE_UPGRADE has invalid value" + ;; + esac + case "${MCSD_SAFEMODE}" in + "true") + export "ARGS=${ARGS} --safeMode" + ;; + "false") ;; + *) + internal_error "\$MCSD_SAFEMODE has invalid value" + ;; + esac } -function kill_server() { - kill -SIGINT "$CHILDPID" +# Compile java flags +function compile_flags() { + echo ":: Compiling java flags" + export "FLAGS=-Xms${MCSD_MEMORY_MIN} -Xmx${MCSD_MEMORY_MAX}" + case "${MCSD_OPTIMIZED_FLAGS}" in + "true") + export "FLAGS=${FLAGS} -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+AlwaysActAsServerClassMachine -XX:+AlwaysPreTouch -XX:+DisableExplicitGC -XX:+UseNUMA -XX:NmethodSweepActivity=1 -XX:ReservedCodeCacheSize=400M -XX:NonNMethodCodeHeapSize=12M -XX:ProfiledCodeHeapSize=194M -XX:NonProfiledCodeHeapSize=194M -XX:-DontCompileHugeMethods -XX:MaxNodeLimit=240000 -XX:NodeLimitFudgeFactor=8000 -XX:+UseVectorCmov -XX:+PerfDisableSharedMem -XX:+UseFastUnorderedTimeStamps -XX:+UseCriticalJavaThreadPriority -XX:ThreadPriorityPolicy=1 -XX:AllocatePrefetchStyle=3" + ;; + "false") ;; + *) + internal_error "\$MCSD_OPTIMIZED_FLAGS has invalid value" + ;; + esac + case "${MCSD_GARBAGECOLLECTOR}" in + "G1GC") + export "FLAGS=${FLAGS} -XX:+UseG1GC -XX:MaxGCPauseMillis=130 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=28 -XX:G1HeapRegionSize=16M -XX:G1ReservePercent=20 -XX:G1MixedGCCountTarget=3 -XX:InitiatingHeapOccupancyPercent=10 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=0 -XX:SurvivorRatio=32 -XX:MaxTenuringThreshold=1 -XX:G1SATBBufferEnqueueingThresholdPercent=30 -XX:G1ConcMarkStepDurationMillis=5 -XX:G1ConcRSHotCardLimit=16 -XX:G1ConcRefinementServiceIntervalMillis=150" + ;; + "ZGC") + export "FLAGS=${FLAGS} -XX:+UseZGC -XX:AllocatePrefetchStyle=1 -XX:-ZProactive" + ;; + *) + internal_error "\$MCSD_GARBAGECOLLECTOR has invalid value" + ;; + esac } +# Starts the server function run() { echo ":: Starting server" cd /data||exit 69 # We want to use ${ARGS} without using parenthesis # shellcheck disable=SC2086 - "/usr/lib/jvm/java-${MCSD_JAVA_VERSION}-openjdk/bin/java" -jar "/data/server.jar" ${ARGS} & + "/usr/lib/jvm/java-${MCSD_JAVA_VERSION}-openjdk/bin/java" ${FLAGS} -jar "/data/server.jar" ${ARGS} & + # Set server pid export "CHILDPID=${!}" + # Setup trap trap kill_server SIGTERM trap kill_server SIGINT + # Wait for server to end wait +} + +# Need I say more? +function main() { + print_banner + initialize + check + accept_eula + clean_logs + compile_args + compile_flags + # Run server once + run + # Restart server if enabled case "${MCSD_AUTO_RESTART}" in "true") - echo ":: Restarting server after shutdown" + echo ":: Restarting server" run ;; "false") @@ -78,18 +199,9 @@ function run() { exit 0 ;; *) - echo ":: An internal error occured (run function, can't decide MCSD_AUTO_RESTART)" + internal_error "\$MCSD_AUTO_RESTART has invalid value" ;; - esac -} - -function main() { - print_banner - initialize - check - accept_eula - if [ "${MCSD_CLEAN_LOGS}" == "true" ]; then rm -rf "/data/logs" "/data/crash-reports"; fi - run + esac } main