Files
Reims-Go/internal/qmp/watcher.go
T
NwflowerandClaude Opus 5.5 2f43182f0c feat: offline installer downloads, task progress events, Win10 cold reboot
Offline installers (roadmap M1.3):
- internal/installer drives `reims-util --json offline-installer` and parses
  its JSON event stream (upstream reims-vgpu 3731afd, reims_util.rs:423-509)
- downloads hold installers/.lock like reims-ctl, so the two never write the
  same file; checked against the real reims-ctl (TestLockExcludesReimsCtl)
- pause = stop the process, rerun resumes; deletes go through
  `reims-ctl downloads delete`, which refuses files in use or downloading
- CLI: reims-go installer list | download | cached | delete
- Wails bindings: ListOfflineReleases, DownloadInstaller, ListInstallers,
  DeleteInstaller

Task events (ui-redesign B1):
- tasks.go tracks long-running work and emits "reims:task" (throttled to
  200 ms per task); VM creation turns reims-ctl's progress lines into task
  progress and logs each step once
- the status bar shows the current task with a progress bar, details in a
  tooltip and a pause button; the frontend regex merging is removed

Win10 cold reboot workaround:
- host probes whether WinHvPlatform.dll lacks WHvResetPartition (Win10 19045)
- internal/qmp watches a dedicated QMP channel for guest resets; with
  -no-reboot the app restarts the VM in a fresh partition
- architecture.md documents download routing and the workaround

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 03:35:03 -07:00

134 lines
3.3 KiB
Go

package qmp
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
"strings"
"time"
)
type qmpEvent struct {
Event string `json:"event"`
Data struct {
Guest bool `json:"guest"`
Reason string `json:"reason"`
} `json:"data"`
}
// AllocateFreePort picks an available localhost TCP port for an independent QMP channel.
func AllocateFreePort() (int, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, fmt.Errorf("allocate localhost QMP port: %w", err)
}
defer ln.Close()
addr, ok := ln.Addr().(*net.TCPAddr)
if !ok || addr.Port == 0 {
return 0, fmt.Errorf("unexpected listener address %v", ln.Addr())
}
return addr.Port, nil
}
// MergeWin10RebootArgs idempotently merges `-no-reboot` and an independent
// `-qmp tcp:127.0.0.1:<port>,server=on,wait=off` into existing extra_args while
// preserving any user-specified QEMU arguments.
func MergeWin10RebootArgs(existing string, qmpPort int) string {
tokens := strings.Fields(existing)
cleaned := make([]string, 0, len(tokens)+3)
for i := 0; i < len(tokens); i++ {
tok := tokens[i]
if tok == "-no-reboot" {
continue
}
if tok == "-qmp" && i+1 < len(tokens) && isManagedQMPTarget(tokens[i+1]) {
i++
continue
}
cleaned = append(cleaned, tok)
}
cleaned = append(cleaned, "-no-reboot", "-qmp", fmt.Sprintf("tcp:127.0.0.1:%d,server=on,wait=off", qmpPort))
return strings.Join(cleaned, " ")
}
func isManagedQMPTarget(arg string) bool {
arg = strings.Trim(arg, "'\"")
return strings.HasPrefix(arg, "tcp:127.0.0.1:") && strings.HasSuffix(arg, ",server=on,wait=off")
}
// WatchForGuestReset connects to QEMU's secondary QMP TCP endpoint, negotiates
// capabilities, and waits for a SHUTDOWN event. It returns true iff the VM exited
// because the guest requested a reset (`reason == "guest-reset"` under `-no-reboot`).
func WatchForGuestReset(ctx context.Context, addr string) (bool, error) {
conn, err := dialWithRetry(ctx, addr, 20*time.Second)
if err != nil {
return false, err
}
defer conn.Close()
go func() {
<-ctx.Done()
_ = conn.Close()
}()
reader := bufio.NewScanner(conn)
// Increase buffer in case a QMP greeting or event is large.
reader.Buffer(make([]byte, 0, 64*1024), 512*1024)
// Read QMP greeting.
if !reader.Scan() {
if ctx.Err() != nil {
return false, ctx.Err()
}
return false, reader.Err()
}
if _, err := conn.Write([]byte("{\"execute\":\"qmp_capabilities\"}\n")); err != nil {
return false, err
}
for reader.Scan() {
line := strings.TrimSpace(reader.Text())
if line == "" {
continue
}
var ev qmpEvent
if err := json.Unmarshal([]byte(line), &ev); err != nil {
continue
}
if ev.Event == "SHUTDOWN" {
return ev.Data.Reason == "guest-reset", nil
}
}
if ctx.Err() != nil {
return false, ctx.Err()
}
return false, reader.Err()
}
func dialWithRetry(ctx context.Context, addr string, timeout time.Duration) (net.Conn, error) {
deadline := time.Now().Add(timeout)
var lastErr error
for {
if ctx.Err() != nil {
return nil, ctx.Err()
}
d := net.Dialer{Timeout: 500 * time.Millisecond}
conn, err := d.DialContext(ctx, "tcp", addr)
if err == nil {
return conn, nil
}
lastErr = err
if time.Now().After(deadline) {
return nil, fmt.Errorf("connect QMP %s: %w", addr, lastErr)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(250 * time.Millisecond):
}
}
}