-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
platform/local: Extract swtpm functionality
So that it can be added to kola spawn more easily. Signed-off-by: Jeremi Piotrowski <[email protected]>
- Loading branch information
Showing
2 changed files
with
63 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package local | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/coreos/pkg/capnslog" | ||
"github.com/flatcar/mantle/system/exec" | ||
"github.com/flatcar/mantle/util" | ||
) | ||
|
||
type SoftwareTPM struct { | ||
process *exec.ExecCmd | ||
socketPath string | ||
dir string | ||
} | ||
|
||
func NewSwtpm(dir string) (*SoftwareTPM, error) { | ||
swtpm := &SoftwareTPM{} | ||
|
||
os.Mkdir(dir, 0700) | ||
swtpm.dir = dir | ||
swtpm.socketPath = fmt.Sprintf("%v/sock", swtpm.dir) | ||
|
||
swtpm.process = exec.Command("swtpm", "socket", "--tpmstate", fmt.Sprintf("dir=%v", swtpm.dir), "--ctrl", fmt.Sprintf("type=unixio,path=%v", swtpm.socketPath), "--tpm2") | ||
out, err := swtpm.process.StderrPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
go util.LogFrom(capnslog.INFO, out) | ||
|
||
if err = swtpm.process.Start(); err != nil { | ||
return nil, err | ||
} | ||
|
||
plog.Debugf("swtpm PID: %v", swtpm.process.Pid()) | ||
|
||
return swtpm, nil | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
func (swtpm *SoftwareTPM) SocketPath() string { | ||
return swtpm.socketPath | ||
} |