#!/bin/bash
#
# ssrgennvidiaxorgconf - Generates xorg.conf suitable for use with NVIDIA vGPU
#
# © Rocket Software, Inc. or its affiliates. All Rights Reserved.
# ROCKET SOFTWARE, INC. CONFIDENTIAL
#

basename=$(basename $0)

echo "Suggested Xorg configuration command:"
echo ""
echo "nvidia-xconfig --enable-all-gpus --use-display-device=none --busid=<BUSID>"
echo ""
echo "Note: If your GPU has physical outputs then --use-display-device=none is"
echo "likely required.  However, with more recent Tesla cards this same option"
echo "may prevent Xorg from starting."
echo ""

nvxconfig=nvidia-xconfig

if [ -z "$(which ${nvxconfig} 2>/dev/null)" ]; then
	echo "${basename}: Warning: ${nvxconfig} not found in path. Unable to query BusIDs."
	exit 1
fi

echo "Available devices:"
${nvxconfig} --query-gpu-info | egrep "^ *(Name|PCI BusID)"
echo ""
echo "For more information run: ${nvxconfig} --query-gpu-info"
echo ""
devname=$(${nvxconfig} --query-gpu-info 2>/dev/null | sed -n 's/^  Name      : //p' | head -n 1)
devbusid=$(${nvxconfig} --query-gpu-info 2>/dev/null | sed -n 's/^  PCI BusID : //p' | head -n 1)

if [ -n "$devname" -a -n "$devbusid" ]; then
	echo "Example to configure ${devname} @ PCI BusID ${devbusid}:"
	echo ""
	echo "${nvxconfig} --enable-all-gpus --use-display-device=none --busid=${devbusid}"
	echo""
	echo "If you have more than one GPU you may need to modify /etc/X11/xorg.conf to use"
	echo "unique bus IDs for each device section."
fi


