Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packer hangs on Waiting for WinRM even after the WinRM service has begun running. #11885

Open
jjimmykang opened this issue Jul 19, 2022 · 1 comment

Comments

@jjimmykang
Copy link

jjimmykang commented Jul 19, 2022

Hello,

I'm trying to build Windows 2022 with Packer using qemu. The issue is that even after the image boots up, Packer cannot connect to WinRM at all. I've verified through VNC that a listener exists at port 5985(using non-SSL). I also made sure that I add a port opening at 5985 in the batch file to set up WinRM, and I manually verified through VNC that the port opening exists. I've tried both enabling and disabling port forwarding&nat mapping. With skip_nat_mapping set to true, telnetting to 5985 doesn't get a connection refused, which makes me think that the connection refused error that Packer is showing me has to do with WinRM's authentication itself. With skip_nat_mapping set to false, telnetting to the forwarded port again does not get a connection refused.

This error is shown repeatedly, even after I verify through VNC that everything with WinRM should be as it is.

2022/07/19 13:03:06 packer-builder-qemu plugin: Using host value: 127.0.0.1
2022/07/19 13:03:06 packer-builder-qemu plugin: [INFO] Attempting WinRM connection...
2022/07/19 13:03:06 packer-builder-qemu plugin: [DEBUG] connecting to remote shell using WinRM
2022/07/19 13:04:22 packer-builder-qemu plugin: [ERROR] connection error: unknown error Post "http://127.0.0.1:4239/wsman": read tcp 127.0.0.1:35268->127.0.0.1:4239: read: connection reset by peer
2022/07/19 13:04:22 packer-builder-qemu plugin: [ERROR] WinRM connection err: unknown error Post "http://127.0.0.1:4239/wsman": read tcp 127.0.0.1:35268->127.0.0.1:4239: read: connection reset by peer
2022/07/19 13:04:27 packer-builder-qemu plugin: Using host value: 127.0.0.1
2022/07/19 13:04:27 packer-builder-qemu plugin: [INFO] Attempting WinRM connection...
2022/07/19 13:04:27 packer-builder-qemu plugin: [DEBUG] connecting to remote shell using WinRM
2022/07/19 13:05:44 packer-builder-qemu plugin: [ERROR] connection error: unknown error Post "http://127.0.0.1:4239/wsman": read tcp 127.0.0.1:35364->127.0.0.1:4239: read: connection reset by peer
2022/07/19 13:05:44 packer-builder-qemu plugin: [ERROR] WinRM connection err: unknown error Post "http://127.0.0.1:4239/wsman": read tcp 127.0.0.1:35364->127.0.0.1:4239: read: connection reset by peer

My Packer configuration file:

{
    "variables": {
        "boot_wait": "5s",
        "disk_size": "100G",
        "iso_checksum": "4f1457c4fe14ce48c9b2324924f33ca4f0470475e6da851b39ccbf98f44e7852",
        "iso_url": "https://software-download.microsoft.com/download/sg/20348.169.210806-2348.fe_release_svc_refresh_SERVER_EVAL_x64FRE_en-us.iso",
        "memsize": "20480",
        "numvcpus": "2",
        "vm_name": "win2022gui",
        "winrm_password" : "Packer!",
        "winrm_username" : "Administrator",
        "memory": "20480",
        "cpus": "1",
        "MemorySize": "20480"
    },
    "builders": [
        {
            "type": "qemu", 
            "vm_name": "{{user `vm_name`}}",
            "headless": true,
            "boot_wait": "{{user `boot_wait`}}",
            "disk_size": "{{user `disk_size`}}",
            "disk_interface": "ide", 
            
            "communicator":"winrm",
            "winrm_host": "127.0.0.1",
            "winrm_username": "Administrator",
            "winrm_password": "Packer!",
            "winrm_use_ssl": false,
            "winrm_insecure": true, 
            "winrm_timeout": "4h",
            
            "winrm_no_proxy": false, 
            "skip_nat_mapping": false, 

            "shutdown_command": "shutdown /s /t 5 /f /d p:4:1 /c \"Packer Shutdown\"",
            "shutdown_timeout": "30m",
            "memory": "{{user `memory`}}",

            "iso_urls": [
                "{{ user `iso_url` }}"
            ],
            "iso_checksum": "{{user `iso_checksum`}}",
            "output_directory": "output-{{ user `vm_name` }}",
            "qemuargs": [
                [ "-m", "{{ user `memory` }}" ],
                [ "-display", "none"],
                [ "-machine", "accel=kvm" ],
                [ "-cpu", "host" ],
                [ "-smp", "cpus={{ user `cpus`}}"],
                [ "-bios", "/usr/share/qemu/OVMF.fd"]
            ],

            "floppy_files": [
                "scripts/uefi/gui/autounattend.xml",
                "scripts/setup.ps1",
                "scripts/win-update.ps1", 
                "scripts/win-update.ps1", 
                "scripts/cleanup.ps1",
                "scripts/winrmConfig2.bat"
            ],
            "boot_command": [
                "<wait3m>",
                "<enter> exit <enter>",
                "<down><down><enter><enter><wait><enter>",
                "<enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter><enter>"
            ]
        }
    ],
    "provisioners": [
        {
            "type": "powershell",
            "scripts": ["scripts/setup.ps1"]
        },
        {
            "type": "windows-restart",
            "restart_timeout": "30m"
        },
        {
            "type": "powershell",
            "scripts": ["scripts/win-update.ps1"]
        },
        {
            "type": "windows-restart",
            "restart_timeout": "30m"
        },
        {
            "type": "powershell",
            "scripts": ["scripts/win-update.ps1"]
        },
        {
            "type": "windows-restart",
            "restart_timeout": "30m"
        },
        {
            "type": "powershell",
            "scripts": ["scripts/cleanup.ps1"],
            "pause_before": "1m"
        }
    ]
}

My batch file that runs under the in my autounattend.xml:

rem basic config for winrm
cmd.exe /c winrm quickconfig -q

rem allow unencrypted traffic, and configure auth to use basic username/password auth
cmd.exe /c winrm set winrm/config/service @{AllowUnencrypted="true"}
cmd.exe /c winrm set winrm/config/service/auth @{Basic="true"}

rem update firewall rules to open the right port and to allow remote administration
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
cmd.exe /c netsh firewall add portopening TCP 5985 "Port 5985"


powershell -command "Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse"
powershell -command "New-Item -Path WSMan:\LocalHost\Listener -Transport HTTP -Address * -HostName '127.0.0.1' -Force"

rem restart winrm
cmd.exe /c net stop winrm
cmd.exe /c net start winrm
@Axell87
Copy link

Axell87 commented Feb 3, 2025

I have the same error with. Is there any solution for a problem?
I wait for 12 hours and there is nothing happen. Only different ports try to connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants