1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-02-27 09:57:43 +03:00

util: add a method for checking if swtpm is available

The QEMU domain capabilities code wants to quietly know whether swtpm is
available on the host.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2021-11-24 13:36:21 +00:00
parent 45f83e3a0b
commit 193436c6a1
3 changed files with 36 additions and 12 deletions

View File

@ -3451,6 +3451,7 @@ virTPMCreateCancelPath;
virTPMGetSwtpm; virTPMGetSwtpm;
virTPMGetSwtpmIoctl; virTPMGetSwtpmIoctl;
virTPMGetSwtpmSetup; virTPMGetSwtpmSetup;
virTPMHasSwtpm;
virTPMSwtpmCapsGet; virTPMSwtpmCapsGet;
virTPMSwtpmFeatureTypeFromString; virTPMSwtpmFeatureTypeFromString;
virTPMSwtpmSetupCapsGet; virTPMSwtpmSetupCapsGet;

View File

@ -132,7 +132,7 @@ static virTPMBinaryInfo swtpmBinaries[VIR_TPM_BINARY_LAST] = {
}, },
}; };
static int virTPMEmulatorInit(void); static int virTPMEmulatorInit(bool quiet);
static char * static char *
virTPMBinaryGetPath(virTPMBinary binary) virTPMBinaryGetPath(virTPMBinary binary)
@ -141,7 +141,7 @@ virTPMBinaryGetPath(virTPMBinary binary)
virMutexLock(&swtpm_tools_lock); virMutexLock(&swtpm_tools_lock);
if (virTPMEmulatorInit() < 0) if (virTPMEmulatorInit(false) < 0)
goto cleanup; goto cleanup;
s = g_strdup(swtpmBinaries[binary].path); s = g_strdup(swtpmBinaries[binary].path);
@ -169,6 +169,24 @@ virTPMGetSwtpmIoctl(void)
return virTPMBinaryGetPath(VIR_TPM_BINARY_SWTPM_IOCTL); return virTPMBinaryGetPath(VIR_TPM_BINARY_SWTPM_IOCTL);
} }
bool virTPMHasSwtpm(void)
{
bool ret = false;
virMutexLock(&swtpm_tools_lock);
if (virTPMEmulatorInit(true) < 0)
goto cleanup;
ret = swtpmBinaries[VIR_TPM_BINARY_SWTPM].path != NULL &&
swtpmBinaries[VIR_TPM_BINARY_SWTPM_SETUP].path != NULL &&
swtpmBinaries[VIR_TPM_BINARY_SWTPM_IOCTL].path != NULL;
cleanup:
virMutexUnlock(&swtpm_tools_lock);
return ret;
}
/* virTPMExecGetCaps /* virTPMExecGetCaps
* *
* Execute the prepared command and parse the returned JSON object * Execute the prepared command and parse the returned JSON object
@ -269,7 +287,7 @@ virTPMGetCaps(virTPMBinaryCapsParse capsParse,
* executables that we will use to start and setup the swtpm * executables that we will use to start and setup the swtpm
*/ */
static int static int
virTPMEmulatorInit(void) virTPMEmulatorInit(bool quiet)
{ {
size_t i; size_t i;
@ -293,20 +311,23 @@ virTPMEmulatorInit(void)
path = virFindFileInPath(virTPMBinaryTypeToString(i)); path = virFindFileInPath(virTPMBinaryTypeToString(i));
if (!path) { if (!path) {
virReportSystemError(ENOENT, if (!quiet)
_("Unable to find '%s' binary in $PATH"), virReportSystemError(ENOENT,
virTPMBinaryTypeToString(i)); _("Unable to find '%s' binary in $PATH"),
virTPMBinaryTypeToString(i));
return -1; return -1;
} }
if (!virFileIsExecutable(path)) { if (!virFileIsExecutable(path)) {
virReportError(VIR_ERR_INTERNAL_ERROR, if (!quiet)
_("%s is not an executable"), virReportError(VIR_ERR_INTERNAL_ERROR,
path); _("%s is not an executable"),
path);
return -1; return -1;
} }
if (stat(path, &swtpmBinaries[i].stat) < 0) { if (stat(path, &swtpmBinaries[i].stat) < 0) {
virReportSystemError(errno, if (!quiet)
_("Could not stat %s"), path); virReportSystemError(errno,
_("Could not stat %s"), path);
return -1; return -1;
} }
swtpmBinaries[i].path = g_steal_pointer(&path); swtpmBinaries[i].path = g_steal_pointer(&path);
@ -326,7 +347,7 @@ virTPMBinaryGetCaps(virTPMBinary binary,
virMutexLock(&swtpm_tools_lock); virMutexLock(&swtpm_tools_lock);
if (virTPMEmulatorInit() < 0) if (virTPMEmulatorInit(false) < 0)
goto cleanup; goto cleanup;
if (!swtpmBinaries[binary].caps && if (!swtpmBinaries[binary].caps &&

View File

@ -26,6 +26,8 @@ char *virTPMGetSwtpm(void);
char *virTPMGetSwtpmSetup(void); char *virTPMGetSwtpmSetup(void);
char *virTPMGetSwtpmIoctl(void); char *virTPMGetSwtpmIoctl(void);
bool virTPMHasSwtpm(void);
bool virTPMSwtpmCapsGet(unsigned int cap); bool virTPMSwtpmCapsGet(unsigned int cap);
bool virTPMSwtpmSetupCapsGet(unsigned int cap); bool virTPMSwtpmSetupCapsGet(unsigned int cap);