Skip to content

Instantly share code, notes, and snippets.

@abeluck
Created December 7, 2012 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abeluck/4233822 to your computer and use it in GitHub Desktop.
Save abeluck/4233822 to your computer and use it in GitHub Desktop.
Qubes PCI Scripts
#/bin/sh
########
# Attach pci device to vm
#
# usage: pci-attach <domain> <pci-device>
#
# Will attempt to partially match device names, e.g., "1a" -> "0000:00:1a.0"
#
domain=$1
local_dev=$2
if [ $# -ne 2 ]; then
echo "usage: $0 <doman> <pci_dev>"
exit 1
fi
# determine pci device name inside vm
count=`sudo xl pci-list-assignable-devices | grep $local_dev | wc -l`
if [ $count -gt 1 ]; then
echo "$local_dev has multiple matches, be more specific"
exit 2
elif [ $count -lt 1 ]; then
echo "$local_dev is not present in dom0, maybe it's already attached?"
exit 2
fi
local_dev=`sudo xl pci-list-assignable-devices | grep $local_dev | awk '{ print $1 }'`
echo "mapped $2 to $local_dev"
sudo xl pci-attach $domain $local_dev
##/bin/sh
########
# Detach pci device from vm in safe way
#
# usage: pci-detach <domain> <pci-device>
#
# Will attempt to partially match device names, e.g., "1a" -> "0000:00:1a.0"
domain=$1
local_dev=$2
if [ $# -ne 2 ]; then
echo "usage: $0 <doman> <pci_dev>"
exit 1
fi
# determine pci device name inside vm
count=`sudo xl pci-list $domain | grep $local_dev | wc -l`
if [ $count -gt 1 ]; then
echo "$local_dev has multiple matches, be more specific"
exit 2
elif [ $count -lt 1 ]; then
echo "$local_dev is not present in dom0"
exit 2
fi
vm_dev=`sudo xl pci-list $domain | grep $local_dev | awk '{ print $1 }'`
local_dev=`sudo xl pci-list $domain | grep $local_dev | awk '{ print $2 }'`
echo "mapped $local_dev to $vm_dev"
# detach driver
qvm-run $domain "echo BDF > /sys/bus/pci/devices/0000:00:$vm_dev/driver/unbind"
sudo xl pci-detach $domain $local_dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment