CLOUDSTACK-6470: while stopping vm in hyper-v, now we are first trying to shutdown it gracefully before turning it off forcefully

This commit is contained in:
Anshul Gangwar 2014-04-21 23:07:47 -07:00 committed by Devdeep Singh
parent 6ca4e3acb6
commit 1dc76a2743
3 changed files with 1663 additions and 4 deletions

View File

@ -1014,8 +1014,16 @@ namespace HypervResource
return;
}
// Stop VM
logger.DebugFormat("Stop VM {0} (GUID {1})", vm.ElementName, vm.Name);
//try to shutdown vm first
ShutdownVm(vm);
if(GetComputerSystem(vm.ElementName).EnabledState != EnabledState.Disabled)
{
logger.Info("Could not shutdown system cleanly, will forcefully delete the system");
}
// Remove VM
logger.DebugFormat("Remove VM {0} (GUID {1})", vm.ElementName, vm.Name);
SetState(vm, RequiredState.Disabled);
// Delete SwitchPort
@ -1051,6 +1059,32 @@ namespace HypervResource
while (vm != null);
}
public void ShutdownVm(ComputerSystem vm)
{
ShutdownComponent sc = GetShutdownComponent(vm);
if (sc != null)
{
var ret_val = sc.InitiateShutdown(true, "need to shutdown");
if (ret_val != ReturnCode.Completed)
{
logger.Info("Shutting down of system failed, may be shutdown integration services are missing");
}
else
{
// shutdown job is not returned so checking for shutdown completion by checking the current state of system.
// poll every one second and timeout after 10 minutes
for (int period = 0 ; period < 600 && (GetComputerSystem(vm.ElementName).EnabledState != EnabledState.Disabled); period++)
{
System.Threading.Thread.Sleep(1000);
}
}
}
else
{
logger.Info("Shutting down of system failed; may be shutdown integration services are missing");
}
}
/// <summary>
/// Migrates a vm to the given destination host
/// </summary>
@ -2106,6 +2140,19 @@ namespace HypervResource
return ComputerSystem.GetInstances(wmiQuery);
}
public ShutdownComponent GetShutdownComponent(ComputerSystem vm)
{
var wmiQuery = String.Format("SystemName=\"{0}\"", vm.Name);
ShutdownComponent.ShutdownComponentCollection vmCollection = ShutdownComponent.GetInstances(wmiQuery);
// Return the first one
foreach (ShutdownComponent sc in vmCollection)
{
return sc;
}
return null;
}
public Dictionary<String, VmState> GetVmSync(String privateIpAddress)
{
List<String> vms = GetVmElementNames();

View File

@ -135,6 +135,9 @@
<Compile Include="ROOT.virtualization.v2.Msvm_ResourceAllocationSettingData.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ROOT.virtualization.v2.Msvm_ShutdownComponent.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ROOT.virtualization.v2.Msvm_StorageAllocationSettingData.cs">
<SubType>Component</SubType>
</Compile>
@ -181,8 +184,8 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="BeforeBuild" Condition="'$(BuildWithMono)' == 'true' ">
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" Condition="Exists('$(ProjectDir)$(BaseIntermediateOutputPath)')"/>
<RemoveDir Directories="$(ProjectDir)$(OutputPath)" Condition="Exists('$(ProjectDir)$(OutputPath)')"/>
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" Condition="Exists('$(ProjectDir)$(BaseIntermediateOutputPath)')" />
<RemoveDir Directories="$(ProjectDir)$(OutputPath)" Condition="Exists('$(ProjectDir)$(OutputPath)')" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.