<html><head></head><body><div style="color:#000; background-color:#fff; font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px"><div id="yui_3_16_0_ym19_1_1506482831914_31040">Yes, xml editor is great. Here is my cmd for generating xml for IGD.</div><div dir="ltr" id="yui_3_16_0_ym19_1_1506482831914_31041">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'"<br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1506482831914_31041"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1506482831914_31041"><br></div> <div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 13px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"><font size="2" face="Arial"> On Wednesday, September 27, 2017 10:31 AM, Daimon Wang <daimon_swang@yahoo.com> wrote:<br></font></div>  <br><br> <div class="y_msg_container"><div id="yiv6130963101"><div><div style="color:#000;background-color:#fff;font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px;"><div id="yiv6130963101yui_3_16_0_ym19_1_1505960164300_267436"><span id="yiv6130963101yui_3_16_0_ym19_1_1505960164300_267455">Thanks Laszlo. Yes, the xml editor looks good to me. I'll try to insert the address property for hostdev device.</span></div><div dir="ltr" id="yiv6130963101yui_3_16_0_ym19_1_1505960164300_267436"><span id="yiv6130963101yui_3_16_0_ym19_1_1505960164300_269335">While the virt-xml can't do that, because it use the same cmd options as virt-install.</span></div> <div class="yiv6130963101qtdSeparateBR"><br clear="none"><br clear="none"></div><div class="yiv6130963101yqt8094746922" id="yiv6130963101yqt82674"><div class="yiv6130963101yahoo_quoted" style="display:block;"> <div style="font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px;"> <div style="font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div dir="ltr"><font size="2" face="Arial"> On Tuesday, September 26, 2017 6:45 PM, Laszlo Ersek <lersek@redhat.com> wrote:<br clear="none"></font></div>  <br clear="none"><br clear="none"> <div class="yiv6130963101y_msg_container"><div dir="ltr">On 09/26/17 11:55, Daimon Wang wrote:<div class="yiv6130963101yqt5825951074" id="yiv6130963101yqtfd15027"><br clear="none">> Hi,    I'm planning to write a program that control an VM with IGD<br clear="none">> passthrough. Now the environment is ready: kernel param && driver<br clear="none">> blacklist ok and the VM start correctly with virt-install plus virsh<br clear="none">> edit the xml. So it's time to convert that process to a program.<br clear="none">> What stop me is the "virsh edit" part, which modify the  "address"<br clear="none">> property of the hostdev to "slot=0x02" (required for IGD). I'm<br clear="none">> wondering if there's already some tool that deal with such an VM<br clear="none">> creation. The virt-manager can't modify the "address" property and<br clear="none">> virsh-install even doesn't create the it. I think libvirt can achieve<br clear="none">> that, but that would require a lot more code (to deal with other<br clear="none">> normal properties, e.g. network/usb/sound).</div><br clear="none"><br clear="none">I can recommend two tools for scripting domain XML editing:<br clear="none"><br clear="none">(1) The same package that gives you "virt-install" should give you<br clear="none">"virt-xml" too.<br clear="none"><br clear="none">I don't have much personal experience with "virt-xml", but it is part of<br clear="none">the standard tools, so you might prefer it to option (2) below.<br clear="none"><br clear="none">(2) I frequently use the following triplet (in scripts, in fact):<br clear="none"><br clear="none">- virsh dumpxml # dump the current domain XML to a file<br clear="none">- xmlstarlet    # modify the XML file<br clear="none">- virsh define  # redefine the domain from the modified XML<br clear="none"><br clear="none">xmlstarlet is a "command line XML/XSLT toolkit". With the "ed"<br clear="none">subcommand, you can update elements and attributes in the XML file,<br clear="none">using XPath expressions. You do need to learn a bit of XML and XPath,<br clear="none">but it's not hard.<br clear="none"><br clear="none">For example, here's a raw script I frequently use to conveniently switch<br clear="none">a domain between OVMF builds:<br clear="none"><br clear="none">------<br clear="none">#!/bin/bash<br clear="none"><br clear="none">set -e -u -C<br clear="none"><br clear="none"># This script flips the domain between the ad-hoc OVMF build and the<br clear="none"># system-wide OVMF package.<br clear="none"><br clear="none">DOMAIN="$1"<br clear="none">FWBIN="$2"<br clear="none"><br clear="none">TMPF=$(mktemp)<br clear="none">trap 'rm -f -- "$TMPF"' EXIT<br clear="none"><br clear="none"># This is how "virsh edit" extracts the current domain info.<br clear="none">virsh dumpxml --inactive --security-info -- "$DOMAIN" >> "$TMPF"<br clear="none"><br clear="none"># Flip the firmware binary.<br clear="none">case "$FWBIN" in<br clear="none">  (installed)<br clear="none">    NEW_FW=/usr/share/OVMF/OVMF_CODE.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (installed-smm)<br clear="none">    NEW_FW=/usr/share/OVMF/OVMF_CODE.secboot.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (ad-hoc)<br clear="none">    NEW_FW=/home/virt-images/OVMF_CODE.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (ad-hoc-smm)<br clear="none">    NEW_FW=/home/virt-images/OVMF_CODE.3264.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (ad-hoc-4m)<br clear="none">    NEW_FW=/home/virt-images/OVMF_CODE.4m.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (ad-hoc-4m-smm)<br clear="none">    NEW_FW=/home/virt-images/OVMF_CODE.4m.3264.fd<br clear="none">    ;;<br clear="none"><br clear="none">  (*)<br clear="none">    echo "unknown firmware option" >&2<br clear="none">    exit 1<br clear="none">esac<br clear="none"><br clear="none">xmlstarlet ed --omit-decl --inplace \<br clear="none">  --update /domain/os/loader --value "$NEW_FW" "$TMPF"<br clear="none"><br clear="none"># This is how "virsh edit" verifies and makes the changes permanent.<br clear="none"><br clear="none"># virt-xml validate doesn't work alas<br clear="none"># virt-xml-validate -- "$TMPF" domain<br clear="none"><br clear="none">virsh define -- "$TMPF"<br clear="none">------<br clear="none"><br clear="none">The above is convenient for modifying existing domains. If you start<br clear="none">from zero (i.e., create a new domain every time), you can pass<br clear="none">"--print-xml" to virt-install, and then insert (not update) the right<br clear="none">address element with xmlstarlet into the XML generated by virt-install.<br clear="none"><br clear="none">Another option is to just take the final, complete XML (one that already<br clear="none">works for a guest), and save it as a template within your program.<br clear="none">Remove the uuid and mac address elements (libvirt will auto-generate<br clear="none">them), and customize the rest (as necessary) with xmlstarlet when the<br clear="none">program is invoked. Then run "virsh define" to create the domain.<br clear="none"><br clear="none">HTH,<br clear="none">Laszlo<div class="yiv6130963101yqt5825951074" id="yiv6130963101yqtfd06582"><br clear="none"></div></div><br clear="none"><br clear="none"></div>  </div> </div>  </div></div></div></div></div><br><br></div>  </div> </div>  </div></div></body></html>