mwrock / packer-templates

Templates for creating vagrant boxes
Other
536 stars 250 forks source link

Packer hangs on "Gracefully halting" when WinRM is disabled in the PackerShutdown.bat #49

Closed robbiepc30 closed 8 years ago

robbiepc30 commented 8 years ago

I've ran into an issue when packer tries to shutdown the VM when it issues the shutdown command which executes PackerShutdown.bat It will stay stuck on Gracefully halting virtual machine... and will never proceed. I have tested by placing a comment line for the part that disables WinRM by typing REM in front of the line and it then works. However that causes another problem when using vagrant to start the machine it starts up connects to WinRM and then when sys prep reboots the machine vagrant looses the connection and forcibly removes the machine (I think vagrant thinks something went wrong and removes it).

Debug output with "netsh advfirewall firewall set rule name="WinRM-HTTP" new action=block" commented out on the PackerShutdown.bat. This will allow packer to complete but seems to cause issues with vagrant when WinRM is enabled during sys prep.

2016/08/02 23:39:19 ui:     parallels-iso: Directory: C:\Windows\Panther
    parallels-iso: Directory: C:\Windows\Panther
2016/08/02 23:39:19 ui:     parallels-iso:
    parallels-iso:
2016/08/02 23:39:19 ui:     parallels-iso:
    parallels-iso:
2016/08/02 23:39:19 ui:     parallels-iso: Mode                LastWriteTime     Length Name
    parallels-iso: Mode                LastWriteTime     Length Name
2016/08/02 23:39:19 ui:     parallels-iso: ----                -------------     ------ ----
    parallels-iso: ----                -------------     ------ ----
2016/08/02 23:39:19 ui:     parallels-iso: d----          8/2/2016  11:39 PM            Unattend
    parallels-iso: d----          8/2/2016  11:39 PM            Unattend
2016/08/02 23:39:19 ui: ==> parallels-iso: Gracefully halting virtual machine...
==> parallels-iso: Gracefully halting virtual machine...
2016/08/02 23:39:19 packer: 2016/08/02 23:39:19 Executing shutdown command: a:/PackerShutdown.bat
2016/08/02 23:39:19 packer: 2016/08/02 23:39:19 [INFO] starting remote command: a:/PackerShutdown.bat
2016/08/02 23:39:19 ui:     parallels-iso:
    parallels-iso:
2016/08/02 23:39:19 ui:     parallels-iso: C:\Users\vagrant>REM netsh advfirewall firewall set rule name="WinRM-HTTP" new action=block
    parallels-iso: C:\Users\vagrant>REM netsh advfirewall firewall set rule name="WinRM-HTTP" new action=block
2016/08/02 23:39:19 ui:     parallels-iso:
    parallels-iso:
2016/08/02 23:39:19 ui:     parallels-iso: C:\Users\vagrant>C:/windows/system32/sysprep/sysprep.exe /generalize /oobe /unattend:C:/Windows/Panther/Unattend/unattend.xml /quiet /shutdown
    parallels-iso: C:\Users\vagrant>C:/windows/system32/sysprep/sysprep.exe /generalize /oobe /unattend:C:/Windows/Panther/Unattend/unattend.xml /quiet /shutdown
2016/08/02 23:40:29 packer: 2016/08/02 23:40:29 [INFO] command 'a:/PackerShutdown.bat' exited with code: 0
2016/08/02 23:40:29 packer: 2016/08/02 23:40:29 Waiting max 15m0s for shutdown to complete
2016/08/02 23:40:30 packer: 2016/08/02 23:40:30 Checking VM state: running
2016/08/02 23:40:31 packer: 2016/08/02 23:40:31 Checking VM state: running
2016/08/02 23:40:32 packer: 2016/08/02 23:40:32 Checking VM state: running
2016/08/02 23:40:32 packer: 2016/08/02 23:40:32 Checking VM state: running
2016/08/02 23:40:33 packer: 2016/08/02 23:40:33 Checking VM state: running
2016/08/02 23:40:34 packer: 2016/08/02 23:40:34 Checking VM state: running
2016/08/02 23:40:35 packer: 2016/08/02 23:40:35 Checking VM state: running
2016/08/02 23:40:36 packer: 2016/08/02 23:40:36 Checking VM state: running
2016/08/02 23:40:37 packer: 2016/08/02 23:40:37 Checking VM state: running
2016/08/02 23:40:37 packer: 2016/08/02 23:40:37 Checking VM state: running
2016/08/02 23:40:38 packer: 2016/08/02 23:40:38 Checking VM state: running
2016/08/02 23:40:39 packer: 2016/08/02 23:40:39 Checking VM state: running
2016/08/02 23:40:40 packer: 2016/08/02 23:40:40 Checking VM state: stopping
2016/08/02 23:40:41 packer: 2016/08/02 23:40:41 Checking VM state: stopped
2016/08/02 23:40:41 packer: 2016/08/02 23:40:41 VM shut down.
2016/08/02 23:40:41 ui: ==> parallels-iso: Compacting the disk image

