diff --git a/builder/hyperv/common/artifact.go b/builder/hyperv/common/artifact.go index de7f94ca7..2baeb2d62 100644 --- a/builder/hyperv/common/artifact.go +++ b/builder/hyperv/common/artifact.go @@ -9,7 +9,7 @@ import ( ) // This is the common builder ID to all of these artifacts. -const BuilderId = "mitchellh.hyperv" +const BuilderId = "MSOpenTech.hyperv" // Artifact is the result of running the hyperv builder, namely a set // of files associated with the resulting machine. diff --git a/builder/hyperv/common/step_unmount_integration_services.go b/builder/hyperv/common/step_unmount_integration_services.go index 7c40389fe..a905f2a8b 100644 --- a/builder/hyperv/common/step_unmount_integration_services.go +++ b/builder/hyperv/common/step_unmount_integration_services.go @@ -6,10 +6,10 @@ package common import ( "fmt" - "log" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" powershell "github.com/mitchellh/packer/powershell" + "log" ) type StepUnmountSecondaryDvdImages struct { @@ -35,6 +35,8 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep. powershell := new(powershell.PowerShellCmd) script.WriteLine("param([string]$vmName,[int]$controllerNumber,[int]$controllerLocation)") + script.WriteLine("$vmDvdDrive = Get-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation") + script.WriteLine("if (!$vmDvdDrive) {throw 'unable to find dvd drive'}") script.WriteLine("Remove-VMDvdDrive -VMName $vmName -ControllerNumber $controllerNumber -ControllerLocation $controllerLocation") err := powershell.Run(script.String(), vmName, controllerNumber, controllerLocation) if err != nil { @@ -44,7 +46,6 @@ func (s *StepUnmountSecondaryDvdImages) Run(state multistep.StateBag) multistep. } } - return multistep.ActionContinue } diff --git a/builder/hyperv/common/step_wait_for_install_to_complete.go b/builder/hyperv/common/step_wait_for_install_to_complete.go index efd8bd8b7..c4fafbef0 100644 --- a/builder/hyperv/common/step_wait_for_install_to_complete.go +++ b/builder/hyperv/common/step_wait_for_install_to_complete.go @@ -26,11 +26,6 @@ func (s *StepWaitForPowerOff) Run(state multistep.StateBag) multistep.StepAction vmName := state.Get("vmName").(string) ui.Say("Waiting for vm to be powered down...") - // unless the person has a super fast disk, it should take at least 5 minutes - // for the install and post-install operations to take. Wait 5 minutes to - // avoid hammering on getting VM status via PowerShell - time.Sleep(time.Second * 300); - var script powershell.ScriptBuilder script.WriteLine("param([string]$vmName)") script.WriteLine("(Get-VM -Name $vmName).State -eq [Microsoft.HyperV.PowerShell.VMState]::Off") diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index c7d4e6405..b330cfc50 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -305,12 +305,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // provision requires communicator to be setup &common.StepProvision{}, - // remove the integration services dvd drive - // after we power down - &hypervcommon.StepUnmountSecondaryDvdImages{}, - &hypervcommon.StepUnmountFloppyDrive{}, - &hypervcommon.StepUnmountDvdDrive{}, - &hypervcommon.StepShutdown{ Command: b.config.ShutdownCommand, Timeout: b.config.ShutdownTimeout, @@ -319,6 +313,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // wait for the vm to be powered off &hypervcommon.StepWaitForPowerOff{}, + // remove the integration services dvd drive + // after we power down + &hypervcommon.StepUnmountSecondaryDvdImages{}, + &hypervcommon.StepUnmountFloppyDrive{}, + &hypervcommon.StepUnmountDvdDrive{}, + &hypervcommon.StepExportVm{ OutputDir: b.config.OutputDir, }, diff --git a/powershell/hyperv/hyperv.go b/powershell/hyperv/hyperv.go index b5e3fc137..63aa8cff6 100644 --- a/powershell/hyperv/hyperv.go +++ b/powershell/hyperv/hyperv.go @@ -40,10 +40,9 @@ Set-VMDvdDrive -VMName $vmName -Path $path } func UnmountDvdDrive(vmName string) error { - var script = ` param([string]$vmName) -Set-VMDvdDrive -VMName $vmName -Path $null +Get-VMDvdDrive -VMName $vmName | Set-VMDvdDrive -Path $null ` var ps powershell.PowerShellCmd @@ -104,6 +103,12 @@ func DeleteVirtualMachine(vmName string) error { var script = ` param([string]$vmName) + +$vm = Get-VM -Name $vmName +if (($vm.State -ne [Microsoft.HyperV.PowerShell.VMState]::Off) -and ($vm.State -ne [Microsoft.HyperV.PowerShell.VMState]::OffCritical)) { + Stop-VM -VM $vm -TurnOff -Force +} + Remove-VM -Name $vmName -Force `