The 'virsh edit' command gets XML validation enabled by default,
with a --skip-validate option to disable it. The 'virsh define'
and 'virsh create' commands get a --validate option to enable
it, to avoid regressions for existing scripts.
The quality of error reporting from libxml2 varies depending
on the type of XML error made. Sometimes it is quite clear
and useful, other times it is obscure & inaccurate. At least
the user will see an error now, rather than having their
XML modification silently disappear.
---
tools/virsh-domain.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 78 insertions(+), 9 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 6733cfa..fe8f4ba 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -133,6 +133,56 @@ vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
return vshLookupDomainInternal(ctl, cmd->def->name, n, flags);
}
+static virDomainPtr
+vshDomainDefine(virConnectPtr conn, const char *xml, unsigned int flags)
+{
+ virDomainPtr dom;
+ if (flags) {
+ dom = virDomainDefineXMLFlags(conn, xml, flags);
+ /* If validate is the only flag, just drop it and
+ * try again.
+ */
+ if (!dom) {
+ virErrorPtr err = virGetLastError();
+ if (err &&
+ (err->code == VIR_ERR_NO_SUPPORT) &&
+ (flags == VIR_DOMAIN_DEFINE_VALIDATE))
+ dom = virDomainDefineXML(conn, xml);
+ }
+ } else {
+ dom = virDomainDefineXML(conn, xml);
+ }
+ return dom;
+}
+
+
+static virDomainPtr
+vshDomainCreateXML(virConnectPtr conn, const char *xml,
+ size_t nfds, int *fds, unsigned int flags)
+{
+ virDomainPtr dom;
+ if (nfds)
+ dom = virDomainCreateXMLWithFiles(conn, xml,
+ nfds, fds, flags);
+ else
+ dom = virDomainCreateXML(conn, xml, flags);
+ /* If validate is set, just drop it and try again */
+ if (!dom) {
+ virErrorPtr err = virGetLastError();
+ if (err &&
+ (err->code == VIR_ERR_INVALID_ARG) &&
+ (flags & VIR_DOMAIN_START_VALIDATE)) {
+ flags &= ~VIR_DOMAIN_START_VALIDATE;
+ if (nfds)
+ dom = virDomainCreateXMLWithFiles(conn, xml,
+ nfds, fds, flags);
+ else
+ dom = virDomainCreateXML(conn, xml, flags);
+ }
+ }
+ return dom;
+}
+
VIR_ENUM_DECL(vshDomainVcpuState)
VIR_ENUM_IMPL(vshDomainVcpuState,
VIR_VCPU_LAST,
@@ -7154,6 +7204,10 @@ static const vshCmdOptDef opts_create[] = {
.type = VSH_OT_STRING,
.help = N_("pass file descriptors N,M,... to the guest")
},
+ {.name = "validate",
+ .type = VSH_OT_BOOL,
+ .help = N_("validate the XML against the schema")
+ },
{.name = NULL}
};
@@ -7167,7 +7221,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
#ifndef WIN32
bool console = vshCommandOptBool(cmd, "console");
#endif
- unsigned int flags = VIR_DOMAIN_NONE;
+ unsigned int flags = 0;
size_t nfds = 0;
int *fds = NULL;
@@ -7184,11 +7238,10 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd)
flags |= VIR_DOMAIN_START_PAUSED;
if (vshCommandOptBool(cmd, "autodestroy"))
flags |= VIR_DOMAIN_START_AUTODESTROY;
+ if (vshCommandOptBool(cmd, "validate"))
+ flags |= VIR_DOMAIN_START_VALIDATE;
- if (nfds)
- dom = virDomainCreateXMLWithFiles(ctl->conn, buffer, nfds, fds, flags);
- else
- dom = virDomainCreateXML(ctl->conn, buffer, flags);
+ dom = vshDomainCreateXML(ctl->conn, buffer, nfds, fds, flags);
if (!dom) {
vshError(ctl, _("Failed to create domain from %s"), from);
@@ -7229,6 +7282,10 @@ static const vshCmdOptDef opts_define[] = {
.flags = VSH_OFLAG_REQ,
.help = N_("file containing an XML domain description")
},
+ {.name = "validate",
+ .type = VSH_OT_BOOL,
+ .help = N_("validate the XML against the schema")
+ },
{.name = NULL}
};
@@ -7239,14 +7296,18 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
const char *from = NULL;
bool ret = true;
char *buffer;
+ unsigned int flags = 0;
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
return false;
+ if (vshCommandOptBool(cmd, "validate"))
+ flags |= VIR_DOMAIN_DEFINE_VALIDATE;
+
if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
return false;
- dom = virDomainDefineXML(ctl->conn, buffer);
+ dom = vshDomainDefine(ctl->conn, buffer, flags);
VIR_FREE(buffer);
if (dom != NULL) {
@@ -11105,6 +11166,10 @@ static const vshCmdOptDef opts_edit[] = {
.flags = VSH_OFLAG_REQ,
.help = N_("domain name, id or uuid")
},
+ {.name = "skip-validate",
+ .type = VSH_OT_BOOL,
+ .help = N_("skip validation of the XML against the schema")
+ },
{.name = NULL}
};
@@ -11114,13 +11179,17 @@ cmdEdit(vshControl *ctl, const vshCmd *cmd)
bool ret = false;
virDomainPtr dom = NULL;
virDomainPtr dom_edited = NULL;
- unsigned int flags = VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_INACTIVE;
+ unsigned int query_flags = VIR_DOMAIN_XML_SECURE | VIR_DOMAIN_XML_INACTIVE;
+ unsigned int define_flags = VIR_DOMAIN_DEFINE_VALIDATE;
dom = vshCommandOptDomain(ctl, cmd, NULL);
if (dom == NULL)
goto cleanup;
-#define EDIT_GET_XML virDomainGetXMLDesc(dom, flags)
+ if (vshCommandOptBool(cmd, "skip-validate"))
+ define_flags &= ~VIR_DOMAIN_DEFINE_VALIDATE;
+
+#define EDIT_GET_XML virDomainGetXMLDesc(dom, query_flags)
#define EDIT_NOT_CHANGED \
do { \
vshPrint(ctl, _("Domain %s XML configuration not changed.\n"), \
@@ -11129,7 +11198,7 @@ cmdEdit(vshControl *ctl, const vshCmd *cmd)
goto edit_cleanup; \
} while (0)
#define EDIT_DEFINE \
- (dom_edited = virDomainDefineXML(ctl->conn, doc_edited))
+ (dom_edited = vshDomainDefine(ctl->conn, doc_edited, define_flags))
#include "virsh-edit.c"
vshPrint(ctl, _("Domain %s XML configuration edited.\n"),
--
2.1.0