You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Packer从Azure Marketplace拉取Win11镜像后创建VM失败求助

问题解决:Azure Packer Windows 11镜像OS Provisioning超时

问题背景

使用Packer基于Azure Marketplace的Windows 11(win11-22h2-avd)镜像制作自定义镜像,镜像创建成功,但从该镜像创建VM时出现报错:

OS Provisioning for VM 'testVM' did not finish in the allotted time。不过检测到VM guest agent正在运行,这表明来宾操作系统未被正确准备为可用于创建VM的镜像(CreateOption=FromImage)

已尝试在Packer模板中添加Sysprep命令,但不确定配置是否正确。

修正后的Packer JSON模板

{
    "builders": [
        {
            "type": "azure-arm",
            "use_azure_cli_auth": true,
            "shared_image_gallery_destination": {
                "subscription": "",
                "resource_group": "",
                "gallery_name": "",
                "image_name": "",
                "image_version": ""
            },
            "build_resource_group_name": "",
            "virtual_network_name": "",
            "virtual_network_subnet_name": "",
            "virtual_network_resource_group_name": "",
            "os_type": "Windows",
            "image_publisher": "MicrosoftWindowsDesktop",
            "image_offer": "windows-11",
            "image_sku": "win11-22h2-avd",
            "image_version": "22621.1848.230621",
            "azure_tags": {
                "assignment_group": ""
            },
            "vm_size": "Standard_D4d_v5",
            "build_key_vault_name": "",
            "communicator": "winrm",
            "winrm_timeout": "3m",
            "winrm_insecure": "true",
            "winrm_port": "5986",
            "winrm_no_proxy": "true",
            "winrm_use_ssl": "true",
            "winrm_use_ntlm": "true"
        }
    ],
    "provisioners": [
        {
            "type": "powershell",
            "inline": [
                "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /mode:vm /quiet /quit",
                "$sysprepProcess = Get-Process -Name sysprep -ErrorAction SilentlyContinue",
                "while ($sysprepProcess -and !$sysprepProcess.HasExited) {",
                "    Write-Output 'Waiting for Sysprep process to exit...'",
                "    Start-Sleep -Seconds 10",
                "    $sysprepProcess = Get-Process -Name sysprep -ErrorAction SilentlyContinue",
                "}",
                "$timeout = 0",
                "while ($timeout -lt 60) {",
                "    $imageState = Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State' -Name ImageState -ErrorAction SilentlyContinue",
                "    if ($imageState.ImageState -eq 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') {",
                "        Write-Output 'Sysprep completed successfully, image is ready.'",
                "        break",
                "    }",
                "    Write-Output \"Current Image State: $($imageState.ImageState), waiting...\"",
                "    Start-Sleep -Seconds 10",
                "    $timeout++",
                "}",
                "if ($timeout -ge 60) {",
                "    Write-Error 'Sysprep timed out before completing.'",
                "    exit 1",
                "}"
            ]
        }
    ]
}

关键调整说明

  • WinRM端口补全:将winrm_port设置为"5986",这是Windows WinRM SSL的默认端口,确保Packer能稳定连接到构建VM执行命令。
  • 添加Sysprep /mode:vm参数:这是Azure官方要求的参数,专门针对VM镜像的通用化处理,避免出现硬件抽象层(HAL)不兼容导致的OS部署失败。
  • 优化Sysprep等待逻辑:
    • 先等待Sysprep进程完全退出,再检查注册表状态,避免过早判断状态。
    • 增加超时机制(最多10分钟),防止脚本无限等待。
    • 补充状态输出,便于排查问题。

额外注意事项

  • 确保构建VM使用的虚拟网络和子网能正常访问Azure服务,避免Sysprep过程中因网络问题导致失败。
  • 不要重复运行Sysprep超过3次,Windows系统对Sysprep的运行次数有限制,超过可能导致系统不稳定。

内容的提问来源于stack exchange,提问作者Priya Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 21:13:21