Introduce run_app_container script

This commit is contained in:
Peter Cai 2022-12-25 17:12:13 -05:00
parent 372523a6d7
commit 60e83e345d
1 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#!/usr/bin/env bash
die() {
echo "$1" >&2
exit 1
}
[ -z "$CONTAINER_NAME" ] && die "\$CONTAINER_NAME not set"
[ -z "$DISPLAY" ] && die "\$DISPLAY not set (you must run the script in a desktop environment"
# From <https://liolok.com/run-desktop-app-with-systemd-nspawn-container>
if [[ -n $DBUS_SESSION_BUS_ADDRESS ]]; then # remove prefix
host_bus=${DBUS_SESSION_BUS_ADDRESS#unix:path=}
else # default guess
host_bus=$XDG_RUNTIME_DIR/bus
fi
if [[ -n $PULSE_SERVER ]]; then # remove prefix
host_pulse=${PULSE_SERVER#unix:}
else # default guess
host_pulse=$XDG_RUNTIME_DIR/pulse
fi
# Default username
run_as=user
homedir=/home/user
if [ "$CONTAINER_RUN_AS_ROOT" = true ]; then
run_as=root
homedir=/root
fi
sudo systemd-nspawn -M $CONTAINER_NAME \
`# DNS (when containers do not have their own netns)` \
--bind-ro=/run/systemd/resolve/stub-resolv.conf:/etc/resolv.conf \
`# GPU` \
--bind=/dev/dri/card0 \
--bind=/dev/shm \
--property=DeviceAllow='char-drm rw' \
`# Input devices` \
--bind-ro=/dev/input \
--property=DeviceAllow='char-input r' \
`# Xorg / Xwayland` \
--bind-ro=/tmp/.X11-unix \
--setenv=DISPLAY=$DISPLAY \
`# PulseAudio` \
--bind-ro=$host_pulse:/run/host/pulse \
--setenv=PULSE_SERVER=unix:/run/host/pulse/native \
`# DBus` \
--bind-ro=$host_bus:/run/host/bus \
--setenv=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/host/bus \
`# Scaling `\
--setenv=GDK_SCALE="$GDK_SCALE" \
`# Extra params` \
$SYSTEMD_NSPAWN_EXTRA_ARGS \
`# Launch app` \
--user=$run_as --chdir=$homedir --as-pid2 $@