Files
Reims-Go/frontend/src/lib/hypervisor.ts
T
Dnyo666 ff99d8d702 feat(host,ui): add opt-in Hyper-V boot switch and hypervisor launch detection (#122)
- Distinguish hypervisor launch disabled (hypervisor_off) from BIOS/UEFI
  virtualization disabled (no_hypervisor) via unprivileged Win32 & DeviceGuard
  probes without spawning background PowerShell/BCD processes
- Add default-off `enable_hypervisor_switch` preference in Settings -> General
  with confirmation modal, active VM/task guards, HVCI notice, same-boot undo,
  and single-UAC System32 execution without auto-restarting Windows
- Update Engine page, first-run Setup, `reims-go doctor`, and VM startprep
  diagnostics for `hypervisor_off` and pending boot states
2026-12-03 13:39:08 +08:00

151 lines
4.8 KiB
TypeScript

import '../i18n/domains/settings.gen'
import { I18nKey, t } from '../i18n'
import { vmRefKey } from './vmRef'
import type { Config, HostReport, Locale, Task, VMListItem } from '../types'
export interface HypervisorSettingsView {
supported: boolean
gateEnabled: boolean
expanded: boolean
badgeClass: 'badge-ok' | 'badge-warn' | 'badge-accent'
badgeLabel: string
subLabel: string
actionLabel: string
actionVariant: 'primary' | 'secondary'
targetEnable: boolean
isUndo: boolean
disableBlockedByVMs: boolean
showHVCINotice: boolean
}
export interface HostWHPXSummary {
short: string
hint: string
tone: 'ok' | 'warn'
canEnable: boolean
}
const BLOCKING_VM_STATES = new Set(['running', 'starting', 'stopping', 'suspending', 'resuming'])
export function hasHypervisorBlockingWork(
vms: VMListItem[] | null | undefined,
transitions: Record<string, string> | undefined,
tasks?: Task[],
): boolean {
for (const vm of vms ?? []) {
const st = transitions?.[vmRefKey(vm)] ?? transitions?.[vm.name] ?? vm.state
if (BLOCKING_VM_STATES.has(st)) return true
}
if (transitions) {
for (const tr of Object.values(transitions)) {
if (tr && BLOCKING_VM_STATES.has(tr)) return true
}
}
for (const task of tasks ?? []) {
if (task.status === 'running' && task.kind === 'vm-create') return true
}
return false
}
export function buildHypervisorSettingsView(
locale: Locale,
host: Pick<HostReport, 'whpx' | 'pending_hypervisor_boot' | 'hvci_enabled' | 'vbs_running'>,
config: Pick<Config, 'enable_hypervisor_switch'>,
hasBlockingWork: boolean,
): HypervisorSettingsView {
const tr = (k: I18nKey) => t(locale, k)
const supported = host.whpx !== 'unsupported'
const gateEnabled = supported && Boolean(config.enable_hypervisor_switch)
const pending = host.pending_hypervisor_boot === 'auto' || host.pending_hypervisor_boot === 'off'
? host.pending_hypervisor_boot
: ''
const isUndo = pending !== ''
const effectiveOn = pending === 'auto' ? true : pending === 'off' ? false : host.whpx === 'ready'
const targetEnable = !effectiveOn
let badgeClass: HypervisorSettingsView['badgeClass'] = 'badge-warn'
let badgeKey: I18nKey = 'whpxNoHvShort'
let subKey: I18nKey = 'hypervisorSubNoHv'
if (pending === 'off') {
badgeClass = 'badge-warn'
badgeKey = 'hypervisorBadgePendingOff'
subKey = 'hypervisorSubPendingOff'
} else if (pending === 'auto') {
badgeClass = 'badge-accent'
badgeKey = 'hypervisorBadgePendingOn'
subKey = 'hypervisorSubPendingOn'
} else if (host.whpx === 'ready') {
badgeClass = 'badge-ok'
badgeKey = 'hypervisorBadgeActive'
subKey = 'hypervisorSubActive'
} else if (host.whpx === 'hypervisor_off') {
badgeClass = 'badge-warn'
badgeKey = 'hypervisorBadgeOff'
subKey = 'hypervisorSubOff'
} else if (host.whpx === 'missing') {
badgeClass = 'badge-warn'
badgeKey = 'whpxMissingShort'
subKey = 'hypervisorSubMissing'
}
const actionKey: I18nKey = isUndo
? 'hypervisorActionUndo'
: effectiveOn
? 'hypervisorActionDisable'
: 'hypervisorActionEnable'
const actionVariant: 'primary' | 'secondary' = !isUndo && !effectiveOn ? 'primary' : 'secondary'
const disableBlockedByVMs = !targetEnable && hasBlockingWork
const showHVCINotice = Boolean(host.hvci_enabled || host.vbs_running) && (pending === 'off' || host.whpx === 'ready')
return {
supported,
gateEnabled,
expanded: gateEnabled,
badgeClass,
badgeLabel: tr(badgeKey),
subLabel: tr(subKey),
actionLabel: tr(actionKey),
actionVariant,
targetEnable,
isUndo,
disableBlockedByVMs,
showHVCINotice,
}
}
export function describeHostWHPX(
locale: Locale,
host: Pick<HostReport, 'whpx' | 'pending_hypervisor_boot'>,
): HostWHPXSummary {
const tr = (k: I18nKey) => t(locale, k)
if (host.pending_hypervisor_boot === 'auto') {
return {
short: tr('whpxPendingOnShort'),
hint: tr('whpxPendingHint'),
tone: 'warn',
canEnable: false,
}
}
if (host.pending_hypervisor_boot === 'off') {
return {
short: tr('whpxPendingOffShort'),
hint: tr(host.whpx === 'ready' ? 'whpxPendingOffHint' : 'whpxPendingHint'),
tone: 'warn',
canEnable: false,
}
}
switch (host.whpx) {
case 'ready':
return { short: tr('whpxReady'), hint: '', tone: 'ok', canEnable: false }
case 'hypervisor_off':
return { short: tr('whpxHvOffShort'), hint: tr('whpxHvOff'), tone: 'warn', canEnable: true }
case 'missing':
return { short: tr('whpxMissingShort'), hint: tr('whpxMissing'), tone: 'warn', canEnable: true }
case 'no_hypervisor':
return { short: tr('whpxNoHvShort'), hint: tr('whpxNoHv'), tone: 'warn', canEnable: false }
default:
return { short: tr('whpxUnsupportedShort'), hint: tr('whpxUnsupported'), tone: 'warn', canEnable: false }
}
}