Skip to content

Commit

Permalink
platform/qemu: Support creating a swtpm instance for a machine
Browse files Browse the repository at this point in the history
Move swtpm creation from the tpm test to the qemu implementation.
This allows it to be reused for various test cases.

Signed-off-by: Jeremi Piotrowski <[email protected]>
  • Loading branch information
jepio committed Apr 11, 2024
1 parent 75e97fb commit f3334a7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
15 changes: 2 additions & 13 deletions kola/tests/misc/tpm.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package misc

import (
"os"
"time"

"github.com/coreos/go-semver/semver"
"github.com/flatcar/mantle/kola/cluster"
"github.com/flatcar/mantle/kola/register"
"github.com/flatcar/mantle/platform"
"github.com/flatcar/mantle/platform/conf"
"github.com/flatcar/mantle/platform/local"
"github.com/flatcar/mantle/platform/machine/qemu"
"github.com/flatcar/mantle/platform/machine/unprivqemu"
)
Expand Down Expand Up @@ -307,23 +305,14 @@ func init() {
}

func tpmTest(c cluster.TestCluster, userData *conf.UserData, mountpoint string, variant string) {
swtpmDir, err := os.MkdirTemp("", "swtpm-")
if err != nil {
c.Fatalf("mkdir: %v", err)
}
swtpm, err := local.NewSwtpm(swtpmDir)
if err != nil {
c.Fatalf("could not start software TPM emulation: %v", err)
}
defer swtpm.Stop()

options := platform.MachineOptions{
AdditionalDisks: []platform.Disk{
{Size: "520M", DeviceOpts: []string{"serial=secondary"}},
},
SoftwareTPMSocket: swtpm.SocketPath(),
EnableTPM: true,
}
var m platform.Machine
var err error
switch pc := c.Cluster.(type) {
// These cases have to be separated because otherwise the golang compiler doesn't type-check
// the case bodies using the proper subtype of `pc`.
Expand Down
8 changes: 2 additions & 6 deletions platform/local/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"fmt"
"os"
"strings"

"github.com/coreos/pkg/capnslog"
"github.com/flatcar/mantle/system/exec"
Expand Down Expand Up @@ -43,11 +42,8 @@ func (swtpm *SoftwareTPM) Stop() {
if err := swtpm.process.Kill(); err != nil {
plog.Errorf("Error killing swtpm: %v", err)
}
// To be double sure that we do not delete the wrong directory, check that "tpm" occurs in the directory path we delete.
if strings.Contains(swtpm.dir, "tpm") {
plog.Debugf("Delete swtpm temporary directory %v", swtpm.dir)
os.RemoveAll(swtpm.dir)
}
plog.Debugf("Delete swtpm temporary directory %v", swtpm.dir)
os.RemoveAll(swtpm.dir)
}

func (swtpm *SoftwareTPM) SocketPath() string {
Expand Down
17 changes: 17 additions & 0 deletions platform/machine/qemu/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ ExecStartPost=/usr/bin/ln -fs /run/metadata/flatcar /run/metadata/coreos
consolePath: filepath.Join(dir, "console.txt"),
}

var swtpm *local.SoftwareTPM
if options.EnableTPM {
swtpm, err = local.NewSwtpm(filepath.Join(dir, "tpm"))
if err != nil {
return nil, fmt.Errorf("starting swtpm: %v", err)
}
options.SoftwareTPMSocket = swtpm.SocketPath()
defer func() {
if swtpm != nil {
swtpm.Stop()
}
}()
}

qmCmd, extraFiles, err := platform.CreateQEMUCommand(qc.flight.opts.Board, qm.id, qc.flight.opts.BIOSImage, qm.consolePath, confPath, qc.flight.diskImagePath, conf.IsIgnition(), options)
if err != nil {
return nil, err
Expand Down Expand Up @@ -150,6 +164,9 @@ ExecStartPost=/usr/bin/ln -fs /run/metadata/flatcar /run/metadata/coreos
return nil, err
}

// from this point on Destroy() is responsible for cleaning up swtpm
qm.swtpm, swtpm = swtpm, nil

plog.Debugf("qemu PID (manual cleanup needed if --remove=false): %v", qm.qemu.Pid())

if err := platform.StartMachine(qm, qm.journal); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion platform/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type machine struct {
journal *platform.Journal
consolePath string
console string
swtpm *local.SoftwareTPM
}

func (m *machine) ID() string {
Expand Down Expand Up @@ -70,7 +71,9 @@ func (m *machine) Destroy() {
if err := m.qemu.Kill(); err != nil {
plog.Errorf("Error killing instance %v: %v", m.ID(), err)
}

if m.swtpm != nil {
m.swtpm.Stop()
}
m.journal.Destroy()

if buf, err := ioutil.ReadFile(m.consolePath); err == nil {
Expand Down
3 changes: 2 additions & 1 deletion platform/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
type MachineOptions struct {
AdditionalDisks []Disk
ExtraPrimaryDiskSize string
EnableTPM bool
SoftwareTPMSocket string
}

Expand Down Expand Up @@ -348,7 +349,7 @@ func CreateQEMUCommand(board, uuid, biosImage, consolePath, confPath, diskImageP
"-device", "virtio-rng-pci,rng=rng0",
)

if options.SoftwareTPMSocket != "" {
if options.EnableTPM {
var tpm string
switch board {
case "amd64-usr":
Expand Down

0 comments on commit f3334a7

Please sign in to comment.