174174import com .cloud .exception .InternalErrorException ;
175175import com .cloud .host .Host .Type ;
176176import com .cloud .hypervisor .Hypervisor .HypervisorType ;
177+ import com .cloud .hypervisor .xen .resource .CitrixHelper ;
177178import com .cloud .network .HAProxyConfigurator ;
178179import com .cloud .network .LoadBalancerConfigurator ;
179180import com .cloud .network .Networks ;
@@ -1247,69 +1248,108 @@ protected VBD createVbd(Connection conn, DiskTO volume, String vmName, VM vm, Bo
12471248
12481249 return vbd ;
12491250 }
1251+
1252+
1253+ public long getStaticMax (String os , boolean b , long dynamicMinRam , long dynamicMaxRam ){
1254+ return dynamicMaxRam ;
1255+ }
1256+
1257+ public long getStaticMin (String os , boolean b , long dynamicMinRam , long dynamicMaxRam ){
1258+ return dynamicMinRam ;
1259+ }
12501260
12511261 protected VM createVmFromTemplate (Connection conn , VirtualMachineTO vmSpec , Host host ) throws XenAPIException , XmlRpcException {
12521262 String guestOsTypeName = getGuestOsType (vmSpec .getOs (), vmSpec .getBootloader () == BootloaderType .CD );
1253- if ( guestOsTypeName == null ) {
1254- String msg = " Hypervisor " + this .getClass ().getName () + " doesn't support guest OS type " + vmSpec .getOs ()
1255- + ". you can choose 'Other install media' to run it as HVM" ;
1256- s_logger .warn (msg );
1257- throw new CloudRuntimeException (msg );
1258- }
12591263 Set <VM > templates = VM .getByNameLabel (conn , guestOsTypeName );
12601264 assert templates .size () == 1 : "Should only have 1 template but found " + templates .size ();
1261- if (!templates .iterator ().hasNext ()) {
1262- throw new CloudRuntimeException ("No matching OS type found for starting a [" + vmSpec .getOs ()
1263- + "] VM on host " + host .getHostname (conn ));
1264- }
12651265 VM template = templates .iterator ().next ();
1266- VM vm = template .createClone (conn , vmSpec .getName ());
1267- VM .Record vmr = vm .getRecord (conn );
1268- if (s_logger .isDebugEnabled ()) {
1269- s_logger .debug ("Created VM " + vmr .uuid + " for " + vmSpec .getName ());
1266+
1267+ VM .Record vmr = template .getRecord (conn );
1268+ vmr .affinity = host ;
1269+ vmr .otherConfig .remove ("disks" );
1270+ vmr .otherConfig .remove ("default_template" );
1271+ vmr .otherConfig .remove ("mac_seed" );
1272+ vmr .isATemplate = false ;
1273+ vmr .nameLabel = vmSpec .getName ();
1274+ vmr .actionsAfterCrash = Types .OnCrashBehaviour .DESTROY ;
1275+ vmr .actionsAfterShutdown = Types .OnNormalExit .DESTROY ;
1276+
1277+ if (isDmcEnabled (conn , host ) && vmSpec .isEnableDynamicallyScaleVm ()) {
1278+ //scaling is allowed
1279+ vmr .memoryStaticMin = getStaticMin (vmSpec .getOs (), vmSpec .getBootloader () == BootloaderType .CD , vmSpec .getMinRam (), vmSpec .getMaxRam ());
1280+ vmr .memoryStaticMax = getStaticMax (vmSpec .getOs (), vmSpec .getBootloader () == BootloaderType .CD , vmSpec .getMinRam (), vmSpec .getMaxRam ());
1281+ vmr .memoryDynamicMin = vmSpec .getMinRam ();
1282+ vmr .memoryDynamicMax = vmSpec .getMaxRam ();
1283+ } else {
1284+ //scaling disallowed, set static memory target
1285+ if (vmSpec .isEnableDynamicallyScaleVm () && !isDmcEnabled (conn , host )) {
1286+ s_logger .warn ("Host " + host .getHostname (conn ) +" does not support dynamic scaling, so the vm " + vmSpec .getName () + " is not dynamically scalable" );
1287+ }
1288+ vmr .memoryStaticMin = vmSpec .getMinRam ();
1289+ vmr .memoryStaticMax = vmSpec .getMaxRam ();
1290+ vmr .memoryDynamicMin = vmSpec .getMinRam ();
1291+ vmr .memoryDynamicMax = vmSpec .getMaxRam ();
12701292 }
12711293
1272- for (Console console : vmr .consoles ) {
1273- console .destroy (conn );
1294+ if (guestOsTypeName .toLowerCase ().contains ("windows" )) {
1295+ vmr .VCPUsMax = (long ) vmSpec .getCpus ();
1296+ } else {
1297+ vmr .VCPUsMax = 32L ;
12741298 }
12751299
1276- vm .setIsATemplate (conn , false );
1277- vm .setAffinity (conn , host );
1278- vm .removeFromOtherConfig (conn , "disks" );
1279- vm .setNameLabel (conn , vmSpec .getName ());
1280- setMemory (conn , vm , vmSpec .getMinRam (),vmSpec .getMaxRam ());
1281- vm .setVCPUsMax (conn , (long )vmSpec .getCpus ());
1282- vm .setVCPUsAtStartup (conn , (long )vmSpec .getCpus ());
1300+ Map <String , String > details = vmSpec .getDetails ();
1301+ if ( details != null ) {
1302+ String timeoffset = details .get ("timeoffset" );
1303+ if (timeoffset != null ) {
1304+ Map <String , String > platform = vmr .platform ;
1305+ platform .put ("timeoffset" , timeoffset );
1306+ vmr .platform = platform ;
1307+ }
1308+
1309+ String coresPerSocket = details .get ("cpu.corespersocket" );
1310+ if (coresPerSocket != null ) {
1311+ Map <String , String > platform = vmr .platform ;
1312+ platform .put ("cores-per-socket" , coresPerSocket );
1313+ vmr .platform = platform ;
1314+ }
1315+ }
1316+
1317+ vmr .VCPUsAtStartup = (long ) vmSpec .getCpus ();
1318+ vmr .consoles .clear ();
1319+
1320+ VM vm = VM .create (conn , vmr );
1321+ if (s_logger .isDebugEnabled ()) {
1322+ s_logger .debug ("Created VM " + vm .getUuid (conn ) + " for " + vmSpec .getName ());
1323+ }
12831324
12841325 Map <String , String > vcpuParams = new HashMap <String , String >();
12851326
12861327 Integer speed = vmSpec .getMinSpeed ();
12871328 if (speed != null ) {
12881329
1289- int cpuWeight = _maxWeight ; //cpu_weight
1290- long utilization = 0 ; // max CPU cap, default is unlimited
1330+ int cpuWeight = _maxWeight ; // cpu_weight
1331+ int utilization = 0 ; // max CPU cap, default is unlimited
12911332
1292- // weight based allocation
1293- cpuWeight = (int )((speed * 0.99 ) / _host .speed * _maxWeight );
1333+ // weight based allocation, CPU weight is calculated per VCPU
1334+ cpuWeight = (int ) ((speed * 0.99 ) / _host .speed * _maxWeight );
12941335 if (cpuWeight > _maxWeight ) {
12951336 cpuWeight = _maxWeight ;
12961337 }
12971338
12981339 if (vmSpec .getLimitCpuUse ()) {
1299- utilization = ((long )speed * 100 * vmSpec .getCpus ()) / _host .speed ;
1340+ // CPU cap is per VM, so need to assign cap based on the number of vcpus
1341+ utilization = (int ) ((speed * 0.99 * vmSpec .getCpus ()) / _host .speed * 100 );
13001342 }
13011343
13021344 vcpuParams .put ("weight" , Integer .toString (cpuWeight ));
1303- vcpuParams .put ("cap" , Long .toString (utilization ));
1345+ vcpuParams .put ("cap" , Integer .toString (utilization ));
1346+
13041347 }
13051348
13061349 if (vcpuParams .size () > 0 ) {
13071350 vm .setVCPUsParams (conn , vcpuParams );
13081351 }
13091352
1310- vm .setActionsAfterCrash (conn , Types .OnCrashBehaviour .DESTROY );
1311- vm .setActionsAfterShutdown (conn , Types .OnNormalExit .DESTROY );
1312-
13131353 String bootArgs = vmSpec .getBootArgs ();
13141354 if (bootArgs != null && bootArgs .length () > 0 ) {
13151355 String pvargs = vm .getPVArgs (conn );
@@ -1322,43 +1362,40 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
13221362
13231363 if (!(guestOsTypeName .startsWith ("Windows" ) || guestOsTypeName .startsWith ("Citrix" ) || guestOsTypeName .startsWith ("Other" ))) {
13241364 if (vmSpec .getBootloader () == BootloaderType .CD ) {
1325- DiskTO [] disks = vmSpec .getDisks ();
1365+ DiskTO [] disks = vmSpec .getDisks ();
13261366 for (DiskTO disk : disks ) {
1327- Volume .Type type = disk .getType ();
1328- if (type == Volume .Type .ISO ) {
1329- TemplateObjectTO tmpl = (TemplateObjectTO )disk .getData ();
1330- String osType = tmpl .getGuestOsType ();
1331- if (tmpl .getFormat () == ImageFormat .ISO && osType != null ) {
1332- String isoGuestOsName = getGuestOsType (osType , vmSpec .getBootloader () == BootloaderType .CD );
1333- if (!isoGuestOsName .equals (guestOsTypeName )) {
1334- vmSpec .setBootloader (BootloaderType .PyGrub );
1335- }
1336- }
1367+ if (disk .getType () == Volume .Type .ISO ) {
1368+ TemplateObjectTO iso = (TemplateObjectTO )disk .getData ();
1369+ String osType = iso .getGuestOsType ();
1370+ if (osType != null ) {
1371+ String isoGuestOsName = getGuestOsType (osType , vmSpec .getBootloader () == BootloaderType .CD );
1372+ if (!isoGuestOsName .equals (guestOsTypeName )) {
1373+ vmSpec .setBootloader (BootloaderType .PyGrub );
1374+ }
1375+ }
13371376 }
13381377 }
13391378 }
13401379 if (vmSpec .getBootloader () == BootloaderType .CD ) {
13411380 vm .setPVBootloader (conn , "eliloader" );
1342- Map <String , String > otherConfig = vm .getOtherConfig (conn );
1343- if ( ! vm .getOtherConfig (conn ).containsKey ("install-repository" ) ) {
1344- otherConfig .put ( "install-repository" , "cdrom" );
1381+ if (!vm .getOtherConfig (conn ).containsKey ("install-repository" )) {
1382+ vm .addToOtherConfig (conn , "install-repository" , "cdrom" );
13451383 }
1346- vm .setOtherConfig (conn , otherConfig );
1347- } else if (vmSpec .getBootloader () == BootloaderType .PyGrub ){
1384+ } else if (vmSpec .getBootloader () == BootloaderType .PyGrub ) {
13481385 vm .setPVBootloader (conn , "pygrub" );
13491386 } else {
13501387 vm .destroy (conn );
13511388 throw new CloudRuntimeException ("Unable to handle boot loader type: " + vmSpec .getBootloader ());
13521389 }
13531390 }
1354-
13551391 try {
13561392 finalizeVmMetaData (vm , conn , vmSpec );
13571393 } catch ( Exception e ) {
13581394 throw new CloudRuntimeException ("Unable to finalize VM MetaData: " + vmSpec );
13591395 }
13601396 return vm ;
13611397 }
1398+
13621399
13631400 protected void finalizeVmMetaData (VM vm , Connection conn , VirtualMachineTO vmSpec ) throws Exception {
13641401 }
0 commit comments