Skip to content

Commit 1bd1bc7

Browse files
committed
vm: use error wrapping to detect ssh connection errors
This is a much cleaner logic than string matching.
1 parent 12039a0 commit 1bd1bc7

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

vm/qemu/qemu.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"context"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"net"
@@ -348,10 +349,8 @@ func (pool *Pool) Create(ctx context.Context, workdir string, index int) (vmimpl
348349
if err == nil {
349350
return inst, nil
350351
}
351-
if strings.Contains(err.Error(), "can't ssh into the instance") {
352-
// If there was a boot error, immediately return it.
353-
// In this case, the string search below also matches against boot time output,
354-
// and e.g. "Device or resource busy" is quite often in there.
352+
if errors.Is(err, vmimpl.ErrCantSSH) {
353+
// It is most likely a boot crash, just return the error as is.
355354
return nil, err
356355
}
357356
// Older qemu prints "could", newer -- "Could".

vm/vmimpl/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ func WaitForSSH(timeout time.Duration, opts SSHOptions, OS string, stop chan err
5858
}
5959
if time.Since(startTime) > timeout {
6060
return &osutil.VerboseError{
61-
Err: fmt.Errorf("can't ssh into the instance"),
61+
Err: ErrCantSSH,
6262
Output: []byte(err.Error()),
6363
}
6464
}
6565
}
6666
}
6767

68+
var ErrCantSSH = fmt.Errorf("can't ssh into the instance")
69+
6870
func SSHArgs(debug bool, sshKey string, port int, systemSSHCfg bool) []string {
6971
return sshArgs(debug, sshKey, "-p", port, 0, systemSSHCfg)
7072
}

0 commit comments

Comments
 (0)