diff --git a/changes/recover-retained-vulkan-manifest.json b/changes/recover-retained-vulkan-manifest.json new file mode 100644 index 0000000..288a2e6 --- /dev/null +++ b/changes/recover-retained-vulkan-manifest.json @@ -0,0 +1,7 @@ +{ + "group": "fixes", + "order": 0, + "authors": ["Nwflower"], + "zh": "虚拟机的 Vulkan 清单丢失或无法枚举显卡时,客户端会保存唯一匹配的新清单,找不到时改为自动选卡,再继续启动。", + "en": "If a VM's Vulkan manifest is missing or cannot enumerate GPUs, the client saves the only matching replacement if one exists, otherwise selects Automatic, then continues startup." +} diff --git a/internal/i18n/catalog_gen.go b/internal/i18n/catalog_gen.go index bb832ba..5be2113 100644 --- a/internal/i18n/catalog_gen.go +++ b/internal/i18n/catalog_gen.go @@ -71,6 +71,7 @@ var catalog = map[Locale]map[string]string{ "closeDownloadsTitle": "退出 Reims-Go?", "closeWindow": "关闭", "collapseLogs": "收起日志", + "compat.gpu_icd_auto": "Vulkan 清单不可用({path}),已将显卡选择改为自动", "compat.gpu_icd_rebound": "显卡 Vulkan ICD 路径已从 {old} 更新为 {new}", "confirm": "确认", "copy": "复制", @@ -176,7 +177,6 @@ var catalog = map[Locale]map[string]string{ "err.fullscreen_failed": "无法切换 {name} 的全屏状态", "err.fullscreen_vmware_svga": "在 {name} 的 QEMU 窗口中按 Ctrl+Alt+F 切换全屏", "err.gpu_device_lost": "宿主显卡 Vulkan 设备丢失(ERROR_DEVICE_LOST)。请更新显卡驱动;若为 AMD Vega/Polaris 等受影响显卡,请改用其他独显或在设置中将虚拟显卡切为 VMware SVGA", - "err.gpu_icd_stale": "原显卡的 Vulkan ICD 文件已不存在({path})。在设置 → 图形与显示器中重新选择显卡,或选择自动", "err.gpu_vulkan_floor": "宿主显卡不满足 Reims vGPU 的 Vulkan 1.3 硬件要求。请更新显卡驱动,或在设置中将虚拟显卡切为 VMware SVGA(软件渲染)", "err.guest_tools_disabled": "Guest Tools 传输服务已在插件页停用,启用后才能使用剪贴板、共享文件夹与防休眠", "err.hypervisor.busy_tasks": "等待虚拟机创建任务结束或取消后再更改系统虚拟化启动设置", @@ -778,6 +778,7 @@ var catalog = map[Locale]map[string]string{ "closeDownloadsTitle": "Quit Reims-Go?", "closeWindow": "Close", "collapseLogs": "Hide logs", + "compat.gpu_icd_auto": "Switched GPU selection to Automatic because the Vulkan manifest is unavailable ({path})", "compat.gpu_icd_rebound": "Updated the GPU Vulkan ICD path from {old} to {new}", "confirm": "Confirm", "copy": "Copy", @@ -883,7 +884,6 @@ var catalog = map[Locale]map[string]string{ "err.fullscreen_failed": "Couldn’t toggle full screen for {name}", "err.fullscreen_vmware_svga": "Press Ctrl+Alt+F in the QEMU window for {name} to toggle full screen", "err.gpu_device_lost": "Host Vulkan GPU device lost (ERROR_DEVICE_LOST). Update GPU drivers; on affected AMD Vega/Polaris GPUs, switch to another discrete GPU or use VMware SVGA in VM Settings", - "err.gpu_icd_stale": "The selected GPU's Vulkan ICD file no longer exists ({path}). In Settings → Graphics & displays, reselect the GPU or choose Automatic", "err.gpu_vulkan_floor": "Host GPU does not meet Reims vGPU's Vulkan 1.3 requirements. Update GPU drivers or switch graphics to VMware SVGA (software rendering) in VM Settings", "err.guest_tools_disabled": "Guest Tools Transport is turned off on the Plugins page; turn it on to use clipboard, shared folders and keep-awake", "err.hypervisor.busy_tasks": "Wait for active VM creation tasks to finish or cancel them before changing host virtualization boot settings", @@ -1441,6 +1441,7 @@ var paramOrder = map[string][]string{ "cli.start.running_in_tray": {"name"}, "cli.start.spawning_tray": {"name"}, "cli.start.timeout": {"name", "timeout"}, + "compat.gpu_icd_auto": {"path"}, "compat.gpu_icd_rebound": {"old", "new"}, "doctor.cow_chain.broken": {"vm", "depth", "path"}, "doctor.cow_chain.item": {"vm", "depth", "warn"}, @@ -1479,7 +1480,6 @@ var paramOrder = map[string][]string{ "err.dataroot.newer": {"dir", "known", "version"}, "err.fullscreen_failed": {"name"}, "err.fullscreen_vmware_svga": {"name"}, - "err.gpu_icd_stale": {"path"}, "err.plugin.high_risk_unapproved": {"id"}, "err.plugin.in_use": {"id", "reason"}, "err.plugin.system_locked": {"id"}, diff --git a/internal/macos/reims/compat.go b/internal/macos/reims/compat.go index 5ebbf60..dc463d5 100644 --- a/internal/macos/reims/compat.go +++ b/internal/macos/reims/compat.go @@ -272,6 +272,9 @@ func ResolveStaleDriverStoreICD(oldPath string, gpus []host.GPUInfo) (newPath st return "", len(matched) > 1 } gpu := matched[0] + if parseDriverStoreICD(gpu.VulkanICD)[0] == old[0] { + return "", false + } // Warning grades can also come from PCI IDs alone. A Vulkan version // confirms that the adapter was probed. if gpu.VulkanVersion == "" || (gpu.Compat != host.GPUCompatSupported && gpu.Compat != host.GPUCompatWarn) { @@ -280,66 +283,91 @@ func ResolveStaleDriverStoreICD(oldPath string, gpus []host.GPUInfo) (newPath st return gpu.VulkanICD, false } -func (c *Client) reconcileStaleGPUICD(ctx context.Context, name string, stderr io.Writer, onNote func(string)) error { +func (c *Client) reconcileStaleGPUICD(ctx context.Context, name string, enumerationFailed bool, stderr io.Writer, onNote func(string)) (bool, error) { detail, err := c.Show(ctx, name) if err != nil || detail.Graphics != "reims-vgpu" || detail.State == "running" { - return nil + return false, nil } oldPath := strings.TrimSpace(detail.GPU) if !strings.HasSuffix(strings.ToLower(oldPath), ".json") { - return nil + return false, nil } stat := c.statFile if stat == nil { stat = os.Stat } - if _, err := stat(oldPath); !errors.Is(err, os.ErrNotExist) { - return nil + _, statErr := stat(oldPath) + if !errors.Is(statErr, os.ErrNotExist) && !(enumerationFailed && statErr == nil) { + return false, nil } - probe := c.probeGPUs - if probe == nil { - probe = host.ProbeVulkanGPUs - } - if onNote == nil && stderr != nil { - onNote = func(msg string) { fmt.Fprintln(stderr, msg) } - } - newPath, _ := ResolveStaleDriverStoreICD(oldPath, probe()) - if newPath == "" { - msg := i18n.M(c.Locale, "err.gpu_icd_stale", i18n.Vars{"path": oldPath}) - if onNote != nil { - onNote(msg) + var newPath string + if parseDriverStoreICD(oldPath) != nil { + probe := c.probeGPUs + if probe == nil { + probe = host.ProbeVulkanGPUs } - return errors.New(msg) + newPath, _ = ResolveStaleDriverStoreICD(oldPath, probe()) + } + selection := newPath + if selection == "" { + selection = "auto" } var setErrBuf bytes.Buffer - if err := c.Set(ctx, SetOpts{Name: name, GPU: newPath}, io.Discard, &setErrBuf); err != nil { + if err := c.Set(ctx, SetOpts{Name: name, GPU: selection}, io.Discard, &setErrBuf); err != nil { if detail := strings.TrimSpace(setErrBuf.String()); detail != "" { - return fmt.Errorf("reims-ctl set %s --gpu: %w (%s)", name, err, detail) + return false, fmt.Errorf("reims-ctl set %s --gpu: %w (%s)", name, err, detail) } - return fmt.Errorf("reims-ctl set %s --gpu: %w", name, err) + return false, fmt.Errorf("reims-ctl set %s --gpu: %w", name, err) + } + var msg string + if selection == "auto" { + msg = i18n.M(c.Locale, "compat.gpu_icd_auto", i18n.Vars{"path": oldPath}) + } else { + msg = i18n.M(c.Locale, "compat.gpu_icd_rebound", i18n.Vars{"old": oldPath, "new": newPath}) } if onNote != nil { - onNote(i18n.M(c.Locale, "compat.gpu_icd_rebound", i18n.Vars{"old": oldPath, "new": newPath})) + onNote(msg) + } else if stderr != nil { + fmt.Fprintln(stderr, msg) } - return nil + return true, nil } -// StartWithCompat checks the configured Vulkan ICD before starting a VM and -// automatically recovers once if QEMU aborts with the WHPX nested-virtualization -// property error (hr=80370302) by setting `-machine kernel-irqchip=off` via -// `reims-ctl set` and retrying start. +func isVulkanDeviceEnumerationError(output string) bool { + for line := range strings.SplitSeq(output, "\n") { + if strings.Contains(line, "rt-refused-vulkan") && strings.Contains(line, "vkEnumeratePhysicalDevices failed") { + return true + } + } + return false +} + +// StartWithCompat recovers from stale Windows Vulkan manifests and the WHPX +// nested-virtualization property error (hr=80370302). func (c *Client) StartWithCompat(ctx context.Context, name string, stdout, stderr io.Writer, onNote func(string)) error { - if err := c.reconcileStaleGPUICD(ctx, name, stderr, onNote); err != nil { + if _, err := c.reconcileStaleGPUICD(ctx, name, false, stderr, onNote); err != nil { return err } - outCap := &captureWriter{dst: stdout} - errCap := &captureWriter{dst: stderr} - err := c.Start(ctx, name, outCap, errCap) + start := func() (string, error) { + outCap := &captureWriter{dst: stdout} + errCap := &captureWriter{dst: stderr} + err := c.Start(ctx, name, outCap, errCap) + return outCap.String() + "\n" + errCap.String(), err + } + combined, err := start() + if err != nil && isVulkanDeviceEnumerationError(combined) { + updated, repairErr := c.reconcileStaleGPUICD(ctx, name, true, stderr, onNote) + if repairErr != nil { + return errors.Join(err, repairErr) + } + if updated { + combined, err = start() + } + } if err == nil { return nil } - combined := outCap.String() + "\n" + errCap.String() if IsWHPXNoAcceleratorError(combined) { msg := i18n.T(c.Locale, "err.whpx_no_hypervisor") if onNote != nil { diff --git a/internal/macos/reims/compat_test.go b/internal/macos/reims/compat_test.go index 74fe627..f93207f 100644 --- a/internal/macos/reims/compat_test.go +++ b/internal/macos/reims/compat_test.go @@ -59,6 +59,7 @@ func TestResolveStaleDriverStoreICD(t *testing.T) { {name: "mixed case and separators", oldPath: strings.ReplaceAll(intelOldICD, "/", "\\"), gpus: []host.GPUInfo{mixed}, wantPath: mixed.VulkanICD}, {name: "Intel and NVIDIA", oldPath: intelOldICD, gpus: []host.GPUInfo{nvidia, intel}, wantPath: intelNewICD}, {name: "different vendor", oldPath: intelOldICD, gpus: []host.GPUInfo{nvidia}}, + {name: "manifest already current", oldPath: intel.VulkanICD, gpus: []host.GPUInfo{mixed}}, {name: "same vendor multiple adapters", oldPath: intelOldICD, gpus: []host.GPUInfo{intel, secondIntel}, ambiguous: true}, {name: "shared manifest", oldPath: intelOldICD, gpus: []host.GPUInfo{intel, intel}, ambiguous: true}, {name: "unprobed also counts towards ambiguity", oldPath: intelOldICD, gpus: []host.GPUInfo{intel, unprobed}, ambiguous: true}, @@ -87,13 +88,15 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { const newPath = "C:/Windows/System32/DriverStore/FileRepository/iigd_dch.inf_amd64_413e993a06fd9302/ICD manifests/igvk64.json" const oldPath = "C:/Windows/System32/DriverStore/FileRepository/iigd_dch.inf_amd64_010a7b33a54cb65d/ICD manifests/igvk64.json" const customPath = "C:/custom/igvk64.JSON" + const enumerationError = "reims-vgpu: MTLCreateSystemDefaultDevice: metal2vulkan-runtime refused it (vkEnumeratePhysicalDevices failed: Initialization of an object has failed); refused (rt-refused-vulkan), the first of its kind" intel := host.GPUInfo{Kind: host.GPUIntegrated, VulkanICD: newPath, VulkanVersion: "1.4.0", Compat: host.GPUCompatSupported} show := "show\t" + name start := "start\t" + name set := "set\t" + name + "\t--gpu\t" + newPath + setAuto := "set\t" + name + "\t--gpu\tauto" rebound := "显卡 Vulkan ICD 路径已从 " + oldPath + " 更新为 " + newPath - stale := "原显卡的 Vulkan ICD 文件已不存在(" + oldPath + ")。在设置 → 图形与显示器中重新选择显卡,或选择自动" - staleCustom := "The selected GPU's Vulkan ICD file no longer exists (" + customPath + "). In Settings → Graphics & displays, reselect the GPU or choose Automatic" + auto := "Vulkan 清单不可用(" + oldPath + "),已将显卡选择改为自动" + autoCustom := "Switched GPU selection to Automatic because the Vulkan manifest is unavailable (" + customPath + ")" binary, err := os.Executable() if err != nil { t.Fatal(err) @@ -107,15 +110,23 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { statErr error gpus []host.GPUInfo fail string + startLog string + failAuto bool twice bool useStderr bool wantCalls []string wantStats []string wantProbe int wantNote string - wantStale bool + wantExit bool }{ {name: "existing file", gpu: oldPath, wantCalls: []string{show, start}, wantStats: []string{oldPath}}, + {name: "old file still exists after driver update", gpu: oldPath, startLog: enumerationError, gpus: []host.GPUInfo{intel}, twice: true, wantCalls: []string{show, start, show, set, start, show, start}, wantStats: []string{oldPath, oldPath, newPath}, wantProbe: 1, wantNote: rebound}, + {name: "enumeration failed for current manifest", gpu: newPath, startLog: enumerationError, gpus: []host.GPUInfo{intel}, wantCalls: []string{show, start, show, setAuto, start}, wantStats: []string{newPath, newPath}, wantProbe: 1, wantNote: "Vulkan 清单不可用(" + newPath + "),已将显卡选择改为自动"}, + {name: "enumeration failed for custom manifest", gpu: customPath, startLog: enumerationError, wantCalls: []string{show, start, show, setAuto, start}, wantStats: []string{customPath, customPath}, wantNote: autoCustom}, + {name: "GPU unplugged with manifest still present", gpu: oldPath, startLog: enumerationError, twice: true, wantCalls: []string{show, start, show, setAuto, start, show, start}, wantStats: []string{oldPath, oldPath}, wantProbe: 1, wantNote: auto}, + {name: "no usable GPU even after switching to auto", gpu: oldPath, startLog: enumerationError, failAuto: true, wantCalls: []string{show, start, show, setAuto, start}, wantStats: []string{oldPath, oldPath}, wantProbe: 1, wantNote: auto, wantExit: true}, + {name: "Vulkan refusal outside device enumeration", gpu: oldPath, startLog: "reims-vgpu: vkCreateDevice failed (rt-refused-vulkan)", gpus: []host.GPUInfo{intel}, wantCalls: []string{show, start}, wantStats: []string{oldPath}, wantExit: true}, {name: "auto", gpu: "automatic: the only GPU", wantCalls: []string{show, start}}, {name: "GPU name", gpu: "Intel Arc B390", wantCalls: []string{show, start}}, {name: "SVGA", gpu: oldPath, graphics: "VMware SVGA", wantCalls: []string{show, start}}, @@ -124,10 +135,11 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { {name: "stat permission denied", gpu: oldPath, statErr: os.ErrPermission, wantCalls: []string{show, start}, wantStats: []string{oldPath}}, {name: "rebind once across two starts", gpu: oldPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel}, twice: true, wantCalls: []string{show, set, start, show, start}, wantStats: []string{oldPath, newPath}, wantProbe: 1, wantNote: rebound}, {name: "rebind note falls back to stderr", gpu: oldPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel}, useStderr: true, wantCalls: []string{show, set, start}, wantStats: []string{oldPath}, wantProbe: 1, wantNote: rebound}, - {name: "ambiguous", gpu: oldPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel, intel}, wantCalls: []string{show}, wantStats: []string{oldPath}, wantProbe: 1, wantNote: stale, wantStale: true}, - {name: "no match", gpu: oldPath, statErr: os.ErrNotExist, wantCalls: []string{show}, wantStats: []string{oldPath}, wantProbe: 1, wantNote: stale, wantStale: true}, - {name: "missing custom JSON", gpu: customPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel}, useStderr: true, wantCalls: []string{show}, wantStats: []string{customPath}, wantProbe: 1, wantNote: staleCustom, wantStale: true}, + {name: "ambiguous", gpu: oldPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel, intel}, wantCalls: []string{show, setAuto, start}, wantStats: []string{oldPath}, wantProbe: 1, wantNote: auto}, + {name: "no match", gpu: oldPath, statErr: os.ErrNotExist, twice: true, wantCalls: []string{show, setAuto, start, show, start}, wantStats: []string{oldPath}, wantProbe: 1, wantNote: auto}, + {name: "missing custom JSON", gpu: customPath, statErr: os.ErrNotExist, useStderr: true, wantCalls: []string{show, setAuto, start}, wantStats: []string{customPath}, wantNote: autoCustom}, {name: "set failure", gpu: oldPath, statErr: os.ErrNotExist, gpus: []host.GPUInfo{intel}, fail: "set", wantCalls: []string{show, set}, wantStats: []string{oldPath}, wantProbe: 1}, + {name: "auto setting fails", gpu: oldPath, statErr: os.ErrNotExist, fail: "set", wantCalls: []string{show, setAuto}, wantStats: []string{oldPath}, wantProbe: 1}, } { t.Run(tc.name, func(t *testing.T) { dir := t.TempDir() @@ -148,6 +160,9 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { "REIMS_TEST_CTL_GRAPHICS="+cmp.Or(tc.graphics, "reims-vgpu"), "REIMS_TEST_CTL_STATE="+cmp.Or(tc.state, "stopped"), "REIMS_TEST_CTL_FAIL="+tc.fail, + "REIMS_TEST_CTL_START_LOG="+tc.startLog, + "REIMS_TEST_CTL_FAIL_GPU="+tc.gpu, + "REIMS_TEST_CTL_FAIL_AUTO="+fmt.Sprint(tc.failAuto), ), Locale: locale, statFile: func(path string) (os.FileInfo, error) { @@ -173,15 +188,16 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { err := client.StartWithCompat(t.Context(), name, nil, &stderr, onNote) switch { - case tc.wantStale: - if err == nil || err.Error() != tc.wantNote { - t.Fatalf("start error = %v, want %q", err, tc.wantNote) - } case tc.fail == "set": var exitErr *exec.ExitError if !errors.As(err, &exitErr) || exitErr.ExitCode() != 7 || !strings.Contains(err.Error(), "reims-ctl set "+name+" --gpu") { t.Fatalf("start error = %v, want wrapped set exit status 7", err) } + case tc.wantExit: + var exitErr *exec.ExitError + if !errors.As(err, &exitErr) || exitErr.ExitCode() != 1 { + t.Fatalf("start error = %v, want start exit status 1", err) + } case err != nil: t.Fatal(err) } @@ -204,14 +220,23 @@ func TestStartWithCompatStaleGPUICD(t *testing.T) { if slices.Contains(tc.wantCalls, set) && tc.fail != "set" { wantGPU = newPath } + if slices.Contains(tc.wantCalls, setAuto) && tc.fail != "set" { + wantGPU = "auto" + } gpu, err := os.ReadFile(gpuFile) if err != nil || string(gpu) != wantGPU { t.Errorf("saved GPU = %q, err = %v, want %q", gpu, err, wantGPU) } var wantNotes []string wantStderr := "" + if tc.startLog != "" { + wantStderr = tc.startLog + "\n" + } + if tc.failAuto { + wantStderr += tc.startLog + "\n" + } if tc.useStderr { - wantStderr = tc.wantNote + "\n" + wantStderr += tc.wantNote + "\n" } else if tc.wantNote != "" { wantNotes = []string{tc.wantNote} } @@ -446,6 +471,16 @@ func TestHelperProcess(t *testing.T) { t.Fatal(err) } case "start": + if log := os.Getenv("REIMS_TEST_CTL_START_LOG"); log != "" { + gpu, err := os.ReadFile(gpuFile) + if err != nil { + t.Fatal(err) + } + if string(gpu) == os.Getenv("REIMS_TEST_CTL_FAIL_GPU") || (string(gpu) == "auto" && os.Getenv("REIMS_TEST_CTL_FAIL_AUTO") == "true") { + fmt.Fprintln(os.Stderr, log) + os.Exit(1) + } + } default: t.Fatalf("unexpected ctl command %q", args) } diff --git a/locales/en-US/native.json b/locales/en-US/native.json index df35b3c..db8f51c 100644 --- a/locales/en-US/native.json +++ b/locales/en-US/native.json @@ -175,8 +175,8 @@ "new" ] }, - "err.gpu_icd_stale": { - "message": "The selected GPU's Vulkan ICD file no longer exists ({path}). In Settings → Graphics & displays, reselect the GPU or choose Automatic", + "compat.gpu_icd_auto": { + "message": "Switched GPU selection to Automatic because the Vulkan manifest is unavailable ({path})", "$params": [ "path" ] diff --git a/locales/zh-CN/native.json b/locales/zh-CN/native.json index 225d19f..19ca56c 100644 --- a/locales/zh-CN/native.json +++ b/locales/zh-CN/native.json @@ -175,8 +175,8 @@ "new" ] }, - "err.gpu_icd_stale": { - "message": "原显卡的 Vulkan ICD 文件已不存在({path})。在设置 → 图形与显示器中重新选择显卡,或选择自动", + "compat.gpu_icd_auto": { + "message": "Vulkan 清单不可用({path}),已将显卡选择改为自动", "$params": [ "path" ]