<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 8, 2021 at 5:56 PM Stefan Berger <<a href="mailto:stefanb@linux.ibm.com">stefanb@linux.ibm.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Using swtpm v0.7.0 we can run swtpm_setup to create default config files<br>
for swtpm_setup and swtpm-localca in session mode. Now a user can start<br>
a VM with an attached TPM without having to run this program on the<br>
command line before. This program needs to run once.<br>
<br>
This patch addresses the issue raised in<br>
<a href="https://bugzilla.redhat.com/show_bug.cgi?id=2010649" rel="noreferrer" target="_blank">https://bugzilla.redhat.com/show_bug.cgi?id=2010649</a><br>
<br>
Signed-off-by: Stefan Berger <<a href="mailto:stefanb@linux.ibm.com" target="_blank">stefanb@linux.ibm.com</a>><br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
v2:<br>
- fixed return code if swtpm_setup doesn't support the option<br>
---<br>
src/qemu/qemu_tpm.c | 43 +++++++++++++++++++++++++++++++++++++++++++<br>
src/util/virtpm.c | 1 +<br>
src/util/virtpm.h | 1 +<br>
3 files changed, 45 insertions(+)<br>
<br>
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c<br>
index 100481503c..bf6c8e5ad5 100644<br>
--- a/src/qemu/qemu_tpm.c<br>
+++ b/src/qemu/qemu_tpm.c<br>
@@ -385,6 +385,46 @@ qemuTPMSetupEncryption(const unsigned char *secretuuid,<br>
return virCommandSetSendBuffer(cmd, g_steal_pointer(&secret), secret_len);<br>
}<br>
<br>
+<br>
+/*<br>
+ * qemuTPMCreateConfigFiles: run swtpm_setup --create-config-files skip-if-exist<br>
+ *<br>
+ * @logfile: The file to write the log into; it must be writable<br>
+ * for the user given by userid or 'tss'<br>
+ */<br>
+static int<br>
+qemuTPMCreateConfigFiles(const char *logfile)<br>
+{<br>
+ g_autofree char *swtpm_setup = virTPMGetSwtpmSetup();<br>
+ g_autoptr(virCommand) cmd = NULL;<br>
+ int exitstatus;<br>
+<br>
+ if (!swtpm_setup)<br>
+ return -1;<br>
+<br>
+ if (!virTPMSwtpmSetupCapsGet(<br>
+ VIR_TPM_SWTPM_SETUP_FEATURE_CMDARG_CREATE_CONFIG_FILES))<br>
+ return 0;<br>
+<br>
+ cmd = virCommandNew(swtpm_setup);<br>
+ if (!cmd)<br>
+ return -1;<br>
+<br>
+ virCommandAddArgList(cmd, "--create-config-files", "skip-if-exist", NULL);<br>
+ virCommandClearCaps(cmd);<br>
+<br>
+ if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus != 0) {<br>
+ virReportError(VIR_ERR_INTERNAL_ERROR,<br>
+ _("Could not run '%s' to create config files. exitstatus: %d; "<br>
+ "Check error log '%s' for details."),<br>
+ swtpm_setup, exitstatus, logfile);<br></blockquote><div><br></div><div>logfile isn't used, it seems<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+ return -1;<br>
+ }<br>
+<br>
+ return 0;<br>
+}<br>
+<br>
+<br>
/*<br>
* qemuTPMEmulatorRunSetup<br>
*<br>
@@ -432,6 +472,9 @@ qemuTPMEmulatorRunSetup(const char *storagepath,<br>
"this requires privileged mode for a "<br>
"TPM 1.2\n"), 0600);<br>
<br>
+ if (!privileged && qemuTPMCreateConfigFiles(logfile))<br>
+ return -1;<br>
+<br>
cmd = virCommandNew(swtpm_setup);<br>
if (!cmd)<br>
return -1;<br>
diff --git a/src/util/virtpm.c b/src/util/virtpm.c<br>
index 1a567139b4..0f50de866c 100644<br>
--- a/src/util/virtpm.c<br>
+++ b/src/util/virtpm.c<br>
@@ -45,6 +45,7 @@ VIR_ENUM_IMPL(virTPMSwtpmFeature,<br>
VIR_ENUM_IMPL(virTPMSwtpmSetupFeature,<br>
VIR_TPM_SWTPM_SETUP_FEATURE_LAST,<br>
"cmdarg-pwdfile-fd",<br>
+ "cmdarg-create-config-files",<br>
);<br>
<br>
/**<br>
diff --git a/src/util/virtpm.h b/src/util/virtpm.h<br>
index d021a083b4..3bb03b3b33 100644<br>
--- a/src/util/virtpm.h<br>
+++ b/src/util/virtpm.h<br>
@@ -38,6 +38,7 @@ typedef enum {<br>
<br>
typedef enum {<br>
VIR_TPM_SWTPM_SETUP_FEATURE_CMDARG_PWDFILE_FD,<br>
+ VIR_TPM_SWTPM_SETUP_FEATURE_CMDARG_CREATE_CONFIG_FILES,<br>
<br>
VIR_TPM_SWTPM_SETUP_FEATURE_LAST<br>
} virTPMSwtpmSetupFeature;<br>
-- <br>
2.31.1<br>
<br></blockquote><div><br></div><div>lgtm otherwise <br></div></div></div>