Files
Reims-Go/internal/qmp/client.go
T
Nwflower 17497aee73 feat: add offline helper disk, recovery mode switch, and QMP typing
- Pure-Go HFS+ offline helper disk (REIMSHELP) bypassing Recovery's missing pkgutil & read-only installer target, with automatic blank-disk APFS formatting and non-blank safety confirmation
- Recovery mode UX: rename Finish install to Exit recovery mode, add Enter recovery mode via OpenCore boot picker (--boot-selection), show Attach offline image on VM page with ~1 GB Apple online recovery fallback download
- Pre-apply WHPX kernel-irqchip=off when nested-virt probe fails and filter benign TSC deadline timer warnings
- QMP keyboard typing (type clipboard / one-click Recovery command) and offline-install network toggle
2026-11-28 07:53:40 -07:00

86 lines
2.2 KiB
Go

package qmp
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
"time"
)
// Conn is a request/response QMP session. QEMU's QMP chardev serves one
// client at a time, so callers keep a Conn only as long as one job takes.
type Conn struct {
nc net.Conn
scanner *bufio.Scanner
}
// Dial connects to a QMP server ("unix" or "tcp") and negotiates capabilities.
func Dial(ctx context.Context, network, addr string) (*Conn, error) {
d := net.Dialer{Timeout: 3 * time.Second}
nc, err := d.DialContext(ctx, network, addr)
if err != nil {
return nil, fmt.Errorf("connect QMP %s: %w", addr, err)
}
c := &Conn{nc: nc, scanner: bufio.NewScanner(nc)}
c.scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
_ = nc.SetDeadline(time.Now().Add(5 * time.Second))
if !c.scanner.Scan() { // greeting
nc.Close()
if err := c.scanner.Err(); err != nil {
return nil, fmt.Errorf("QMP greeting: %w", err)
}
return nil, fmt.Errorf("QMP greeting: connection closed")
}
if err := c.Execute("qmp_capabilities", nil); err != nil {
nc.Close()
return nil, err
}
return c, nil
}
func (c *Conn) Close() error { return c.nc.Close() }
type qmpReply struct {
Return json.RawMessage `json:"return"`
Error *struct {
Class string `json:"class"`
Desc string `json:"desc"`
} `json:"error"`
Event string `json:"event"`
}
// Execute runs one command and waits for its reply, skipping async events.
func (c *Conn) Execute(command string, args any) error {
req := map[string]any{"execute": command}
if args != nil {
req["arguments"] = args
}
b, err := json.Marshal(req)
if err != nil {
return err
}
_ = c.nc.SetDeadline(time.Now().Add(5 * time.Second))
if _, err := c.nc.Write(append(b, '\n')); err != nil {
return fmt.Errorf("QMP %s: %w", command, err)
}
for c.scanner.Scan() {
var r qmpReply
if err := json.Unmarshal(c.scanner.Bytes(), &r); err != nil || r.Event != "" {
continue
}
if r.Error != nil {
return fmt.Errorf("QMP %s: %s: %s", command, r.Error.Class, r.Error.Desc)
}
if r.Return != nil {
return nil
}
}
if err := c.scanner.Err(); err != nil {
return fmt.Errorf("QMP %s: %w", command, err)
}
return fmt.Errorf("QMP %s: connection closed", command)
}