Skip to content

Commit cca33d9

Browse files
committed
pkg/driver/qemu: Wait for SSH to be ready in AdditionalSetupForSSH()
Signed-off-by: Norio Nomura <[email protected]>
1 parent cc0b29b commit cca33d9

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

pkg/driver/qemu/qemu_driver.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ import (
3131
"github.com/lima-vm/lima/v2/pkg/driver/qemu/entitlementutil"
3232
"github.com/lima-vm/lima/v2/pkg/executil"
3333
"github.com/lima-vm/lima/v2/pkg/limatype"
34+
"github.com/lima-vm/lima/v2/pkg/limatype/dirnames"
3435
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
3536
"github.com/lima-vm/lima/v2/pkg/limayaml"
3637
"github.com/lima-vm/lima/v2/pkg/networks/usernet"
3738
"github.com/lima-vm/lima/v2/pkg/osutil"
3839
"github.com/lima-vm/lima/v2/pkg/ptr"
3940
"github.com/lima-vm/lima/v2/pkg/reflectutil"
41+
"github.com/lima-vm/lima/v2/pkg/sshutil"
4042
"github.com/lima-vm/lima/v2/pkg/version/versionutil"
4143
)
4244

@@ -721,6 +723,23 @@ func (l *LimaQemuDriver) ForwardGuestAgent() bool {
721723
return l.vSockPort == 0 && l.virtioPort == ""
722724
}
723725

724-
func (l *LimaQemuDriver) AdditionalSetupForSSH(_ context.Context) error {
726+
func (l *LimaQemuDriver) AdditionalSetupForSSH(ctx context.Context) error {
727+
// Ensure that the QEMU instance is ready to accept SSH connections.
728+
time.Sleep(10 * time.Second)
729+
// Wait until the port is available.
730+
addr := net.JoinHostPort("127.0.0.1", fmt.Sprintf("%d", l.SSHLocalPort))
731+
dialContext := func(ctx context.Context) (net.Conn, error) {
732+
dialer := net.Dialer{Timeout: 1 * time.Second}
733+
return dialer.DialContext(ctx, "tcp", addr)
734+
}
735+
user := *l.Instance.Config.User.Name
736+
configDir, err := dirnames.LimaConfigDir()
737+
if err != nil {
738+
return err
739+
}
740+
privateKeyPath := filepath.Join(configDir, filenames.UserPrivateKey)
741+
if err := sshutil.WaitSSHReady(ctx, dialContext, addr, user, privateKeyPath, 600); err != nil {
742+
return err
743+
}
725744
return nil
726745
}

0 commit comments

Comments
 (0)