Skip to content

Commit 80a5dd7

Browse files
committed
Changes for allowing migration of a vm on hyperv. Migration was blocked in cloudstack for hyperv.
Enabled it. Also updated the agent code to allow hyperv migration.
1 parent 089c7bc commit 80a5dd7

10 files changed

Lines changed: 4486 additions & 10 deletions

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/CloudStackTypes.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public string FullFileName
160160
else
161161
{
162162
fileName = @"\\" + this.primaryDataStore.uri.Host + this.primaryDataStore.uri.LocalPath + @"\" + this.name;
163+
fileName = fileName.Replace(@"/", @"\");
163164
}
164165

165166
if (this.format != null)

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/HypervResourceController.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,14 +1477,15 @@ public JContainer PrepareForMigrationCommand([FromBody]dynamic cmd)
14771477
logger.Info(CloudStackTypes.PrepareForMigrationCommand + cmd.ToString());
14781478

14791479
string details = null;
1480-
bool result = false;
1480+
bool result = true;
14811481

14821482
try
14831483
{
1484-
details = "NOP - failure";
1484+
details = "NOP - success";
14851485
}
14861486
catch (Exception sysEx)
14871487
{
1488+
result = false;
14881489
details = CloudStackTypes.PrepareForMigrationCommand + " failed due to " + sysEx.Message;
14891490
logger.Error(details, sysEx);
14901491
}
@@ -1514,7 +1515,10 @@ public JContainer MigrateCommand([FromBody]dynamic cmd)
15141515

15151516
try
15161517
{
1517-
details = "NOP - failure";
1518+
string vm = (string)cmd.vmName;
1519+
string destination = (string)cmd.destIp;
1520+
wmiCallsV2.MigrateVm(vm, destination);
1521+
result = true;
15181522
}
15191523
catch (Exception sysEx)
15201524
{

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/IWmiCallsV2.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface IWmiCallsV2
3838
ComputerSystem DeployVirtualMachine(dynamic jsonObj, string systemVmIso);
3939
void DestroyVm(dynamic jsonObj);
4040
void DestroyVm(string displayName);
41+
void MigrateVm(string vmName, string destination);
4142
void DetachDisk(string displayName, string diskFileName);
4243
ComputerSystem GetComputerSystem(string displayName);
4344
string GetDefaultDataRoot();

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/WmiCallsV2.cs‎

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ public class WmiCallsV2 : IWmiCallsV2
3636
{
3737
public static String CloudStackUserDataKey = "cloudstack-vm-userdata";
3838

39+
/// <summary>
40+
/// Defines the migration types.
41+
/// </summary>
42+
public enum MigrationType
43+
{
44+
VirtualSystem = 32768,
45+
Storage = 32769,
46+
Staged = 32770,
47+
VirtualSystemAndStorage = 32771
48+
};
49+
50+
/// <summary>
51+
/// Defines migration transport types.
52+
/// </summary>
53+
public enum TransportType
54+
{
55+
TCP = 5,
56+
SMB = 32768
57+
};
58+
3959
public static void Initialize()
4060
{
4161
// Trigger assembly load into curren appdomain
@@ -858,7 +878,42 @@ public void DestroyVm(string displayName)
858878
}
859879
while (vm != null);
860880
}
861-
881+
882+
/// <summary>
883+
/// Migrates a vm to the given destination host
884+
/// </summary>
885+
/// <param name="desplayName"></param>
886+
/// <param name="destination host"></param>
887+
public void MigrateVm(string vmName, string destination)
888+
{
889+
ComputerSystem vm = GetComputerSystem(vmName);
890+
VirtualSystemMigrationSettingData migrationSettingData = VirtualSystemMigrationSettingData.CreateInstance();
891+
VirtualSystemMigrationService service = GetVirtualisationSystemMigrationService();
892+
893+
migrationSettingData.LateBoundObject["MigrationType"] = MigrationType.VirtualSystem;
894+
migrationSettingData.LateBoundObject["TransportType"] = TransportType.TCP;
895+
string migrationSettings = migrationSettingData.LateBoundObject.GetText(System.Management.TextFormat.CimDtd20);
896+
897+
ManagementPath jobPath;
898+
string destinationHost = "band-cloud153.blr.cloudstack.org";
899+
var ret_val = service.MigrateVirtualSystemToHost(vm.Path, destinationHost, migrationSettings, null, null, out jobPath);
900+
if (ret_val == ReturnCode.Started)
901+
{
902+
MigrationJobCompleted(jobPath);
903+
}
904+
else if (ret_val != ReturnCode.Completed)
905+
{
906+
var errMsg = string.Format(
907+
"Failed migrating VM {0} (GUID {1}) due to {2}",
908+
vm.ElementName,
909+
vm.Name,
910+
ReturnCode.ToString(ret_val));
911+
var ex = new WmiException(errMsg);
912+
logger.Error(errMsg, ex);
913+
throw ex;
914+
}
915+
}
916+
862917
/// <summary>
863918
/// Create new storage media resources, e.g. hard disk images and ISO disk images
864919
/// see http://msdn.microsoft.com/en-us/library/hh859775(v=vs.85).aspx
@@ -1475,6 +1530,21 @@ public VirtualSystemManagementService GetVirtualisationSystemManagementService()
14751530
throw ex;
14761531
}
14771532

1533+
public VirtualSystemMigrationService GetVirtualisationSystemMigrationService()
1534+
{
1535+
1536+
var virtSysMigSvcCollection = VirtualSystemMigrationService.GetInstances();
1537+
foreach (VirtualSystemMigrationService item in virtSysMigSvcCollection)
1538+
{
1539+
return item;
1540+
}
1541+
1542+
var errMsg = string.Format("No Hyper-V migration service subsystem on server");
1543+
var ex = new WmiException(errMsg);
1544+
logger.Error(errMsg, ex);
1545+
throw ex;
1546+
}
1547+
14781548
/// <summary>
14791549
/// Similar to http://msdn.microsoft.com/en-us/library/hh850031%28v=vs.85%29.aspx
14801550
/// </summary>
@@ -1508,6 +1578,32 @@ private static void JobCompleted(ManagementPath jobPath)
15081578
logger.DebugFormat("WMI job succeeded: {0}, Elapsed={1}", jobObj.Description, jobObj.ElapsedTime);
15091579
}
15101580

1581+
private static void MigrationJobCompleted(ManagementPath jobPath)
1582+
{
1583+
MigrationJob jobObj = null;
1584+
for (;;)
1585+
{
1586+
jobObj = new MigrationJob(jobPath);
1587+
if (jobObj.JobState != JobState.Starting && jobObj.JobState != JobState.Running)
1588+
{
1589+
break;
1590+
}
1591+
logger.InfoFormat("In progress... {0}% completed.", jobObj.PercentComplete);
1592+
System.Threading.Thread.Sleep(1000);
1593+
}
1594+
1595+
if (jobObj.JobState != JobState.Completed)
1596+
{
1597+
var errMsg = string.Format(
1598+
"Hyper-V Job failed, Error Code:{0}, Description: {1}",
1599+
jobObj.ErrorCode,
1600+
jobObj.ErrorDescription);
1601+
var ex = new WmiException(errMsg);
1602+
logger.Error(errMsg, ex);
1603+
throw ex;
1604+
}
1605+
}
1606+
15111607
public void GetProcessorResources(out uint cores, out uint mhz)
15121608
{
15131609
// Processor processors

0 commit comments

Comments
 (0)