Skip to content

Instantly share code, notes, and snippets.

@mwvent
Created May 10, 2018 13:13
Show Gist options
  • Save mwvent/ba57ff1d1c2a2a32029540dcca65502a to your computer and use it in GitHub Desktop.
Save mwvent/ba57ff1d1c2a2a32029540dcca65502a to your computer and use it in GitHub Desktop.
Automatically find GLX capable display and run vglrun with it. Useful for multiseat with one powerful GPU. Assumes displays are accessible by all users
#!/bin/bash
# This script is intended to be used as a replacement for vglrun -d ? program - the display is auto selected
# it can also be included in a game launcher script to automatically get virtualgl if needed and fix LD_PRELOAD order issues with Steam games
# if you have a preffered GPU put name here as it appears on glxinfo renderer string - a partial string is fine if it matches more than one
# GPU the result will be randomly selected
PREFFERED_CARD="GTX 650 Ti"
# ------------------------------------------------------------------------------------------------------------------------------------
# This portion of the script checks libdlfaker.so and libvglfaker.so are at the END of the LD_PRELOAD string
LD_PRELOAD=$(echo ${LD_PRELOAD//libdlfaker.so/} | awk '{$1=$1};1')":libdlfaker.so"
LD_PRELOAD=$(echo ${LD_PRELOAD//libvglfaker.so/} | awk '{$1=$1};1')":libvglfaker.so"
# ------------------------------------------------------------------------------------------------------------------------------------
# This portion of the script checks if glx is suported on the DISPLAY and if not tries to re-invoke the script with vglrun and
# an auto-selected GPU (designed for this script to be included in bash script with source ) - if an argument was supplied that is executed
#try glxinfo on current DISPLAY - if it fails begin process of setting up VirtualGL
if [ "$( glxinfo 2>&1 | grep "Error:" )" != "" ] && [ "$VGL_ISACTIVE" != 1 ]; then
echo "GLX not found on display $DISPLAY - setting up VirtualGL" 1>&2
# Get a list of sessions from loginctl - filtered to ones with a DISPLAY set then filter DISPLAY number
DISPLAYS=$(loginctl list-sessions | grep -v "SESSION" | grep -v "listed" | tr -s " " | sed '/^$/d' | cut -d " " -f2 | xargs loginctl show-session | grep "Display=" | cut -d "=" -f 2)
# Iterate found list of DISPLAYS - attempt to determine which are GLX capable, add them to the GLX_DISPLAYS list when found
GLX_DISPLAYS=""
FOUND_PREFERRED=""
for CUR_DISPLAY in $DISPLAYS; do
if [ "$( DISPLAY=$CUR_DISPLAY glxinfo 2>&1 | grep "Error:" )" = "" ]; then
GPU_NAME="$(DISPLAY=$CUR_DISPLAY glxinfo -B | grep "OpenGL renderer string" | cut -d ":" -f2 | cut -d " " -f2-10000)"
if [ "$(echo "$GPU_NAME" | grep "$PREFFERED_CARD")" != "" ]; then
echo "Found preferred card $GPU_NAME on $CUR_DISPLAY" 1>&2
GLX_DISPLAYS="$GLX_DISPLAYS$CUR_DISPLAY "
FOUND_PREFERRED="$FOUND_PREFERRED$CUR_DISPLAY"
else
echo "Found $GPU_NAME on $CUR_DISPLAY" 1>&2
fi
GLX_DISPLAYS="$GLX_DISPLAYS$CUR_DISPLAY "
fi
done
# if preffered card(s) were found use them instead of full list
if [ "$FOUND_PREFERRED" != "" ]; then GLX_DISPLAYS="$FOUND_PREFERRED"; fi
# If nothing found apologise - else get to picking a DISPLAY to use
if [ "$GLX_DISPLAYS" = "" ]; then
echo "Error: Did not find a GLX display from loginctl" 1>&2
GLX_DISPLAY_FOUND=0
else
# pick care from resulting list
GLX_DISPLAY="$(printf "%s\n" $GLX_DISPLAYS | sort -R | xargs echo | head -n 1)"
GLX_DISPLAY_FOUND=1
fi
# Setup VirtualGL
if [ $GLX_DISPLAY_FOUND -gt 0 ]; then
GPU_NAME="$(DISPLAY=$GLX_DISPLAY glxinfo -B | grep "OpenGL renderer string" | cut -d ":" -f2 | cut -d " " -f2-10000)"
echo "Using $GPU_NAME on $GLX_DISPLAY for VirtualGL" 1>&2
if [ -z "$1" ]; then
vglrun -d $GLX_DISPLAY $0
else
vglrun -d $GLX_DISPLAY $1
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment