213213import com .cloud .utils .StringUtils ;
214214import com .cloud .utils .exception .CloudRuntimeException ;
215215import com .cloud .utils .net .NetUtils ;
216+ import com .cloud .utils .StringUtils ;
216217import com .cloud .vm .DiskProfile ;
217218import com .cloud .vm .VirtualMachine ;
218219import com .cloud .vm .VirtualMachine .State ;
@@ -1298,23 +1299,6 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
12981299 vmr .VCPUsMax = 32L ;
12991300 }
13001301
1301- Map <String , String > details = vmSpec .getDetails ();
1302- if ( details != null ) {
1303- String timeoffset = details .get ("timeoffset" );
1304- if (timeoffset != null ) {
1305- Map <String , String > platform = vmr .platform ;
1306- platform .put ("timeoffset" , timeoffset );
1307- vmr .platform = platform ;
1308- }
1309-
1310- String coresPerSocket = details .get ("cpu.corespersocket" );
1311- if (coresPerSocket != null ) {
1312- Map <String , String > platform = vmr .platform ;
1313- platform .put ("cores-per-socket" , coresPerSocket );
1314- vmr .platform = platform ;
1315- }
1316- }
1317-
13181302 vmr .VCPUsAtStartup = (long ) vmSpec .getCpus ();
13191303 vmr .consoles .clear ();
13201304
@@ -1399,9 +1383,40 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
13991383
14001384
14011385 protected void finalizeVmMetaData (VM vm , Connection conn , VirtualMachineTO vmSpec ) throws Exception {
1386+
1387+ Map <String , String > details = vmSpec .getDetails ();
1388+ if ( details != null ) {
1389+ String platformstring = details .get ("platform" );
1390+ if (platformstring != null && !platformstring .isEmpty () ) {
1391+ Map <String , String > platform = StringUtils .stringToMap (platformstring );
1392+ vm .setPlatform (conn , platform );
1393+ } else {
1394+ String timeoffset = details .get ("timeoffset" );
1395+ if (timeoffset != null ) {
1396+ Map <String , String > platform = vm .getPlatform (conn );
1397+ platform .put ("timeoffset" , timeoffset );
1398+ vm .setPlatform (conn , platform );
1399+ }
1400+ String coresPerSocket = details .get ("cpu.corespersocket" );
1401+ if (coresPerSocket != null ) {
1402+ Map <String , String > platform = vm .getPlatform (conn );
1403+ platform .put ("cores-per-socket" , coresPerSocket );
1404+ vm .setPlatform (conn , platform );
1405+ }
1406+
1407+ String xentoolsversion = details .get ("hypervisortoolsversion" );
1408+ if ( xentoolsversion == null || !xentoolsversion .equalsIgnoreCase ("xenserver61" ) ) {
1409+ Map <String , String > platform = vm .getPlatform (conn );
1410+ platform .remove ("device_id" );
1411+ vm .setPlatform (conn , platform );
1412+ }
1413+
1414+ }
1415+ }
14021416 }
14031417
14041418
1419+
14051420 protected String handleVmStartFailure (Connection conn , String vmName , VM vm , String message , Throwable th ) {
14061421 String msg = "Unable to start " + vmName + " due to " + message ;
14071422 s_logger .warn (msg , th );
@@ -2957,7 +2972,7 @@ protected HashMap<String, Ternary<String, State, String>> getAllVms(Connection c
29572972 s_logger .trace ("VM " + record .nameLabel + ": powerstate = " + ps + "; vm state=" + state .toString ());
29582973 }
29592974 Host host = record .residentOn ;
2960- String xstoolsversion = getVMXenToolsVersion (record .platform );
2975+ String platformstring = StringUtils . mapToString (record .platform );
29612976 String host_uuid = null ;
29622977 if ( ! isRefNull (host ) ) {
29632978 try {
@@ -2969,7 +2984,7 @@ protected HashMap<String, Ternary<String, State, String>> getAllVms(Connection c
29692984 } catch (XmlRpcException e ) {
29702985 s_logger .error ("Failed to get host uuid for host " + host .toWireString (), e );
29712986 }
2972- vmStates .put (record .nameLabel , new Ternary <String , State , String >(host_uuid , state , xstoolsversion ));
2987+ vmStates .put (record .nameLabel , new Ternary <String , State , String >(host_uuid , state , platformstring ));
29732988 }
29742989 }
29752990
@@ -3974,6 +3989,7 @@ protected String callHostPluginAsync(Connection conn, String plugin, String cmd,
39743989 @ Override
39753990 public StopAnswer execute (StopCommand cmd ) {
39763991 String vmName = cmd .getVmName ();
3992+ String platformstring = null ;
39773993 try {
39783994 Connection conn = getConnection ();
39793995 Set <VM > vms = VM .getByNameLabel (conn , vmName );
@@ -4003,7 +4019,7 @@ public StopAnswer execute(StopCommand cmd) {
40034019 }
40044020 for (VM vm : vms ) {
40054021 VM .Record vmr = vm .getRecord (conn );
4006-
4022+ platformstring = StringUtils . mapToString ( vmr . platform );
40074023 if (vmr .isControlDomain ) {
40084024 String msg = "Tring to Shutdown control domain" ;
40094025 s_logger .warn (msg );
@@ -4013,7 +4029,7 @@ public StopAnswer execute(StopCommand cmd) {
40134029 if (vmr .powerState == VmPowerState .RUNNING && !isRefNull (vmr .residentOn ) && !vmr .residentOn .getUuid (conn ).equals (_host .uuid )) {
40144030 String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + _host .uuid + " but host:" + vmr .residentOn .getUuid (conn );
40154031 s_logger .warn (msg );
4016- return new StopAnswer (cmd , msg , false );
4032+ return new StopAnswer (cmd , msg , platformstring , false );
40174033 }
40184034
40194035 State state = s_vms .getState (_cluster , vmName );
@@ -4042,21 +4058,11 @@ public StopAnswer execute(StopCommand cmd) {
40424058 } catch (Exception e ) {
40434059 String msg = "Catch exception " + e .getClass ().getName () + " when stop VM:" + cmd .getVmName () + " due to " + e .toString ();
40444060 s_logger .debug (msg );
4045- return new StopAnswer (cmd , msg , false );
4061+ return new StopAnswer (cmd , msg , platformstring , false );
40464062 } finally {
40474063
40484064 try {
40494065 if (vm .getPowerState (conn ) == VmPowerState .HALTED ) {
4050- Map <String , String > platform = vm .getPlatform (conn );
4051- Integer timeoffset = null ;
4052- try {
4053- if (platform .containsKey ("timeoffset" )) {
4054- timeoffset = Integer .valueOf (platform .get ("timeoffset" ));
4055- }
4056- } catch (NumberFormatException e ) {
4057- s_logger .error ("Error while reading the platform:timeoffset field of the instance" , e );
4058- }
4059- String xentoolsversion = getVMXenToolsVersion (platform );
40604066
40614067 Set <VIF > vifs = vm .getVIFs (conn );
40624068 List <Network > networks = new ArrayList <Network >();
@@ -4078,11 +4084,8 @@ public StopAnswer execute(StopCommand cmd) {
40784084 // network might be destroyed by other host
40794085 }
40804086 }
4081- return new StopAnswer (cmd , "Stop VM " + vmName + " Succeed" , xentoolsversion , timeoffset , true );
4087+ return new StopAnswer (cmd , "Stop VM " + vmName + " Succeed" , platformstring , true );
40824088 }
4083- } catch (XenAPIException e ) {
4084- String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e .toString ();
4085- s_logger .warn (msg , e );
40864089 } catch (Exception e ) {
40874090 String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e .getMessage ();
40884091 s_logger .warn (msg , e );
@@ -4095,19 +4098,12 @@ public StopAnswer execute(StopCommand cmd) {
40954098 }
40964099 }
40974100
4098- } catch (XenAPIException e ) {
4101+ } catch (Exception e ) {
40994102 String msg = "Stop Vm " + vmName + " fail due to " + e .toString ();
41004103 s_logger .warn (msg , e );
4101- return new StopAnswer (cmd , msg , false );
4102- } catch (XmlRpcException e ) {
4103- String msg = "Stop Vm " + vmName + " fail due to " + e .getMessage ();
4104- s_logger .warn (msg , e );
4105- return new StopAnswer (cmd , msg , false );
4106- } catch (Exception e ) {
4107- s_logger .warn ("Unable to stop " + vmName + " due to " , e );
4108- return new StopAnswer (cmd , e );
4104+ return new StopAnswer (cmd , msg , platformstring , false );
41094105 }
4110- return new StopAnswer (cmd , "Stop VM failed" , false );
4106+ return new StopAnswer (cmd , "Stop VM failed" , platformstring , false );
41114107 }
41124108
41134109
@@ -8132,13 +8128,12 @@ protected HashMap<String, Ternary<String, State, String>> fullClusterSync(Connec
81328128 String vm_name = record .nameLabel ;
81338129 VmPowerState ps = record .powerState ;
81348130 final State state = convertToState (ps );
8135- String xstoolsversion = getVMXenToolsVersion (record .platform );
81368131 Host host = record .residentOn ;
81378132 String host_uuid = null ;
81388133 if ( ! isRefNull (host ) ) {
81398134 host_uuid = host .getUuid (conn );
81408135 synchronized (_cluster .intern ()) {
8141- s_vms .put (_cluster , host_uuid , vm_name , state , xstoolsversion );
8136+ s_vms .put (_cluster , host_uuid , vm_name , state , null );
81428137 }
81438138 }
81448139 if (s_logger .isTraceEnabled ()) {
@@ -8169,30 +8164,30 @@ protected HashMap<String, Ternary<String, State, String>> deltaClusterSync(Conne
81698164
81708165 for (final Map .Entry <String , Ternary <String , State , String >> entry : newStates .entrySet ()) {
81718166 final String vm = entry .getKey ();
8172- String xstoolsversion = entry .getValue ().third ();
8167+ String platform = entry .getValue ().third ();
81738168 State newState = entry .getValue ().second ();
81748169 String host_uuid = entry .getValue ().first ();
81758170 final Ternary <String , State , String > oldState = oldStates .remove (vm );
81768171
8177- // check if xstoolsversion changed
8178- if (xstoolsversion != null && oldState != null ){
8179- if (xstoolsversion != oldState .third () && newState != State .Stopped && newState != State .Stopping ){
8180- s_logger .warn ("Detecting a change in xstoolsversion for " + vm );
8181- changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , xstoolsversion ));
8172+ // check if platform changed
8173+ if (platform != null && oldState != null ){
8174+ if (! platform . equals ( oldState .third () ) && newState != State .Stopped && newState != State .Stopping ){
8175+ s_logger .warn ("Detecting a change in platform for " + vm );
8176+ changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , platform ));
81828177
81838178 s_logger .debug ("11. The VM " + vm + " is in " + newState + " state" );
8184- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8179+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
81858180 continue ;
81868181 }
81878182 }
81888183 //check if host is changed
81898184 if (host_uuid != null && oldState != null ){
81908185 if (!host_uuid .equals (oldState .first ()) && newState != State .Stopped && newState != State .Stopping ){
81918186 s_logger .warn ("Detecting a change in host for " + vm );
8192- changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , null ));
8187+ changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , platform ));
81938188
81948189 s_logger .debug ("11. The VM " + vm + " is in " + newState + " state" );
8195- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8190+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
81968191 continue ;
81978192 }
81988193 }
@@ -8211,31 +8206,31 @@ protected HashMap<String, Ternary<String, State, String>> deltaClusterSync(Conne
82118206 continue ;
82128207 }
82138208 if (oldState == null ) {
8214- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8209+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
82158210 s_logger .warn ("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm );
8216- changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , null ));
8211+ changes .put (vm , new Ternary <String , State , String >(host_uuid , newState , platform ));
82178212 } else if (oldState .second () == State .Starting ) {
82188213 if (newState == State .Running ) {
82198214 s_logger .debug ("12. The VM " + vm + " is in " + State .Running + " state" );
8220- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8215+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
82218216 } else if (newState == State .Stopped ) {
82228217 s_logger .warn ("Ignoring vm " + vm + " because of a lag in starting the vm." );
82238218 }
82248219 } else if (oldState .second () == State .Migrating ) {
82258220 if (newState == State .Running ) {
82268221 s_logger .debug ("Detected that an migrating VM is now running: " + vm );
8227- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8222+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
82288223 }
82298224 } else if (oldState .second () == State .Stopping ) {
82308225 if (newState == State .Stopped ) {
82318226 s_logger .debug ("13. The VM " + vm + " is in " + State .Stopped + " state" );
8232- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8227+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
82338228 } else if (newState == State .Running ) {
82348229 s_logger .warn ("Ignoring vm " + vm + " because of a lag in stopping the vm. " );
82358230 }
82368231 } else if (oldState .second () != newState ) {
82378232 s_logger .debug ("14. The VM " + vm + " is in " + newState + " state was " + oldState .second ());
8238- s_vms .put (_cluster , host_uuid , vm , newState , xstoolsversion );
8233+ s_vms .put (_cluster , host_uuid , vm , newState , platform );
82398234 if (newState == State .Stopped ) {
82408235 /*
82418236 * if (s_vmsKilled.remove(vm)) { s_logger.debug("VM " + vm + " has been killed for storage. ");
0 commit comments