Files
Reims-Go/internal/app/hypervisor_flow.go
T
NwflowerandClaude Opus 5.5 565577589a refactor: give each VM action one window binding
Every VM action had two bindings: one by name and one by driver and name.
The window used only the second, so the first is gone and the WithDriver
suffix with it. Unused bindings and frontend exports are removed too.

Tests now reject bindings the window never calls, stale generated
bindings and unused frontend exports. TypeScript rejects unused locals.

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

132 lines
4.2 KiB
Go

package app
import (
"context"
"errors"
"github.com/Nwflower/reims-go/internal/dataroot"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
)
var (
inspectHostForHypervisor = host.Inspect
setHypervisorBootElevatedFn = host.SetHypervisorBootElevated
recordPendingHypervisorBootFn = host.RecordPendingHypervisorBoot
openWindowsCoreIsolationHostFn = host.OpenWindowsCoreIsolation
)
// SetEnableHypervisorSwitch persists the user's opt-in preference to unlock the
// Hyper-V / WHPX boot switch in Settings. Toggling this flag alone never runs
// any system virtualization or boot configuration command.
func (a *App) SetEnableHypervisorSwitch(enabled bool) error {
a.mu.Lock()
prev := a.cfg.EnableHypervisorSwitch
a.cfg.EnableHypervisorSwitch = enabled
err := a.saveConfigLocked()
if err != nil {
a.cfg.EnableHypervisorSwitch = prev
}
a.mu.Unlock()
if err == nil {
a.emitStateChanged()
}
return err
}
// SetHypervisorBoot prompts for UAC and updates Windows hypervisor boot launch
// (`auto` when enable is true, `off` when false). It returns changed=false with
// err=nil when the person declines the UAC prompt, and never restarts Windows.
func (a *App) SetHypervisorBoot(enable bool) (bool, error) {
a.mu.Lock()
loc := i18n.EffectiveLocale(a.cfg.Locale)
dataRoot := a.cfg.DataRoot
needsSetup := a.needsSetup
rootStatus := a.root
if rootStatus.ReadOnly {
a.mu.Unlock()
return false, dataroot.Error(loc, rootStatus)
}
if !enable {
for _, v := range a.lastVMs {
if isHypervisorBlockingVMState(v.State) {
a.mu.Unlock()
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_BUSY_VMS", "err.hypervisor.busy_vms", nil, nil)
}
}
for _, tr := range a.transitions {
if tr != "" {
a.mu.Unlock()
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_BUSY_VMS", "err.hypervisor.busy_vms", nil, nil)
}
}
}
a.mu.Unlock()
if !enable {
if a.dataRootUsable() {
if vms, err := a.polledVMs(context.Background()); err == nil {
for _, v := range vms {
if isHypervisorBlockingVMState(v.State) {
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_BUSY_VMS", "err.hypervisor.busy_vms", nil, nil)
}
}
}
}
for _, t := range tasks.list() {
if t.Status == TaskRunning && t.Kind == "vm-create" {
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_BUSY_TASKS", "err.hypervisor.busy_tasks", nil, nil)
}
}
}
persistRoot := dataRoot
if needsSetup || rootStatus.ReadOnly {
persistRoot = ""
}
rep := inspectHostForHypervisor(persistRoot)
if rep.WHPX == host.WHPXUnsupported {
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_UNSUPPORTED", "err.hypervisor.unsupported", nil, nil)
}
if enable {
a.emitLog("host", "Requesting Windows UAC to enable hypervisor launch on next boot...")
} else {
a.emitLog("host", "Requesting Windows UAC to disable hypervisor launch on next boot...")
}
if err := setHypervisorBootElevatedFn(a.baseCtx(), enable); err != nil {
if errors.Is(err, host.ErrElevationDeclined) {
a.emitLog("host", "UAC prompt declined; hypervisor boot configuration unchanged")
return false, nil
}
a.emitLog("host", "Hypervisor boot configuration failed: "+err.Error())
return false, i18n.NewCodedError(loc, "E_HOST_HYPERVISOR_SET_FAILED", "err.hypervisor.set_failed", nil, err)
}
pending := recordPendingHypervisorBootFn(persistRoot, rep.WHPX, enable)
if pending == "" {
a.emitLog("host", "Restored Windows hypervisor boot configuration to match current session")
} else {
a.emitLog("host", "Updated Windows hypervisor boot configuration (next boot: "+pending+"); restart Windows manually to apply")
}
a.emitStateChanged()
return true, nil
}
func isHypervisorBlockingVMState(state string) bool {
switch state {
case "running", "starting", "stopping", "suspending", "resuming":
return true
default:
return false
}
}
// OpenWindowsCoreIsolation opens Windows Security's Core Isolation page so the
// user can inspect or adjust Memory Integrity (HVCI) themselves.
func (a *App) OpenWindowsCoreIsolation() error {
a.emitLog("host", "Opening Windows Security Core Isolation settings...")
return openWindowsCoreIsolationHostFn()
}