Skip to content

Commit 57f60a3

Browse files
committed
Add LIMA_SSH_OVER_VSOCK_TIMEOUT to configure the timeout for waiting for the SSH server on the VM
Signed-off-by: Norio Nomura <norio.nomura@gmail.com> change `grep -iq` to `grep -i` Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent d2587fb commit 57f60a3

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

hack/test-templates.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,23 +349,23 @@ if [[ -n ${CHECKS["ssh-over-vsock"]} ]]; then
349349
set -x
350350
INFO "Testing LIMA_SSH_OVER_VSOCK=true environment"
351351
limactl stop "${NAME}"
352-
if ! LIMA_SSH_OVER_VSOCK=true limactl start "${NAME}" 2>&1 | grep -iq "started vsock forwarder"; then
352+
if ! LIMA_SSH_OVER_VSOCK=true LIMA_SSH_OVER_VSOCK_TIMEOUT=60 limactl start "${NAME}" 2>&1 | grep -i "started vsock forwarder"; then
353353
set +x
354354
diagnose "${NAME}"
355355
ERROR "LIMA_SSH_OVER_VSOCK=true did not enable vsock forwarder"
356356
exit 1
357357
fi
358358
INFO 'Testing LIMA_SSH_OVER_VSOCK="" environment'
359359
limactl stop "${NAME}"
360-
if ! LIMA_SSH_OVER_VSOCK="" limactl start "${NAME}" 2>&1 | grep -iq "started vsock forwarder"; then
360+
if ! LIMA_SSH_OVER_VSOCK="" LIMA_SSH_OVER_VSOCK_TIMEOUT=60 limactl start "${NAME}" 2>&1 | grep -i "started vsock forwarder"; then
361361
set +x
362362
diagnose "${NAME}"
363363
ERROR "LIMA_SSH_OVER_VSOCK= did not enable vsock forwarder"
364364
exit 1
365365
fi
366366
INFO "Testing LIMA_SSH_OVER_VSOCK=false environment"
367367
limactl stop "${NAME}"
368-
if ! LIMA_SSH_OVER_VSOCK=false limactl start "${NAME}" 2>&1 | grep -iq "skipping detection of SSH server on vsock port"; then
368+
if ! LIMA_SSH_OVER_VSOCK=false LIMA_SSH_OVER_VSOCK_TIMEOUT=60 limactl start "${NAME}" 2>&1 | grep -i "skipping detection of SSH server on vsock port"; then
369369
set +x
370370
diagnose "${NAME}"
371371
ERROR "LIMA_SSH_OVER_VSOCK=false did not disable vsock forwarder"

pkg/networks/usernet/client.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
gvproxyclient "github.com/containers/gvisor-tap-vsock/pkg/client"
1818
"github.com/containers/gvisor-tap-vsock/pkg/types"
19+
"github.com/sirupsen/logrus"
1920

2021
"github.com/lima-vm/lima/v2/pkg/httpclientutil"
2122
"github.com/lima-vm/lima/v2/pkg/limatype"
@@ -131,14 +132,24 @@ func (c *Client) Leases(ctx context.Context) (map[string]string, error) {
131132

132133
// WaitOpeningSSHPort Wait until the guest ssh server is available.
133134
func (c *Client) WaitOpeningSSHPort(ctx context.Context, inst *limatype.Instance) error {
134-
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
135+
timeoutSeconds := 10
136+
if envVar := os.Getenv("LIMA_SSH_OVER_VSOCK_TIMEOUT"); envVar != "" {
137+
b, err := strconv.ParseInt(envVar, 10, 0)
138+
if err != nil {
139+
logrus.WithError(err).Warnf("invalid LIMA_SSH_OVER_VSOCK_TIMEOUT value %q", envVar)
140+
} else {
141+
logrus.Infof("Using LIMA_SSH_OVER_VSOCK_TIMEOUT=%d", b)
142+
timeoutSeconds = int(b)
143+
}
144+
}
145+
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
135146
defer cancel()
136147
macAddress := limayaml.MACAddress(inst.Dir)
137148
ipAddr, err := c.ResolveIPAddress(ctx, macAddress)
138149
if err != nil {
139150
return err
140151
}
141-
u := fmt.Sprintf("%s/extension/wait_port?ip=%s&port=22", c.base, ipAddr)
152+
u := fmt.Sprintf("%s/extension/wait_port?ip=%s&port=22&timeout=%d", c.base, ipAddr, timeoutSeconds)
142153
res, err := httpclientutil.Get(ctx, c.client, u)
143154
if err != nil {
144155
return err

pkg/networks/usernet/gvproxy.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,19 @@ func muxWithExtension(n *virtualnetwork.VirtualNetwork) *http.ServeMux {
256256
return
257257
}
258258
port := uint16(port16)
259-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
260-
defer cancel()
261259
addr := fmt.Sprintf("%s:%d", ip, port)
260+
261+
timeoutSeconds := 10
262+
if timeoutString := r.URL.Query().Get("timeout"); timeoutString != "" {
263+
timeout16, err := strconv.ParseUint(timeoutString, 10, 16)
264+
if err != nil {
265+
http.Error(w, err.Error(), http.StatusBadRequest)
266+
return
267+
}
268+
timeoutSeconds = int(timeout16)
269+
}
270+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds)*time.Second)
271+
defer cancel()
262272
// Wait until the port is available.
263273
for {
264274
conn, err := n.DialContextTCP(ctx, addr)

0 commit comments

Comments
 (0)