* fix(installer): use 8 KiB HFS+ catalog B-tree nodes for REIMSTOOLS image * feat(guesttools): support Windows-to-macOS window drag-and-drop (Agent v13) --------- Co-authored-by: Shroap <184123002+YinLiaoCo@users.noreply.github.com>
217 lines
5.7 KiB
Go
217 lines
5.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// SuspendVM saves a VM's running state and closes its QEMU process.
|
|
func (a *App) SuspendVM(name string) error {
|
|
return a.SuspendVMWithDriver("", name)
|
|
}
|
|
|
|
func (a *App) SuspendVMWithDriver(driverID, name string) error {
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
svc, gt := a.lifecycleSvc, a.guestTools
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
ctx := a.baseCtx()
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
key := ref.Key()
|
|
end, err := a.beginSuspendTransition(key, ref.Name, "suspending")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer end()
|
|
|
|
lastPct := -1
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
|
|
res, err := svc.Suspend(ctx, ref, func(p driver.SuspendProgress) {
|
|
if pct, ok := suspendProgressPercent(p); ok {
|
|
if pct >= lastPct+25 || p.Status == "completed" {
|
|
lastPct = pct
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, fmt.Sprintf("Suspend progress: %s (%d%%)", p.Status, pct))
|
|
}
|
|
}
|
|
}, w, w)
|
|
if err != nil {
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, "Suspend error: "+err.Error())
|
|
return err
|
|
}
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcherKey(key)
|
|
stopVMMonitor(ref.Name)
|
|
}
|
|
if gt != nil {
|
|
gt.UnregisterVM(key)
|
|
}
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, fmt.Sprintf("Suspended %s (%.2f GiB saved)", ref.Name, float64(res.VMStateSize)/float64(1<<30)))
|
|
return nil
|
|
}
|
|
|
|
// ResumeVM restores a suspended VM with its original media and devices.
|
|
func (a *App) ResumeVM(name string) error {
|
|
return a.ResumeVMWithDriver("", name)
|
|
}
|
|
|
|
func (a *App) ResumeVMWithDriver(driverID, name string) error {
|
|
if err := a.beginVMStart(); err != nil {
|
|
return err
|
|
}
|
|
defer a.vmStarts.Done()
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
cfgCopy := *a.cfg
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
ctx := a.baseCtx()
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
key := ref.Key()
|
|
end, err := a.beginSuspendTransition(key, ref.Name, "resuming")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer end()
|
|
|
|
var extraEnv []string
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
a.cancelRebootWatcherKey(key)
|
|
if active, rerr := rt.ResolveActive(&cfgCopy); rerr == nil && active != nil {
|
|
if active.Manifest.HasCapability("win32-unicode-input") {
|
|
extraEnv = append(extraEnv, a.inputBridgeEnv(ref.Name, vm.Directory, a.vmDeleteEpoch(ref.Name))...)
|
|
}
|
|
if active.Manifest.HasCapability("window-file-drop") {
|
|
extraEnv = append(extraEnv, a.dropBridgeEnv(ref.Name, vm.Directory, a.vmDeleteEpoch(ref.Name))...)
|
|
}
|
|
}
|
|
}
|
|
|
|
startAt := time.Now()
|
|
w := vmstart.LineWriter(func(line string) { a.emitVMLogForDriver(ref.DriverID, ref.Name, line) })
|
|
result, err := svc.Resume(ctx, ref, extraEnv, w, w)
|
|
if err != nil {
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, "Resume error: "+err.Error())
|
|
return err
|
|
}
|
|
if ref.DriverID == driver.BuiltinReimsMacOSDriverID {
|
|
for _, action := range result.PostStart {
|
|
if action.Kind != "qmp.watch_cold_reboot" {
|
|
continue
|
|
}
|
|
if addr, _ := action.Params["qmp_addr"].(string); addr != "" {
|
|
a.watchWin10ColdReboot(ref.Name, addr)
|
|
}
|
|
}
|
|
a.startVMMonitor(ref.Name, vm, startAt)
|
|
}
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, fmt.Sprintf("Resumed %s in %.1fs", ref.Name, time.Since(startAt).Seconds()))
|
|
return nil
|
|
}
|
|
|
|
// DiscardSuspend deletes a stopped VM's saved execution state.
|
|
func (a *App) DiscardSuspend(name string) error {
|
|
return a.DiscardSuspendWithDriver("", name)
|
|
}
|
|
|
|
func (a *App) DiscardSuspendWithDriver(driverID, name string) error {
|
|
if err := a.requireWritableDataRoot(); err != nil {
|
|
return err
|
|
}
|
|
a.mu.Lock()
|
|
svc := a.lifecycleSvc
|
|
a.mu.Unlock()
|
|
if svc == nil {
|
|
return errors.New("driver lifecycle is not initialized")
|
|
}
|
|
ctx := a.baseCtx()
|
|
_, vm, err := svc.Resolve(ctx, driverID, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ref := vm.Ref()
|
|
if err := svc.DiscardSuspend(ctx, ref); err != nil {
|
|
return err
|
|
}
|
|
a.emitVMLogForDriver(ref.DriverID, ref.Name, "Discarded suspend state for "+ref.Name)
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return nil
|
|
}
|
|
|
|
func (a *App) beginSuspendTransition(key, displayName, state string) (func(), error) {
|
|
a.mu.Lock()
|
|
loc := a.cfg.Locale
|
|
if a.transitions[key] != "" {
|
|
a.mu.Unlock()
|
|
return nil, i18n.NewCodedError(loc, "E_SUSPEND_BUSY", "err.suspend.busy", i18n.Vars{"name": displayName}, nil)
|
|
}
|
|
a.transitions[key] = state
|
|
a.mu.Unlock()
|
|
a.emitStateChanged()
|
|
return func() {
|
|
a.mu.Lock()
|
|
delete(a.transitions, key)
|
|
a.mu.Unlock()
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
}, nil
|
|
}
|
|
|
|
func suspendProgressPercent(p driver.SuspendProgress) (int, bool) {
|
|
if p.Status == "completed" {
|
|
return 100, true
|
|
}
|
|
if p.Total <= 0 {
|
|
return 0, false
|
|
}
|
|
// With mapped-ram, zero pages are skipped so Transferred stays below Total;
|
|
// Total - Remaining tracks actual scan/save completion across all RAM.
|
|
done := p.Total - p.Remaining
|
|
if done < 0 {
|
|
done = 0
|
|
}
|
|
if done > p.Total {
|
|
done = p.Total
|
|
}
|
|
pct := int(done * 100 / p.Total)
|
|
if pct >= 100 {
|
|
pct = 99
|
|
}
|
|
return pct, true
|
|
}
|
|
|
|
func (a *App) emitStateChanged() {
|
|
if a.ctx != nil {
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
}
|
|
}
|
|
|
|
// Keep context import used cleanly for future cancellation hooks.
|
|
var _ context.Context
|