[PATCH v4 5/7] qemu: tpm: Pass --migration option to swtpm if supported and needed

Stefan Berger stefanb at linux.ibm.com
Mon Oct 24 10:28:46 UTC 2022


Pass the --migration option to swtpm if swptm supports it (starting
with v0.8) and if the TPM's state is written on shared storage. If this
is the case apply the 'release-lock-outgoing' parameter with this
option and apply the 'incoming' parameter for incoming migration so that
swtpm releases the file lock on the source side when the state is migrated
and locks the file on the destination side when the state is received.

If a started swtpm instance is running with the necessary options of
migrating with share storage then remember this with a flag in the
virDomainTPMPrivateDef.

Report an error if swtpm does not support the --migration option and an
incoming migration across shared storage is requested.

Signed-off-by: Stefan Berger <stefanb at linux.ibm.com>
---
 src/qemu/qemu_migration.c | 10 ++++++++
 src/qemu/qemu_tpm.c       | 48 +++++++++++++++++++++++++++++++++++++--
 src/qemu/qemu_tpm.h       |  3 +++
 3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 16bf7ac178..2aa0b6e89e 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -2786,6 +2786,7 @@ qemuMigrationSrcBegin(virConnectPtr conn,
     g_autofree char *xml = NULL;
     char *ret = NULL;
     virDomainAsyncJob asyncJob;
+    int rc;
 
     if (cfg->migrateTLSForce &&
         !(flags & VIR_MIGRATE_TUNNELLED) &&
@@ -2795,6 +2796,15 @@ qemuMigrationSrcBegin(virConnectPtr conn,
         goto cleanup;
     }
 
+    rc = qemuTPMHasSharedStorage(driver, vm->def);
+    if (rc < 0)
+        goto cleanup;
+    if (rc == 1 && !qemuTPMCanMigrateSharedStorage(vm->def)) {
+        virReportError(VIR_ERR_NO_SUPPORT, "%s",
+                       _("the running swtpm does not support migration with shared storage"));
+        goto cleanup;
+    }
+
     if (flags & VIR_MIGRATE_POSTCOPY_RESUME) {
         ret = qemuMigrationSrcBeginResumePhase(conn, driver, vm, xmlin,
                                                cookieout, cookieoutlen, flags);
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 79d7a0e671..cffa77cfa3 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -557,6 +557,7 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
     int migpwdfile_fd = -1;
     const unsigned char *secretuuid = NULL;
     bool create_storage = true;
+    bool on_shared_storage;
 
     if (!swtpm)
         return NULL;
@@ -564,8 +565,8 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
     /* Do not create storage and run swtpm_setup on incoming migration over
      * shared storage
      */
-    if (incomingMigration &&
-        virFileIsSharedFS(tpm->data.emulator.storagepath) == 1)
+    on_shared_storage = virFileIsSharedFS(tpm->data.emulator.storagepath) == 1;
+    if (incomingMigration && on_shared_storage)
         create_storage = false;
 
     if (create_storage &&
@@ -643,6 +644,30 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
         virCommandAddArgFormat(cmd, "pwdfd=%d,mode=aes-256-cbc", migpwdfile_fd);
     }
 
+    /* If swtpm supports it and the TPM state is stored on shared storage,
+     * start swtpm with --migration release-lock-outgoing so it can migrate
+     * across shared storage if needed.
+     */
+    QEMU_DOMAIN_TPM_PRIVATE(tpm)->swtpm.can_migrate_shared_storage = false;
+    if (on_shared_storage &&
+        virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_CMDARG_MIGRATION)) {
+
+        virCommandAddArg(cmd, "--migration");
+        virCommandAddArgFormat(cmd, "release-lock-outgoing%s",
+                               incomingMigration ? ",incoming": "");
+        QEMU_DOMAIN_TPM_PRIVATE(tpm)->swtpm.can_migrate_shared_storage = true;
+    } else {
+        /* Report an error if there's an incoming migration across shared
+         * storage and swtpm does not support the --migration option.
+         */
+        if (incomingMigration && on_shared_storage) {
+            virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
+                _("%s (on destination side) does not support the --migration option needed for migration with shared storage"),
+                   swtpm);
+            goto error;
+        }
+    }
+
     return g_steal_pointer(&cmd);
 
  error:
@@ -991,6 +1016,25 @@ qemuTPMHasSharedStorage(virQEMUDriver *driver,
 }
 
 
+bool
+qemuTPMCanMigrateSharedStorage(virDomainDef *def)
+{
+    size_t i;
+
+    for (i = 0; i < def->ntpms; i++) {
+        virDomainTPMDef *tpm = def->tpms[i];
+        switch (tpm->type) {
+        case VIR_DOMAIN_TPM_TYPE_EMULATOR:
+            return QEMU_DOMAIN_TPM_PRIVATE(tpm)->
+                                           swtpm.can_migrate_shared_storage;
+        case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
+        case VIR_DOMAIN_TPM_TYPE_LAST:
+        }
+    }
+    return true;
+}
+
+
 /* ---------------------
  *  Module entry points
  * ---------------------
diff --git a/src/qemu/qemu_tpm.h b/src/qemu/qemu_tpm.h
index 531d93846b..e6e32a0c4a 100644
--- a/src/qemu/qemu_tpm.h
+++ b/src/qemu/qemu_tpm.h
@@ -61,3 +61,6 @@ int qemuTPMHasSharedStorage(virQEMUDriver *driver,
                             virDomainDef *def)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
     G_GNUC_WARN_UNUSED_RESULT;
+
+bool qemuTPMCanMigrateSharedStorage(virDomainDef *def)
+    ATTRIBUTE_NONNULL(1);
-- 
2.37.3



More information about the libvir-list mailing list