Debug output with "netsh advfirewall firewall set rule name="WinRM-HTTP" new action=block". This gets hung on Gracefully halting virtual machine...

2016/08/03 09:13:52 ui:     parallels-iso: copying auto unattend file
    parallels-iso: copying auto unattend file
2016/08/03 09:13:52 [INFO] 0 bytes written for 'stderr'
2016/08/03 09:13:52 [INFO] 664 bytes written for 'stdout'
2016/08/03 09:13:52 [INFO] RPC client: Communicator ended with: 0
2016/08/03 09:13:52 [INFO] RPC endpoint: Communicator ended with: 0
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] command 'powershell "& { $env:PACKER_BUILDER_TYPE=\"parallels-iso\"; $env:PACKER_BUILD_NAME=\"parallels-iso\"; c:/Windows/Temp/script.ps1; exit $LastExitCode}"' exited with code: 0
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] RPC endpoint: Communicator ended with: 0
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] 0 bytes written for 'stderr'
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] RPC client: Communicator ended with: 0
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] 664 bytes written for 'stdout'
2016/08/03 09:13:52 ui:     parallels-iso:
    parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso:
    parallels-iso:
    parallels-iso: Directory: C:\Windows\setup
    parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso: Directory: C:\Windows\setup
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso: Mode                LastWriteTime     Length Name
2016/08/03 09:13:52 ui:     parallels-iso: ----                -------------     ------ ----
2016/08/03 09:13:52 ui:     parallels-iso: d----          8/3/2016   9:13 AM            scripts
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso: Directory: C:\Windows\Panther
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso:
2016/08/03 09:13:52 ui:     parallels-iso: Mode                LastWriteTime     Length Name
2016/08/03 09:13:52 ui:     parallels-iso: ----                -------------     ------ ----
2016/08/03 09:13:52 ui:     parallels-iso: d----          8/3/2016   9:13 AM            Unattend
2016/08/03 09:13:52 ui: ==> parallels-iso: Gracefully halting virtual machine...
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 Executing shutdown command: a:/PackerShutdown.bat
    parallels-iso:
    parallels-iso: Mode                LastWriteTime     Length Name
    parallels-iso: ----                -------------     ------ ----
    parallels-iso: d----          8/3/2016   9:13 AM            scripts
    parallels-iso:
    parallels-iso:
    parallels-iso: Directory: C:\Windows\Panther
    parallels-iso:
    parallels-iso:
    parallels-iso: Mode                LastWriteTime     Length Name
    parallels-iso: ----                -------------     ------ ----
    parallels-iso: d----          8/3/2016   9:13 AM            Unattend
==> parallels-iso: Gracefully halting virtual machine...
2016/08/03 09:13:52 packer: 2016/08/03 09:13:52 [INFO] starting remote command: a:/PackerShutdown.bat

It doesn't ever get pass the last line. The VM does get powered down though it just seems like packer never knows this

Any ideas of what is causing this? it seems like if it looses connection to WinRM during shutdown before it gets a successful return code from the PackerShutdown.bat it hangs

I am using the vbox-2012r2.json i've modified it to work with parallels.

