hashicorp / hashicorp/packer

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

Open
#11,885 2 comments 12 reactions 0 assignees View on GitHub
communicator/winrm
Dominant language
Go
Stars
15.8k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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": [
"",
" exit ",
"",
""
]
}
],
"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
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the provided qemu configuration and reviewing the WinRM connection path alongside scripts/uefi/gui/autounattend.xml and scripts/winrmConfig2.bat. Compare the configured listener, firewall, NAT mapping, and reported endpoint with the connection attempts. Done means identifying whether Packer or the Windows setup causes the reset and adding a regression test or clear diagnostic if the repository behavior is at fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
devops, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.