From 1bd1bc7d21be9920a31f48d2e962dc74274d216f Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 26 Sep 2025 15:31:32 +0200 Subject: vm: use error wrapping to detect ssh connection errors This is a much cleaner logic than string matching. --- vm/qemu/qemu.go | 7 +++---- vm/vmimpl/util.go | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'vm') diff --git a/vm/qemu/qemu.go b/vm/qemu/qemu.go index 6714703fe..55473a419 100644 --- a/vm/qemu/qemu.go +++ b/vm/qemu/qemu.go @@ -7,6 +7,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net" @@ -348,10 +349,8 @@ func (pool *Pool) Create(ctx context.Context, workdir string, index int) (vmimpl if err == nil { return inst, nil } - if strings.Contains(err.Error(), "can't ssh into the instance") { - // If there was a boot error, immediately return it. - // In this case, the string search below also matches against boot time output, - // and e.g. "Device or resource busy" is quite often in there. + if errors.Is(err, vmimpl.ErrCantSSH) { + // It is most likely a boot crash, just return the error as is. return nil, err } // Older qemu prints "could", newer -- "Could". diff --git a/vm/vmimpl/util.go b/vm/vmimpl/util.go index 729e78de6..a0e3ce841 100644 --- a/vm/vmimpl/util.go +++ b/vm/vmimpl/util.go @@ -58,13 +58,15 @@ func WaitForSSH(timeout time.Duration, opts SSHOptions, OS string, stop chan err } if time.Since(startTime) > timeout { return &osutil.VerboseError{ - Err: fmt.Errorf("can't ssh into the instance"), + Err: ErrCantSSH, Output: []byte(err.Error()), } } } } +var ErrCantSSH = fmt.Errorf("can't ssh into the instance") + func SSHArgs(debug bool, sshKey string, port int, systemSSHCfg bool) []string { return sshArgs(debug, sshKey, "-p", port, 0, systemSSHCfg) } -- cgit mrf-deployment