391 lines
13 KiB
Bash
Executable File
391 lines
13 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Container entrypoint: starts the patched Project Zomboid dedicated server
|
|
# with the ZomboidForge agent. Arguments are passed through to
|
|
# zombie.network.GameServer. Java mods are loaded from /forge/mods/<id>/.
|
|
#
|
|
# Environment variables (unset or empty keep the value in the server .ini, or the game default):
|
|
#
|
|
# Server
|
|
# SERVER_NAME Server config name (/data/Server/<name>.ini). Default: zomboidforge
|
|
# SERVER_PASSWORD Password clients need to join (.ini Password)
|
|
# PUBLIC true/false: listed in the in-game server browser (.ini Public)
|
|
# PUBLIC_NAME Name in the server browser (.ini PublicName)
|
|
# PORT Game port (-port). Default: 16261
|
|
# UDP_PORT Direct connection port (-udpport). Default: 16262
|
|
# STEAM 1 = Steam mode (required for official Steam clients), 0 = no Steam. Default: 1
|
|
# UPNP true/false: UPnP port mapping on the router (.ini UPnP). false is
|
|
# recommended: in a container the server sees only its container address,
|
|
# publishing the ports is the container runtime's job, and the router
|
|
# discovery delays every start by about 12 s.
|
|
#
|
|
# Admin account
|
|
# ADMIN_USERNAME Admin account name (-adminusername), passed on every start.
|
|
# Project Zomboid creates the account when it does not exist. Default: admin
|
|
# ADMIN_PASSWORD Password for creating that account (-adminpassword). Required while
|
|
# the account does not exist; not passed once it does.
|
|
#
|
|
# New server
|
|
# SERVER_PRESET Sandbox preset from media/lua/shared/Sandbox (e.g. Apocalypse),
|
|
# copied to <name>_SandboxVars.lua when that file does not exist
|
|
#
|
|
# Mods
|
|
# MODS Semicolon-separated mod IDs (.ini Mods). Build 42 format: \ModA;\ModB
|
|
# WORKSHOP_ITEMS Semicolon-separated Workshop item IDs (.ini WorkshopItems)
|
|
#
|
|
# RCON
|
|
# RCON_PASSWORD Written into the .ini before every start. RCON is disabled when empty.
|
|
#
|
|
# JVM
|
|
# JVM_MIN_HEAP Value for -Xms. Default: JVM default. ZGC returns unused heap memory
|
|
# only down to this size.
|
|
# JVM_MAX_HEAP Value for -Xmx. Default: 75 % of the container memory limit
|
|
# (cgroup v2 memory.max); 8g, the vanilla ProjectZomboid64.json value,
|
|
# when the container has no limit
|
|
# JVM_UNCOMMIT_DELAY
|
|
# Seconds of disuse after which ZGC returns heap memory to the system
|
|
# (-XX:ZUncommitDelay). Default: 60 (JVM default: 300)
|
|
#
|
|
# Maintenance
|
|
# SOFT_RESET true: start in Project Zomboid's soft reset mode (-Dsoftreset).
|
|
# Intended for a single start; unset it afterwards.
|
|
#
|
|
# ZomboidForge features (also /forge/forge.toml [features]; a set variable takes precedence)
|
|
# ZF_EXPERIMENTAL_PERFORMANCE
|
|
# true/false: performance changes that alter timing or resource use
|
|
# (more threads, shorter waits). Default: false
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
|
|
readonly INSTALL_DIR="/opt/pz"
|
|
readonly DATA_DIR="/data"
|
|
readonly FORGE_DIR="/forge"
|
|
readonly AGENT_JAR="${INSTALL_DIR}/zomboidforge-agent.jar"
|
|
readonly JAVA="${INSTALL_DIR}/jre64/bin/java"
|
|
readonly SANDBOX_PRESET_DIR="${INSTALL_DIR}/media/lua/shared/Sandbox"
|
|
|
|
readonly SERVER_NAME="${SERVER_NAME:-zomboidforge}"
|
|
readonly SERVER_PASSWORD="${SERVER_PASSWORD:-}"
|
|
readonly PUBLIC="${PUBLIC:-}"
|
|
readonly PUBLIC_NAME="${PUBLIC_NAME:-}"
|
|
readonly PORT="${PORT:-}"
|
|
readonly UDP_PORT="${UDP_PORT:-}"
|
|
readonly STEAM="${STEAM:-1}"
|
|
readonly UPNP="${UPNP:-}"
|
|
readonly ADMIN_USERNAME="${ADMIN_USERNAME:-}"
|
|
readonly ADMIN_PASSWORD="${ADMIN_PASSWORD:-}"
|
|
readonly SERVER_PRESET="${SERVER_PRESET:-}"
|
|
readonly MODS="${MODS:-}"
|
|
readonly WORKSHOP_ITEMS="${WORKSHOP_ITEMS:-}"
|
|
readonly RCON_PASSWORD="${RCON_PASSWORD:-}"
|
|
readonly JVM_MIN_HEAP="${JVM_MIN_HEAP:-}"
|
|
readonly JVM_MAX_HEAP="${JVM_MAX_HEAP:-}"
|
|
readonly JVM_UNCOMMIT_DELAY="${JVM_UNCOMMIT_DELAY:-60}"
|
|
readonly SOFT_RESET="${SOFT_RESET:-}"
|
|
|
|
readonly SERVER_INI="${DATA_DIR}/Server/${SERVER_NAME}.ini"
|
|
readonly SERVER_SANDBOX_VARS="${DATA_DIR}/Server/${SERVER_NAME}_SandboxVars.lua"
|
|
readonly SERVER_DB="${DATA_DIR}/db/${SERVER_NAME}.db"
|
|
# Target of the /opt/pz/steamapps symlink; Steam stores Workshop downloads there.
|
|
readonly STEAMAPPS_DIR="${DATA_DIR}/steamapps"
|
|
readonly CGROUP_MEMORY_MAX="/sys/fs/cgroup/memory.max"
|
|
# Keys of the ZomboidForge features (core/config/Feature): ZF_<KEY> becomes
|
|
# -Dzomboidforge.features.<key>.
|
|
readonly FEATURES=(experimental_performance)
|
|
readonly UNLIMITED_MAX_HEAP="8g"
|
|
|
|
log() {
|
|
echo "[entrypoint] $*" >&2
|
|
}
|
|
|
|
fail() {
|
|
log "$*"
|
|
exit 1
|
|
}
|
|
|
|
# True for 1/true/yes, case-insensitive.
|
|
is_true() {
|
|
local value="${1,,}"
|
|
[[ "${value}" == "1" || "${value}" == "true" || "${value}" == "yes" ]]
|
|
}
|
|
|
|
# True for 0/false/no, case-insensitive.
|
|
is_false() {
|
|
local value="${1,,}"
|
|
[[ "${value}" == "0" || "${value}" == "false" || "${value}" == "no" ]]
|
|
}
|
|
|
|
# Same lookup as ServerWorldDatabase.create(), which prompts on stdin for a
|
|
# password when the admin account does not exist and no -adminpassword is given.
|
|
admin_account_exists() {
|
|
local username="${ADMIN_USERNAME:-admin}"
|
|
local escaped_username="${username//\'/\'\'}"
|
|
local account_id
|
|
|
|
if [[ ! -f "${SERVER_DB}" ]]; then
|
|
return 1
|
|
fi
|
|
account_id="$(sqlite3 -readonly "${SERVER_DB}" \
|
|
"SELECT id FROM whitelist WHERE username = '${escaped_username}';")"
|
|
[[ -n "${account_id}" ]]
|
|
}
|
|
|
|
# Sets <key>=<value> in the server .ini. Creates the file or the key if missing.
|
|
# Project Zomboid reads a partial .ini and writes the missing defaults on start.
|
|
set_ini_value() {
|
|
local key="$1"
|
|
local value="$2"
|
|
local temp_file="${SERVER_INI}.tmp"
|
|
local key_found=false
|
|
local line
|
|
|
|
mkdir --parents "$(dirname "${SERVER_INI}")"
|
|
: > "${temp_file}"
|
|
|
|
if [[ -f "${SERVER_INI}" ]]; then
|
|
while IFS= read -r line || [[ -n "${line}" ]]; do
|
|
if [[ "${line}" == "${key}="* ]]; then
|
|
printf '%s=%s\n' "${key}" "${value}" >> "${temp_file}"
|
|
key_found=true
|
|
else
|
|
printf '%s\n' "${line}" >> "${temp_file}"
|
|
fi
|
|
done < "${SERVER_INI}"
|
|
fi
|
|
|
|
if [[ "${key_found}" == false ]]; then
|
|
printf '%s=%s\n' "${key}" "${value}" >> "${temp_file}"
|
|
fi
|
|
|
|
mv "${temp_file}" "${SERVER_INI}"
|
|
}
|
|
|
|
# Sets <key>=<value> only when <value> is not empty.
|
|
set_ini_value_if_given() {
|
|
local key="$1"
|
|
local value="$2"
|
|
|
|
if [[ -n "${value}" ]]; then
|
|
set_ini_value "${key}" "${value}"
|
|
fi
|
|
}
|
|
|
|
apply_ini_settings() {
|
|
set_ini_value_if_given "Password" "${SERVER_PASSWORD}"
|
|
set_ini_value_if_given "PublicName" "${PUBLIC_NAME}"
|
|
set_ini_value_if_given "Mods" "${MODS}"
|
|
set_ini_value_if_given "WorkshopItems" "${WORKSHOP_ITEMS}"
|
|
|
|
if [[ -n "${PUBLIC}" ]]; then
|
|
if is_true "${PUBLIC}"; then
|
|
set_ini_value "Public" "true"
|
|
elif is_false "${PUBLIC}"; then
|
|
set_ini_value "Public" "false"
|
|
else
|
|
fail "PUBLIC must be true or false, got '${PUBLIC}'."
|
|
fi
|
|
fi
|
|
|
|
if [[ -n "${UPNP}" ]]; then
|
|
if is_true "${UPNP}"; then
|
|
set_ini_value "UPnP" "true"
|
|
elif is_false "${UPNP}"; then
|
|
set_ini_value "UPnP" "false"
|
|
else
|
|
fail "UPNP must be true or false, got '${UPNP}'."
|
|
fi
|
|
fi
|
|
|
|
if [[ -n "${RCON_PASSWORD}" ]]; then
|
|
set_ini_value "RCONPassword" "${RCON_PASSWORD}"
|
|
else
|
|
log "RCON_PASSWORD is not set; RCON stays disabled unless the .ini sets RCONPassword."
|
|
fi
|
|
}
|
|
|
|
# Preset names, without SandboxVars.lua (which only references a preset).
|
|
list_sandbox_presets() {
|
|
local file
|
|
local names=()
|
|
|
|
for file in "${SANDBOX_PRESET_DIR}"/*.lua; do
|
|
file="$(basename "${file}" .lua)"
|
|
if [[ "${file}" != "SandboxVars" ]]; then
|
|
names+=("${file}")
|
|
fi
|
|
done
|
|
echo "${names[*]}"
|
|
}
|
|
|
|
# Presets start with "return {" and use CRLF line endings; the server's
|
|
# <name>_SandboxVars.lua starts with "SandboxVars = {".
|
|
install_sandbox_preset() {
|
|
local preset_file="${SANDBOX_PRESET_DIR}/${SERVER_PRESET}.lua"
|
|
local first_line=true
|
|
local line
|
|
|
|
if [[ -z "${SERVER_PRESET}" || -f "${SERVER_SANDBOX_VARS}" ]]; then
|
|
return
|
|
fi
|
|
if [[ "${SERVER_PRESET}" == "SandboxVars" || ! -f "${preset_file}" ]]; then
|
|
fail "Unknown SERVER_PRESET '${SERVER_PRESET}'. Available: $(list_sandbox_presets)"
|
|
fi
|
|
|
|
log "Creating ${SERVER_SANDBOX_VARS} from preset ${SERVER_PRESET}"
|
|
mkdir --parents "$(dirname "${SERVER_SANDBOX_VARS}")"
|
|
while IFS= read -r line || [[ -n "${line}" ]]; do
|
|
line="${line%$'\r'}"
|
|
if [[ "${first_line}" == true ]]; then
|
|
if [[ "${line}" != "return {" ]]; then
|
|
fail "Unexpected first line in ${preset_file}: '${line}'"
|
|
fi
|
|
line="SandboxVars = {"
|
|
first_line=false
|
|
fi
|
|
printf '%s\n' "${line}"
|
|
done < "${preset_file}" > "${SERVER_SANDBOX_VARS}"
|
|
}
|
|
|
|
# Sets needs_admin_password, used by build_server_args.
|
|
check_admin_account() {
|
|
if admin_account_exists; then
|
|
needs_admin_password=false
|
|
if [[ -n "${ADMIN_PASSWORD}" ]]; then
|
|
log "ADMIN_PASSWORD is ignored: the admin account '${ADMIN_USERNAME:-admin}' already exists."
|
|
fi
|
|
else
|
|
needs_admin_password=true
|
|
if [[ -z "${ADMIN_PASSWORD}" ]]; then
|
|
fail "The admin account '${ADMIN_USERNAME:-admin}' of '${SERVER_NAME}' does not exist yet: ADMIN_PASSWORD is required to create it."
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# 75 % of the cgroup v2 memory limit in MiB; UNLIMITED_MAX_HEAP without a limit.
|
|
# The rest of the limit is left for native memory (JVM, native libraries, thread stacks).
|
|
default_max_heap() {
|
|
local limit
|
|
|
|
if [[ ! -r "${CGROUP_MEMORY_MAX}" ]]; then
|
|
echo "${UNLIMITED_MAX_HEAP}"
|
|
return
|
|
fi
|
|
limit="$(< "${CGROUP_MEMORY_MAX}")"
|
|
if [[ "${limit}" == "max" ]]; then
|
|
echo "${UNLIMITED_MAX_HEAP}"
|
|
return
|
|
fi
|
|
echo "$(( limit * 3 / 4 / 1024 / 1024 ))m"
|
|
}
|
|
|
|
# Appends -Dzomboidforge.features.<key>=true|false for every feature whose ZF_<KEY> is set.
|
|
# Unset or empty: no property, so forge.toml or the default (off) decides.
|
|
add_feature_properties() {
|
|
local key
|
|
local variable
|
|
local value
|
|
|
|
for key in "${FEATURES[@]}"; do
|
|
variable="ZF_${key^^}"
|
|
value="${!variable:-}"
|
|
|
|
if [[ -z "${value}" ]]; then
|
|
continue
|
|
fi
|
|
if is_true "${value}"; then
|
|
jvm_args+=("-Dzomboidforge.features.${key}=true")
|
|
elif is_false "${value}"; then
|
|
jvm_args+=("-Dzomboidforge.features.${key}=false")
|
|
else
|
|
fail "${variable} must be true or false, got '${value}'."
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Same JVM setup as the vanilla ProjectZomboid64.json and start-server.sh.
|
|
# Java runs directly instead of through the native ProjectZomboid64 launcher.
|
|
build_jvm_args() {
|
|
local max_heap="${JVM_MAX_HEAP}"
|
|
|
|
if [[ -z "${max_heap}" ]]; then
|
|
max_heap="$(default_max_heap)"
|
|
log "JVM_MAX_HEAP is not set: using -Xmx${max_heap}."
|
|
fi
|
|
if [[ ! "${JVM_UNCOMMIT_DELAY}" =~ ^[0-9]+$ ]]; then
|
|
fail "JVM_UNCOMMIT_DELAY must be a number of seconds, got '${JVM_UNCOMMIT_DELAY}'."
|
|
fi
|
|
|
|
jvm_args=(
|
|
"-javaagent:${AGENT_JAR}"
|
|
"-Dzomboidforge.dir=${FORGE_DIR}"
|
|
"-Djava.awt.headless=true"
|
|
"--enable-native-access=ALL-UNNAMED"
|
|
"-Xmx${max_heap}"
|
|
"-XX:+UseZGC"
|
|
"-XX:ZUncommitDelay=${JVM_UNCOMMIT_DELAY}"
|
|
"-XX:-OmitStackTraceInFastThrow"
|
|
"-Dzomboid.steam=${STEAM}"
|
|
"-Dzomboid.znetlog=1"
|
|
"-Djava.library.path=linux64/"
|
|
"-Djava.security.egd=file:/dev/urandom"
|
|
"-cp" "java/:java/projectzomboid.jar"
|
|
)
|
|
|
|
if [[ -n "${JVM_MIN_HEAP}" ]]; then
|
|
jvm_args+=("-Xms${JVM_MIN_HEAP}")
|
|
fi
|
|
if [[ -n "${SOFT_RESET}" ]] && is_true "${SOFT_RESET}"; then
|
|
log "SOFT_RESET is set: starting in soft reset mode. Unset it for the next start."
|
|
jvm_args+=("-Dsoftreset")
|
|
fi
|
|
add_feature_properties
|
|
}
|
|
|
|
build_server_args() {
|
|
server_args=(
|
|
"-cachedir=${DATA_DIR}"
|
|
"-servername" "${SERVER_NAME}"
|
|
)
|
|
|
|
if [[ -n "${PORT}" ]]; then
|
|
server_args+=("-port" "${PORT}")
|
|
fi
|
|
if [[ -n "${UDP_PORT}" ]]; then
|
|
server_args+=("-udpport" "${UDP_PORT}")
|
|
fi
|
|
|
|
if [[ -n "${ADMIN_USERNAME}" ]]; then
|
|
server_args+=("-adminusername" "${ADMIN_USERNAME}")
|
|
fi
|
|
# Only while the account does not exist: the value is visible in the process arguments.
|
|
if [[ "${needs_admin_password}" == true ]]; then
|
|
server_args+=("-adminpassword" "${ADMIN_PASSWORD}")
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
mkdir --parents "${STEAMAPPS_DIR}"
|
|
|
|
local needs_admin_password
|
|
check_admin_account
|
|
apply_ini_settings
|
|
install_sandbox_preset
|
|
|
|
local jvm_args
|
|
local server_args
|
|
build_jvm_args
|
|
build_server_args
|
|
|
|
# Native libraries: game (linux64/), Steam (install root), JVM (jre64/lib).
|
|
# libjsig.so chains signal handlers between the JVM and native libraries.
|
|
export LD_LIBRARY_PATH="${INSTALL_DIR}/linux64:${INSTALL_DIR}:${INSTALL_DIR}/jre64/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
|
|
export LD_PRELOAD="libjsig.so${LD_PRELOAD:+:${LD_PRELOAD}}"
|
|
|
|
cd "${INSTALL_DIR}"
|
|
exec "${JAVA}" "${jvm_args[@]}" zombie.network.GameServer "${server_args[@]}" "$@"
|
|
}
|
|
|
|
main "$@"
|