Search for tdx in kernel parameters

This commit is contained in:
dorcaslitunya
2025-06-10 11:16:28 +00:00
parent e9e777f438
commit da5277c55a
2 changed files with 16 additions and 15 deletions
+10 -9
View File
@@ -3,7 +3,6 @@
package qemu
import (
"bytes"
"fmt"
"log/slog"
"os"
@@ -209,8 +208,8 @@ func SEVSNPEnabled(cpuinfo, kernelParam string) bool {
return strings.Contains(cpuinfo, "sev_snp") && strings.TrimSpace(kernelParam) == "1"
}
func TDXEnabled(dmesg string) bool {
return strings.Contains(strings.ToLower(dmesg), "module initialized")
func TDXEnabled(cpuinfo, kernelParam string) bool {
return strings.Contains(cpuinfo, "tdx_host_platform") && strings.TrimSpace(kernelParam) == "1"
}
// Checks if SEV is supported and usable by verifying both CPU flags and the /dev/sev device.
@@ -239,13 +238,15 @@ func SEVSNPEnabledOnHost() bool {
}
func TDXEnabledOnHost() bool {
cmd := exec.Command("bash", "-c", "dmesg | grep -i tdx")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
cpuinfo, err := os.ReadFile("/proc/cpuinfo")
if err != nil {
return false
}
return TDXEnabled(out.String())
kernelParam, err := os.ReadFile("/sys/module/kvm_intel/parameters/tdx")
if err != nil {
return false
}
return TDXEnabled(string(cpuinfo), string(kernelParam))
}
+6 -6
View File
@@ -191,15 +191,15 @@ func TestSEVSNPEnabled(t *testing.T) {
}
func TestTDXEnabled(t *testing.T) {
t.Run("dmesg contains module initialized", func(t *testing.T) {
assert.True(t, TDXEnabled("Intel TDX: Module initialized successfully"))
t.Run("cpuinfo and kvm param correct", func(t *testing.T) {
assert.True(t, TDXEnabled("flags: tdx_host_platform abc", "1"))
})
t.Run("dmesg does not contain it", func(t *testing.T) {
assert.False(t, TDXEnabled("some unrelated log"))
t.Run("missing tdx_host_platform in cpuinfo", func(t *testing.T) {
assert.False(t, TDXEnabled("flags: abc", "1"))
})
t.Run("case insensitive check", func(t *testing.T) {
assert.True(t, TDXEnabled("module Initialized"))
t.Run("kernel param not enabled", func(t *testing.T) {
assert.False(t, TDXEnabled("flags: tdx_host_platform", "0"))
})
}