Coder Social home page Coder Social logo

andre-richter / vfio-pci-bind Goto Github PK

View Code? Open in Web Editor NEW
106.0 5.0 22.0 25 KB

Automatically bind a PCI device and all of it's IOMMU group members to vfio-pci

License: MIT License

Shell 100.00%
vfio pci pcie sr-iov iommu-group vfio-pci bind script

vfio-pci-bind's Introduction

vfio-pci-bind

This script takes one or two parameters in any order:

  • Vendor:Device i.e. vvvv:dddd
  • Domain:Bus:Device.Function i.e. dddd:vv:dd.f

and then:

  1. If both Vendor:Device and Domain:Bus:Device.Function were provided, validate that the requested Vendor:Device exists at Domain:Bus:Device.Function

    If only Vendor:Device was provided, determine the current Domain:Bus:Device.Function for that device.

    If only Domain:Bus:Device.Function was provided, use it.

  2. Unbinds all devices that are in the same iommu group as the supplied device from their current driver (except PCIe bridges).

  3. Binds to vfio-pci:

    1. The supplied device.
    2. All devices that are in the same iommu group.
  4. Transfers ownership of the respective iommu group inside /dev/vfio to $SUDO_USER

Suggestions:

  • If you have a single piece of hardware with a given Vendor:Device, you can call the script like this:

    vfio-pci-bind.sh Vendor:Device

    The script will target that device regardless of how the PCI address might change due to the addition or removal of other hardware.

  • If you have multiple pieces of hardware with the same Vendor:Device code, you need to pass the PCI address as well:

    vfio-pci-bind.sh Vendor:Device Domain:Bus:Device.Function

    This will ensure the correct instance of the hardware is bound to vfio-pci.

    Note: If the PCI address for this device changes as a result of adding or removing hardware, you will need to update the PCI address in this call.

  • For backwards compatibility you can also specify just the PCI address:

    vfio-pci-bind.sh Domain:Bus:Device.Function

    Note: If you add or remove hardware, the device associated with that PCI address can change resulting in the wrong device being bound to vfio-pci. Consider passing the Vendor:Device as well.

Script must be executed via sudo!

Automatically binding devices on boot

Devices can be automatically bound to vfio-pci on boot using the supplied 25-vfio-pci-bind.rules udev rules file.

  1. Copy vfio-pci-bind.sh to /lib/udev/ and ensure it is marked executable.
  2. Copy 25-vfio-pci-bind.rules to /etc/udev/rules.d/
  3. Edit /etc/udev/rules.d/25-vfio-pci-bind.rules and add PCI device matching rules following the examples in the file.
  4. Reboot.

License

See supplied LICENSE file.

vfio-pci-bind's People

Contributors

andre-richter avatar ljm42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vfio-pci-bind's Issues

vfio-pci-bind.sh Bindinh Hangs System

Hi

Thank you for your work.

When I try to use the script my system hangs (waited for 10 min and still hanged) and I have to do a manual Computer Reset (press the physical button).

I have run it using:
sudo vfio-pci-bind.sh 0000:10:00.0 10de:107d
sudo vfio-pci-bind.sh 0000:10:00.1 10de:0e08
reversing the ID orders (10de:0e08 0000:10:00.1)

sudo vfio-pci-bind.sh 0000:10:00.0
sudo vfio-pci-bind.sh 0000:10:00.1

sudo vfio-pci-bind.sh 10de:107d
sudo vfio-pci-bind.sh 10de:0e08

Using "sudo su" before executing the commands.

After I press Enter, it outputs nothing and just hangs the PC, can't even use the mouse.

I have Debian 11.5 KDE. The MB BIOS has IOMMU and virtualization turned on. Linux has it enabled also. I tried using a manual procedure and it worked, but when installing the drivers on the guest I got the Code 43 error (turned off kvm and other workaround I found on the internet with no succes.. Even tried older NVidia Drivers). So I thought it was some misstep I did or the guide was to old and was missing a step. So I formatted the PC and installed Debian again (nothing lost, the previous installation was also fresh, preferred to star over from 0 then to reverse the steps of the manual guide).

What am I doing wrong?

Thank you in advanced!

binding devices on boot

Hi,
Thanks for making such great script it saves my day because I have some identical vfio ids on both gpu.
I have to add multiple devices bind on boot I tried like this but not working
KERNEL=="0000:4e:00.0","0000:4e:00.1","0000:4e:00.2","0000:4e:00.3", TAG="vfio-pci-bind"

and like this only first device was bounded
KERNEL=="0000:4e:00.0", TAG="vfio-pci-bind"
KERNEL=="0000:4e:00.1", TAG="vfio-pci-bind"
KERNEL=="0000:4e:00.2", TAG="vfio-pci-bind"
KERNEL=="0000:4e:00.3", TAG="vfio-pci-bind"

can you help please loop all devices ?
and besides script doesn't work using "sudo" but only as "sudo su -" root works
Using vfio-pci-bind.sh 0000:4e:00.0 one by one works but the bound gone after reboot ubuntu.

GPU 1
03:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73af] (rev c0)
03:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:ab28]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:ab28]
03:00.2 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a6]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a6]
03:00.3 Serial bus controller [0c80]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a4]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:0408]

GPU 2
4e:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73bf] (rev c0)
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:0e3a]
4e:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:ab28]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:ab28]
4e:00.2 USB controller [0c03]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a6]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a6]
4e:00.3 Serial bus controller [0c80]: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:73a4]
Subsystem: Advanced Micro Devices, Inc. [AMD/ATI] Device [1002:0408]

Thanks in advance
Cheers.

syntax error in shell script

I believe this line in the shell script is incorrect:

if [[ $curr_driver -ne "vfio-pci" ]]; then

In the Bourne Shell the "-ne" operator is meant for integer operations, since this is a string operation it should use "!=" or "==", right?

Also, it looks like the logic is reversed. Based on what the script is doing, and the corresponding line in the Ruby file:

if curr_driver == 'vfio-pci'

I believe this line should be:
if [[ "$curr_driver" == "vfio-pci" ]]; then

Can you double check and see if I am off base here?

bind specific Vendor:Device

Thanks for this script. It works well until you add hardware that changes your PCI addresses, and then there is a risk that the wrong device will be bound to vfio-pci. It would be a big problem if the host lost access to a nic or drive controller as a result.

I'd like to propose that the script accept a second (optional) parameter of the Vendor:Device to bind. If the supplied Vendor:Device is found at Domain:Bus:Device.Function, then it can go ahead and bind the device. If something else is found at that address then it should not bind the device.

I will submit a pull request for a potential implementation of this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.