143 lines
4.7 KiB
Go
143 lines
4.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/dataroot"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
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 {
|
|
if err := a.setPreference(func(cfg *config.Config) { cfg.EnableHypervisorSwitch = enabled }); err != nil {
|
|
return err
|
|
}
|
|
if a.ctx != nil {
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
ctx := a.ctx
|
|
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")
|
|
}
|
|
if ctx != nil {
|
|
wailsRuntime.EventsEmit(ctx, "reims:state-changed", true)
|
|
}
|
|
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()
|
|
}
|