0

I am running a virtual machine that requires vfio access to a PCI card. Using this script I am able to successfully bind the PCI device and its iommu group to vfio-pci, which then lets me boot up the VM:

  1. ./vfio-pci-bind.sh 0000:03:00.0
  2. virsh start myvm

I want to do this automatically instead of having to manually bind to vfio-pci every time. Is there some sort of preflight/preboot script I can make virsh run before it attempts to boot a vm?

2

You can use a libvirt hook to run arbitrary programs when a VM or network is started or stopped.

For instance, you can create a file /etc/libvirt/hooks/qemu which determines if your VM is being called, and then does whatever needs to be done.

#!/bin/bash

if [ "$1" = "myvm" ] && [ "$2" = "prepare" ]; then
    /path/to/vfio-pci-bind.sh '0000:03:00.0'
fi
  • Exactly what I wanted to know, thanks! I edited the answer so it successfully runs. – erp Apr 24 at 23:25
0

@michael-hampton has the most correct answer, but for anyone's reference I was impatient and also made a systemd service called vfio-pci-bind.service that executes vfio-pci-bind.sh:

[Unit]
Description=bind PCI card to vfio

[Service]
ExecStart=/opt/vfio-pci-bind/vfio-pci-bind.sh "0000:03:00.1"
Type=oneshot
RemainAfterExit=yes

then I added to libvirt-guests.service:

[Service]
...
Wants=vfio-pci-bind.service
After=vfio-pci-bind.service

The issues with this being

  • it relies on systemd rather than being self-contained to libvirt
  • it executes vfio-pci-bind regardless of whether or not the affected vm will actually be started at boot

so yeah I'll switch over to libvirt hooks, thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.