tools: only fail validations if VIR_HOST_VALIDATE_FAIL is set

Currently `virt-host-validate` will fail whenever one of its calls fail,
regardless of virHostValidateLevel set.

This behaviour is not optimal and makes it not exactly reliable as a
command line tool as other tools or scripts using it would have to check
its output to figure out whether something really failed or if a warning
was mistakenly treated as failure.

With this change, the behaviour of whether to fail or not, is defined by
the caller of those functions, based on the virHostValidateLevel passed
to them.

https://gitlab.com/libvirt/libvirt/-/issues/175

Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Fabiano Fidêncio 2021-06-07 18:22:18 +02:00 committed by Michal Privoznik
parent 1832c0a02b
commit f798970b18
2 changed files with 29 additions and 15 deletions

View File

@ -142,7 +142,7 @@ int virHostValidateDeviceExists(const char *hvname,
if (access(dev_name, F_OK) < 0) { if (access(dev_name, F_OK) < 0) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
virHostMsgPass(); virHostMsgPass();
@ -159,7 +159,7 @@ int virHostValidateDeviceAccessible(const char *hvname,
if (access(dev_name, R_OK|W_OK) < 0) { if (access(dev_name, R_OK|W_OK) < 0) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
virHostMsgPass(); virHostMsgPass();
@ -180,7 +180,7 @@ int virHostValidateNamespace(const char *hvname,
if (access(nspath, F_OK) < 0) { if (access(nspath, F_OK) < 0) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
virHostMsgPass(); virHostMsgPass();
@ -264,17 +264,17 @@ int virHostValidateLinuxKernel(const char *hvname,
if (STRNEQ(uts.sysname, "Linux")) { if (STRNEQ(uts.sysname, "Linux")) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
if (virParseVersionString(uts.release, &thisversion, true) < 0) { if (virParseVersionString(uts.release, &thisversion, true) < 0) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
if (thisversion < version) { if (thisversion < version) {
virHostMsgFail(level, "%s", hint); virHostMsgFail(level, "%s", hint);
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} else { } else {
virHostMsgPass(); virHostMsgPass();
return 0; return 0;
@ -291,7 +291,7 @@ int virHostValidateCGroupControllers(const char *hvname,
size_t i; size_t i;
if (virCgroupNew("/", -1, &group) < 0) if (virCgroupNew("/", -1, &group) < 0)
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
int flag = 1 << i; int flag = 1 << i;
@ -303,7 +303,7 @@ int virHostValidateCGroupControllers(const char *hvname,
virHostMsgCheck(hvname, "for cgroup '%s' controller support", cg_name); virHostMsgCheck(hvname, "for cgroup '%s' controller support", cg_name);
if (!virCgroupHasController(group, i)) { if (!virCgroupHasController(group, i)) {
ret = -1; ret = VIR_HOST_VALIDATE_FAILURE(level);
virHostMsgFail(level, "Enable '%s' in kernel Kconfig file or " virHostMsgFail(level, "Enable '%s' in kernel Kconfig file or "
"mount/enable cgroup controller in your system", "mount/enable cgroup controller in your system",
cg_name); cg_name);
@ -320,7 +320,7 @@ int virHostValidateCGroupControllers(const char *hvname G_GNUC_UNUSED,
virHostValidateLevel level) virHostValidateLevel level)
{ {
virHostMsgFail(level, "%s", "This platform does not support cgroups"); virHostMsgFail(level, "%s", "This platform does not support cgroups");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
#endif /* !__linux__ */ #endif /* !__linux__ */
@ -354,7 +354,7 @@ int virHostValidateIOMMU(const char *hvname,
"No ACPI DMAR table found, IOMMU either " "No ACPI DMAR table found, IOMMU either "
"disabled in BIOS or not supported by this " "disabled in BIOS or not supported by this "
"hardware platform"); "hardware platform");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
} else if (isAMD) { } else if (isAMD) {
virHostMsgCheck(hvname, "%s", _("for device assignment IOMMU support")); virHostMsgCheck(hvname, "%s", _("for device assignment IOMMU support"));
@ -366,7 +366,7 @@ int virHostValidateIOMMU(const char *hvname,
"No ACPI IVRS table found, IOMMU either " "No ACPI IVRS table found, IOMMU either "
"disabled in BIOS or not supported by this " "disabled in BIOS or not supported by this "
"hardware platform"); "hardware platform");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
} else if (ARCH_IS_PPC64(arch)) { } else if (ARCH_IS_PPC64(arch)) {
/* Empty Block */ /* Empty Block */
@ -385,7 +385,7 @@ int virHostValidateIOMMU(const char *hvname,
} else { } else {
virHostMsgFail(level, virHostMsgFail(level,
"Unknown if this platform has IOMMU support"); "Unknown if this platform has IOMMU support");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
@ -404,7 +404,7 @@ int virHostValidateIOMMU(const char *hvname,
"Add %s to kernel cmdline arguments", bootarg); "Add %s to kernel cmdline arguments", bootarg);
else else
virHostMsgFail(level, "IOMMU capability not compiled into kernel."); virHostMsgFail(level, "IOMMU capability not compiled into kernel.");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
virHostMsgPass(); virHostMsgPass();
return 0; return 0;
@ -468,7 +468,7 @@ int virHostValidateSecureGuests(const char *hvname,
} }
if (virFileReadValueString(&cmdline, "/proc/cmdline") < 0) if (virFileReadValueString(&cmdline, "/proc/cmdline") < 0)
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
/* we're prefix matching rather than equality matching here, because /* we're prefix matching rather than equality matching here, because
* kernel would treat even something like prot_virt='yFOO' as * kernel would treat even something like prot_virt='yFOO' as
@ -516,7 +516,7 @@ int virHostValidateSecureGuests(const char *hvname,
} else { } else {
virHostMsgFail(level, virHostMsgFail(level,
"Unknown if this platform has Secure Guest support"); "Unknown if this platform has Secure Guest support");
return -1; return VIR_HOST_VALIDATE_FAILURE(level);
} }
return 0; return 0;

View File

@ -45,6 +45,20 @@ typedef enum {
VIR_ENUM_DECL(virHostValidateCPUFlag); VIR_ENUM_DECL(virHostValidateCPUFlag);
/**
* VIR_HOST_VALIDATE_FAILURE
* @level: the virHostValidateLevel to be checked
*
* This macro is to be used in to return a failures based on the
* virHostValidateLevel use in the function.
*
* If the virHostValidateLevel is VIR_HOST_VALIDATE_FAIL, -1 is returned.
* 0 is returned otherwise (as the virHosValidateLevel is then either a
* Warn or a Note).
*/
#define VIR_HOST_VALIDATE_FAILURE(level) (level == VIR_HOST_VALIDATE_FAIL) ? -1 : 0
void virHostMsgSetQuiet(bool quietFlag); void virHostMsgSetQuiet(bool quietFlag);
void virHostMsgCheck(const char *prefix, void virHostMsgCheck(const char *prefix,