Remove devices after it has been shut down

Attempt to stop vm, before deleting it
pull/2576/head
Taliesin Sisson 11 years ago
parent 2d7cfcd65d
commit 03b0698edd

@ -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.

@ -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
}

@ -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")

@ -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,
},

@ -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
`

Loading…
Cancel
Save