Skip to content

Commit 6b52ec6

Browse files
committed
Fix user switch edge-case (thanks @putnam)
1 parent b1c0fff commit 6b52ec6

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

init.sh

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
set -e
44

5+
CURRENTUID=$(id -u)
6+
NUMCHECK='^[0-9]+$'
7+
RAMAVAILABLE=$(awk '/MemAvailable/ {printf( "%d\n", $2 / 1024000 )}' /proc/meminfo)
8+
USER="steam"
9+
510
if [[ "$DEBUG" == "true" ]]; then
611
printf "Debugging enabled (the container will exit after printing the debug info)\\n\\nPrinting environment variables:\\n"
712
export
@@ -18,17 +23,14 @@ HDD: $(df -h | awk '$NF=="/"{printf "%dGB/%dGB (%s used)\n", $3,$2,$5}')"
1823
exit 1
1924
fi
2025

21-
CURRENTUID=$(id -u)
2226
if [[ "$CURRENTUID" -ne "0" ]]; then
2327
printf "Current user is not root (%s)\\nPass your user and group to the container using the PGID and PUID environment variables\\nDo not use the --user flag (or user: field in Docker Compose)\\n" "$CURRENTUID"
2428
exit 1
2529
fi
2630

27-
ramAvailable=$(awk '/MemAvailable/ {printf( "%d\n", $2 / 1024000 )}' /proc/meminfo)
28-
printf "Checking available memory...%sGB detected\\n" "$ramAvailable"
29-
30-
if [[ "$ramAvailable" -lt 12 ]]; then
31-
printf "You have less than the required 12GB minmum (%sGB detected) of available RAM to run the game server.\\nIt is likely that the server will fail to load properly.\\n" "$ramAvailable"
31+
printf "Checking available memory...%sGB detected\\n" "$RAMAVAILABLE"
32+
if [[ "$RAMAVAILABLE" -lt 12 ]]; then
33+
printf "You have less than the required 12GB minmum (%sGB detected) of available RAM to run the game server.\\nIt is likely that the server will fail to load properly.\\n" "$RAMAVAILABLE"
3234
fi
3335

3436
mkdir -p \
@@ -41,8 +43,6 @@ mkdir -p \
4143
"${GAMESAVESDIR}/server" \
4244
|| exit 1
4345

44-
NUMCHECK='^[0-9]+$'
45-
4646
# check if the user and group IDs have been set
4747
if ! [[ "$PGID" =~ $NUMCHECK ]] ; then
4848
printf "Invalid group id given: %s\\n" "$PGID"
@@ -60,7 +60,17 @@ elif [[ "$PUID" -eq 0 ]]; then
6060
exit 1
6161
fi
6262

63-
groupmod -g "$PGID" steam
64-
usermod -u "$PUID" steam
63+
if [[ $(getent group $PGID | cut -d: -f1) ]]; then
64+
usermod -a -G "$PGID" steam
65+
else
66+
groupmod -g "$PGID" steam
67+
fi
68+
69+
if [[ $(getent passwd ${PUID} | cut -d: -f1) ]]; then
70+
USER=$(getent passwd $PUID | cut -d: -f1)
71+
else
72+
usermod -u "$PUID" steam
73+
fi
74+
6575
chown -R "$PUID":"$PGID" /config /home/steam
66-
exec gosu steam "/home/steam/run.sh" "$@"
76+
exec gosu "$USER" "/home/steam/run.sh" "$@"

0 commit comments

Comments
 (0)