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 Answers 2

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
1
  • Exactly what I wanted to know, thanks! I edited the answer so it successfully runs. Commented Apr 24, 2019 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!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.