Skip to content

Commit

Permalink
Various Fixes for Podman/ Docker (#113)
Browse files Browse the repository at this point in the history
* Update to Fix Permissions and XHost

* Partial Commit of Refactor and Fix

* Making back to a daemon

* Test under podman

* Small Update

* Fix test Build script

* Fix Passing Arguments

* Fix spelling of container

* Fix Build and Highlight Script update

* Update to Test Image for Docker

* Fix Docker initial build.

* More Docker Fix.

* Some More Updates

* Auto start Zwift when Testing.

* Fix for permissions issue on existing file.

* Fix Minor issues.

* Fix for Dcoker and ZWIFT_UID

* replace back to using -d

* More Accurate XWayland

* Another Small change

* Fix issue with Audio on Docker

* Merge Build for Podman and Docker

* Remove Save Folder to fix Podman Permissions

* Xhost no loner needed

* Fix update and ZWIFT_UID

* Change how xhost is invocked
  • Loading branch information
sHedC authored May 12, 2024
1 parent 0f06074 commit f679ae7
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 96 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ RUN adduser --disabled-password --gecos '' user && \

RUN wget https://dl.winehq.org/wine/wine-mono/${WINE_MONO_VERSION}/wine-mono-${WINE_MONO_VERSION}-x86.msi \
-P /home/user/.cache/wine
RUN chown -R user:user /home/user/.cache

FROM wine-base

Expand Down Expand Up @@ -91,3 +92,4 @@ COPY --from=build-runfromprocess /usr/src/target/x86_64-pc-windows-gnu/release/r
RUN chmod +x /bin/runfromprocess-rs.exe

ENTRYPOINT ["entrypoint"]
CMD [$@]
87 changes: 70 additions & 17 deletions bin/build-image.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
#!/usr/bin/env bash
set -x
set -e


SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
ZWIFT_UID=$(id -u)
ZWIFT_GID=$(id -g)

# Use podman if available
if [[ ! $CONTAINER_TOOL ]]
then
if [[ -x "$(command -v podman)" ]]
then
CONTAINER_TOOL=podman
else
CONTAINER_TOOL=docker
fi
fi

# Update information based on Container Tool
if [[ "$CONTAINER_TOOL" == "podman" ]]; then
BUILD_NAME="zwift"
IMAGE="localhost/zwift"
else
BUILD_NAME="netbrain/zwift"
IMAGE="netbrain/zwift"
fi

GENERAL_FLAGS=(
-it
--privileged
--network bridge
--name zwift
--security-opt label=disable

-e DISPLAY=$DISPLAY
-e XAUTHORITY=$XAUTHORITY

-v /tmp/.X11-unix:/tmp/.X11-unix
-v /run/user/$UID:/run/user/$ZWIFT_UID
)


# Check for proprietary nvidia driver and set correct device to use
if [[ -f "/proc/driver/nvidia/version" ]]
Expand All @@ -10,20 +49,34 @@ else
VGA_DEVICE_FLAG="--device /dev/dri:/dev/dri"
fi

docker build -t netbrain/zwift .
docker run -it \
--name zwift \
--privileged \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
# Initiate podman Volume with correct permissions
if [[ "$CONTAINER_TOOL" == "podman" ]]
then
PODMAN_FLAGS=(
--userns keep-id:uid=$ZWIFT_UID,gid=$ZWIFT_GID
)
fi

# Cleanup on error
trap cleanup ERR
cleanup()
{
$CONTAINER_TOOL container rm zwift
exit
}


$CONTAINER_TOOL build --force-rm -t $BUILD_NAME $SCRIPT_DIR/../.
$CONTAINER_TOOL run ${GENERAL_FLAGS[@]} \
$VGA_DEVICE_FLAG \
netbrain/zwift:latest

BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
VERSION=$(curl -s http://cdn.zwift.com/gameassets/Zwift_Updates_Root/Zwift_ver_cur.xml | grep -oP 'sversion="\K.*?(?=")' | cut -f 1 -d ' ')
docker commit --change="LABEL org.opencontainers.image.created=$BUILD_DATE" \
--change="LABEL org.opencontainers.image.version=$VERSION" \
--change='CMD [""]' \
-m "updated to version $VERSION" \
zwift \
netbrain/zwift:$VERSION
${PODMAN_FLAGS[@]} \
$IMAGE:latest \
$@


$CONTAINER_TOOL commit zwift $BUILD_NAME:latest
$CONTAINER_TOOL container rm zwift

export IMAGE=$IMAGE
export DONT_PULL=1
$SCRIPT_DIR/../zwift.sh
94 changes: 64 additions & 30 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,75 @@
set -e
set -x

# This script runs as the root user in Docker so need to do this to find the
# home directory of the "user" user.
ZWIFT_USER_HOME=$( getent passwd "user" | cut -d: -f6 )
ZWIFT_HOME="$ZWIFT_USER_HOME/.wine/drive_c/Program Files (x86)/Zwift"
# Check whetehr we are running in Docker/ Podman
# Docker has the file /.dockerenv
# Podman exposes itself in /run/.containerenv
if [[ -f "/.dockerenv" ]]; then
CONTAINER="docker"
elif [[ ! -z $(cat /run/.containerenv | grep "podman") ]]; then
CONTAINER="podman"
else
echo "Unknown Container."
exit 1
fi

cd "$ZWIFT_HOME"
# Check what container we are in:
if [[ "$CONTAINER" == "docker" ]]; then
# This script runs as the root user in Docker so need to do this to find the
# home directory of the "user" user.
ZWIFT_USER_HOME=$( getent passwd "user" | cut -d: -f6 )
ZWIFT_HOME="$ZWIFT_USER_HOME/.wine/drive_c/Program Files (x86)/Zwift"

USER_UID=`id -u user`
USER_GID=`id -g user`
mkdir -p "$ZWIFT_HOME"
cd "$ZWIFT_HOME"

# Test that it exists and is a number
if [ -n "$ZWIFT_UID" ] && [ "$ZWIFT_UID" -eq "$ZWIFT_UID" ]; then
USER_UID=$ZWIFT_UID
else
echo "ZWIFT_UID is not set or not a number: '$ZWIFT_UID'"
fi
if [ -n "$ZWIFT_GID" ] && [ "$ZWIFT_GID" -eq "$ZWIFT_GID" ]; then
USER_GID=$ZWIFT_GID
else
echo "ZWIFT_GID is not set or not a number: ''$ZWIFT_GID'"
fi
USER_UID=`id -u user`
USER_GID=`id -g user`

# The next two should be no-ops if ZWIFT_UID/GID are not set but no harm
# running them anyway.
usermod -o -u ${USER_UID} user
groupmod -o -g ${USER_GID} user
chown -R ${USER_UID}:${USER_GID} /home/user
# Test that it exists and is a number
if [ -n "$ZWIFT_UID" ] && [ "$ZWIFT_UID" -eq "$ZWIFT_UID" ]; then
USER_UID=$ZWIFT_UID
else
echo "ZWIFT_UID is not set or not a number: '$ZWIFT_UID'"
fi
if [ -n "$ZWIFT_GID" ] && [ "$ZWIFT_GID" -eq "$ZWIFT_GID" ]; then
USER_GID=$ZWIFT_GID
else
echo "ZWIFT_GID is not set or not a number: ''$ZWIFT_GID'"
fi

mkdir -p /run/user/${USER_UID}
chown -R user:user /run/user/${USER_UID}
sed -i "s/1000/${USER_UID}/g" /etc/pulse/client.conf
# The next two should be no-ops if ZWIFT_UID/GID are not set but no harm
# running them anyway.
usermod -o -u ${USER_UID} user
groupmod -o -g ${USER_GID} user
chown -R ${USER_UID}:${USER_GID} /home/user

# Run update if that's the first argument or if zwift directory is empty
if [ "$1" = "update" ] || [ ! "$(ls -A .)" ] ; then
gosu user:user /bin/update_zwift.sh "$@"
# Only make the directory if not there.
if [ ! -d "/run/user/${USER_ID}" ]; then
mkdir -p /run/user/${USER_UID}
fi

# Only alter pulse if the user id changes.
if [ $ZWIFT_UID -ne 1000 ]; then
chown -R user:user /run/user/${USER_UID}
sed -i "s/1000/${USER_UID}/g" /etc/pulse/client.conf
fi

# Run update if that's the first argument or if zwift directory is empty
if [ "$1" = "update" ] || [ ! "$(ls -A .)" ] ; then
gosu user:user /bin/update_zwift.sh "$@"
else
gosu user:user /bin/run_zwift.sh "$@"
fi
else
gosu user:user /bin/run_zwift.sh "$@"
# We are running in podman.
ZWIFT_HOME="/home/user/.wine/drive_c/Program Files (x86)/Zwift"
mkdir -p "$ZWIFT_HOME"
cd "$ZWIFT_HOME"

if [ "$1" = "update" ] || [ ! "$(ls -A .)" ] ; then
/bin/update_zwift.sh "$@"
else
/bin/run_zwift.sh "$@"
fi
fi
1 change: 1 addition & 0 deletions run_zwift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if [[ ! -z "$WINE_EXPERIMENTAL_WAYLAND" ]];
then
echo "enabling wayland support in wine 9.0"
wine reg.exe add HKCU\\Software\\Wine\\Drivers /v Graphics /d x11,wayland
DISPLAY=
fi

echo "starting zwift..."
Expand Down
20 changes: 12 additions & 8 deletions update_zwift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,10 @@ function wait_for_zwift_game_update() {

echo "updating done, waiting 5 seconds..."
sleep 5
}

if [ "$1" = "update" ]
then
wait_for_zwift_game_update

wineserver -k
exit 0
fi
# Remove as causes PODMAN Save Permisison issues.
rm -rf "$HOME/.wine/drive_c/users/user/Documents/Zwift"
}

if [ ! "$(ls -A .)" ] # is directory empty?
then
Expand Down Expand Up @@ -87,3 +82,12 @@ then
rm -rf "$HOME/.cache/wine*"
exit 0
fi

if [ "$1" = "update" ]
then
wait_for_zwift_game_update

wineserver -k
exit 0
fi

Loading

0 comments on commit f679ae7

Please sign in to comment.