File krypton-docker.sh of Package openSUSE-Krypton-docker

#!/bin/bash
# krypton-docker.sh - script to start krypton containers with display forwarding
# Copyright (C) 2018 Fabian Vogt <fabian@ritter-vogt.de>
#
# This file is part of the Krypton Docker container
#
# krypton-docker.sh is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# krypton-docker.sh is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with krypton-docker.sh.  If not, see <http://www.gnu.org/licenses/>.

# This is a wrapper around docker to start Krypton containers with access to the
# X11 display and if available also the wayland socket.

version="0.1"
default_image="registry.opensuse.org/kde/medias/images/krypton/plasma"

# User options
image="${default_image}"
docker_options=()
docker_command=()
operation="run"

show_help() {
	cat <<EOF
krypton-docker $version
Usage:
	$0 pull [options] [-- docker options]
	$0 [run] [options] [-- [docker options] [-- command]]

Available options:
	--image <image>
		Use the specified <image> instead of the default:
		$image
	--version
		Print "krypton-docker $version" and exit.
	--help
		Display this helpful message and exit.

Examples:
	Start an interactive bash in a new container:
	\$ $0 -- -it

	Update the local copy of the image:
	\$ $0 pull

	Start plasma in a windowed kwin session:
	\$ $0 -- --rm -- dbus-run-session startplasmacompositor

	Install konsole in a new container and start konsole with wayland
	with your home as working directory:
	\$ $0 -- --name=install-konsole -it -- sudo zypper in konsole
        \$ docker commit install-konsole krypton-konsole # Save as new image
	\$ $0 --image krypton_konsole -- -v $HOME:/mnt -w /mnt -- konsole -platform wayland

krypton-docker is a wrapper around "docker run", providing access to the
available X11 and Wayland connections into the created container.

For commands other than "run", docker can be invoked directly.
EOF
}

parse_options() {
	while [ $# -ne 0 ]; do
		local arg="$1"
		shift

		case $arg in
		--image)
			image=$1
			shift
		;;
		--version)
			echo "krypton-docker $version"
			exit 0
		;;
		--help)
			show_help
			exit 0
		;;
		--)
			while [ $# -ne 0 ] && [ $1 != "--" ]; do
				docker_options+=("$1")
				shift
			done
			[ "$1" = "--" ] && shift
			while [ $# -ne 0 ]; do
				docker_command+=("$1")
				shift
			done
			break
		;;
		--*)
			echo "Unknown option '$arg'"
			exit 1
		;;
		run|pull)
			operation="$arg"
		;;
		*)
			echo "Unknown command '$arg'"
			exit 1
		;;
		esac
	done
}

parse_options "$@"

if ! which docker &>/dev/null; then
	echo "Docker is not installed."
	exit 1
fi

if [ "$operation" = "pull" ]; then
	exec docker pull "${docker_options[@]}" "$image"
fi

OLDIFS="$IFS"
IFS=. docker_version=($(docker --version | sed 's/^.* \(.*\)-.*$/\1/g'))
IFS="$OLDIFS"

if [ "${docker_version[0]}" -lt 18 -o \( "${docker_version[0]}" -eq 18 -a "${docker_version[1]}" -lt 4 \) ]; then
	echo "Warning: Your docker is older than 18.04 and needs '--privileged' to support Qt programs."
	OPTS+=("--privileged")
fi

OPTS_XFORWARD=()
if [ -n "$DISPLAY" ]; then
	OLDIFS="$IFS"
	IFS=: display_parts=(${DISPLAY})
	IFS="$OLDIFS"

	if [ -z "${display_parts[0]}" ]; then
		# For a local display, forward the socket and set XAUTHLOCALHOSTNAME
		display_no="${display_parts[1]}"
		OPTS_XFORWARD+=("--env" "XAUTHLOCALHOSTNAME=${XAUTHLOCALHOSTNAME-$(hostname)}")
		OPTS_XFORWARD+=("--mount" "type=bind,source=/tmp/.X11-unix/X${display_parts[1]},target=/tmp/.X11-unix/X${display_parts[1]}")
	fi

	OPTS_XFORWARD+=("--mount" "type=bind,source=${XAUTHORITY-~/.Xauthority},target=/run/.Xauthority,ro")
	OPTS_XFORWARD+=("--env" "DISPLAY=${DISPLAY}")
	OPTS_XFORWARD+=("--env" "XAUTHORITY=/run/.Xauthority")
fi

OPTS_WLFORWARD=()
if [ -n "$WAYLAND_DISPLAY" ]; then
	wl_disp_path="${WAYLAND_DISPLAY}"
	# Only prepend XDG_RUNTIME_DIR if not an absolute path
	[ -e "${wl_disp_path}" ] || wl_disp_path="${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY}"
	OPTS_WLFORWARD+=("--mount" "type=bind,source=${wl_disp_path},target=/run/hostwayland-0")
	OPTS_WLFORWARD+=("--env" "XDG_SESSION_TYPE=wayland")
	OPTS_WLFORWARD+=("--env" "WAYLAND_DISPLAY=/run/hostwayland-0")
fi

if [ -z "${OPTS_XFORWARD}" ]; then
	echo "Warning: No X11 connection to forward."
else
	echo "Forwarding X11 connection."
fi

if [ -n "${OPTS_WLFORWARD}" ]; then
	echo "Forwarding Wayland connection."
fi

exec docker run --mount type=tmpfs,target=/run --user root --entrypoint=/entrypoint.sh --env XDG_RUNTIME_DIR=/run/user/linux --env KDE_FULL_SESSION=true --env KDE_SESSION_VERSION=5 --cap-add=SYS_NICE "${OPTS_XFORWARD[@]}" "${OPTS_WLFORWARD[@]}" "${docker_options[@]}" $image "${docker_command[@]}"
openSUSE Build Service is sponsored by