Backend - Per-VM CPU affinity (vmstart/affinity.go, affinity_flow.go): modes auto / all / P-cores / non-LP-E / custom, saved in the VM folder, applied to the QEMU process on start (GUI and CLI) and live via SetVMAffinity; copied on clone. Start summary logs the resolved mapping. Host report gains CPU topology helpers (mask <-> CPU list). - Offline qcow2 restore points via qemu-img (driver/reims/snapshot.go, snapshot_flow.go): list / create / restore / delete. - SSH helpers (ssh_flow.go): remembered user, open a terminal, write an ~/.ssh/config entry, open VS Code Remote-SSH. - FocusVMWindow brings the QEMU window forward (restoring it if minimized); CopyVMScreenshot copies a full-resolution frame to the clipboard. - DeliverPathsToGuest copies dropped host files into the shared folder; main.go enables Wails file drop on elements marked --wails-drop-target. - VM note (.reims-go-note) and a storage breakdown (extras_flow.go). Frontend - VM page: running state's "Show Window" and the screen preview use FocusVMWindow; camera button and More-menu item copy a screenshot; editable note under the name; SSH row shows the saved user with Terminal / config / VS Code actions; restore-point list; install guide steps can be ticked off; drop files onto the page to send them. - Sidebar right-click menu per VM (start, show window, screenshot, stop, restart, settings, folders, clone, delete); double-click and Ctrl+Enter still run the main action. - AffinityDialog opened from the VM menu and VM settings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
// startHandoffTimeout bounds how long `vm start` waits for the window to
|
|
// report back. Preparation can rebuild the OpenCore image before QEMU starts.
|
|
const startHandoffTimeout = 5 * time.Minute
|
|
|
|
// startVM checks the VM can start, then hands it to the desktop window: the
|
|
// one already running, or a new one in the tray (`--background`). The window
|
|
// runs the GUI's StartVM, so a VM started from the command line gets Guest
|
|
// Tools and the Win10 cold-reboot watcher too; both need a process that stays
|
|
// up after this command exits.
|
|
//
|
|
// viaWindow is false when --data-root points elsewhere than the window's
|
|
// data root could; the VM then starts here, without those two.
|
|
func startVM(ctx context.Context, cfg *config.Config, prep *vmstart.Prep, name string, viaWindow bool) error {
|
|
rep := host.Inspect(cfg.DataRoot)
|
|
d, err := prep.Client.Show(ctx, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.State == "running" {
|
|
// Preparation rewrites extra-args and the OpenCore image; never under a live VM.
|
|
return fmt.Errorf("%s is already running", name)
|
|
}
|
|
if err := prep.CheckMemory(d.Memory, rep); err != nil {
|
|
return err
|
|
}
|
|
if viaWindow && windowHandoffWorks {
|
|
return startInWindow(ctx, cfg, name)
|
|
}
|
|
|
|
prep.Prepare(ctx, name, rep, &d)
|
|
if err := prep.Client.StartWithCompat(ctx, name, os.Stdout, os.Stderr, func(msg string) {
|
|
fmt.Fprintln(os.Stderr, msg)
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
vms, err := prep.Client.List(ctx)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
for _, vm := range vms {
|
|
if vm.Name == name && vm.State == "running" && vm.PID != nil && *vm.PID > 0 {
|
|
vmDir := vm.Dir
|
|
if vmDir == "" {
|
|
vmDir = d.Directory
|
|
}
|
|
if line, _ := vmstart.ApplyVMAffinity(cfg.Locale, rep, vmDir, cfg.AutoPinPCores, *vm.PID, d.CPUs, false); line != "" {
|
|
fmt.Fprintln(os.Stderr, line)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func startInWindow(ctx context.Context, cfg *config.Config, name string) error {
|
|
zh := cfg.Locale == i18n.ZhCN
|
|
if err := os.MkdirAll(cfg.TempDir(), 0o755); err != nil {
|
|
return err
|
|
}
|
|
result := filepath.Join(cfg.TempDir(), fmt.Sprintf("cli-start-%d.json", time.Now().UnixNano()))
|
|
w := WindowArgs{StartVM: name, StartResult: result}
|
|
if hwnd := findWindow(); hwnd != 0 {
|
|
if zh {
|
|
fmt.Fprintf(os.Stderr, "交给正在运行的 Reims-Go 启动 %s…\n", name)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Handing %s to the running Reims-Go...\n", name)
|
|
}
|
|
if err := sendToWindow(hwnd, w.Args()); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
w.Background = true
|
|
if zh {
|
|
fmt.Fprintf(os.Stderr, "在后台拉起 Reims-Go(托盘)来启动 %s…\n", name)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Starting Reims-Go in the tray to run %s...\n", name)
|
|
}
|
|
if err := spawnWindow(w.Args()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
|
|
defer stop()
|
|
res, err := waitStartResult(ctx, result, startHandoffTimeout)
|
|
switch {
|
|
case errors.Is(err, context.Canceled):
|
|
if zh {
|
|
return fmt.Errorf("已停止等待;%s 仍由 Reims-Go 继续启动", name)
|
|
}
|
|
return fmt.Errorf("stopped waiting; Reims-Go keeps starting %s", name)
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
if zh {
|
|
return fmt.Errorf("Reims-Go 在 %v 内没有回报 %s 的启动结果;请看界面的日志面板(若正在运行的是旧版 Reims-Go,请先退出它)", startHandoffTimeout, name)
|
|
}
|
|
return fmt.Errorf("Reims-Go did not report starting %s within %v; check its log panel (quit it first if it is an older version)", name, startHandoffTimeout)
|
|
case err != nil:
|
|
return err
|
|
}
|
|
if !res.OK {
|
|
return errors.New(res.Error)
|
|
}
|
|
// Guest Tools only connects once macOS is up and its agent runs, so say
|
|
// what this process can know: who now looks after the VM.
|
|
if zh {
|
|
fmt.Printf("%s 已启动,由 Reims-Go 界面进程管理;macOS 启动后,已安装 Guest Tools 的客体会接入剪贴板与共享目录\n", name)
|
|
} else {
|
|
fmt.Printf("%s is running, looked after by the Reims-Go window process; once macOS is up, a guest with Guest Tools installed gets clipboard and shared folders\n", name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// waitStartResult polls for the window's StartResult file and removes it.
|
|
// The window writes it with a rename, so any file seen is complete.
|
|
func waitStartResult(ctx context.Context, path string, timeout time.Duration) (StartResult, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
tick := time.NewTicker(250 * time.Millisecond)
|
|
defer tick.Stop()
|
|
for {
|
|
if b, err := os.ReadFile(path); err == nil {
|
|
_ = os.Remove(path)
|
|
var res StartResult
|
|
if err := json.Unmarshal(b, &res); err != nil {
|
|
return StartResult{}, fmt.Errorf("unreadable start result %s: %w", path, err)
|
|
}
|
|
return res, nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return StartResult{}, ctx.Err()
|
|
case <-tick.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
// WriteStartResult is the window's side of waitStartResult.
|
|
func WriteStartResult(path string, startErr error) error {
|
|
res := StartResult{OK: startErr == nil}
|
|
if startErr != nil {
|
|
res.Error = startErr.Error()
|
|
}
|
|
b, err := json.Marshal(res)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
part := path + ".part"
|
|
if err := os.WriteFile(part, b, 0o644); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(part, path)
|
|
}
|