On Sat, Sep 19, 2026 at 13:53:15 +0530, Arun Menon via Devel wrote:
From: Arun Menon <armenon@redhat.com>
This commit adds the backup and fsync options while constructing the swtpm command. Both of these attributes only work with the directory backend.
Which means they ought to be rejected from the others; your patch doesn't do that. Possibly also for the passthrough backend; you need to check if that is the case and potentially fix it later.
Refactor qemuTPMVirCommandSwtpmAddTPMState() function, use virBuffer API to construct the qemu command and use virReportError to report configuration errors.
Mixing refactors and code additions is not a good idea. Next time refactor first and add new functionality in a separate patch. This time I'll split it for you.
Signed-off-by: Arun Menon <armenon@redhat.com> --- src/qemu/qemu_tpm.c | 55 ++++++++++++++++++++++++++++++++++----------- src/util/virtpm.c | 2 ++ src/util/virtpm.h | 2 ++ 3 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 34e11cc02f..2421c0ddba 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -669,34 +669,61 @@ qemuTPMVirCommandSwtpmAddEncryption(virCommand *cmd, return 0; }
-static void +static int qemuTPMVirCommandSwtpmAddTPMState(virCommand *cmd, const virDomainTPMEmulatorDef *emulator, const virDomainTPMDef *tpmDef, const virQEMUDriverConfig *cfg) { - const char *lock = ",lock"; + g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; + g_autofree char *swtpm = NULL; + bool hasLock = virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_TPMSTATE_OPT_LOCK);
- if (!virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_TPMSTATE_OPT_LOCK)) { - if (qemuTPMHasSharedStorage(cfg, tpmDef)) - VIR_WARN("This swtpm version doesn't support explicit locking"); + swtpm = virTPMGetSwtpm();
- lock = ""; - } + if (!hasLock && qemuTPMHasSharedStorage(cfg, tpmDef)) + VIR_WARN("This swtpm version doesn't support explicit locking");
Pre-existing, but VIR_WARN ought not to be used in code. It just logs output to syslog. users will not read it. I suggest removing this completely or turning it into a hard error if that's appropriate.
virCommandAddArg(cmd, "--tpmstate"); + switch (emulator->source_type) { case VIR_DOMAIN_TPM_SOURCE_TYPE_FILE: - virCommandAddArgFormat(cmd, "backend-uri=file://%s%s", - emulator->source_path, lock); + virBufferAsprintf(&buf, "backend-uri=file://%s", emulator->source_path); + if (hasLock) + virBufferAddLit(&buf, ",lock");
This ought to reject the unsupported parameters.
break; + case VIR_DOMAIN_TPM_SOURCE_TYPE_DIR: case VIR_DOMAIN_TPM_SOURCE_TYPE_DEFAULT: case VIR_DOMAIN_TPM_SOURCE_TYPE_LAST: - virCommandAddArgFormat(cmd, "dir=%s,mode=0600%s", - emulator->source_path, lock); + virBufferAsprintf(&buf, "dir=%s,mode=0600", emulator->source_path); + if (hasLock) + virBufferAddLit(&buf, ",lock"); + + if (emulator->backup == VIR_TRISTATE_BOOL_YES) { + if (!virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_TPMSTATE_OPT_BACKUP)) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, + _("%1$s does not support backup feature"),
Our coding style guidelines for error messages suggest that both the path to the swtpm as well as 'backup' ought to be surrouded by quotes.
+ swtpm); + return -1; + } + virBufferAddLit(&buf, ",backup"); + } + + if (emulator->fsync == VIR_TRISTATE_BOOL_YES) { + if (!virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_TPMSTATE_OPT_FSYNC)) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, + _("%1$s does not support fsync feature"), + swtpm); + return -1; + } + virBufferAddLit(&buf, ",fsync"); + } break; } + + virCommandAddArgBuffer(cmd, &buf); + return 0; }
I'll add what I've pointed out and split this commit; Reviewed-by: Peter Krempa <pkrempa@redhat.com> Please have a look whether the passhtrough TPM mode also needs to reject the arguments similarly to the fixed version