lloesche / valheim-server-docker

Valheim dedicated gameserver with automatic update, World backup, BepInEx and ValheimPlus mod support
https://hub.docker.com/r/lloesche/valheim-server
Apache License 2.0
1.92k stars 269 forks source link

crontab entries broken when uid and gid are defined in valheim.env #529

Closed pixel8ed closed 1 year ago

pixel8ed commented 1 year ago

The bootstrap script fails to create crontab entries due to missing permissions if you set the UID and GID for the valheim user.

valheim.env parameters defined:

PUID=1000
GUID=1000

Error example:

valheim@a950e2c141e2:~$ crontab -l
crontabs/valheim/: fopen: Permission denied

Temporary fix: in valheim.env:

PRE_SUPERVISOR_HOOK=/usr/sbin/usermod -a -G crontab valheim

Fix example:

valheim@5e7699d3b6cb:~$ crontab -l
0 * * * * [ -f "/var/run/valheim/valheim-backup.pid" ] && kill -HUP $(cat /var/run/valheim/valheim-backup.pid)
*/15 * * * * [ -f "/var/run/valheim/valheim-updater.pid" ] && kill -HUP $(cat /var/run/valheim/valheim-updater.pid)
0 5 * * * /usr/local/bin/valheim-is-idle && /usr/local/bin/supervisorctl restart valheim-server
lloesche commented 1 year ago

I'm trying to reproduce the issue but so far can't. Maybe you can see where I'm going wrong:

I run a test server using

[lukas@blackhole ~]$ docker run --rm -it -e PUID=1000 -e PGID=1000 lloesche/valheim-server
INFO - Setting uid:gid of valheim to 1000:1000
INFO - Setting timezone Etc/UTC
INFO - Setting up syslogd - logging to stdout
2022-12-22 02:27:22,510 INFO Included extra file "/usr/local/etc/supervisor/conf.d/syslog.conf" during parsing
2022-12-22 02:27:22,510 INFO Set uid to user 0 succeeded
2022-12-22 02:27:22,512 INFO RPC interface 'supervisor' initialized
2022-12-22 02:27:22,512 INFO supervisord started with pid 1
2022-12-22 02:27:23,513 INFO spawned: 'syslogd' with pid 24
2022-12-22 02:27:23,514 INFO spawned: 'crond' with pid 25
2022-12-22 02:27:23,515 INFO spawned: 'valheim-bootstrap' with pid 26
Dec 22 02:27:23 syslogd started: BusyBox v1.34.1
2022-12-22 02:27:23,515 INFO success: valheim-bootstrap entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
Dec 22 02:27:23 cron[25]: (CRON) INFO (pidfile fd = 3)
Dec 22 02:27:23 cron[25]: (CRON) INFO (Running @reboot jobs)
Dec 22 02:27:23 supervisord: valheim-bootstrap DEBUG - [26] - Running commit 5bc8d5d84bf2436027bb639aa15d4d46d94e1895
Dec 22 02:27:23 supervisord: valheim-bootstrap DEBUG - [26] - Creating cron to do world backups using schedule 0 * * * *
Dec 22 02:27:23 supervisord: valheim-bootstrap DEBUG - [26] - Creating cron to check for updates using schedule */15 * * * *
Dec 22 02:27:23 supervisord: valheim-bootstrap DEBUG - [26] - Creating cron to restart valheim-server using schedule 0 5 * * *
Dec 22 02:27:23 crontab[33]: (valheim) REPLACE (valheim)

Then when I enter the container:

[lukas@blackhole ~]$ docker exec -it 3e670d55da8e bash
root@d12f51cced38:/# ps axu
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.2  0.0  32572 24832 pts/0    Ss+  02:27   0:00 /usr/bin/python3 /usr/local/bin/supervisord -c /usr/local/etc/supervisord.conf
root          24  0.0  0.0   4768   908 pts/0    S    02:27   0:00 /usr/local/sbin/syslogd -n -S -t -O -
root          25  0.0  0.0   7088  2960 pts/0    S    02:27   0:00 /usr/sbin/cron -f
valheim       36  0.0  0.0   7768  4284 pts/0    S    02:27   0:00 /bin/bash /usr/local/bin/valheim-updater
valheim       71  0.0  0.0   7636  4036 pts/0    S    02:27   0:00 /bin/bash /usr/local/bin/valheim-backup
valheim      171  0.0  0.0   7636  3908 pts/0    S    02:27   0:00 /bin/bash /usr/local/bin/valheim-server
valheim      175  111  3.3 8408408 2211428 pts/0 Rl   02:27   0:38 /opt/valheim/server/valheim_server.x86_64 -nographics -batchmode -name My Server -por
valheim      176  0.0  0.0   7636  2004 pts/0    S    02:27   0:00 /bin/bash /usr/local/bin/valheim-server
valheim      179  0.0  0.0 1076388 4052 pts/0    Sl   02:27   0:00 /usr/local/bin/valheim-logfilter -logtostderr -v 2
valheim      259  0.0  0.0   5736   564 pts/0    S    02:28   0:00 sleep 9
root         260  0.1  0.0   4164  3332 pts/1    Ss   02:28   0:00 bash
valheim      268  0.0  0.0   5736   508 pts/0    S    02:28   0:00 sleep 20
root         269  0.0  0.0   6760  2940 pts/1    R+   02:28   0:00 ps axu
root@d12f51cced38:/# su - valheim
valheim@d12f51cced38:~$ crontab -l
0 * * * * [ -f "/var/run/valheim/valheim-backup.pid" ] && kill -HUP $(cat /var/run/valheim/valheim-backup.pid)
*/15 * * * * [ -f "/var/run/valheim/valheim-updater.pid" ] && kill -HUP $(cat /var/run/valheim/valheim-updater.pid)
0 5 * * * /usr/local/bin/valheim-is-idle && /usr/local/bin/supervisorctl restart valheim-server
valheim@d12f51cced38:~$
logout
root@d12f51cced38:/# ls -al /var/spool/cron/crontabs/
total 4
drwx-wx--T 1 root    crontab  21 Dec 22 02:27 .
drwxr-xr-x 1 root    root     22 Dec 22 02:01 ..
-rw------- 1 valheim crontab 516 Dec 22 02:27 valheim

This all seems fine to me. Are there any other permissions you're using? I have no issue adding the usermod -a -G crontab valheim to the Dockerfile. Seems like an easy fix. I'd just like to confirm it's actually needed.

pixel8ed commented 1 year ago

That's super weird. Here's my docker-compose - the only thing I added was no-new-privileges:

  valheim:
    container_name: valheim
    image: ghcr.io/lloesche/valheim-server
    cap_add:
      - sys_nice
    security_opt:
      - no-new-privileges:true
    volumes:
      - $DOCKERDIR/valheim-server/config:/config
      - $DOCKERDIR/valheim-server/data:/opt/valheim
    ports:
      - "2456-2457:2456-2457/udp"
      - "9002:9002/tcp"
    env_file:
      - $DOCKERDIR/valheim-server/valheim.env
    restart: always
    stop_grace_period: 2m

My valheim.env has nothing remarkable, apart from the PRE_SUPERVISOR_HOOK addition and the PUID & GUID