| 1 | |
| 2 | |
| 3 | package guestos |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "debug/pe" |
| 8 | "errors" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | "syscall" |
| 14 | "time" |
| 15 | |
| 16 | "golang.org/x/sys/windows" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | func probeWindowsHost(ctx context.Context) (bool, string) { |
| 22 | version := windows.RtlGetVersion() |
| 23 | const workstation = 1 |
| 24 | if version.ProductType != workstation || version.MajorVersion < 10 || version.BuildNumber < 22000 { |
| 25 | return false, "Windows 原生客机需要 Windows 11 桌面版宿主。" |
| 26 | } |
| 27 | var processMachine, nativeMachine uint16 |
| 28 | if err := windows.IsWow64Process2(windows.CurrentProcess(), &processMachine, &nativeMachine); err != nil { |
| 29 | return false, "无法读取 Windows 主机的原生处理器架构。" |
| 30 | } |
| 31 | if runtime.GOARCH != "amd64" || nativeMachine != pe.IMAGE_FILE_MACHINE_AMD64 { |
| 32 | return false, "Windows 原生客机需要 Intel 或 AMD x64 主机及 x64 版 Cage。" |
| 33 | } |
| 34 | ctx, cancel := context.WithTimeout(ctx, 12*time.Second) |
| 35 | defer cancel() |
| 36 | cmd, err := systemPowerShellQuery(ctx, hyperVHostProbe) |
| 37 | if err != nil { |
| 38 | return false, "无法定位系统自带的 Windows PowerShell。" |
| 39 | } |
| 40 | output, err := cmd.Output() |
| 41 | if errors.Is(ctx.Err(), context.DeadlineExceeded) { |
| 42 | return false, "查询 Hyper-V 超时,请检查本机虚拟机管理服务后重试。" |
| 43 | } |
| 44 | if ctx.Err() != nil { |
| 45 | return false, "已取消 Windows 主机预检。" |
| 46 | } |
| 47 | if err != nil { |
| 48 | var exit *exec.ExitError |
| 49 | if errors.As(err, &exit) && exit.ExitCode() == 2 { |
| 50 | if reason := strings.TrimSpace(string(output)); reason != "" { |
| 51 | return false, reason |
| 52 | } |
| 53 | } |
| 54 | return false, "无法通过系统 PowerShell 查询 Hyper-V,请检查 PowerShell 和 Windows 管理服务。" |
| 55 | } |
| 56 | if strings.TrimSpace(string(output)) != "ready" { |
| 57 | return false, "Hyper-V 预检返回了无法识别的结果。" |
| 58 | } |
| 59 | return true, "Hyper-V 平台已就绪;首次安装确认系统授权后,Cage 管理助手仅操作已绑定客机。" |
| 60 | } |
| 61 | |
| 62 | func systemPowerShellQuery(ctx context.Context, script string) (*exec.Cmd, error) { |
| 63 | systemDir, err := windows.GetSystemDirectory() |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | shell := filepath.Join(systemDir, "WindowsPowerShell", "v1.0", "powershell.exe") |
| 68 | cmd := exec.CommandContext(ctx, shell, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script) |
| 69 | cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} |
| 70 | return cmd, nil |
| 71 | } |
| 72 | |
| 73 | const hyperVHostProbe = ` |
| 74 | $ErrorActionPreference = 'Stop' |
| 75 | [Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) |
| 76 | function Unavailable([string]$reason) { Write-Output $reason; exit 2 } |
| 77 | |
| 78 | $edition = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name EditionID).EditionID |
| 79 | if ($edition -like 'Core*') { |
| 80 | Unavailable 'Windows 家庭版未提供完整的 Hyper-V 角色。此原型支持专业版、企业版和教育版。' |
| 81 | } |
| 82 | $service = Get-Service -Name vmms -ErrorAction SilentlyContinue |
| 83 | if (!$service) { |
| 84 | Unavailable '未检测到 Hyper-V 虚拟机管理服务(vmms),请检查 Windows 功能中的 Hyper-V 平台。' |
| 85 | } |
| 86 | if ($service.Status -ne 'Running') { |
| 87 | Unavailable 'Hyper-V 虚拟机管理服务(vmms)当前未运行。' |
| 88 | } |
| 89 | if ((Get-CimInstance Win32_ComputerSystem).HypervisorPresent -ne $true) { |
| 90 | Unavailable 'Windows 未检测到正在运行的虚拟机监控程序,请检查固件虚拟化设置和 Hyper-V 启动状态。' |
| 91 | } |
| 92 | if (!(Get-Command 'Hyper-V\Get-VMHost' -ErrorAction SilentlyContinue)) { |
| 93 | Unavailable '缺少 Hyper-V PowerShell 管理模块,请检查 Windows 功能中的 Hyper-V 管理工具。' |
| 94 | } |
| 95 | Write-Output 'ready' |
| 96 | ` |