[PATCH v3 10/10] qemu: Read the profile name after creation of TPM instance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Get the JSON profile that the swtpm instance was created with from the
output of 'swtpm socket --tpm2 --print-info 0x20 --tpmstate ...'. Get the
name of the profile from the JSON and if it differs from the original
profile name then update the profile name in the current and persistent
emulator descriptions and have the persistent stored with the update.

Signed-off-by: Stefan Berger <stefanb@xxxxxxxxxxxxx>
---
 src/qemu/qemu_extdevice.c |   5 +-
 src/qemu/qemu_tpm.c       | 105 ++++++++++++++++++++++++++++++++++++--
 src/qemu/qemu_tpm.h       |   3 +-
 src/util/virtpm.c         |   1 +
 src/util/virtpm.h         |   1 +
 5 files changed, 109 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c
index dc1bb56237..a6f31f9773 100644
--- a/src/qemu/qemu_extdevice.c
+++ b/src/qemu/qemu_extdevice.c
@@ -175,6 +175,7 @@ qemuExtDevicesStart(virQEMUDriver *driver,
                     virDomainObj *vm,
                     bool incomingMigration)
 {
+    virDomainDef *persistentDef = vm->newDef;
     virDomainDef *def = vm->def;
     size_t i;
 
@@ -189,9 +190,11 @@ qemuExtDevicesStart(virQEMUDriver *driver,
 
     for (i = 0; i < def->ntpms; i++) {
         virDomainTPMDef *tpm = def->tpms[i];
+        virDomainTPMDef *persistentTPMDef = persistentDef->tpms[i];
 
         if (tpm->type == VIR_DOMAIN_TPM_TYPE_EMULATOR &&
-            qemuExtTPMStart(driver, vm, tpm, incomingMigration) < 0)
+            qemuExtTPMStart(driver, vm, tpm, persistentTPMDef,
+                            incomingMigration) < 0)
             return -1;
     }
 
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 99473bba87..cbb6af8314 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -579,15 +579,94 @@ qemuTPMVirCommandSwtpmAddEncryption(virCommand *cmd,
     return 0;
 }
 
+
+/* qemuTPMEmulatorUpdateProfileName:
+ *
+ * @emulator: TPM emulator definition
+ * @persistentTPMDef: TPM definition from the persistent domain definition
+ * @cfg: virQEMUDriverConfig
+ * @saveDef: whether caller should save the persistent domain def
+ */
+static int
+qemuTPMEmulatorUpdateProfileName(virDomainTPMEmulatorDef *emulator,
+                                 virDomainTPMDef *persistentTPMDef,
+                                 const virQEMUDriverConfig *cfg,
+                                 bool *saveDef)
+{
+    g_autoptr(virJSONValue) object = NULL;
+    g_autofree char *stderr_buf = NULL;
+    g_autofree char *stdout_buf = NULL;
+    g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *swtpm = NULL;
+    virJSONValue *active_profile;
+    const char *profile_name;
+    int exitstatus;
+
+    if (emulator->version != VIR_DOMAIN_TPM_VERSION_2_0 ||
+        !virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_CMDARG_PRINT_INFO))
+        return 0;
+
+    swtpm = virTPMGetSwtpm();
+    if (!swtpm)
+        return -1;
+
+    cmd = virCommandNew(swtpm);
+
+    virCommandSetUID(cmd, cfg->swtpm_user); /* should be uid of 'tss' or 'root' */
+    virCommandSetGID(cmd, cfg->swtpm_group);
+
+    virCommandAddArgList(cmd, "socket", "--print-info", "0x20", "--tpm2", NULL);
+
+    virCommandAddArg(cmd, "--tpmstate");
+    virCommandAddArgFormat(cmd, "dir=%s",
+                           emulator->storagepath);
+
+    if (qemuTPMVirCommandSwtpmAddEncryption(cmd, emulator, swtpm) < 0)
+        return -1;
+
+    virCommandClearCaps(cmd);
+
+    virCommandSetOutputBuffer(cmd, &stdout_buf);
+    virCommandSetErrorBuffer(cmd, &stderr_buf);
+
+    if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Could not run '%1$s --print-info'. exitstatus: %2$d; stderr: %3$s\n"),
+                          swtpm, exitstatus, stderr_buf);
+        return -1;
+    }
+
+    if (!(object = virJSONValueFromString(stdout_buf)))
+        return -1;
+
+    if (!(active_profile = virJSONValueObjectGetObject(object, "ActiveProfile")))
+        return -1;
+
+    profile_name = g_strdup(virJSONValueObjectGetString(active_profile, "Name"));
+    if (STRNEQ_NULLABLE(emulator->profile_name, profile_name)) {
+        g_free(emulator->profile_name);
+        emulator->profile_name = g_strdup(profile_name);
+    }
+    if (STRNEQ_NULLABLE(persistentTPMDef->data.emulator.profile_name, profile_name)) {
+        *saveDef = true;
+        g_free(persistentTPMDef->data.emulator.profile_name);
+        persistentTPMDef->data.emulator.profile_name = g_strdup(profile_name);
+    }
+
+    return 0;
+}
+
 /*
  * qemuTPMEmulatorBuildCommand:
  *
  * @tpm: TPM definition
+ * @persistentTPMDef: TPM definition from the persistent domain definition
  * @vmname: The name of the VM
  * @vmuuid: The UUID of the VM
  * @privileged: whether we are running in privileged mode
  * @cfg: virQEMUDriverConfig
  * @incomingMigration: whether we have an incoming migration
+ * @saveDef: whether caller should save the persistent domain def
  *
  * Create the virCommand use for starting the emulator
  * Do some initializations on the way, such as creation of storage
@@ -595,11 +674,13 @@ qemuTPMVirCommandSwtpmAddEncryption(virCommand *cmd,
  */
 static virCommand *
 qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
