From 6ab378a5e10db168a47cbc29b4a8e6adf8ff2c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Mon, 2 Dec 2024 00:43:30 -0500 Subject: [PATCH] incusd/device/tpm: Wait for swtpm to be ready MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- internal/server/device/tpm.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/server/device/tpm.go b/internal/server/device/tpm.go index 71314e8c99c..1abcf97e9a7 100644 --- a/internal/server/device/tpm.go +++ b/internal/server/device/tpm.go @@ -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 @@ -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