1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-12 04:58:32 +03:00

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 <pkrempa@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Peter Krempa 2022-10-18 13:19:05 +02:00
parent 0268270b0f
commit d8791c3c7c
4 changed files with 47 additions and 6 deletions

View File

@ -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 Create a device on the host node that can then be assigned to virtual
machines. Normally, libvirt is able to automatically determine which 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* host hardware that libvirt did not automatically detect. *file*
contains xml for a top-level <device> description of a node device. contains xml for a top-level <device> description of a node device.
If *--validate* is specified, validates the format of the XML document against
an internal RNG schema.
nodedev-destroy 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 Define an inactive persistent device or modify an existing persistent one from
the XML *FILE*. the XML *FILE*.
If *--validate* is specified, validates the format of the XML document against
an internal RNG schema.
nodedev-undefine nodedev-undefine
---------------- ----------------

View File

@ -130,12 +130,31 @@ int virNodeDeviceDetachFlags(virNodeDevicePtr dev,
int virNodeDeviceReAttach (virNodeDevicePtr dev); int virNodeDeviceReAttach (virNodeDevicePtr dev);
int virNodeDeviceReset (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, virNodeDevicePtr virNodeDeviceCreateXML (virConnectPtr conn,
const char *xmlDesc, const char *xmlDesc,
unsigned int flags); unsigned int flags);
int virNodeDeviceDestroy (virNodeDevicePtr dev); 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, virNodeDevicePtr virNodeDeviceDefineXML(virConnectPtr conn,
const char *xmlDesc, const char *xmlDesc,
unsigned int flags); unsigned int flags);

View File

@ -694,7 +694,7 @@ virNodeDeviceReset(virNodeDevicePtr dev)
* virNodeDeviceCreateXML: * virNodeDeviceCreateXML:
* @conn: pointer to the hypervisor connection * @conn: pointer to the hypervisor connection
* @xmlDesc: string containing an XML description of the device to be created * @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 * Create a new device on the VM host machine, for example, virtual
* HBAs created using vport_create. * HBAs created using vport_create.
@ -778,7 +778,7 @@ virNodeDeviceDestroy(virNodeDevicePtr dev)
* virNodeDeviceDefineXML: * virNodeDeviceDefineXML:
* @conn: pointer to the hypervisor connection * @conn: pointer to the hypervisor connection
* @xmlDesc: string containing an XML description of the device to be defined * @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 * Define a new device on the VM host machine, for example, a mediated device
* *

View File

@ -50,6 +50,10 @@ static const vshCmdInfo info_node_device_create[] = {
static const vshCmdOptDef opts_node_device_create[] = { static const vshCmdOptDef opts_node_device_create[] = {
VIRSH_COMMON_OPT_FILE(N_("file containing an XML description " VIRSH_COMMON_OPT_FILE(N_("file containing an XML description "
"of the device")), "of the device")),
{.name = "validate",
.type = VSH_OT_BOOL,
.help = N_("validate the XML against the schema")
},
{.name = NULL} {.name = NULL}
}; };
@ -60,6 +64,7 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
const char *from = NULL; const char *from = NULL;
g_autofree char *buffer = NULL; g_autofree char *buffer = NULL;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
unsigned int flags = 0;
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
return false; return false;
@ -67,7 +72,10 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
return false; 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); vshError(ctl, _("Failed to create node device from %s"), from);
return false; return false;
} }
@ -1058,6 +1066,10 @@ static const vshCmdInfo info_node_device_define[] = {
static const vshCmdOptDef opts_node_device_define[] = { static const vshCmdOptDef opts_node_device_define[] = {
VIRSH_COMMON_OPT_FILE(N_("file containing an XML description " VIRSH_COMMON_OPT_FILE(N_("file containing an XML description "
"of the device")), "of the device")),
{.name = "validate",
.type = VSH_OT_BOOL,
.help = N_("validate the XML against the schema")
},
{.name = NULL} {.name = NULL}
}; };
@ -1068,6 +1080,7 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
const char *from = NULL; const char *from = NULL;
g_autofree char *buffer = NULL; g_autofree char *buffer = NULL;
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
unsigned int flags = 0;
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
return false; return false;
@ -1075,7 +1088,10 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
return false; 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); vshError(ctl, _("Failed to define node device from '%s'"), from);
return false; return false;
} }