+                            virDomainTPMDef *persistentTPMDef,
                             const char *vmname,
                             const unsigned char *vmuuid,
                             bool privileged,
                             const virQEMUDriverConfig *cfg,
-                            bool incomingMigration)
+                            bool incomingMigration,
+                            bool *saveDef)
 {
     g_autoptr(virCommand) cmd = NULL;
     bool created = false;
@@ -633,6 +714,11 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
                                 incomingMigration) < 0)
         goto error;
 
+    if (created && !incomingMigration &&
+        qemuTPMEmulatorUpdateProfileName(&tpm->data.emulator, persistentTPMDef,
+                                         cfg, saveDef) < 0)
+        goto error;
+
     if (!incomingMigration &&
         qemuTPMEmulatorReconfigure(&tpm->data.emulator, cfg, secretuuid) < 0)
         goto error;
@@ -934,6 +1020,7 @@ qemuExtTPMEmulatorSetupCgroup(const char *swtpmStateDir,
  * @driver: QEMU driver
  * @vm: the domain object
  * @tpm: TPM definition
+ * @persistentTPMDef: TPM definition from persistent domain definition
  * @shortName: short and unique name of the domain
  * @incomingMigration: whether we have an incoming migration
  *
@@ -946,6 +1033,7 @@ qemuTPMEmulatorStart(virQEMUDriver *driver,
                      virDomainObj *vm,
                      const char *shortName,
                      virDomainTPMDef *tpm,
+                     virDomainTPMDef *persistentTPMDef,
                      bool incomingMigration)
 {
     g_autoptr(virCommand) cmd = NULL;
@@ -954,6 +1042,7 @@ qemuTPMEmulatorStart(virQEMUDriver *driver,
     g_autofree char *pidfile = NULL;
     virTimeBackOffVar timebackoff;
     const unsigned long long timeout = 1000; /* ms */
+    bool saveDef = false;
     pid_t pid = -1;
     bool lockMetadataException = false;
 
@@ -962,12 +1051,18 @@ qemuTPMEmulatorStart(virQEMUDriver *driver,
     /* stop any left-over TPM emulator for this VM */
     qemuTPMEmulatorStop(cfg->swtpmStateDir, shortName);
 
-    if (!(cmd = qemuTPMEmulatorBuildCommand(tpm, vm->def->name, vm->def->uuid,
+    if (!(cmd = qemuTPMEmulatorBuildCommand(tpm, persistentTPMDef,
+                                            vm->def->name, vm->def->uuid,
                                             driver->privileged,
                                             cfg,
-                                            incomingMigration)))
+                                            incomingMigration,
+                                            &saveDef)))
         return -1;
 
+    if (saveDef &&
+        virDomainDefSave(vm->newDef, driver->xmlopt, cfg->configDir) < 0)
+        goto error;
+
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "TPM Emulator") < 0)
         return -1;
 
@@ -1151,6 +1246,7 @@ int
 qemuExtTPMStart(virQEMUDriver *driver,
                 virDomainObj *vm,
                 virDomainTPMDef *tpm,
+                virDomainTPMDef *persistentTPMDef,
                 bool incomingMigration)
 {
     g_autofree char *shortName = virDomainDefGetShortName(vm->def);
@@ -1158,7 +1254,8 @@ qemuExtTPMStart(virQEMUDriver *driver,
     if (!shortName)
         return -1;
 
-    return qemuTPMEmulatorStart(driver, vm, shortName, tpm, incomingMigration);
+    return qemuTPMEmulatorStart(driver, vm, shortName, tpm, persistentTPMDef,
+                                incomingMigration);
 }
 
 
diff --git a/src/qemu/qemu_tpm.h b/src/qemu/qemu_tpm.h
index 3071dc3f71..7096060a2a 100644
--- a/src/qemu/qemu_tpm.h
+++ b/src/qemu/qemu_tpm.h
@@ -44,9 +44,10 @@ void qemuExtTPMCleanupHost(virQEMUDriver *driver,
 int qemuExtTPMStart(virQEMUDriver *driver,
                     virDomainObj *vm,
                     virDomainTPMDef *def,
+                    virDomainTPMDef *persistentDefTPM,
                     bool incomingMigration)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
-    ATTRIBUTE_NONNULL(3)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4)
     G_GNUC_WARN_UNUSED_RESULT;
 
 void qemuExtTPMStop(virQEMUDriver *driver,
diff --git a/src/util/virtpm.c b/src/util/virtpm.c
index d991657696..d639271b4f 100644
--- a/src/util/virtpm.c
+++ b/src/util/virtpm.c
@@ -40,6 +40,7 @@ VIR_ENUM_IMPL(virTPMSwtpmFeature,
               VIR_TPM_SWTPM_FEATURE_LAST,
               "cmdarg-pwd-fd",
               "cmdarg-migration",
+              "cmdarg-print-info",
 );
 
 VIR_ENUM_IMPL(virTPMSwtpmSetupFeature,
diff --git a/src/util/virtpm.h b/src/util/virtpm.h
index 18c2877c03..a4ed9a3f95 100644
--- a/src/util/virtpm.h
+++ b/src/util/virtpm.h
@@ -31,6 +31,7 @@ bool virTPMHasSwtpm(void);
 typedef enum {
     VIR_TPM_SWTPM_FEATURE_CMDARG_PWD_FD,
     VIR_TPM_SWTPM_FEATURE_CMDARG_MIGRATION,
+    VIR_TPM_SWTPM_FEATURE_CMDARG_PRINT_INFO,
 
     VIR_TPM_SWTPM_FEATURE_LAST
 } virTPMSwtpmFeature;
-- 
2.47.0



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux