From 60e83e345d3938574f8d1727df1a57cb1fca54cc Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sun, 25 Dec 2022 17:12:13 -0500 Subject: [PATCH] Introduce run_app_container script --- app_containers/.local/bin/run_app_container | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 app_containers/.local/bin/run_app_container diff --git a/app_containers/.local/bin/run_app_container b/app_containers/.local/bin/run_app_container new file mode 100755 index 0000000..88d94d3 --- /dev/null +++ b/app_containers/.local/bin/run_app_container @@ -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 +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 $@