[libvirt PATCH v2 08/12] util: add a method for checking if swtpm is available

Daniel P. Berrangé berrange at redhat.com
Wed Nov 24 15:44:12 UTC 2021


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

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virtpm.c        | 45 +++++++++++++++++++++++++++++-----------
 src/util/virtpm.h        |  2 ++
 3 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 6b179a61b9..6aa57abad4 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3450,6 +3450,7 @@ virTPMCreateCancelPath;
 virTPMGetSwtpm;
 virTPMGetSwtpmIoctl;
 virTPMGetSwtpmSetup;
+virTPMHasSwtpm;
 virTPMSwtpmCapsGet;
 virTPMSwtpmFeatureTypeFromString;
 virTPMSwtpmSetupCapsGet;
diff --git a/src/util/virtpm.c b/src/util/virtpm.c
index ec51c0efb3..63579b8e69 100644
--- a/src/util/virtpm.c
+++ b/src/util/virtpm.c
@@ -132,7 +132,7 @@ static virTPMBinaryInfo swtpmBinaries[VIR_TPM_BINARY_LAST] = {
     },
 };
 
-static int virTPMEmulatorInit(void);
+static int virTPMEmulatorInit(bool quiet);
 
 static char *
 virTPMBinaryGetPath(virTPMBinary binary)
@@ -141,7 +141,7 @@ virTPMBinaryGetPath(virTPMBinary binary)
 
     virMutexLock(&swtpm_tools_lock);
 
-    if (virTPMEmulatorInit() < 0)
+    if (virTPMEmulatorInit(false) < 0)
         goto cleanup;
 
     s = g_strdup(swtpmBinaries[binary].path);
@@ -169,6 +169,24 @@ virTPMGetSwtpmIoctl(void)
     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
  *
  * 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
  */
 static int
-virTPMEmulatorInit(void)
+virTPMEmulatorInit(bool quiet)
 {
     size_t i;
 
@@ -293,20 +311,23 @@ virTPMEmulatorInit(void)
 
             path = virFindFileInPath(virTPMBinaryTypeToString(i));
             if (!path) {
-                virReportSystemError(ENOENT,
-                                     _("Unable to find '%s' binary in $PATH"),
-                                     virTPMBinaryTypeToString(i));
+                if (!quiet)
+                    virReportSystemError(ENOENT,
+                                         _("Unable to find '%s' binary in $PATH"),
+                                         virTPMBinaryTypeToString(i));
                 return -1;
             }
             if (!virFileIsExecutable(path)) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("%s is not an executable"),
-                               path);
+                if (!quiet)
+                    virReportError(VIR_ERR_INTERNAL_ERROR,
+                                   _("%s is not an executable"),
+                                   path);
                 return -1;
             }
             if (stat(path, &swtpmBinaries[i].stat) < 0) {
-                virReportSystemError(errno,
-                                     _("Could not stat %s"), path);
+                if (!quiet)
+                    virReportSystemError(errno,
+                                         _("Could not stat %s"), path);
                 return -1;
             }
             swtpmBinaries[i].path = g_steal_pointer(&path);
@@ -326,7 +347,7 @@ virTPMBinaryGetCaps(virTPMBinary binary,
 
     virMutexLock(&swtpm_tools_lock);
 
-    if (virTPMEmulatorInit() < 0)
+    if (virTPMEmulatorInit(false) < 0)
         goto cleanup;
 
     if (!swtpmBinaries[binary].caps &&
diff --git a/src/util/virtpm.h b/src/util/virtpm.h
index 4c16332f9b..0a82a03b69 100644
--- a/src/util/virtpm.h
+++ b/src/util/virtpm.h
@@ -26,6 +26,8 @@ char *virTPMGetSwtpm(void);
 char *virTPMGetSwtpmSetup(void);
 char *virTPMGetSwtpmIoctl(void);
 
+bool virTPMHasSwtpm(void);
+
 bool virTPMSwtpmCapsGet(unsigned int cap);
 bool virTPMSwtpmSetupCapsGet(unsigned int cap);
 
-- 
2.33.1




More information about the libvir-list mailing list