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