Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awilliam/2ccd31e85923ac8135694a7db2306646 to your computer and use it in GitHub Desktop.
Save awilliam/2ccd31e85923ac8135694a7db2306646 to your computer and use it in GitHub Desktop.
Kludgy bash code to determine if vfio-pci display option is supported and requires GL support
#!/bin/sh
MLOCK=0
lspci -s $1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
# PCI device
if [ $(lspci -s $1 | wc -l) -gt 1 ]; then
echo "Ambiguous device: $1"
exit 1
fi
PCI_DEV=$(lspci -Ds $1 | awk '{print $1}')
OPT="host=$PCI_DEV"
if [ ! -L /sys/bus/pci/devices/$PCI_DEV/iommu_group ]; then
echo "No IOMMU group for device"
exit 1
fi
GROUP=$(basename $(readlink -f /sys/bus/pci/devices/$PCI_DEV/iommu_group))
MLOCK=1
else
OPT="sysfsdev=$1"
if [ ! -L $1/iommu_group ]; then
echo "No IOMMU group for device"
exit 1
fi
GROUP=$(basename $(readlink -f $1/iommu_group))
SUBSYS=$(basename $(readlink -f $1/subsystem))
if [ $SUBSYS != "mdev" ]; then
MLOCK=1
fi
fi
if [ $EUID -eq 0 ]; then
MLOCK=0
fi
if [ $MLOCK -ne 0 ]; then
if [ $(ulimit -l) -lt $(( 1024 * 1024 )) ]; then
echo "Increase locked memory limit to 1MB for test"
exit 1
fi
fi
if [ ! -c /dev/vfio/$GROUP ]; then
echo "No VFIO group for device"
exit 1
fi
if [ ! -w /dev/vfio/$GROUP ]; then
echo "No permissions to VFIO group"
exit 1
fi
TMP=$(mktemp)
exec /usr/local/bin/qemu-system-x86_64 \
-machine pc,accel=kvm \
-uuid $(uuidgen) \
-m 1 \
-S \
-no-user-config \
-nodefaults \
-M graphics=off \
-display none \
-device vfio-pci,$OPT,display=on \
> $TMP 2>&1 &
PID=$!
sleep 1
if kill -0 $PID 2>/dev/null; then
kill -TERM $PID
wait
echo "Display supported, GL NOT required"
else
if grep -q "opengl not available" $TMP; then
echo "Display supported, GL required"
elif grep -q "device doesn't support any (known) display method" $TMP; then
echo "Display NOT supported"
else
echo "Unknown failure"
fi
fi
rm -f $TMP
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment