Fixed UID/GID startup logic to properly handle running as root

Fixes #425
This commit is contained in:
Geoff Bourne 2020-01-17 08:27:11 -06:00
parent 949faf1620
commit e94f6608d2

28
start
View File

@ -4,23 +4,41 @@ umask 0002
chmod g+w /data chmod g+w /data
if [ $(id -u) = 0 ]; then if [ $(id -u) = 0 ]; then
if [[ -v UID && $UID != $(id -u) ]]; then runAsUser=minecraft
usermod -u $UID minecraft runAsGroup=minecraft
if [[ -v UID ]]; then
if [[ $UID != 0 ]]; then
if [[ $UID != $(id -u minecraft) ]]; then
echo "Changing uid of minecraft to $UID"
usermod -u $UID minecraft
fi
else
runAsUser=root
fi
fi fi
if [[ -v GID ]]; then if [[ -v GID ]]; then
groupmod -o -g $GID minecraft if [[ $GID != 0 ]]; then
if [[ $GID != $(id -g minecraft) ]]; then
echo "Changing gid of minecraft to $GID"
groupmod -o -g $GID minecraft
fi
else
runAsGroup=root
fi
fi fi
if [[ $(stat -c "%u" /data) != $UID ]]; then if [[ $(stat -c "%u" /data) != $UID ]]; then
echo "Changing ownership of /data to $UID ..." echo "Changing ownership of /data to $UID ..."
chown -R minecraft:minecraft /data chown -R ${runAsUser}:${runAsGroup} /data
fi fi
if [[ ${SKIP_NSSWITCH_CONF^^} != TRUE ]]; then if [[ ${SKIP_NSSWITCH_CONF^^} != TRUE ]]; then
echo 'hosts: files dns' > /etc/nsswitch.conf echo 'hosts: files dns' > /etc/nsswitch.conf
fi fi
exec su-exec minecraft:minecraft /start-configuration $@ exec su-exec ${runAsUser}:${runAsGroup} /start-configuration $@
else else
exec /start-configuration $@ exec /start-configuration $@
fi fi