{
  "builders": [
    {
      "type": "parallels-iso",
      "prlctl": [
        [ "set", "{{.Name}}", "--memsize", "1536" ],
        [ "set", "{{.Name}}", "--cpus", "1" ],
        [ "set", "{{.Name}}", "--efi-boot", "off" ]
      ],
      "guest_os_type": "win-2012",
      "parallels_tools_flavor": "win",
      "parallels_tools_mode": "attach",
      "iso_url": "{{ user `iso_url` }}",
      "iso_checksum": "{{ user `iso_checksum` }}",
      "iso_checksum_type": "md5",
      "communicator": "winrm",
      "winrm_username": "vagrant",
      "winrm_password": "vagrant",
      "winrm_timeout": "12h",
      "shutdown_command": "a:/PackerShutdown.bat",
      "shutdown_timeout": "15m",
      "floppy_files": [
        "answer_files/2012_r2{{user `core`}}/Autounattend.xml",
        "scripts/oracle.cer",
        "scripts/postunattend.xml",
        "scripts/boxstarter.ps1",
        "scripts/PackerShutdown.bat",
        "scripts/package.ps1",
        "scripts/SetupComplete-2012.cmd",
        "scripts/Test-Command.ps1"
      ]      
    }
  ],
  "provisioners": [
    {
      "type": "powershell",
      "script": "scripts/provision.ps1"
    }
  ],
  "post-processors": [
    [{
      "type": "vagrant",
      "keep_input_artifact": false,
      "output": "windows2012r2min-{{.Provider}}.box",
      "vagrantfile_template": "vagrantfile-windows.template"
    }]
  ],
  "variables": {
    "core": "",
    "guest_additions_mode": "attach",
    "headless": "false",
    "iso_checksum": "5b5e08c490ad16b59b1d9fab0def883a",
    "iso_url": "/Users/xbob/Software/_Trial Software/2012 R2/9600.17050.WINBLUE_REFRESH.140317-1640_X64FRE_SERVER_EVAL_EN-US-IR3_SSS_X64FREE_EN-US_DV9.ISO"
  }
}
mwrock commented 8 years ago

I have seen this before while debugging templates. It usually happens because the sysprep command had problems. In my experience its almost always because the unattend file it points to does not exist. In my templates, I copy a post unattenf file to C:/Windows/Panther/Unattend/unattend.xml but if you don't do that copy and then ask sysprep to run and use that file. It wont actually shutdown the box and packer will timeout.

robbiepc30 commented 8 years ago

hmm. I'll double check. I believe it is copying the unattend file over, I didn't change anything in the provision scripts (Except for comment some stuff out like removing extra files, win update etc... to speed up troubleshooting this problem, but the part where it copies the unattend.xml is there).

It does seem to sysprep and shutdown the vm , just that packer never seems to know. and never gets passed that part.

All I have to do is comment out the part of the batch file that blocks WinRM and it will complete in packer, but then it seems to cause issues for vagrant when it connects via WinRM on the first boot and then does a reboot, causing vagrant to tear down the vm (not quite sure what is going on there)

I am on MaxOSX 10.11.5 using the latest version of packer

mwrock commented 8 years ago

oh interesting. Well, I'm about to refactor out that winrm disabling there anyways. TBH: I cant recall why I added that.

mwrock commented 8 years ago

I just remembered why I block the winrm port. On first boot during vagrant up, the WinRM and other services are starting and stopping and restarting and somewhere in the middle of all of that vagrant finds that WinRM is accessible but something is shutdown in the middle of its connection attempt sometimes resulting in a strange error.

I work around this by disabling the WinRM firewall in the shutdown command and turning it back on in the SetupComplete.cmd file. This way while the box is going through its first boot cycles of restarting services, it cant get to WinRM. Then once its fully up it runs SetupComplete and the WinRM port is made accessible.

I've not seen it interfering with the shutdown.

robbiepc30 commented 8 years ago

Ok, thanks, thats what I figured. It might just be with the packer parallels build provider. I figured out a workaround. Instead of disabling WinRM I just change the service startup type to manual, and then on SetupComplete I changed it back to auto and start the WinRM service.

Replaced the WinRM firewall rule with this in PackerShutdown.bat

sc config winrm start=demand

then replaced the WinRM firewall rule in SetupComplete with this

cmd.exe /c sc config winrm start= auto
cmd.exe /c net start winrm