tools: add '--xpath EXPRESSION --wrap' args to all dumpxml commands

While you can chain the virsh output up to a later 'xmllint' or 'xpath'
command, integrating it into virsh avoids needs for installing extra
binaries which we've often found to be missing on production installs
of libvirt. It also gives better response if the initial virsh command
hits an error, as you don't get an aborted pipeline.

    $ virsh pool-dumpxml --xpath //permissions default
    <permissions>
      <mode>0711</mode>
      <owner>1000</owner>
      <group>1000</group>
      <label>unconfined_u:object_r:svirt_home_t:s0</label>
    </permissions>

If multiple nodes match, they are emitted individually:

    $ virsh dumpxml --xpath '//devices/*/address[@type="pci"]' --wrap demo
    <address type="pci" domain="0x0000" bus="0x05" slot="0x00" function="0x0"/>
    <address type="pci" domain="0x0000" bus="0x03" slot="0x00" function="0x0"/>
    ...snip...
    <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x0" multifunction="on"/>
    <address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>

but if intending to post-process the output further, the results
can be wrapped in a parent node

    $ virsh dumpxml --xpath '//devices/*/address[@type="pci"]' --wrap demo
    <nodes>
      <address type="pci" domain="0x0000" bus="0x05" slot="0x00" function="0x0"/>
      <address type="pci" domain="0x0000" bus="0x03" slot="0x00" function="0x0"/>
      ...snip...
      <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x0" multifunction="on"/>
      <address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>
    </nodes>

Fixes https://gitlab.com/libvirt/libvirt/-/issues/244
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé
2022-06-16 16:29:54 +01:00
parent 8615c19b5d
commit 8603b3d76c
12 changed files with 370 additions and 84 deletions

View File

@@ -852,6 +852,15 @@ static const vshCmdOptDef opts_checkpoint_dumpxml[] = {
.type = VSH_OT_BOOL,
.help = N_("include backup size estimate in XML dump")
},
{.name = "xpath",
.type = VSH_OT_STRING,
.completer = virshCompleteEmpty,
.help = N_("xpath expression to filter the XML document")
},
{.name = "wrap",
.type = VSH_OT_BOOL,
.help = N_("wrap xpath results in an common root element"),
},
{.name = NULL}
};
@@ -864,6 +873,8 @@ cmdCheckpointDumpXML(vshControl *ctl,
g_autoptr(virshDomainCheckpoint) checkpoint = NULL;
g_autofree char *xml = NULL;
unsigned int flags = 0;
bool wrap = vshCommandOptBool(cmd, "wrap");
const char *xpath = NULL;
if (vshCommandOptBool(cmd, "security-info"))
flags |= VIR_DOMAIN_CHECKPOINT_XML_SECURE;
@@ -875,6 +886,9 @@ cmdCheckpointDumpXML(vshControl *ctl,
if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
return false;
if (vshCommandOptStringQuiet(ctl, cmd, "xpath", &xpath) < 0)
return false;
if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom,
&checkpoint, &name) < 0)
return false;
@@ -882,8 +896,7 @@ cmdCheckpointDumpXML(vshControl *ctl,
if (!(xml = virDomainCheckpointGetXMLDesc(checkpoint, flags)))
return false;
vshPrint(ctl, "%s", xml);
return true;
return virshDumpXML(ctl, xml, "domain-checkpoint", xpath, wrap);
}