- Pure-Go Win32 clipboard + localhost HTTP bridge (127.0.0.1:19847) with per-VM 256-bit random Bearer token and jailed shared directory (<DataRoot>/shared/<vm>) - Pure-Go HFS+ REIMSTOOLS virtual USB disk with stock-macOS POSIX LaunchAgent installer (no Xcode or Python needed) for two-way UTF-8 clipboard and ~/Desktop/ReimsShare sync - VMPage Guest Tools card with live connection badge, Open shared folder, Attach/Eject tools disk, and one-click QMP install command - Update docs/roadmap.md with v0.3/v0.4 competitor-aligned follow-up tasks
715 lines
20 KiB
Go
715 lines
20 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver/reims"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/qmp"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
type PendingChange struct {
|
|
Field string `json:"field"`
|
|
From string `json:"from"`
|
|
To string `json:"to"`
|
|
}
|
|
|
|
type AppState struct {
|
|
Config *config.Config `json:"config"`
|
|
Host host.Report `json:"host"`
|
|
Optimizations []host.Optimization `json:"optimizations"`
|
|
Runtimes []rt.Installed `json:"runtimes"`
|
|
ActiveRuntime *rt.Installed `json:"active_runtime,omitempty"`
|
|
VMs []reims.VMListItem `json:"vms"`
|
|
Transitions map[string]string `json:"transitions"`
|
|
PendingChanges map[string][]PendingChange `json:"pending_changes"`
|
|
// NeedsSetup is true on first launch, until a data folder is chosen (setup.go).
|
|
NeedsSetup bool `json:"needs_setup"`
|
|
}
|
|
|
|
type rebootWatcherEntry struct {
|
|
id uint64
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
type App struct {
|
|
ctx context.Context
|
|
mu sync.Mutex
|
|
cfg *config.Config
|
|
transitions map[string]string
|
|
pendingChanges map[string][]PendingChange
|
|
rebootWatchers map[string]rebootWatcherEntry
|
|
nextWatcherID uint64
|
|
stopPoll context.CancelFunc
|
|
guestTools *guesttools.Server
|
|
needsSetup bool
|
|
}
|
|
|
|
func NewApp() *App {
|
|
cfg, needsSetup, err := config.Resolve()
|
|
if err != nil {
|
|
cfg = config.Default()
|
|
}
|
|
return &App{
|
|
cfg: cfg,
|
|
needsSetup: needsSetup,
|
|
transitions: make(map[string]string),
|
|
pendingChanges: make(map[string][]PendingChange),
|
|
rebootWatchers: make(map[string]rebootWatcherEntry),
|
|
}
|
|
}
|
|
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
pollCtx, cancel := context.WithCancel(ctx)
|
|
a.stopPoll = cancel
|
|
go a.registerExistingGuestTools(pollCtx)
|
|
go a.pollVMs(pollCtx)
|
|
}
|
|
|
|
func (a *App) shutdown(ctx context.Context) {
|
|
if a.stopPoll != nil {
|
|
a.stopPoll()
|
|
}
|
|
tasks.cancelAll()
|
|
stopAllVMMonitors()
|
|
a.mu.Lock()
|
|
for name, entry := range a.rebootWatchers {
|
|
entry.cancel()
|
|
delete(a.rebootWatchers, name)
|
|
}
|
|
gt := a.guestTools
|
|
a.mu.Unlock()
|
|
if gt != nil {
|
|
shutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
_ = gt.Close(shutCtx)
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
func (a *App) pollVMs(ctx context.Context) {
|
|
ticker := time.NewTicker(2 * time.Second)
|
|
defer ticker.Stop()
|
|
// Cache static hybrid P-core mask once instead of querying host every tick.
|
|
a.mu.Lock()
|
|
initRoot := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
cachedHost := host.Inspect(initRoot)
|
|
var lastSig string
|
|
pinnedPIDs := make(map[int]bool)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
vms, err := a.listVMsInternal(ctx)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
type pinCandidate struct {
|
|
name string
|
|
pid int
|
|
}
|
|
var toPin []pinCandidate
|
|
a.mu.Lock()
|
|
loc := a.cfg.Locale
|
|
autoPin := a.cfg.AutoPinPCores
|
|
var b strings.Builder
|
|
for _, vm := range vms {
|
|
if tr, ok := a.transitions[vm.Name]; ok {
|
|
if (tr == "starting" && vm.State == "running") || (tr == "stopping" && vm.State == "stopped") {
|
|
delete(a.transitions, vm.Name)
|
|
if vm.State == "stopped" {
|
|
delete(a.pendingChanges, vm.Name)
|
|
}
|
|
}
|
|
}
|
|
if autoPin && vm.State == "running" && vm.PID != nil && *vm.PID > 0 && !pinnedPIDs[*vm.PID] {
|
|
if cachedHost.HasHybridCores && cachedHost.PCoreMask > 0 {
|
|
pinnedPIDs[*vm.PID] = true
|
|
toPin = append(toPin, pinCandidate{name: vm.Name, pid: *vm.PID})
|
|
}
|
|
}
|
|
fmt.Fprintf(&b, "%s:%s:%s;", vm.Name, vm.State, a.transitions[vm.Name])
|
|
}
|
|
sig := b.String()
|
|
a.mu.Unlock()
|
|
for _, cand := range toPin {
|
|
vmCPUs := 0
|
|
if c, cerr := a.client(); cerr == nil {
|
|
if det, derr := c.Show(ctx, cand.name); derr == nil {
|
|
vmCPUs = det.CPUs
|
|
}
|
|
}
|
|
plan := host.SelectSafeAffinity(cachedHost, vmCPUs)
|
|
switch plan.Mode {
|
|
case "all":
|
|
if loc == i18n.ZhCN {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] 跳过绑核:虚拟机配置了 %d vCPU,超过可用安全绑定线程上限(P 核仅 %d 线程),已使用全部核心避免超售卡顿",
|
|
vmCPUs, plan.PThreads))
|
|
} else {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] Skipped CPU pinning: VM has %d vCPUs, exceeding safe headroom (only %d P-core threads); using all host CPUs to avoid spinlock stall",
|
|
vmCPUs, plan.PThreads))
|
|
}
|
|
continue
|
|
case "non_lpe":
|
|
if err := host.PinProcessToPCores(uint32(cand.pid), plan.Mask); err == nil {
|
|
if loc == i18n.ZhCN {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] 虚拟机配置了 %d vCPU(超过 %d 个 P 核线程),已自动将 QEMU (PID %d) 绑定到 P+E 核(%d 核 / %d 线程,掩码 %s;仅排除 %d 个慢速 LP-E 核)",
|
|
vmCPUs, plan.PThreads, cand.pid, plan.CoreCount, plan.ThreadCount, plan.MaskHex, cachedHost.LPECoreCount))
|
|
} else {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] VM has %d vCPUs (exceeds %d P-core threads); pinned QEMU (PID %d) to P+E cores (%d cores / %d logical CPUs, mask %s; %d slow LP-E cores excluded)",
|
|
vmCPUs, plan.PThreads, cand.pid, plan.CoreCount, plan.ThreadCount, plan.MaskHex, cachedHost.LPECoreCount))
|
|
}
|
|
} else {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] Warning: P+E core pinning failed for PID %d: %v", cand.pid, err))
|
|
}
|
|
default:
|
|
if err := host.PinProcessToPCores(uint32(cand.pid), plan.Mask); err == nil {
|
|
if loc == i18n.ZhCN {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] 已将 QEMU (PID %d) 绑定到 %d 个 P 核(%d 个逻辑线程,掩码 %s;未占用 %d 个 E 核)",
|
|
cand.pid, plan.CoreCount, plan.ThreadCount, plan.MaskHex, cachedHost.ECoreCount+cachedHost.LPECoreCount))
|
|
} else {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] Pinned QEMU (PID %d) to %d P-cores (%d logical CPUs, mask %s; %d E/LP-E cores excluded)",
|
|
cand.pid, plan.CoreCount, plan.ThreadCount, plan.MaskHex, cachedHost.ECoreCount+cachedHost.LPECoreCount))
|
|
}
|
|
} else {
|
|
a.emitLog(cand.name, fmt.Sprintf("[CPU] Warning: P-core pinning failed for PID %d: %v", cand.pid, err))
|
|
}
|
|
}
|
|
}
|
|
if sig != lastSig {
|
|
lastSig = sig
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *App) client() (*reims.Client, error) {
|
|
a.mu.Lock()
|
|
cfg := a.cfg
|
|
a.mu.Unlock()
|
|
active, err := rt.ResolveActive(cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s", i18n.T(cfg.Locale, "err.runtime_not_installed"))
|
|
}
|
|
return reims.NewClient(cfg, *active), nil
|
|
}
|
|
|
|
func (a *App) listVMsInternal(ctx context.Context) ([]reims.VMListItem, error) {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.List(ctx)
|
|
}
|
|
|
|
func (a *App) emitLog(source, line string) {
|
|
if a.ctx == nil {
|
|
return
|
|
}
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:log", map[string]string{
|
|
"ts": time.Now().Format("15:04:05"),
|
|
"source": source,
|
|
"line": reims.RedactLogLine(line),
|
|
})
|
|
}
|
|
|
|
type logWriter struct {
|
|
emit func(string)
|
|
buf strings.Builder
|
|
}
|
|
|
|
func (w *logWriter) Write(p []byte) (int, error) {
|
|
sc := bufio.NewScanner(strings.NewReader(string(p)))
|
|
for sc.Scan() {
|
|
line := strings.TrimSpace(sc.Text())
|
|
if line != "" {
|
|
w.emit(line)
|
|
}
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
func (a *App) GetState() AppState {
|
|
a.mu.Lock()
|
|
cfgCopy := *a.cfg
|
|
trCopy := make(map[string]string, len(a.transitions))
|
|
for k, v := range a.transitions {
|
|
trCopy[k] = v
|
|
}
|
|
pcCopy := make(map[string][]PendingChange, len(a.pendingChanges))
|
|
for k, v := range a.pendingChanges {
|
|
pcCopy[k] = append([]PendingChange(nil), v...)
|
|
}
|
|
needsSetup := a.needsSetup
|
|
a.mu.Unlock()
|
|
|
|
rep := host.Inspect(cfgCopy.DataRoot)
|
|
runtimes, _ := rt.List(&cfgCopy)
|
|
active, _ := rt.ResolveActive(&cfgCopy)
|
|
vms, _ := a.listVMsInternal(context.Background())
|
|
|
|
var activePatches []string
|
|
if active != nil {
|
|
for _, p := range active.Manifest.Patches {
|
|
activePatches = append(activePatches, p.Name)
|
|
}
|
|
}
|
|
opts := host.EvaluateOptimizations(rep, activePatches)
|
|
|
|
return AppState{
|
|
Config: &cfgCopy,
|
|
Host: rep,
|
|
Optimizations: opts,
|
|
Runtimes: runtimes,
|
|
ActiveRuntime: active,
|
|
VMs: vms,
|
|
Transitions: trCopy,
|
|
PendingChanges: pcCopy,
|
|
NeedsSetup: needsSetup,
|
|
}
|
|
}
|
|
|
|
func (a *App) GetVMDetail(name string) (reims.VMDetail, error) {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return reims.VMDetail{}, err
|
|
}
|
|
d, err := c.Show(context.Background(), name)
|
|
if err != nil {
|
|
return d, err
|
|
}
|
|
d.RecoveryMode = isRecoveryModeActive(d)
|
|
a.populateGuestToolsDetail(&d)
|
|
return d, nil
|
|
}
|
|
|
|
func (a *App) SetLocale(raw string) error {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.Locale = i18n.Normalize(raw)
|
|
return config.Save("", a.cfg)
|
|
}
|
|
|
|
func (a *App) SetAutoPinPCores(enabled bool) error {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.AutoPinPCores = enabled
|
|
return config.Save("", a.cfg)
|
|
}
|
|
|
|
func (a *App) SetDataRoot(dir string) error {
|
|
dir = strings.TrimSpace(dir)
|
|
if dir == "" {
|
|
return fmt.Errorf("data root cannot be empty")
|
|
}
|
|
a.mu.Lock()
|
|
a.cfg.DataRoot = dir
|
|
err := a.cfg.EnsureDirs()
|
|
if err == nil {
|
|
err = config.Save("", a.cfg)
|
|
}
|
|
if err == nil {
|
|
err = config.SaveLocation(dir)
|
|
}
|
|
a.mu.Unlock()
|
|
return err
|
|
}
|
|
|
|
func (a *App) PickDirectory(title string) (string, error) {
|
|
return wailsRuntime.OpenDirectoryDialog(a.ctx, wailsRuntime.OpenDialogOptions{
|
|
Title: title,
|
|
})
|
|
}
|
|
|
|
func (a *App) PickFile(title, pattern string) (string, error) {
|
|
opts := wailsRuntime.OpenDialogOptions{Title: title}
|
|
if pattern != "" {
|
|
opts.Filters = []wailsRuntime.FileFilter{{DisplayName: pattern, Pattern: pattern}}
|
|
}
|
|
return wailsRuntime.OpenFileDialog(a.ctx, opts)
|
|
}
|
|
|
|
func (a *App) EnableWHPX() error {
|
|
a.emitLog("host", "Requesting Windows UAC to enable HypervisorPlatform...")
|
|
return host.EnableWHPXElevated()
|
|
}
|
|
|
|
func (a *App) InstallRuntimeArchive(archivePath string) (*rt.Installed, error) {
|
|
a.mu.Lock()
|
|
cfg := a.cfg
|
|
a.mu.Unlock()
|
|
a.emitLog("runtime", "Installing portable runtime: "+archivePath)
|
|
inst, err := rt.InstallFromArchive(cfg, archivePath)
|
|
if err != nil {
|
|
a.emitLog("runtime", "Install failed: "+err.Error())
|
|
return nil, err
|
|
}
|
|
a.mu.Lock()
|
|
a.cfg.ActiveRuntime = inst.Commit
|
|
_ = config.Save("", a.cfg)
|
|
a.mu.Unlock()
|
|
a.emitLog("runtime", fmt.Sprintf("Runtime %s installed & activated -> %s", inst.Commit, inst.Dir))
|
|
return inst, nil
|
|
}
|
|
|
|
func (a *App) UseRuntime(commit string) error {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.ActiveRuntime = commit
|
|
return config.Save("", a.cfg)
|
|
}
|
|
|
|
func (a *App) ValidateCreate(opts reims.CreateOpts) []reims.ValidationIssue {
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
issues := reims.ValidateCreate(opts, host.Inspect(root))
|
|
// reims-ctl refuses a name already registered; say so before the create runs.
|
|
if vms, err := a.listVMsInternal(context.Background()); err == nil {
|
|
for _, vm := range vms {
|
|
if strings.EqualFold(vm.Name, strings.TrimSpace(opts.Name)) {
|
|
issues = append(issues, reims.ValidationIssue{Field: "name", Code: "name_taken", Message: "A VM named " + vm.Name + " already exists"})
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func (a *App) PreviewCreate(opts reims.CreateOpts) (string, error) {
|
|
return reims.PreviewCreateCommand(opts)
|
|
}
|
|
|
|
func (a *App) CreateVM(opts reims.CreateOpts) error {
|
|
return a.createVMFlow(opts)
|
|
}
|
|
|
|
func (a *App) ValidateSet(opts reims.SetOpts) ([]reims.ValidationIssue, error) {
|
|
cur, err := a.GetVMDetail(opts.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
return reims.ValidateSet(opts, cur, host.Inspect(root)), nil
|
|
}
|
|
|
|
func (a *App) ApplyVMSettings(opts reims.SetOpts) error {
|
|
cur, err := a.GetVMDetail(opts.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
if issues := reims.ValidateSet(opts, cur, host.Inspect(root)); len(issues) > 0 {
|
|
return fmt.Errorf("%s", issues[0].Message)
|
|
}
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w := &logWriter{emit: func(line string) { a.emitLog(opts.Name, line) }}
|
|
if err := c.Set(context.Background(), opts, w, w); err != nil {
|
|
a.emitLog(opts.Name, "Set error: "+err.Error())
|
|
return err
|
|
}
|
|
if cur.State == "running" {
|
|
var diffs []PendingChange
|
|
if opts.CPUs > 0 && opts.CPUs != cur.CPUs {
|
|
diffs = append(diffs, PendingChange{Field: "cpus", From: fmt.Sprintf("%d", cur.CPUs), To: fmt.Sprintf("%d", opts.CPUs)})
|
|
}
|
|
if opts.Memory != "" && !strings.EqualFold(opts.Memory, cur.Memory) {
|
|
diffs = append(diffs, PendingChange{Field: "memory", From: cur.Memory, To: opts.Memory})
|
|
}
|
|
if opts.Resolution != "" && opts.Resolution != cur.Resolution {
|
|
diffs = append(diffs, PendingChange{Field: "resolution", From: cur.Resolution, To: opts.Resolution})
|
|
}
|
|
if opts.GPU != "" && opts.GPU != cur.GPU {
|
|
diffs = append(diffs, PendingChange{Field: "gpu", From: cur.GPU, To: opts.GPU})
|
|
}
|
|
if len(diffs) > 0 {
|
|
a.mu.Lock()
|
|
a.pendingChanges[opts.Name] = append(a.pendingChanges[opts.Name], diffs...)
|
|
a.mu.Unlock()
|
|
}
|
|
}
|
|
a.emitLog(opts.Name, "Updated VM settings for "+opts.Name)
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|
|
|
|
func (a *App) needsColdRebootWorkaround() bool {
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
return host.Inspect(root).NeedsColdRebootWorkaround
|
|
}
|
|
|
|
func (a *App) cancelRebootWatcher(name string) {
|
|
a.mu.Lock()
|
|
if entry, ok := a.rebootWatchers[name]; ok {
|
|
entry.cancel()
|
|
delete(a.rebootWatchers, name)
|
|
}
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
func (a *App) watchWin10ColdReboot(name, qmpAddr string) {
|
|
a.cancelRebootWatcher(name)
|
|
baseCtx := a.ctx
|
|
if baseCtx == nil {
|
|
baseCtx = context.Background()
|
|
}
|
|
watchCtx, cancel := context.WithCancel(baseCtx)
|
|
a.mu.Lock()
|
|
a.nextWatcherID++
|
|
myID := a.nextWatcherID
|
|
a.rebootWatchers[name] = rebootWatcherEntry{id: myID, cancel: cancel}
|
|
a.mu.Unlock()
|
|
|
|
go func() {
|
|
defer func() {
|
|
cancel()
|
|
a.mu.Lock()
|
|
if cur, ok := a.rebootWatchers[name]; ok && cur.id == myID {
|
|
delete(a.rebootWatchers, name)
|
|
}
|
|
a.mu.Unlock()
|
|
}()
|
|
|
|
reason, err := qmp.WatchShutdownReason(watchCtx, qmpAddr)
|
|
if err != nil {
|
|
if watchCtx.Err() == nil {
|
|
a.emitLog(name, "Win10 WHPX cold-reboot monitor disconnected: "+err.Error())
|
|
}
|
|
return
|
|
}
|
|
if reason != "guest-reset" {
|
|
if reason != "" {
|
|
a.emitLog(name, "Win10 WHPX cold-reboot: VM exited ("+reason+"), not restarting")
|
|
}
|
|
return
|
|
}
|
|
a.mu.Lock()
|
|
tr := a.transitions[name]
|
|
a.mu.Unlock()
|
|
if tr == "stopping" {
|
|
return
|
|
}
|
|
|
|
a.emitLog(name, "Win10 WHPX cold-reboot: guest requested reboot (guest-reset), restarting in a fresh Hyper-V partition...")
|
|
c, err := a.client()
|
|
if err != nil {
|
|
a.emitLog(name, "Win10 WHPX cold-reboot aborted: "+err.Error())
|
|
return
|
|
}
|
|
// Wait briefly for QEMU to finish exiting cleanly under -no-reboot.
|
|
deadline := time.Now().Add(12 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if watchCtx.Err() != nil {
|
|
return
|
|
}
|
|
vms, err := c.List(watchCtx)
|
|
if err == nil {
|
|
stopped := false
|
|
for _, vm := range vms {
|
|
if vm.Name == name && vm.State == "stopped" {
|
|
stopped = true
|
|
break
|
|
}
|
|
}
|
|
if stopped {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(300 * time.Millisecond)
|
|
}
|
|
if watchCtx.Err() != nil {
|
|
return
|
|
}
|
|
if err := a.StartVM(name); err != nil {
|
|
a.emitLog(name, "Win10 WHPX cold-reboot start failed: "+err.Error())
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (a *App) StartVM(name string) error {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := a.checkStartMemory(c, name); err != nil {
|
|
a.emitLog(name, "Start refused: "+err.Error())
|
|
return err
|
|
}
|
|
a.cancelRebootWatcher(name)
|
|
|
|
a.mu.Lock()
|
|
root := a.cfg.DataRoot
|
|
tmpDir := a.cfg.TempDir()
|
|
a.mu.Unlock()
|
|
rep := host.Inspect(root)
|
|
curDetail, _ := c.Show(context.Background(), name)
|
|
|
|
a.syncOfflineHelper(c, name, &curDetail)
|
|
a.syncGuestTools(c, name, &curDetail)
|
|
a.syncRecoveryBootPicker(c, name, &curDetail)
|
|
a.applyProbedWHPXCompat(c, name, rep, &curDetail)
|
|
var qmpAddr string
|
|
if rep.NeedsColdRebootWorkaround {
|
|
sockPath := qmp.RebootSocketPath(curDetail.Directory, tmpDir, name)
|
|
_ = os.MkdirAll(filepath.Dir(sockPath), 0o755)
|
|
_ = os.Remove(sockPath)
|
|
merged := qmp.MergeWin10RebootUnixArgs(curDetail.ExtraArgs, sockPath)
|
|
if err := c.Set(context.Background(), reims.SetOpts{Name: name, ExtraArgs: &merged}, io.Discard, io.Discard); err != nil {
|
|
a.emitLog(name, "Win10 cold-reboot extra-args setup skipped: "+err.Error())
|
|
} else {
|
|
qmpAddr = "unix:" + sockPath
|
|
curDetail.ExtraArgs = merged
|
|
}
|
|
}
|
|
|
|
a.mu.Lock()
|
|
a.transitions[name] = "starting"
|
|
delete(a.pendingChanges, name)
|
|
a.mu.Unlock()
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
|
|
startAt := time.Now()
|
|
a.emitStartSummary(name, curDetail, rep, qmpAddr)
|
|
w := &logWriter{emit: func(line string) { a.emitLog(name, line) }}
|
|
a.emitLog(name, "Starting "+name+"...")
|
|
if err := c.StartWithCompat(context.Background(), name, w, w, func(msg string) {
|
|
a.emitLog(name, msg)
|
|
}); err != nil {
|
|
a.mu.Lock()
|
|
delete(a.transitions, name)
|
|
a.mu.Unlock()
|
|
a.emitLog(name, "Start error: "+err.Error())
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return err
|
|
}
|
|
if qmpAddr != "" {
|
|
a.watchWin10ColdReboot(name, qmpAddr)
|
|
}
|
|
a.startVMMonitor(name, curDetail, startAt)
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|
|
|
|
func (a *App) StopVM(name string, force bool) error {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.cancelRebootWatcher(name)
|
|
stopVMMonitor(name)
|
|
a.mu.Lock()
|
|
a.transitions[name] = "stopping"
|
|
a.mu.Unlock()
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
|
|
w := &logWriter{emit: func(line string) { a.emitLog(name, line) }}
|
|
a.emitLog(name, fmt.Sprintf("Stopping %s (force=%v)...", name, force))
|
|
if force {
|
|
err = c.StopForce(context.Background(), name, w, w)
|
|
} else {
|
|
err = c.Stop(context.Background(), name, w, w)
|
|
}
|
|
if err != nil {
|
|
a.mu.Lock()
|
|
delete(a.transitions, name)
|
|
a.mu.Unlock()
|
|
a.emitLog(name, "Stop error: "+err.Error())
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
delete(a.pendingChanges, name)
|
|
a.mu.Unlock()
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|
|
|
|
func (a *App) RestartVM(name string) error {
|
|
if err := a.StopVM(name, false); err != nil {
|
|
return err
|
|
}
|
|
return a.StartVM(name)
|
|
}
|
|
|
|
func (a *App) EjectInstaller(name string) error {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
w := &logWriter{emit: func(line string) { a.emitLog(name, line) }}
|
|
a.emitLog(name, "Ejecting installer from "+name+"...")
|
|
if err := c.Eject(context.Background(), name, w, w); err != nil {
|
|
a.emitLog(name, "Eject error: "+err.Error())
|
|
return err
|
|
}
|
|
a.emitLog(name, "Installer ejected.")
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|
|
|
|
func (a *App) DeleteVM(name string, keepFiles bool) error {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.cancelRebootWatcher(name)
|
|
stopVMMonitor(name)
|
|
w := &logWriter{emit: func(line string) { a.emitLog(name, line) }}
|
|
a.emitLog(name, fmt.Sprintf("Deleting VM %s (keepFiles=%v)...", name, keepFiles))
|
|
if err := c.Delete(context.Background(), name, keepFiles, w, w); err != nil {
|
|
a.emitLog(name, "Delete error: "+err.Error())
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
delete(a.pendingChanges, name)
|
|
delete(a.transitions, name)
|
|
a.mu.Unlock()
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
return nil
|
|
}
|
|
|
|
// OpenVMFolder shows the VM's directory in File Explorer.
|
|
func (a *App) OpenVMFolder(name string) error {
|
|
d, err := a.GetVMDetail(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.Directory == "" {
|
|
return fmt.Errorf("VM %s has no directory", name)
|
|
}
|
|
return host.OpenFolder(d.Directory)
|
|
}
|
|
|
|
func (a *App) ReadVMLogs(name string, serial bool) (string, error) {
|
|
c, err := a.client()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return c.ReadLogs(context.Background(), name, serial)
|
|
}
|