Yes, xml editor is great. Here is my cmd for generating xml for IGD.virt-install -n test --memory 3092 --vcpu cpuset="2,3",threads=2,cores=1,sockets=1 --cpu host --hostdev pci_0000_00_02_0 --import --disk path=/home/ctos/win7.qcow2,format=qcow2 --network none --graphics none --noautoconsole --print-xml --autostart | xmlstarlet ed--omit-decl --append domain/devices/hostdev/source/address[@domain=0][@bus=0][@slot=2]/.. -t elem -n "address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'"
On Wednesday, September 27, 2017 10:31 AM, Daimon Wang <daimon_sw...@yahoo.com> wrote: Thanks Laszlo. Yes, the xml editor looks good to me. I'll try to insert the address property for hostdev device.While the virt-xml can't do that, because it use the same cmd options as virt-install. On Tuesday, September 26, 2017 6:45 PM, Laszlo Ersek <ler...@redhat.com> wrote: 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
_______________________________________________ vfio-users mailing list vfio-users@redhat.com https://www.redhat.com/mailman/listinfo/vfio-users