From d8791c3c7caa6e3cadaf98a5a2c94b232ac30fed Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 18 Oct 2022 13:19:05 +0200 Subject: [PATCH] nodedev: Add VIR_NODE_DEVICE_(CREATE|DEFINE)_XML_VALIDATE flags The node device APIs which get XML from the user don't yet support XML validation flags. Introduce virNodeDeviceCreateXMLFlags and virNodeDeviceDefineXMLFlags with the appropriate flags and add virsh support for the new flags. Signed-off-by: Peter Krempa Reviewed-by: Jonathon Jongsma --- docs/manpages/virsh.rst | 10 ++++++++-- include/libvirt/libvirt-nodedev.h | 19 +++++++++++++++++++ src/libvirt-nodedev.c | 4 ++-- tools/virsh-nodedev.c | 20 ++++++++++++++++++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 6ea87d2e76..b33e6a3a86 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -5235,7 +5235,7 @@ nodedev-create :: - nodedev-create FILE + nodedev-create FILE [--validate] Create a device on the host node that can then be assigned to virtual machines. Normally, libvirt is able to automatically determine which @@ -5243,6 +5243,9 @@ host nodes are available for use, but this allows registration of host hardware that libvirt did not automatically detect. *file* contains xml for a top-level description of a node device. +If *--validate* is specified, validates the format of the XML document against +an internal RNG schema. + nodedev-destroy --------------- @@ -5266,11 +5269,14 @@ nodedev-define :: - nodedev-define FILE + nodedev-define FILE [--validate] Define an inactive persistent device or modify an existing persistent one from the XML *FILE*. +If *--validate* is specified, validates the format of the XML document against +an internal RNG schema. + nodedev-undefine ---------------- diff --git a/include/libvirt/libvirt-nodedev.h b/include/libvirt/libvirt-nodedev.h index 4fccd3f614..428b0d722f 100644 --- a/include/libvirt/libvirt-nodedev.h +++ b/include/libvirt/libvirt-nodedev.h @@ -130,12 +130,31 @@ int virNodeDeviceDetachFlags(virNodeDevicePtr dev, int virNodeDeviceReAttach (virNodeDevicePtr dev); int virNodeDeviceReset (virNodeDevicePtr dev); +/** + * virNodeDeviceCreateXMLFlags: + * + * Since: 8.10.0 + */ +typedef enum { + VIR_NODE_DEVICE_CREATE_XML_VALIDATE = 1 << 0, /* Validate the XML document against schema (Since: 8.10.0) */ +} virNodeDeviceCreateXMLFlags; + virNodeDevicePtr virNodeDeviceCreateXML (virConnectPtr conn, const char *xmlDesc, unsigned int flags); int virNodeDeviceDestroy (virNodeDevicePtr dev); + +/** + * virNodeDeviceDefineXMLFlags: + * + * Since: 8.10.0 + */ +typedef enum { + VIR_NODE_DEVICE_DEFINE_XML_VALIDATE = 1 << 0, /* Validate the XML document against schema (Since: 8.10.0) */ +} virNodeDeviceDefineXMLFlags; + virNodeDevicePtr virNodeDeviceDefineXML(virConnectPtr conn, const char *xmlDesc, unsigned int flags); diff --git a/src/libvirt-nodedev.c b/src/libvirt-nodedev.c index d5a24ea2ef..1b7dee113e 100644 --- a/src/libvirt-nodedev.c +++ b/src/libvirt-nodedev.c @@ -694,7 +694,7 @@ virNodeDeviceReset(virNodeDevicePtr dev) * virNodeDeviceCreateXML: * @conn: pointer to the hypervisor connection * @xmlDesc: string containing an XML description of the device to be created - * @flags: extra flags; not used yet, so callers should always pass 0 + * @flags: bitwise-OR of supported virNodeDeviceCreateXMLFlags * * Create a new device on the VM host machine, for example, virtual * HBAs created using vport_create. @@ -778,7 +778,7 @@ virNodeDeviceDestroy(virNodeDevicePtr dev) * virNodeDeviceDefineXML: * @conn: pointer to the hypervisor connection * @xmlDesc: string containing an XML description of the device to be defined - * @flags: extra flags; not used yet, so callers should always pass 0 + * @flags: bitwise-OR of supported virNodeDeviceDefineXMLFlags * * Define a new device on the VM host machine, for example, a mediated device * diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c index 2adcad9c10..5dbec65367 100644 --- a/tools/virsh-nodedev.c +++ b/tools/virsh-nodedev.c @@ -50,6 +50,10 @@ static const vshCmdInfo info_node_device_create[] = { static const vshCmdOptDef opts_node_device_create[] = { VIRSH_COMMON_OPT_FILE(N_("file containing an XML description " "of the device")), + {.name = "validate", + .type = VSH_OT_BOOL, + .help = N_("validate the XML against the schema") + }, {.name = NULL} }; @@ -60,6 +64,7 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd) const char *from = NULL; g_autofree char *buffer = NULL; virshControl *priv = ctl->privData; + unsigned int flags = 0; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; @@ -67,7 +72,10 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd) if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; - if (!(dev = virNodeDeviceCreateXML(priv->conn, buffer, 0))) { + if (vshCommandOptBool(cmd, "validate")) + flags |= VIR_NODE_DEVICE_CREATE_XML_VALIDATE; + + if (!(dev = virNodeDeviceCreateXML(priv->conn, buffer, flags))) { vshError(ctl, _("Failed to create node device from %s"), from); return false; } @@ -1058,6 +1066,10 @@ static const vshCmdInfo info_node_device_define[] = { static const vshCmdOptDef opts_node_device_define[] = { VIRSH_COMMON_OPT_FILE(N_("file containing an XML description " "of the device")), + {.name = "validate", + .type = VSH_OT_BOOL, + .help = N_("validate the XML against the schema") + }, {.name = NULL} }; @@ -1068,6 +1080,7 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) const char *from = NULL; g_autofree char *buffer = NULL; virshControl *priv = ctl->privData; + unsigned int flags = 0; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; @@ -1075,7 +1088,10 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; - if (!(dev = virNodeDeviceDefineXML(priv->conn, buffer, 0))) { + if (vshCommandOptBool(cmd, "validate")) + flags |= VIR_NODE_DEVICE_DEFINE_XML_VALIDATE; + + if (!(dev = virNodeDeviceDefineXML(priv->conn, buffer, flags))) { vshError(ctl, _("Failed to define node device from '%s'"), from); return false; }