Repair prior state on redeploy instead of tripping over it

A redeploy onto a host where earlier runs failed partway had no way to recover:
the obsolete minecraft.socket stayed, a hand-made `systemctl edit` drop-in kept
overriding ExecStart, units sat in a failed state, and a stale FIFO lingered.

mc-service-setup.sh now clears that up front: stops both services, removes the
socket unit, moves any minecraft.service.d / packwiz-http.service.d drop-in
aside with a timestamp (moved, not deleted, in case one was deliberate), removes
stale FIFOs at both the old and current paths, resets failed state and reloads.

It also gives the pack and the generated guides back to the admin user, undoing
the blanket chown an earlier version applied on every run.

START_SERVER now defaults to yes, so unpacking the zip and running install.sh
ends with a running server.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
main
Amir Alexander Abdelbaki 2026-08-06 00:42:42 +02:00
parent c36b54c7d3
commit c4074e8dbc
2 changed files with 41 additions and 10 deletions

View File

@ -30,5 +30,5 @@ XMS="10G"
ACCEPT_EULA="no" ACCEPT_EULA="no"
# --- optional --------------------------------------------------------------- # --- optional ---------------------------------------------------------------
START_SERVER="no" # yes to start Minecraft at the end START_SERVER="yes" # no to leave Minecraft stopped
CAP_ZFS_ARC="2147483648" # bytes; empty to leave ARC alone CAP_ZFS_ARC="2147483648" # bytes; empty to leave ARC alone

View File

@ -62,6 +62,39 @@ done
|| die "-H wants a port between 1 and 65535, got: $HTTP_PORT" || die "-H wants a port between 1 and 65535, got: $HTTP_PORT"
[ "$HTTP_PORT" != "$PORT" ] || die "the HTTP port and the Minecraft port cannot both be $PORT" [ "$HTTP_PORT" != "$PORT" ] || die "the HTTP port and the Minecraft port cannot both be $PORT"
# --------------------------------------------------------------- cleanup ----
# Re-running has to repair whatever an earlier, partly-applied run left behind,
# not trip over it. Everything here is safe when there is nothing to clean.
info "clearing previous service state"
systemctl stop minecraft.service packwiz-http.service >/dev/null 2>&1 || true
# The old socket unit could never work: SELinux denies init_t read/write on a
# var_run_t fifo_file, so systemd could not open the FIFO.
if [ -e "$UNIT_DIR/minecraft.socket" ]; then
info "removing obsolete minecraft.socket"
systemctl disable --now minecraft.socket >/dev/null 2>&1 || true
rm -f "$UNIT_DIR/minecraft.socket"
fi
# Drop-ins override the unit we are about to write and win silently — including
# any hand-made `systemctl edit` workaround. Move them aside rather than delete,
# in case one was deliberate.
for d in "$UNIT_DIR/minecraft.service.d" "$UNIT_DIR/packwiz-http.service.d"; do
if [ -d "$d" ]; then
bak="$d.bak.$(date +%Y%m%d%H%M%S)"
warn "moving override drop-in aside: $d -> $bak"
mv "$d" "$bak"
fi
done
# FIFO from the old socket-unit layout, and any left by a killed run.
rm -f /run/minecraft-console /run/minecraft/console
systemctl reset-failed minecraft.service minecraft.socket packwiz-http.service >/dev/null 2>&1 || true
systemctl daemon-reload
# ----------------------------------------------------------------- share ---- # ----------------------------------------------------------------- share ----
[ -d "$SHARE" ] || die "$SHARE does not exist" [ -d "$SHARE" ] || die "$SHARE does not exist"
@ -110,6 +143,13 @@ if [ -n "$ADMIN" ]; then
chown "$ADMIN":"$MCUSER" "$SHARE" chown "$ADMIN":"$MCUSER" "$SHARE"
chmod 2775 "$SHARE" chmod 2775 "$SHARE"
install -d -o "$ADMIN" -g "$MCUSER" -m 2775 "$SHARE/packs" install -d -o "$ADMIN" -g "$MCUSER" -m 2775 "$SHARE/packs"
# An earlier version chowned the whole share on every run, which took the
# pack and the generated guides away from the author. Give them back.
chown -R "$ADMIN":"$MCUSER" "$SHARE/packs" 2>/dev/null || true
for f in "$SHARE"/setup-*-packwiz.html; do
[ -e "$f" ] && chown "$ADMIN":"$MCUSER" "$f" 2>/dev/null || true
done
warn "$ADMIN's new group membership needs a fresh login to take effect in warn "$ADMIN's new group membership needs a fresh login to take effect in
existing shells — 'newgrp $MCUSER' works for the current one." existing shells — 'newgrp $MCUSER' works for the current one."
fi fi
@ -287,15 +327,6 @@ fi
# ----------------------------------------------------------------- units ---- # ----------------------------------------------------------------- units ----
# A previous version shipped a minecraft.socket that had systemd itself open
# the FIFO. SELinux denies init_t read/write on a var_run_t fifo_file, so that
# never worked; remove it if it is still around.
if [ -e "$UNIT_DIR/minecraft.socket" ]; then
info "removing obsolete minecraft.socket"
systemctl disable --now minecraft.socket >/dev/null 2>&1 || true
rm -f "$UNIT_DIR/minecraft.socket"
fi
info "writing $UNIT_DIR/minecraft.service" info "writing $UNIT_DIR/minecraft.service"
cat > "$UNIT_DIR/minecraft.service" <<EOF cat > "$UNIT_DIR/minecraft.service" <<EOF
[Unit] [Unit]