⚠️ 重要前提
- 系统要求
- 宿主机:Windows 11 22H2+ 或 Win10 21H2+(WDDM 2.5+)
- 安装Hyper,Powershell输入以下命令并按回车键。系统可能会提示是否重启,输入 Y 然后按回车,以完成安装。
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
- 虚拟机:与宿主机同版本的 Windows(避免驱动兼容问题)
- 显卡:NVIDIA/AMD 最新驱动( → 设备管理器 → 显示适配器 → 驱动程序 → 驱动程序版本)
- 禁用检查点
Set-VM -Name "VM_Name" -CheckpointType Disabled # 防配置冲突
🔧 配置脚本(管理员 PowerShell)
- 新建文本文件:HyperV_GPU_Setup.ps1
- 复制脚本内容到文件中
# Hyper-V GPU 虚拟化配置助手
# 需要管理员权限运行
# 版本: 1.2
# 禁用脚本执行策略警告
Set-ExecutionPolicy Bypass -Scope Process -Force > $null
function Show-Header {
Clear-Host
Write-Host "====================================================" -ForegroundColor Cyan
Write-Host " Hyper-V GPU 虚拟化配置助手" -ForegroundColor Yellow
Write-Host " (支持 Windows 10/11)" -ForegroundColor Yellow
Write-Host "====================================================" -ForegroundColor Cyan
Write-Host "此脚本将帮助您配置 Hyper-V 虚拟机的 GPU 加速功能"
Write-Host "操作包括:"
Write-Host " 1. 配置 GPU 分区参数 (显存和计算资源分配)"
Write-Host " 2. 设置内存映射优化"
Write-Host " 3. 自动生成驱动复制指南"
Write-Host ""
Write-Host "注意:"
Write-Host " - 操作前请确保虚拟机已关闭" -ForegroundColor Red
Write-Host " - 建议提前创建虚拟机快照" -ForegroundColor Red
Write-Host "====================================================" -ForegroundColor Cyan
}
# 显示帮助信息
function Show-CommandHelp {
param($command)
switch ($command) {
"Stop-VM" {
Write-Host "命令作用: 强制关闭虚拟机" -ForegroundColor DarkCyan
Write-Host "参数说明: -Force 确保即使虚拟机在运行状态也强制关闭"
}
"Get-VMGpuPartitionAdapter" {
Write-Host "命令作用: 获取虚拟机的 GPU 分区适配器配置" -ForegroundColor DarkCyan
Write-Host "参数说明: -VMName 指定虚拟机名称"
}
"Remove-VMGpuPartitionAdapter" {
Write-Host "命令作用: 移除现有的 GPU 分区配置" -ForegroundColor DarkCyan
Write-Host "操作说明: 清理旧配置,防止冲突"
}
"Add-VMGpuPartitionAdapter" {
Write-Host "命令作用: 添加 GPU 分区适配器到虚拟机" -ForegroundColor DarkCyan
Write-Host "操作说明: 这是启用 GPU 虚拟化的核心步骤"
}
"Set-VMGpuPartitionAdapter" {
Write-Host "命令作用: 配置 GPU 分区参数" -ForegroundColor DarkCyan
Write-Host "参数说明:"
Write-Host " -Min/Max/OptimalPartitionVRAM: 显存分配 (单位: 字节)"
Write-Host " -Min/Max/OptimalPartitionCompute: 计算资源分配"
Write-Host " 注意: 1GB = 1,073,741,824 字节"
}
"Set-VM" {
Write-Host "命令作用: 配置虚拟机高级参数" -ForegroundColor DarkCyan
Write-Host "参数说明:"
Write-Host " -GuestControlledCacheTypes: 允许访客系统控制缓存类型"
Write-Host " -LowMemoryMappedIoSpace: 低端内存映射空间 (推荐 1GB)"
Write-Host " -HighMemoryMappedIoSpace: 高端内存映射空间 (推荐 32GB)"
}
"Start-VM" {
Write-Host "命令作用: 启动虚拟机" -ForegroundColor DarkCyan
}
}
Write-Host ""
}
# 主交互脚本
Show-Header
# 步骤1: 获取虚拟机名称
$vmName = Read-Host "请输入虚拟机名称"
if (-not (Get-VM -Name $vmName -ErrorAction SilentlyContinue)) {
Write-Host "错误: 找不到虚拟机 '$vmName'!" -ForegroundColor Red
exit
}
# 步骤2: 确认关闭虚拟机
if ((Get-VM -Name $vmName).State -ne 'Off') {
$choice = Read-Host "虚拟机正在运行,需要关闭才能继续 (Y/N)"
if ($choice -eq 'Y' -or $choice -eq 'y') {
Write-Host "正在关闭虚拟机..." -ForegroundColor Yellow
Stop-VM -Name $vmName -Force -WarningAction SilentlyContinue
Start-Sleep -Seconds 3
} else {
Write-Host "操作已取消" -ForegroundColor Yellow
exit
}
}
# 步骤3: 显存配置
Write-Host "`n=== 显存配置 ===" -ForegroundColor Green
Write-Host "推荐值: 总显存8GB的显卡可分配1-4GB给虚拟机"
$minVRAM = 80000000 # 固定最小值 ≈76MB
$maxVRAM = 2147483648 # 默认最大值 2GB
$optimalVRAM = 1073741824 # 默认推荐值 1GB
$userOptimal = Read-Host "请输入推荐显存大小 (GB) [默认:1]"
if (-not [string]::IsNullOrWhiteSpace($userOptimal)) {
$optimalVRAM = [int]($userOptimal * 1073741824)
$maxVRAM = $optimalVRAM * 2 # 最大值设为推荐值的2倍
}
# 步骤4: 确认配置
Write-Host "`n即将应用以下配置:"
Write-Host " - 最小显存: $($minVRAM/1MB) MB"
Write-Host " - 推荐显存: $($optimalVRAM/1GB) GB"
Write-Host " - 最大显存: $($maxVRAM/1GB) GB"
Write-Host " - 计算资源: 与显存相同比例配置"
$confirm = Read-Host "`n是否应用配置? (Y/N)"
if ($confirm -ne 'Y' -and $confirm -ne 'y') {
Write-Host "配置已取消" -ForegroundColor Yellow
exit
}
# 步骤5: 执行配置
Write-Host "`n正在配置 GPU 虚拟化..." -ForegroundColor Cyan
# 移除旧配置防冲突
Write-Host "> 移除旧配置..."
Show-CommandHelp "Get-VMGpuPartitionAdapter"
Get-VMGpuPartitionAdapter -VMName $vmName | Remove-VMGpuPartitionAdapter -ErrorAction SilentlyContinue
# 添加并配置 GPU 分区适配器
Write-Host "> 添加虚拟 GPU 适配器..."
Show-CommandHelp "Add-VMGpuPartitionAdapter"
Add-VMGpuPartitionAdapter -VMName $vmName
Write-Host "> 配置分区参数..."
Show-CommandHelp "Set-VMGpuPartitionAdapter"
Set-VMGpuPartitionAdapter -VMName $vmName `
-MinPartitionVRAM $minVRAM `
-MaxPartitionVRAM $maxVRAM `
-OptimalPartitionVRAM $optimalVRAM `
-MinPartitionCompute $minVRAM `
-MaxPartitionCompute $maxVRAM `
-OptimalPartitionCompute $optimalVRAM
# 设置关键内存映射
Write-Host "> 优化内存映射..."
Show-CommandHelp "Set-VM"
Set-VM -VMName $vmName -GuestControlledCacheTypes $true `
-LowMemoryMappedIoSpace 1GB `
-HighMemoryMappedIoSpace 32GB
# 启动虚拟机
Write-Host "> 启动虚拟机..."
Show-CommandHelp "Start-VM"
Start-VM -Name $vmName
# 步骤6: 生成驱动指南
Write-Host "`n=== 后续操作指南 ===" -ForegroundColor Green
Write-Host "1. 在虚拟机中创建目录:"
Write-Host " mkdir C:\Windows\System32\HostDriverStore"
Write-Host " icacls `"C:\Windows\System32\HostDriverStore`" /grant `"Everyone:(OI)(CI)F`""
Write-Host "`n2. 从宿主机复制以下文件:"
Write-Host " 宿主机路径 -> 虚拟机路径"
Write-Host " C:\Windows\System32\DriverStore\FileRepository\<显卡驱动文件夹>"
Write-Host " -> C:\Windows\System32\HostDriverStore\FileRepository\"
Write-Host " C:\Windows\System32\nvapi64.dll (NVIDIA) 或 amdvlk64.dll (AMD)"
Write-Host " -> C:\Windows\System32\"
Write-Host "`n3. 重启虚拟机后检查设备管理器中的显卡状态"
Write-Host "`n配置完成! 建议进行以下验证:"
Write-Host " - 运行 dxdiag.exe 检查显示设备"
Write-Host " - 使用 GPU-Z 验证显存分配"
Write-Host " - 进行游戏或 GPU 基准测试"
Write-Host "`n注意: 宿主机更新显卡驱动后,需重新复制驱动文件到虚拟机!"
运行脚本(管理员权限)
# 方法1:通过PowerShell运行
1. 右键点击开始菜单 → Windows PowerShell (管理员)
2. 输入:Set-ExecutionPolicy RemoteSigned -Scope Process
3. 输入:Y 确认
4. 输入:.\HyperV_GPU_Setup.ps1
# 方法2:通过批处理文件运行(推荐)
1. 新建文本文件:Run_Script.bat
2. 添加内容:
@echo off
PowerShell.exe -ExecutionPolicy Bypass -File "%~dp0HyperV_GPU_Setup.ps1"
pause
3. 右键点击Run_Script.bat → 以管理员身份运行
脚本操作流程(交互式步骤)
步骤 | 操作 | 说明 |
---|---|---|
1 | 输入虚拟机名称 | 输入您要配置的Hyper-V虚拟机名称(区分大小写) |
2 | 确认关闭虚拟机 | 如果虚拟机正在运行,需要关闭才能继续 |
3 | 设置显存分配 | 输入推荐显存大小(GB),默认1GB |
4 | 确认配置 | 查看配置参数并确认应用 |
5 | 自动执行配置 | 脚本自动完成所有技术配置 |
6 | 获取驱动复制指南 | 脚本显示后续手动操作步骤 |
驱动文件复制(关键步骤)
# 在虚拟机中执行(需启动虚拟机后操作):
1. 创建目录:
mkdir C:\Windows\System32\HostDriverStore\FileRepository
icacls "C:\Windows\System32\HostDriverStore" /grant "Everyone:(OI)(CI)F"
# 在宿主机中执行:
2. 查找显卡驱动文件夹:
路径:C:\Windows\System32\DriverStore\FileRepository\
找到类似:nv_dispi.inf_amd64_xxxxxxx 的文件夹(NVIDIA)
或:u039xxxx.inf_xxxx(AMD)
3. 复制文件:
a. 将整个显卡驱动文件夹复制到虚拟机:
宿主机:C:\Windows\System32\DriverStore\FileRepository\<显卡驱动文件夹>
虚拟机:C:\Windows\System32\HostDriverStore\FileRepository\
b. 复制API文件:
NVIDIA:复制 C:\Windows\System32\nvapi64.dll
到虚拟机 C:\Windows\System32\
AMD:复制 C:\Windows\System32\amdvlk64.dll
到虚拟机 C:\Windows\System32\
4. 重启虚拟机
验证配置成功
# 在虚拟机中检查:
1. 运行 dxdiag:
开始菜单 → 运行 → dxdiag
查看"显示"选项卡 → 确认显卡型号和驱动状态
2. 设备管理器:
右键开始菜单 → 设备管理器
检查"显示适配器"下显卡是否有感叹号
3. 性能测试(可选):
- 运行 GPU-Z 查看显存分配
- 运行轻量游戏(如《英雄联盟》)测试性能
常见问题解决
- 虚拟机启动黑屏:
- 确保复制的驱动文件夹完整
- 检查是否复制了 nvapi64.dll/amdvlk64.dll
- 尝试降低显存分配值(在脚本步骤3中输入0.5或更小)
- 设备管理器显示感叹号:
# 在虚拟机中手动更新驱动: 1. 设备管理器 → 显示适配器 → 右键显卡 2. 选择"更新驱动程序" 3. 浏览我的电脑以查找驱动程序 4. 输入路径:C:\Windows\System32\HostDriverStore\FileRepository\ 5. 勾选"包括子文件夹"
- 性能不佳:
- 关闭虚拟机增强会话模式:
Set-VM -Name "VM_Name" -EnhancedSessionTransportType None
- 增加显存分配值(脚本步骤3输入更大值)
- 确保虚拟机有足够内存(推荐8GB+)
- 关闭虚拟机增强会话模式:
维护建议
- 驱动更新后:
- 宿主机更新显卡驱动后
- 将新驱动文件夹复制到虚拟机覆盖旧文件
- 在虚拟机中更新驱动程序(参考”常见问题解决”第2点)
- 卸载配置:
# 恢复默认配置 Get-VMGpuPartitionAdapter -VMName "VM_Name" | Remove-VMGpuPartitionAdapter
- 定期清理:
- 删除虚拟机中旧的驱动文件夹
- 使用磁盘清理工具释放空间
提示:首次配置建议创建虚拟机快照,以便出现问题时快速恢复。
文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/12352