2017-11-01 05:42:44 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-01-27 02:25:52 +00:00
|
|
|
# shellcheck source=start-utils
|
|
|
|
. "${SCRIPTS:-/}start-utils"
|
2021-03-11 22:21:59 +00:00
|
|
|
isDebugging && set -x
|
2019-05-26 16:59:39 +00:00
|
|
|
|
2021-01-13 22:47:15 +00:00
|
|
|
if [ -n "$ICON" ]; then
|
2022-03-15 01:28:41 +00:00
|
|
|
if [ ! -e server-icon.png ] || isTrue "${OVERRIDE_ICON}"; then
|
2021-01-13 22:47:15 +00:00
|
|
|
log "Using server icon from $ICON..."
|
2022-04-10 16:39:58 +00:00
|
|
|
if isURL "$ICON"; then
|
|
|
|
# Not sure what it is yet...call it "img"
|
|
|
|
if ! get -o /tmp/icon.img "$ICON"; then
|
|
|
|
log "ERROR: failed to download icon from $ICON"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
ICON=/tmp/icon.img
|
|
|
|
iconSrc="url"
|
|
|
|
elif [ -f "$ICON" ]; then
|
|
|
|
iconSrc="file"
|
|
|
|
else
|
|
|
|
log "ERROR: $ICON does not appear to be a URL or existing file"
|
2022-03-15 01:28:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-04-10 16:39:58 +00:00
|
|
|
read -r -a specs < <(identify "$ICON" | awk 'NR == 1 { print $2, $3 }')
|
2022-03-15 01:28:41 +00:00
|
|
|
if [ "${specs[0]} ${specs[1]}" = "PNG 64x64" ]; then
|
2022-04-10 16:39:58 +00:00
|
|
|
if [ $iconSrc = url ]; then
|
|
|
|
mv -f /tmp/icon.img /data/server-icon.png
|
|
|
|
else
|
|
|
|
cp -f "$ICON" /data/server-icon.png
|
|
|
|
fi
|
2022-03-15 01:28:41 +00:00
|
|
|
elif [ "${specs[0]}" = GIF ]; then
|
|
|
|
log "Converting GIF image to 64x64 PNG..."
|
2022-04-10 16:39:58 +00:00
|
|
|
convert "$ICON"[0] -resize 64x64! /data/server-icon.png
|
2021-01-13 22:47:15 +00:00
|
|
|
else
|
|
|
|
log "Converting image to 64x64 PNG..."
|
2022-04-10 16:39:58 +00:00
|
|
|
convert "$ICON" -resize 64x64! /data/server-icon.png
|
2021-01-13 22:47:15 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-11-01 05:42:44 +00:00
|
|
|
fi
|
|
|
|
|
2021-12-12 14:19:16 +00:00
|
|
|
canUseRollingLogs=true
|
2022-01-30 19:46:09 +00:00
|
|
|
useFallbackJvmFlag=false
|
2021-12-12 14:19:16 +00:00
|
|
|
|
|
|
|
patchLog4jConfig() {
|
|
|
|
file=${1?}
|
|
|
|
url=${2?}
|
|
|
|
if ! get -o "$file" "$url"; then
|
2022-01-30 19:46:09 +00:00
|
|
|
log "ERROR: failed to download corrected log4j config, fallback to JVM flag"
|
|
|
|
useFallbackJvmFlag=true
|
|
|
|
return 1
|
2021-12-12 14:19:16 +00:00
|
|
|
fi
|
|
|
|
JVM_OPTS="-Dlog4j.configurationFile=${file} ${JVM_OPTS}"
|
|
|
|
canUseRollingLogs=false
|
|
|
|
}
|
|
|
|
|
|
|
|
# Patch Log4j remote code execution vulnerability
|
|
|
|
# See https://www.minecraft.net/en-us/article/important-message--security-vulnerability-java-edition
|
|
|
|
if versionLessThan 1.7; then
|
|
|
|
: # No patch required here.
|
|
|
|
elif isFamily VANILLA && versionLessThan 1.12; then
|
|
|
|
patchLog4jConfig log4j2_17-111.xml https://launcher.mojang.com/v1/objects/dd2b723346a8dcd48e7f4d245f6bf09e98db9696/log4j2_17-111.xml
|
|
|
|
elif isFamily VANILLA && versionLessThan 1.17; then
|
|
|
|
patchLog4jConfig log4j2_112-116.xml https://launcher.mojang.com/v1/objects/02937d122c86ce73319ef9975b58896fc1b491d1/log4j2_112-116.xml
|
2022-01-17 02:49:15 +00:00
|
|
|
# See https://purpurmc.org/docs/Log4j/
|
|
|
|
elif isType PURPUR && versionLessThan 1.17; then
|
|
|
|
patchLog4jConfig purpur_log4j2_1141-1165.xml https://purpurmc.org/docs/xml/purpur_log4j2_1141-1165.xml
|
|
|
|
elif isType PURPUR && versionLessThan 1.18.1; then
|
|
|
|
patchLog4jConfig purpur_log4j2_117.xml https://purpurmc.org/docs/xml/purpur_log4j2_117.xml
|
2021-12-12 14:19:16 +00:00
|
|
|
elif versionLessThan 1.18.1; then
|
2022-01-30 19:46:09 +00:00
|
|
|
useFallbackJvmFlag=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ${useFallbackJvmFlag}; then
|
2021-12-12 14:19:16 +00:00
|
|
|
JVM_OPTS="-Dlog4j2.formatMsgNoLookups=true ${JVM_OPTS}"
|
|
|
|
fi
|
|
|
|
|
2021-04-02 23:23:05 +00:00
|
|
|
if isTrue ${ENABLE_ROLLING_LOGS:-false}; then
|
2021-12-12 14:19:16 +00:00
|
|
|
if ! ${canUseRollingLogs}; then
|
|
|
|
log "ERROR: Using rolling logs is currently not possible in the selected version due to CVE-2021-44228"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-04-02 23:23:05 +00:00
|
|
|
# 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}"
|
2020-04-05 22:32:26 +00:00
|
|
|
fi
|
2020-03-20 21:29:52 +00:00
|
|
|
|
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."
|
2020-04-15 13:20:11 +00:00
|
|
|
JSON_FILES=$(find /data -maxdepth 1 -name '*.json')
|
2018-10-14 17:47:39 +00:00
|
|
|
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"
|
2018-10-14 17:47:39 +00:00
|
|
|
echo '[]' > $j
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-11-01 05:42:44 +00:00
|
|
|
# Optional disable console
|
2020-04-13 23:29:44 +00:00
|
|
|
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
|
2022-01-27 02:25:52 +00:00
|
|
|
if [[ ${GUI,,} = false ]]; then
|
2021-03-11 21:08:26 +00:00
|
|
|
EXTRA_ARGS+=" nogui"
|
2017-11-01 05:42:44 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-10 15:58:18 +00:00
|
|
|
: "${MEMORY:=1G}"
|
2021-10-31 14:42:27 +00:00
|
|
|
: "${INIT_MEMORY:=${MEMORY}}"
|
|
|
|
: "${MAX_MEMORY:=${MEMORY}}"
|
2017-11-01 05:42:44 +00:00
|
|
|
|
2019-01-14 04:22:27 +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
|
2021-02-20 23:34:00 +00:00
|
|
|
-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
|
|
|
|
|
2020-04-11 15:41:20 +00:00
|
|
|
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
|
2020-04-11 15:41:20 +00:00
|
|
|
-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
|
2020-04-11 15:41:20 +00:00
|
|
|
-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
|
|
|
|
|
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
|
2020-04-27 13:04:11 +00:00
|
|
|
log "Memory usage and availability (in MB)"
|
|
|
|
uname -a
|
2020-04-27 12:55:38 +00:00
|
|
|
free -m
|
|
|
|
fi
|
|
|
|
|
2021-10-31 14:42:27 +00:00
|
|
|
if [[ ${INIT_MEMORY} || ${MAX_MEMORY} ]]; then
|
|
|
|
log "Setting initial memory to ${INIT_MEMORY:=${MEMORY}} and max to ${MAX_MEMORY:=${MEMORY}}"
|
|
|
|
if [[ ${INIT_MEMORY} ]]; then
|
|
|
|
JVM_OPTS="-Xms${INIT_MEMORY} ${JVM_OPTS}"
|
|
|
|
fi
|
|
|
|
if [[ ${MAX_MEMORY} ]]; then
|
|
|
|
JVM_OPTS="-Xmx${MAX_MEMORY} ${JVM_OPTS}"
|
|
|
|
fi
|
|
|
|
fi
|
2020-07-19 20:01:19 +00:00
|
|
|
|
2020-07-19 21:29:41 +00:00
|
|
|
function copyFilesForCurseForge() {
|
|
|
|
# copy player modification files unconditionally since their
|
|
|
|
# processing into json is additive anyway
|
2021-01-07 04:38:18 +00:00
|
|
|
[ -f /data/ops.txt ] && cp -f /data/ops.txt "${FTB_DIR}/"
|
|
|
|
[ -f /data/white-list.txt ] && cp -f /data/white-list.txt "${FTB_DIR}/"
|
2020-07-19 21:29:41 +00:00
|
|
|
|
|
|
|
if [ ! -e "${FTB_DIR}/server-icon.png" -a -e /data/server-icon.png ]; then
|
2021-01-07 04:38:18 +00:00
|
|
|
cp -f /data/server-icon.png "${FTB_DIR}/"
|
2020-07-19 21:29:41 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
cp -f /data/eula.txt "${FTB_DIR}/"
|
|
|
|
}
|
|
|
|
|
2021-10-09 16:34:37 +00:00
|
|
|
mcServerRunnerArgs=(
|
|
|
|
--stop-duration "${STOP_DURATION:-60}s"
|
|
|
|
--named-pipe "${CONSOLE_IN_NAMED_PIPE:-/tmp/minecraft-console-in}"
|
|
|
|
)
|
2021-08-01 17:09:18 +00:00
|
|
|
if [[ ${STOP_SERVER_ANNOUNCE_DELAY} ]]; then
|
2021-10-09 16:34:37 +00:00
|
|
|
mcServerRunnerArgs+=(--stop-server-announce-delay "${STOP_SERVER_ANNOUNCE_DELAY}s")
|
2021-08-01 17:09:18 +00:00
|
|
|
fi
|
|
|
|
|
2021-10-23 02:10:59 +00:00
|
|
|
if [[ ${TYPE} == "CURSEFORGE" && "${SERVER}" ]]; then
|
2020-07-19 21:29:41 +00:00
|
|
|
copyFilesForCurseForge
|
|
|
|
|
2021-10-09 16:34:37 +00:00
|
|
|
cd "${FTB_DIR}" || (log "ERROR: can't go into ${FTB_DIR}"; exit 1)
|
2020-07-19 20:01:19 +00:00
|
|
|
log "Starting CurseForge server in ${FTB_DIR}..."
|
|
|
|
if isTrue ${DEBUG_EXEC}; then
|
|
|
|
set -x
|
|
|
|
fi
|
2021-10-09 16:34:37 +00:00
|
|
|
exec mc-server-runner ${bootstrapArgs} "${mcServerRunnerArgs[@]}" java $JVM_XX_OPTS $JVM_OPTS $expandedDOpts -jar $(basename "${SERVER}") "$@" $EXTRA_ARGS
|
2021-04-26 13:19:50 +00:00
|
|
|
elif [[ ${TYPE} == "CURSEFORGE" ]]; then
|
2021-10-09 16:34:37 +00:00
|
|
|
mcServerRunnerArgs+=(--shell bash)
|
2020-04-01 23:50:07 +00:00
|
|
|
|
2020-07-19 21:29:41 +00:00
|
|
|
copyFilesForCurseForge
|
2018-02-06 20:21:23 +00:00
|
|
|
|
2020-07-19 21:29:41 +00:00
|
|
|
cat > "${FTB_DIR}/settings-local.sh" <<EOF
|
2018-07-13 21:35:56 +00:00
|
|
|
export MIN_RAM="${INIT_MEMORY}"
|
|
|
|
export MAX_RAM="${MAX_MEMORY}"
|
2021-10-31 14:42:27 +00:00
|
|
|
export JAVA_PARAMETERS="${JVM_XX_OPTS} ${JVM_OPTS} $expandedDOpts"
|
2018-07-13 21:35:56 +00:00
|
|
|
EOF
|
|
|
|
|
2020-07-19 21:29:41 +00:00
|
|
|
# patch CurseForge cfg file, if present
|
2021-10-31 14:42:27 +00:00
|
|
|
if [ -f "${FTB_DIR}/settings.cfg" ] && [[ ${MAX_MEMORY} ]]; then
|
2020-07-19 21:29:41 +00:00
|
|
|
sed -i "s/MAX_RAM=[^;]*/MAX_RAM=${MAX_MEMORY}/" "${FTB_DIR}/settings.cfg"
|
|
|
|
fi
|
2019-01-26 22:35:00 +00:00
|
|
|
|
2021-10-09 16:34:37 +00:00
|
|
|
cd "${FTB_DIR}" || (log "ERROR: can't go into ${FTB_DIR}"; exit 1)
|
2020-07-19 21:29:41 +00:00
|
|
|
log "Running FTB ${FTB_SERVER_START} in ${FTB_DIR} ..."
|
2021-04-02 23:23:05 +00:00
|
|
|
|
2021-10-09 16:34:37 +00:00
|
|
|
finalArgs="${FTB_SERVER_START}"
|
2021-04-02 23:23:05 +00:00
|
|
|
|
2021-10-31 14:42:27 +00:00
|
|
|
if isTrue "${SETUP_ONLY:=false}"; then
|
2021-10-09 16:34:37 +00:00
|
|
|
echo "SETUP_ONLY: ${finalArgs}"
|
2021-05-23 17:39:10 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2020-07-19 21:29:41 +00:00
|
|
|
if isTrue ${DEBUG_EXEC}; then
|
|
|
|
set -x
|
|
|
|
fi
|
2021-04-02 23:23:05 +00:00
|
|
|
if isTrue ${EXEC_DIRECTLY:-false}; then
|
|
|
|
"${finalArgs[@]}"
|
|
|
|
else
|
2021-10-09 16:34:37 +00:00
|
|
|
exec mc-server-runner "${mcServerRunnerArgs[@]}" "${finalArgs[@]}"
|
2021-04-02 23:23:05 +00:00
|
|
|
fi
|
2022-01-08 19:19:30 +00:00
|
|
|
elif [[ $SERVER =~ run.sh ]]; then
|
2021-07-24 02:16:51 +00:00
|
|
|
log "Using Forge supplied run.sh script..."
|
|
|
|
echo $JVM_XX_OPTS $JVM_OPTS $expandedDOpts > user_jvm_args.txt
|
2022-01-29 20:53:34 +00:00
|
|
|
if isTrue ${SETUP_ONLY:=false}; then
|
|
|
|
echo "SETUP_ONLY: bash ${SERVER}"
|
|
|
|
exit
|
|
|
|
fi
|
2022-01-08 19:19:30 +00:00
|
|
|
exec mc-server-runner "${mcServerRunnerArgs[@]}" --shell bash "${SERVER}"
|
2017-11-01 05:42:44 +00:00
|
|
|
else
|
2018-07-13 21:35:56 +00:00
|
|
|
# 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
|
2019-05-13 23:21:38 +00:00
|
|
|
|
2020-03-06 15:52:17 +00:00
|
|
|
log "Starting the Minecraft server..."
|
2021-04-02 23:23:05 +00:00
|
|
|
|
|
|
|
finalArgs=(
|
|
|
|
$JVM_XX_OPTS
|
|
|
|
$JVM_OPTS
|
|
|
|
$expandedDOpts
|
2021-10-09 16:34:37 +00:00
|
|
|
-jar "$SERVER"
|
2021-04-02 23:23:05 +00:00
|
|
|
"$@" $EXTRA_ARGS
|
|
|
|
)
|
|
|
|
|
2021-05-23 17:39:10 +00:00
|
|
|
if isTrue ${SETUP_ONLY:=false}; then
|
2021-10-09 16:34:37 +00:00
|
|
|
echo "SETUP_ONLY: java ${finalArgs[*]}"
|
2021-05-23 17:39:10 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2021-10-31 14:42:27 +00:00
|
|
|
if isTrue "${DEBUG_EXEC}"; then
|
2019-05-26 16:59:39 +00:00
|
|
|
set -x
|
|
|
|
fi
|
2021-04-02 23:23:05 +00:00
|
|
|
|
2021-10-31 14:42:27 +00:00
|
|
|
if isTrue "${EXEC_DIRECTLY:-false}"; then
|
2021-04-02 23:23:05 +00:00
|
|
|
exec java "${finalArgs[@]}"
|
|
|
|
else
|
2021-10-09 16:34:37 +00:00
|
|
|
exec mc-server-runner ${bootstrapArgs} "${mcServerRunnerArgs[@]}" java "${finalArgs[@]}"
|
2021-04-02 23:23:05 +00:00
|
|
|
fi
|
2017-11-01 05:42:44 +00:00
|
|
|
fi
|
2021-05-21 11:51:17 +00:00
|
|
|
|