Files
Reims-Go/internal/app/hypervisor_flow.go
T
NwflowerandClaude Opus 5.5 b203bde650 refactor: merge duplicated helpers and the window event contract
Events to the frontend go through App.emit, and a test keeps their names
in step with wails.ts. QEMU argument splitting, hidden console processes,
the macOS release record, the running check and a few smaller helpers
now exist once.

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

143 lines
4.5 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
}
// EnableWHPX enables Windows Hypervisor Platform (if missing) and sets Windows
// hypervisor boot launch to auto via UAC. Declining UAC is a no-op.
func (a *App) EnableWHPX() error {
_, err := a.setHypervisorBootInternal(true)
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) {
return a.setHypervisorBootInternal(enable)
}
func (a *App) setHypervisorBootInternal(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()
}