[vfio-users] Is there any tools to setup an vm with IGD passthrough?

Laszlo Ersek lersek at redhat.com
Tue Sep 26 10:45:40 UTC 2017


On 09/26/17 11:55, Daimon Wang wrote:
> Hi,    I'm planning to write a program that control an VM with IGD
> passthrough. Now the environment is ready: kernel param && driver
> blacklist ok and the VM start correctly with virt-install plus virsh
> edit the xml. So it's time to convert that process to a program.
> What stop me is the "virsh edit" part, which modify the  "address"
> property of the hostdev to "slot=0x02" (required for IGD). I'm
> wondering if there's already some tool that deal with such an VM
> creation. The virt-manager can't modify the "address" property and
> virsh-install even doesn't create the it. I think libvirt can achieve
> that, but that would require a lot more code (to deal with other
> normal properties, e.g. network/usb/sound).

I can recommend two tools for scripting domain XML editing:

(1) The same package that gives you "virt-install" should give you
"virt-xml" too.

I don't have much personal experience with "virt-xml", but it is part of
the standard tools, so you might prefer it to option (2) below.

(2) I frequently use the following triplet (in scripts, in fact):

- virsh dumpxml # dump the current domain XML to a file
- xmlstarlet    # modify the XML file
- virsh define  # redefine the domain from the modified XML

xmlstarlet is a "command line XML/XSLT toolkit". With the "ed"
subcommand, you can update elements and attributes in the XML file,
using XPath expressions. You do need to learn a bit of XML and XPath,
but it's not hard.

For example, here's a raw script I frequently use to conveniently switch
a domain between OVMF builds:

------
#!/bin/bash

set -e -u -C

# This script flips the domain between the ad-hoc OVMF build and the
# system-wide OVMF package.

DOMAIN="$1"
FWBIN="$2"

TMPF=$(mktemp)
trap 'rm -f -- "$TMPF"' EXIT

# This is how "virsh edit" extracts the current domain info.
virsh dumpxml --inactive --security-info -- "$DOMAIN" >> "$TMPF"

# Flip the firmware binary.
case "$FWBIN" in
  (installed)
    NEW_FW=/usr/share/OVMF/OVMF_CODE.fd
    ;;

  (installed-smm)
    NEW_FW=/usr/share/OVMF/OVMF_CODE.secboot.fd
    ;;

  (ad-hoc)
    NEW_FW=/home/virt-images/OVMF_CODE.fd
    ;;

  (ad-hoc-smm)
    NEW_FW=/home/virt-images/OVMF_CODE.3264.fd
    ;;

  (ad-hoc-4m)
    NEW_FW=/home/virt-images/OVMF_CODE.4m.fd
    ;;

  (ad-hoc-4m-smm)
    NEW_FW=/home/virt-images/OVMF_CODE.4m.3264.fd
    ;;

  (*)
    echo "unknown firmware option" >&2
    exit 1
esac

xmlstarlet ed --omit-decl --inplace \
  --update /domain/os/loader --value "$NEW_FW" "$TMPF"

# This is how "virsh edit" verifies and makes the changes permanent.

# virt-xml validate doesn't work alas
# virt-xml-validate -- "$TMPF" domain

virsh define -- "$TMPF"
------

The above is convenient for modifying existing domains. If you start
from zero (i.e., create a new domain every time), you can pass
"--print-xml" to virt-install, and then insert (not update) the right
address element with xmlstarlet into the XML generated by virt-install.

Another option is to just take the final, complete XML (one that already
works for a guest), and save it as a template within your program.
Remove the uuid and mac address elements (libvirt will auto-generate
them), and customize the rest (as necessary) with xmlstarlet when the
program is invoked. Then run "virsh define" to create the domain.

HTH,
Laszlo




More information about the vfio-users mailing list