Skip to content

Commit

Permalink
incusd/device/tpm: Wait for swtpm to be ready
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Dec 2, 2024
1 parent c2f4950 commit 6ab378a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/server/device/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ func (d *tpm) startVM() (*deviceConfig.RunConfig, error) {
},
}

// Delete any leftover socket.
_ = os.Remove(socketPath)

proc, err := subprocess.NewProcess("swtpm", []string{"socket", "--tpm2", "--tpmstate", fmt.Sprintf("dir=%s", tpmDevPath), "--ctrl", fmt.Sprintf("type=unixio,path=swtpm-%s.sock", d.name)}, "", "")
if err != nil {
return nil, err
Expand All @@ -227,6 +230,21 @@ func (d *tpm) startVM() (*deviceConfig.RunConfig, error) {
return nil, fmt.Errorf("Failed to save swtpm state for device %q: %w", d.name, err)
}

// Wait for the socket to be available.
exists := false
for i := 0; i < 20; i++ {
if util.PathExists(socketPath) {
exists = true
continue
}

time.Sleep(100 * time.Millisecond)
}

if !exists {
return nil, fmt.Errorf("swtpm socket didn't appear within 2s")
}

revert.Success()

return &runConf, nil
Expand Down

0 comments on commit 6ab378a

Please sign in to comment.