docker-minecraft-server/start-finalExec

269 lines
7.5 KiB
Text
Raw Normal View History

2017-11-01 05:42:44 +00:00
#!/bin/bash
. ${SCRIPTS:-/}start-utils
isDebugging && set -x
if [ -n "$OPS" ]; then
log "Updating ops"
rm -f /data/ops.txt.converted
echo $OPS | awk -v RS=, '{print}' > /data/ops.txt
fi
if isTrue "${OVERRIDE_OPS}"; then
log "Recreating ops.json file at server startup"
rm -f /data/ops.json
2017-11-01 05:42:44 +00:00
fi
if [ -n "$WHITELIST" ]; then
log "Updating whitelist"
rm -f /data/white-list.txt.converted
echo $WHITELIST | awk -v RS=, '{print}' > /data/white-list.txt
fi
if isTrue "${OVERRIDE_WHITELIST}"; then
log "Recreating whitelist.json file at server startup"
rm -f /data/whitelist.json
2017-11-01 05:42:44 +00:00
fi
if [ -n "$ICON" ]; then
if [ ! -e server-icon.png ] || [ "${OVERRIDE_ICON}" == "TRUE" ]; then
log "Using server icon from $ICON..."
# Not sure what it is yet...call it "img"
curl -sSL -o /tmp/icon.img $ICON
specs=$(identify /tmp/icon.img | awk '{print $2,$3}')
if [ "$specs" = "PNG 64x64" ]; then
mv /tmp/icon.img /data/server-icon.png
else
log "Converting image to 64x64 PNG..."
convert /tmp/icon.img -resize 64x64! /data/server-icon.png
fi
fi
2017-11-01 05:42:44 +00:00
fi
if isTrue ${ENABLE_ROLLING_LOGS:-false}; then
# Set up log configuration
LOGFILE="/data/log4j2.xml"
if [ ! -e "$LOGFILE" ]; then
log "Creating log4j2.xml in ${LOGFILE}"
cp /tmp/log4j2.xml "$LOGFILE"
else
log "log4j2.xml already created, skipping"
fi
JVM_OPTS="-Dlog4j.configurationFile=/data/log4j2.xml ${JVM_OPTS}"
fi
2017-11-01 05:42:44 +00:00
# Make sure files exist and are valid JSON (for pre-1.12 to 1.12 upgrades)
2020-03-06 15:52:17 +00:00
log "Checking for JSON files."
JSON_FILES=$(find /data -maxdepth 1 -name '*.json')
for j in $JSON_FILES; do
2020-04-11 13:43:17 +00:00
if [[ $(cat "$j" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') == "" ]]; then
2020-03-06 15:52:17 +00:00
log "Fixing JSON $j"
echo '[]' > $j
fi
done
2017-11-01 05:42:44 +00:00
# Optional disable console
if versionLessThan 1.14 && [[ ${CONSOLE,,} = false ]]; then
2021-03-11 21:08:26 +00:00
EXTRA_ARGS+=" --noconsole"
2017-11-01 05:42:44 +00:00
fi
# Optional disable GUI for headless servers
if [[ ${GUI} = false || ${GUI} = FALSE ]]; then
2021-03-11 21:08:26 +00:00
EXTRA_ARGS+=" nogui"
2017-11-01 05:42:44 +00:00
fi
# put these prior JVM_OPTS at the end to give any memory settings there higher precedence
2020-03-06 15:52:17 +00:00
log "Setting initial memory to ${INIT_MEMORY:=${MEMORY}} and max to ${MAX_MEMORY:=${MEMORY}}"
2017-11-01 05:42:44 +00:00
expandedDOpts=
if [ -n "$JVM_DD_OPTS" ]; then
for dopt in $JVM_DD_OPTS
do
expandedDOpts="${expandedDOpts} -D${dopt/:/=}"
done
fi
2020-04-03 17:32:46 +00:00
if isTrue ${ENABLE_JMX}; then
: ${JMX_PORT:=7091}
JVM_OPTS="${JVM_OPTS}
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.port=${JMX_PORT}
-Dcom.sun.management.jmxremote.rmi.port=${JMX_PORT}
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.host=${JMX_BINDING:-0.0.0.0}
-Djava.rmi.server.hostname=${JMX_HOST:-localhost}"
2020-04-03 17:32:46 +00:00
log "JMX is enabled. Make sure you have port forwarding for ${JMX_PORT}"
fi
if isTrue "${USE_AIKAR_FLAGS}"; then
# From https://mcflags.emc.gs/
if (( $(normalizeMemSize "${MAX_MEMORY}") >= $(normalizeMemSize 12g) )); then
log "Using Aikar's >12GB flags"
G1NewSizePercent=40
G1MaxNewSizePercent=50
G1HeapRegionSize=16M
G1ReservePercent=15
InitiatingHeapOccupancyPercent=20
else
log "Using Aikar's flags"
G1NewSizePercent=30
G1MaxNewSizePercent=40
G1HeapRegionSize=8M
G1ReservePercent=20
InitiatingHeapOccupancyPercent=15
fi
JVM_XX_OPTS="${JVM_XX_OPTS}
2020-05-19 00:55:32 +00:00
-XX:+UseG1GC
-XX:+ParallelRefProcEnabled
2020-04-11 13:43:17 +00:00
-XX:MaxGCPauseMillis=200
-XX:+UnlockExperimentalVMOptions
-XX:+DisableExplicitGC
-XX:+AlwaysPreTouch
-XX:G1NewSizePercent=${G1NewSizePercent}
-XX:G1MaxNewSizePercent=${G1MaxNewSizePercent}
-XX:G1HeapRegionSize=${G1HeapRegionSize}
-XX:G1ReservePercent=${G1ReservePercent}
2020-04-11 13:43:17 +00:00
-XX:G1HeapWastePercent=5
2020-05-19 00:55:32 +00:00
-XX:G1MixedGCCountTarget=4
-XX:InitiatingHeapOccupancyPercent=${InitiatingHeapOccupancyPercent}
2020-04-11 13:43:17 +00:00
-XX:G1MixedGCLiveThresholdPercent=90
-XX:G1RSetUpdatingPauseTimePercent=5
-XX:SurvivorRatio=32
2020-05-19 00:55:32 +00:00
-XX:+PerfDisableSharedMem
2020-04-11 13:43:17 +00:00
-XX:MaxTenuringThreshold=1
2020-05-19 00:55:32 +00:00
-Dusing.aikars.flags=https://mcflags.emc.gs
2020-04-11 13:43:17 +00:00
-Daikars.new.flags=true
"
fi
if isTrue "${USE_LARGE_PAGES}"; then
JVM_XX_OPTS="${JVM_XX_OPTS}
-XX:+UseLargePagesInMetaspace
"
fi
2021-04-09 17:25:51 +00:00
if isTrue "${USE_FLARE_FLAGS}"; then
JVM_XX_OPTS="${JVM_XX_OPTS}
-XX:+UnlockDiagnosticVMOptions
-XX:+DebugNonSafepoints
"
fi
2020-04-27 12:55:38 +00:00
if isTrue "${DEBUG_MEMORY}"; then
log "Memory usage and availability (in MB)"
uname -a
2020-04-27 12:55:38 +00:00
free -m
fi
JVM_OPTS="-Xms${INIT_MEMORY} -Xmx${MAX_MEMORY} ${JVM_OPTS}"
function copyFilesForCurseForge() {
# copy player modification files unconditionally since their
# processing into json is additive anyway
[ -f /data/ops.txt ] && cp -f /data/ops.txt "${FTB_DIR}/"
[ -f /data/white-list.txt ] && cp -f /data/white-list.txt "${FTB_DIR}/"
if [ ! -e "${FTB_DIR}/server-icon.png" -a -e /data/server-icon.png ]; then
cp -f /data/server-icon.png "${FTB_DIR}/"
fi
cp -f /data/eula.txt "${FTB_DIR}/"
}
mcServerRunnerArgs=(
--stop-duration "${STOP_DURATION:-60}s"
--named-pipe "${CONSOLE_IN_NAMED_PIPE:-/tmp/minecraft-console-in}"
)
if [[ ${STOP_SERVER_ANNOUNCE_DELAY} ]]; then
mcServerRunnerArgs+=(--stop-server-announce-delay "${STOP_SERVER_ANNOUNCE_DELAY}s")
fi
2020-04-12 19:31:19 +00:00
if [[ ${TYPE} == "CURSE_INSTANCE" ]]; then
if isTrue ${DEBUG_EXEC}; then
set -x
fi
exec mc-server-runner "${mcServerRunnerArgs[@]}" \
2020-04-12 19:31:19 +00:00
--cf-instance-file "${CURSE_INSTANCE_JSON}" \
java $JVM_XX_OPTS $JVM_OPTS $expandedDOpts -jar _SERVERJAR_ "$@" $EXTRA_ARGS
elif [[ ${TYPE} == "CURSEFORGE" && "${SERVER}" ]]; then
copyFilesForCurseForge
cd "${FTB_DIR}" || (log "ERROR: can't go into ${FTB_DIR}"; exit 1)
log "Starting CurseForge server in ${FTB_DIR}..."
if isTrue ${DEBUG_EXEC}; then
set -x
fi
exec mc-server-runner ${bootstrapArgs} "${mcServerRunnerArgs[@]}" java $JVM_XX_OPTS $JVM_OPTS $expandedDOpts -jar $(basename "${SERVER}") "$@" $EXTRA_ARGS
elif [[ ${TYPE} == "CURSEFORGE" ]]; then
mcServerRunnerArgs+=(--shell bash)
copyFilesForCurseForge
cat > "${FTB_DIR}/settings-local.sh" <<EOF
export MIN_RAM="${INIT_MEMORY}"
export MAX_RAM="${MAX_MEMORY}"
export JAVA_PARAMETERS="${JVM_XX_OPTS} -Xms${INIT_MEMORY} ${JVM_OPTS} $expandedDOpts"
EOF
# patch CurseForge cfg file, if present
if [ -f "${FTB_DIR}/settings.cfg" ]; then
sed -i "s/MAX_RAM=[^;]*/MAX_RAM=${MAX_MEMORY}/" "${FTB_DIR}/settings.cfg"
fi
cd "${FTB_DIR}" || (log "ERROR: can't go into ${FTB_DIR}"; exit 1)
log "Running FTB ${FTB_SERVER_START} in ${FTB_DIR} ..."
finalArgs="${FTB_SERVER_START}"
2021-05-23 17:39:10 +00:00
if isTrue ${SETUP_ONLY:=false}; then
echo "SETUP_ONLY: ${finalArgs}"
2021-05-23 17:39:10 +00:00
exit
fi
if isTrue ${DEBUG_EXEC}; then
set -x
fi
if isTrue ${EXEC_DIRECTLY:-false}; then
"${finalArgs[@]}"
else
exec mc-server-runner "${mcServerRunnerArgs[@]}" "${finalArgs[@]}"
fi
elif [[ -x run.sh ]]; then
log "Using Forge supplied run.sh script..."
echo $JVM_XX_OPTS $JVM_OPTS $expandedDOpts > user_jvm_args.txt
exec mc-server-runner "${mcServerRunnerArgs[@]}" --shell bash run.sh
2017-11-01 05:42:44 +00:00
else
# If we have a bootstrap.txt file... feed that in to the server stdin
if [ -f /data/bootstrap.txt ]; then
bootstrapArgs="--bootstrap /data/bootstrap.txt"
fi
2020-03-06 15:52:17 +00:00
log "Starting the Minecraft server..."
finalArgs=(
$JVM_XX_OPTS
$JVM_OPTS
$expandedDOpts
-jar "$SERVER"
"$@" $EXTRA_ARGS
)
2021-05-23 17:39:10 +00:00
if isTrue ${SETUP_ONLY:=false}; then
echo "SETUP_ONLY: java ${finalArgs[*]}"
2021-05-23 17:39:10 +00:00
exit
fi
if isTrue ${DEBUG_EXEC}; then
set -x
fi
if isTrue ${EXEC_DIRECTLY:-false}; then
exec java "${finalArgs[@]}"
else
exec mc-server-runner ${bootstrapArgs} "${mcServerRunnerArgs[@]}" java "${finalArgs[@]}"
fi
2017-11-01 05:42:44 +00:00
fi
2021-05-21 11:51:17 +00:00