-
Notifications
You must be signed in to change notification settings - Fork 2
/
vfio
58 lines (46 loc) · 1.12 KB
/
vfio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
queryDevice() {
dev=0000:$dev
if [ ! -d /sys/bus/pci/devices/$dev ]; then
echo "ERROR: Device $dev does not exist!"
return 1
fi
vendor=$(cat /sys/bus/pci/devices/$dev/vendor)
device=$(cat /sys/bus/pci/devices/$dev/device)
return 0
}
logExitStatus() {
[ $? -eq 0 ] && echo "Success" || echo "Failed"
}
bind() {
modprobe vfio-pci
for dev in $@; do
queryDevice
[ $? -ne 0 ] && continue
# Unbind PCI device $dev, if bound to a driver
driverDir=/sys/bus/pci/devices/$dev/driver
if [ -e $driverDir ]; then
driver=$(basename $(readlink $driverDir))
echo -n "Unbinding $dev from $driver..."
echo $dev > $driverDir/unbind
logExitStatus
fi
echo -n "Binding $dev to vfio-pci..."
echo $vendor $device > /sys/bus/pci/drivers/vfio-pci/new_id
logExitStatus
done
}
unbind() {
for dev in $@; do
queryDevice
[ $? -ne 0 ] && continue
echo -n "Unbinding $dev from vfio-pci..."
echo $vendor $device > /sys/bus/pci/drivers/vfio-pci/remove_id
logExitStatus
done
rmmod vfio-pci vfio-virqfd vfio-iommu-type1 vfio
}
case $1 in
"bind") bind ${@:2} ;;
"unbind") unbind ${@:2} ;;
esac