Add vsock-backed SSH session reminders
Remind users when a VM is still running after hanger vm ssh exits instead of silently dropping them back to the host shell.\n\nAttach a Firecracker vsock device to each VM, persist the host vsock path/CID,\nadd a new guest-side banger-vsock-pingd responder to the runtime bundle and both\nimage-build paths, and expose a vm.ping RPC that the CLI and TUI call after SSH\nreturns. Doctor and start/build preflight now validate the helper plus\n/dev/vhost-vsock so the feature fails early and clearly.\n\nValidated with go mod tidy, bash -n customize.sh, git diff --check, make build,\nand GOCACHE=/tmp/banger-gocache go test ./... outside the sandbox because the\ndaemon tests need real Unix/UDP sockets. Rebuild the image/rootfs used for new\nVMs so the guest ping service is present.
This commit is contained in:
parent
4930d82cb9
commit
08ef706e3f
31 changed files with 912 additions and 75 deletions
|
|
@ -1,17 +1,23 @@
|
|||
package firecracker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
sdk "github.com/firecracker-microvm/firecracker-go-sdk"
|
||||
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
|
||||
sdkvsock "github.com/firecracker-microvm/firecracker-go-sdk/vsock"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"banger/internal/vsockping"
|
||||
)
|
||||
|
||||
type MachineConfig struct {
|
||||
|
|
@ -25,6 +31,8 @@ type MachineConfig struct {
|
|||
KernelArgs string
|
||||
Drives []DriveConfig
|
||||
TapDevice string
|
||||
VSockPath string
|
||||
VSockCID uint32
|
||||
VCPUCount int
|
||||
MemoryMiB int
|
||||
Logger *slog.Logger
|
||||
|
|
@ -132,6 +140,7 @@ func buildConfig(cfg MachineConfig) sdk.Config {
|
|||
HostDevName: cfg.TapDevice,
|
||||
},
|
||||
}},
|
||||
VsockDevices: buildVsockDevices(cfg),
|
||||
MachineCfg: models.MachineConfiguration{
|
||||
VcpuCount: sdk.Int64(int64(cfg.VCPUCount)),
|
||||
MemSizeMib: sdk.Int64(int64(cfg.MemoryMiB)),
|
||||
|
|
@ -141,6 +150,17 @@ func buildConfig(cfg MachineConfig) sdk.Config {
|
|||
}
|
||||
}
|
||||
|
||||
func buildVsockDevices(cfg MachineConfig) []sdk.VsockDevice {
|
||||
if strings.TrimSpace(cfg.VSockPath) == "" || cfg.VSockCID == 0 {
|
||||
return nil
|
||||
}
|
||||
return []sdk.VsockDevice{{
|
||||
ID: "vsock",
|
||||
Path: cfg.VSockPath,
|
||||
CID: cfg.VSockCID,
|
||||
}}
|
||||
}
|
||||
|
||||
func splitDrives(drives []DriveConfig) (DriveConfig, []DriveConfig) {
|
||||
root := DriveConfig{ID: "rootfs"}
|
||||
var extras []DriveConfig
|
||||
|
|
@ -192,6 +212,39 @@ func newLogger(base *slog.Logger) *logrus.Entry {
|
|||
return logrus.NewEntry(logger)
|
||||
}
|
||||
|
||||
func PingVSock(ctx context.Context, logger *slog.Logger, socketPath string) error {
|
||||
conn, err := sdkvsock.DialContext(
|
||||
ctx,
|
||||
socketPath,
|
||||
vsockping.Port,
|
||||
sdkvsock.WithRetryTimeout(3*time.Second),
|
||||
sdkvsock.WithRetryInterval(100*time.Millisecond),
|
||||
sdkvsock.WithLogger(newLogger(logger)),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
_ = conn.SetDeadline(deadline)
|
||||
} else {
|
||||
_ = conn.SetDeadline(time.Now().Add(3 * time.Second))
|
||||
}
|
||||
|
||||
if _, err := io.WriteString(conn, vsockping.RequestLine); err != nil {
|
||||
return err
|
||||
}
|
||||
line, err := bufio.NewReader(conn).ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(line) != strings.TrimSpace(vsockping.ResponseLine) {
|
||||
return fmt.Errorf("unexpected vsock response %q", strings.TrimSpace(line))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type slogHook struct {
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,13 @@ package firecracker
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBuildConfig(t *testing.T) {
|
||||
|
|
@ -21,6 +25,8 @@ func TestBuildConfig(t *testing.T) {
|
|||
{ID: "work", Path: "/var/lib/banger/root.ext4"},
|
||||
},
|
||||
TapDevice: "tap-fc-1",
|
||||
VSockPath: "/tmp/fc.vsock",
|
||||
VSockCID: 10042,
|
||||
VCPUCount: 4,
|
||||
MemoryMiB: 2048,
|
||||
})
|
||||
|
|
@ -46,6 +52,12 @@ func TestBuildConfig(t *testing.T) {
|
|||
if len(cfg.NetworkInterfaces) != 1 {
|
||||
t.Fatalf("interface count = %d, want 1", len(cfg.NetworkInterfaces))
|
||||
}
|
||||
if len(cfg.VsockDevices) != 1 {
|
||||
t.Fatalf("vsock count = %d, want 1", len(cfg.VsockDevices))
|
||||
}
|
||||
if cfg.VsockDevices[0].Path != "/tmp/fc.vsock" || cfg.VsockDevices[0].CID != 10042 {
|
||||
t.Fatalf("unexpected vsock config: %+v", cfg.VsockDevices[0])
|
||||
}
|
||||
if got := cfg.NetworkInterfaces[0].StaticConfiguration.HostDevName; got != "tap-fc-1" {
|
||||
t.Fatalf("host dev name = %q", got)
|
||||
}
|
||||
|
|
@ -115,3 +127,81 @@ func TestSDKLoggerBridgeSuppressesDebugAtInfoLevel(t *testing.T) {
|
|||
t.Fatalf("expected info-level logger to suppress sdk debug chatter, got %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPingVSock(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
socketPath := filepath.Join(dir, "fc.vsock")
|
||||
listener, err := net.Listen("unix", socketPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Listen: %v", err)
|
||||
}
|
||||
defer listener.Close()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
buf := make([]byte, 0, 64)
|
||||
tmp := make([]byte, 64)
|
||||
for {
|
||||
n, err := conn.Read(tmp)
|
||||
if err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
buf = append(buf, tmp[:n]...)
|
||||
if strings.Contains(string(buf), "\n") {
|
||||
break
|
||||
}
|
||||
}
|
||||
if got := string(buf); got != "CONNECT 42070\n" {
|
||||
done <- errUnexpectedString(got)
|
||||
return
|
||||
}
|
||||
if _, err := conn.Write([]byte("OK 55\n")); err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
buf = buf[:0]
|
||||
for {
|
||||
n, err := conn.Read(tmp)
|
||||
if err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
buf = append(buf, tmp[:n]...)
|
||||
if strings.Contains(string(buf), "\n") {
|
||||
break
|
||||
}
|
||||
}
|
||||
if got := string(buf); got != "PING\n" {
|
||||
done <- errUnexpectedString(got)
|
||||
return
|
||||
}
|
||||
_, err = conn.Write([]byte("PONG\n"))
|
||||
done <- err
|
||||
}()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if err := PingVSock(ctx, nil, socketPath); err != nil {
|
||||
t.Fatalf("PingVSock: %v", err)
|
||||
}
|
||||
if err := <-done; err != nil {
|
||||
t.Fatalf("server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type unexpectedStringError string
|
||||
|
||||
func (e unexpectedStringError) Error() string {
|
||||
return "unexpected string: " + string(e)
|
||||
}
|
||||
|
||||
func errUnexpectedString(value string) error {
|
||||
return unexpectedStringError(value)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue