virConnectCompareCPU: Introduce FAIL_INCOMPATIBLE flag

The new VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE flag for
virConnectCompareCPU can be used to get an error
(VIR_ERR_CPU_INCOMPATIBLE) describing the incompatibility instead of the
usual VIR_CPU_COMPARE_INCOMPATIBLE return code.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2014-05-28 15:12:59 +02:00
parent b14954fc12
commit 80d0918bd2
6 changed files with 52 additions and 14 deletions

View File

@ -4122,6 +4122,11 @@ typedef enum {
#endif
} virCPUCompareResult;
typedef enum {
VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE = (1 << 0), /* treat incompatible
CPUs as failure */
} virConnectCompareCPUFlags;
int virConnectCompareCPU(virConnectPtr conn,
const char *xmlDesc,
unsigned int flags);

View File

@ -1318,21 +1318,30 @@ bhyveConnectCompareCPU(virConnectPtr conn,
bhyveConnPtr driver = conn->privateData;
int ret = VIR_CPU_COMPARE_ERROR;
virCapsPtr caps = NULL;
bool failIncompatible;
virCheckFlags(0, VIR_CPU_COMPARE_ERROR);
virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE,
VIR_CPU_COMPARE_ERROR);
if (virConnectCompareCPUEnsureACL(conn) < 0)
goto cleanup;
failIncomaptible = !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE);
if (!(caps = bhyveDriverGetCapabilities(driver)))
goto cleanup;
if (!caps->host.cpu ||
!caps->host.cpu->model) {
VIR_WARN("cannot get host CPU capabilities");
ret = VIR_CPU_COMPARE_INCOMPATIBLE;
if (failIncomaptible) {
virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s",
_("cannot get host CPU capabilities"));
} else {
VIR_WARN("cannot get host CPU capabilities");
ret = VIR_CPU_COMPARE_INCOMPATIBLE;
}
} else {
ret = cpuCompareXML(caps->host.cpu, xmlDesc, false);
ret = cpuCompareXML(caps->host.cpu, xmlDesc, failIncomaptible);
}
cleanup:

View File

@ -17245,11 +17245,16 @@ virConnectIsSecure(virConnectPtr conn)
* virConnectCompareCPU:
* @conn: virConnect connection
* @xmlDesc: XML describing the CPU to compare with host CPU
* @flags: extra flags; not used yet, so callers should always pass 0
* @flags: bitwise-OR of virConnectCompareCPUFlags
*
* Compares the given CPU description with the host CPU
*
* Returns comparison result according to enum virCPUCompareResult
* Returns comparison result according to enum virCPUCompareResult. If
* VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE is used and @xmlDesc CPU is
* incompatible with host CPU, this function will return VIR_CPU_COMPARE_ERROR
* (instead of VIR_CPU_COMPARE_INCOMPATIBLE) and the error will use the
* VIR_ERR_CPU_INCOMPATIBLE code with a message providing more details about
* the incompatibility.
*/
int
virConnectCompareCPU(virConnectPtr conn,

View File

@ -11514,21 +11514,30 @@ qemuConnectCompareCPU(virConnectPtr conn,
virQEMUDriverPtr driver = conn->privateData;
int ret = VIR_CPU_COMPARE_ERROR;
virCapsPtr caps = NULL;
bool failIncomaptible;
virCheckFlags(0, VIR_CPU_COMPARE_ERROR);
virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE,
VIR_CPU_COMPARE_ERROR);
if (virConnectCompareCPUEnsureACL(conn) < 0)
goto cleanup;
failIncomaptible = !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE);
if (!(caps = virQEMUDriverGetCapabilities(driver, false)))
goto cleanup;
if (!caps->host.cpu ||
!caps->host.cpu->model) {
VIR_WARN("cannot get host CPU capabilities");
ret = VIR_CPU_COMPARE_INCOMPATIBLE;
if (failIncomaptible) {
virReportError(VIR_ERR_CPU_INCOMPATIBLE, "%s",
_("cannot get host CPU capabilities"));
} else {
VIR_WARN("cannot get host CPU capabilities");
ret = VIR_CPU_COMPARE_INCOMPATIBLE;
}
} else {
ret = cpuCompareXML(caps->host.cpu, xmlDesc, false);
ret = cpuCompareXML(caps->host.cpu, xmlDesc, failIncomaptible);
}
cleanup:

View File

@ -6214,6 +6214,10 @@ static const vshCmdOptDef opts_cpu_compare[] = {
.flags = VSH_OFLAG_REQ,
.help = N_("file containing an XML CPU description")
},
{.name = "error",
.type = VSH_OT_BOOL,
.help = N_("report error if CPUs are incompatible")
},
{.name = NULL}
};
@ -6225,11 +6229,14 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
char *buffer;
int result;
char *snippet = NULL;
unsigned int flags = 0;
xmlDocPtr xml = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlNodePtr node;
if (vshCommandOptBool(cmd, "error"))
flags |= VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE;
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
return false;
@ -6253,7 +6260,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd)
goto cleanup;
}
result = virConnectCompareCPU(ctl->conn, snippet, 0);
result = virConnectCompareCPU(ctl->conn, snippet, flags);
switch (result) {
case VIR_CPU_COMPARE_INCOMPATIBLE:

View File

@ -535,14 +535,17 @@ resulting XML description will explicitly include all features that make
up the CPU, without this option features that are part of the CPU model
will not be listed in the XML description.
=item B<cpu-compare> I<FILE>
=item B<cpu-compare> I<FILE> [I<--error>]
Compare CPU definition from XML <file> with host CPU. The XML <file> may
contain either host or guest CPU definition. The host CPU definition is the
<cpu> element and its contents as printed by B<capabilities> command. The
guest CPU definition is the <cpu> element and its contents from domain XML
definition. For more information on guest CPU definition see:
L<http://libvirt.org/formatdomain.html#elementsCPU>
L<http://libvirt.org/formatdomain.html#elementsCPU>. If I<--error> is
specified, the command will return an error when the given CPU is
incompatible with host CPU and a message providing more details about the
incompatibility will be printed out.
=item B<echo> [I<--shell>] [I<--xml>] [I<arg>...]