Error stopping Hyper-V VM plus solution

665 views
Skip to first unread message

Kim Rechnagel

unread,
Mar 12, 2019, 5:40:12 AM3/12/19
to Packer
Hey,

I'm posting this as I wasn't able to find the information elsewhere. Maybe this will help others who might run into the same issue.

I have been using MarcinBojkos packer templates (https://github.com/marcinbojko/hv-packer) for building 2016 Hyper-V templates and are currently in the process of transforming them into a Windows Server 2012 R2 packer template. During my testing I ran into a problem at the end, after the SYSPREP provisioner part had run. Packer failed shutting down the VM, probably because SYSPREP had already shut down the server, the logs does not indicate this.

Log snip:
==> hyperv-iso: Forcibly halting virtual machine...
==> hyperv-iso: Error stopping VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop.
==> hyperv-iso: At C:\Users\hn_kre\AppData\Local\Temp\powershell181645961.ps1:5 char:5
==> hyperv-iso: +     Hyper-V\Stop-VM -VM $vm -Force -Confirm:$false
==> hyperv-iso: +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> hyperv-iso:     + CategoryInfo          : NotSpecified: (:) [Stop-VM], VirtualizationException
==> hyperv-iso:     + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StopVM
==> hyperv-iso: Error stopping VM: PowerShell error: Hyper-V\Stop-VM : Failed to stop.
==> hyperv-iso: At C:\Users\hn_kre\AppData\Local\Temp\powershell181645961.ps1:5 char:5
==> hyperv-iso: +     Hyper-V\Stop-VM -VM $vm -Force -Confirm:$false
==> hyperv-iso: +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> hyperv-iso:     + CategoryInfo          : NotSpecified: (:) [Stop-VM], VirtualizationException
==> hyperv-iso:     + FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.StopVM
==> hyperv-iso: Step "StepShutdown" failed

The failing provisioner:
    {
      "type": "powershell",
      "inline": [
        "if( Test-Path $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml ){ rm $Env:SystemRoot\\windows\\system32\\Sysprep\\unattend.xml -Force}",
        "if (!(Test-Path C:\\Windows\\Packer)) {New-Item -ItemType directory -Path C:\\Windows\\Packer}",
"if (Test-Path E:\\unattend.xml) {copy E:\\unattend.xml C:\\Windows\\Packer\\}",
"& $Env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /shutdown /quiet /unattend:$Env:SystemRoot\\Packer\\unattend.xml"
      ]
    }


By comparing the VM sysprep log timestamps with the event log on my hyper-v host builder i noticed that exactly one second after sysprep had shutdown the server, Hyper-V reported a mostly useless hyper-v work error in the "Microsoft-Windows-Hyper-V-Worker/Admin" log:
The description for Event ID 3452 from source Microsoft-Windows-Hyper-V-Worker cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

win2012r2
79BF6CC5-C059-42C3-BF84-623D1338A6BA
%%2147943515
0x8007045B

The locale specific resource for the desired message is not present

I looked through the packer source code for the shutdown function (https://github.com/hashicorp/packer/blob/master/builder/hyperv/common/step_shutdown.go) and noticed that any error during the VM shutdown process would trigger the "Error stopping VM:" error and packer would start the cleanup process and quit. I figured that the hyper-v shutdown cmdlet would probably error out if the server was already off.

The solution
The solution was simple, just change the sysprep commandline option /shutdown to /quit, then packer was able to successfully shut down the server.

For some reason this was not a problem when building Windows Server 2016.

Reply all
Reply to author
Forward
0 new messages