private void FindGpuGroupAndVgpuType(Session xenSession, VirtualHardwareSection_Type system, out GPU_group gpuGroup, out VGPU_type vgpuType) { gpuGroup = null; vgpuType = null; var data = system.VirtualSystemOtherConfigurationData; if (data == null) return; var datum = data.FirstOrDefault(s => s.Name == "vgpu"); if (datum == null) return; Match m = VGPU_REGEX.Match(datum.Value.Value); if (!m.Success) return; var types = m.Groups[1].Value.Split(';'); var gpuGroups = GPU_group.get_all_records(xenSession); var gpuKvp = gpuGroups.FirstOrDefault(g => g.Value.supported_VGPU_types.Count > 0 && g.Value.GPU_types.Length == types.Length && g.Value.GPU_types.Intersect(types).Count() == types.Length); if (gpuKvp.Equals(default(KeyValuePair<XenRef<GPU_group>, GPU_group>))) return; gpuGroup = gpuKvp.Value; gpuGroup.opaque_ref = gpuKvp.Key.opaque_ref; string vendorName = m.Groups[2].Value; string modelName = m.Groups[3].Value; var vgpuTypes = VGPU_type.get_all_records(xenSession); var vgpuKey = vgpuTypes.FirstOrDefault(v => v.Value.vendor_name == vendorName && v.Value.model_name == modelName); if (vgpuKey.Equals(default(KeyValuePair<XenRef<VGPU_type>, VGPU_type>))) return; vgpuType = vgpuKey.Value; vgpuType.opaque_ref = vgpuKey.Key.opaque_ref; }
private void SetDeviceConnections(EnvelopeType ovfEnv, VirtualHardwareSection_Type vhs) { int[] connections = new int[16]; int deviceoffset = 0; List<RASD_Type> rasdList = new List<RASD_Type>(); rasdList.AddRange(vhs.Item); rasdList.Sort(compareControllerRasd); // sorts based on ResourceType.Value // For Xen really nothing to do here, does not support the different // controller types, therefore we must ensure // via positional on controllers. // IDE - #1 // SCSI - #2 // IDE 0 Disk 0 Goes to Xen: userdevice=0 // IDE 0 Disk 1 Goes to Xen: userdevice=1 // IDE 1 CD/DVD 0 Goes to Xen: userdevice=2 // IDE 1 Disk 1 UnUsed // SCSI 0 Disk 0 Goes to Xen: userdevice=3 // SCSI 0 Disk 1 Goes to Xen: userdevice=4 // and so forth. foreach (RASD_Type rasd in rasdList) { switch (rasd.ResourceType.Value) { case 5: // IDE Controller #1 case 6: // Parallel SCSI HBA #2 case 7: // FC HBA #3 case 8: // iSCSI HBA #4 case 9: // IB HCA #5 { List<RASD_Type> connectedrasds = FindConnectedItems(rasd.InstanceID.Value, vhs.Item, null); foreach (RASD_Type _rasd in connectedrasds) { //if (_rasd.Connection != null && // _rasd.Connection.Length > 0 && // _rasd.Connection[0] != null && // _rasd.Connection[0].Value != null && // _rasd.Connection[0].Value.Length > 0) if (_rasd.ResourceType.Value == 15 || _rasd.ResourceType.Value == 16) { deviceoffset = 2; } else { deviceoffset = 0; } if (Tools.ValidateProperty("Connection", _rasd)) { if (!_rasd.Connection[0].Value.ToLower().Contains("device=")) { _rasd.Connection[0].Value = string.Format("{0},device={1}", _rasd.Connection[0].Value, FindNextAvailable(deviceoffset, connections, 0)); } } else { _rasd.Connection = new cimString[] { new cimString(string.Format("device={0}", FindNextAvailable(deviceoffset, connections, 0))) }; } } break; } } } }
private XenRef<VM> DefineSystem(Session xenSession, VirtualHardwareSection_Type system, string ovfName) { // Set Defaults: Random rand = new Random(); string vM_name_label = null; if (ovfName != null) { vM_name_label = ovfName; } string vmUuid = Guid.NewGuid().ToString(); string systemType = "301"; ulong memorySize = 512 * MB; // default size. string description = Messages.DEFAULT_IMPORT_DESCRIPTION; if (system.System == null) { log.Debug("OVF.Import.DefineSystem: System VSSD is empty, guessing. HVM style"); if (vM_name_label == null) { vM_name_label = Messages.UNDEFINED_NAME_LABEL; } vmUuid = Guid.NewGuid().ToString(); systemType = "hvm-3.0-unknown"; } else { if (vM_name_label == null) { vM_name_label = Messages.UNDEFINED_NAME_LABEL; } vmUuid = Guid.NewGuid().ToString(); if (system.System.VirtualSystemType != null && !string.IsNullOrEmpty(system.System.VirtualSystemType.Value)) { systemType = system.System.VirtualSystemType.Value; } if (system.System.Description != null) description = system.System.Description.Value; } #region MEMORY // Get Memory, huh? what? oh.. ya.. RASD_Type[] rasds = OVF.FindRasdByType(system, 4); if (rasds != null && rasds.Length > 0) { // hopefully only one. but if more ... then deal with it. memorySize = 0; // These are Default to MB... if other ensure RASD is correct. double memoryPower = 20.0; double memoryRaise = 2.0; foreach (RASD_Type rasd in rasds) { if (rasd.AllocationUnits.Value.ToLower().StartsWith("bytes")) { // Format: Bytes * 2 ^ 20 string[] a1 = rasd.AllocationUnits.Value.Split(new char[] { '*', '^' }); if (a1.Length == 3) { memoryRaise = Convert.ToDouble(a1[1]); memoryPower = Convert.ToDouble(a1[2]); } } double memoryMultiplier = Math.Pow(memoryRaise,memoryPower); memorySize += rasd.VirtualQuantity.Value * Convert.ToUInt64(memoryMultiplier); } } #endregion #region CPU COUNT // Get Memory, huh? what? oh.. ya.. rasds = OVF.FindRasdByType(system, 3); ulong CpuCount = 1; if (rasds != null && rasds.Length > 0) { // // Ok this can have more than one CPU and cores each so... // Normally each entry is a CPU, the VirtualQuatity is Cores. // CpuCount = 0; foreach (RASD_Type rasd in rasds) { CpuCount += rasd.VirtualQuantity.Value; } } #endregion Hashtable table = new Hashtable(); table.Add("uuid", vmUuid); table.Add("name_label", vM_name_label); table.Add("name_description", description); table.Add("user_version", "1"); table.Add("is_a_template", false); table.Add("is_a_snapshot", false); table.Add("memory_target", Convert.ToString(memorySize)); table.Add("memory_static_max", Convert.ToString(memorySize)); table.Add("memory_dynamic_max", Convert.ToString(memorySize)); table.Add("memory_dynamic_min", Convert.ToString(memorySize)); table.Add("memory_static_min", Convert.ToString(STATMEMMIN)); table.Add("VCPUs_max", Convert.ToString(CpuCount)); table.Add("VCPUs_at_startup", Convert.ToString(CpuCount)); table.Add("actions_after_shutdown", "destroy"); table.Add("actions_after_reboot", "restart"); table.Add("actions_after_crash", "restart"); double hvmshadowmultiplier = 1.0; table.Add("HVM_shadow_multiplier", hvmshadowmultiplier); table.Add("ha_always_run", false); #region XEN SPECIFIC CONFIGURATION INFORMATION if (system.VirtualSystemOtherConfigurationData == null || system.VirtualSystemOtherConfigurationData.Length <= 0) { // DEFAULT should work for all of HVM type or 301 table.Add("HVM_boot_policy", Properties.Settings.Default.xenBootOptions); Hashtable hBoot = new Hashtable(); hBoot.Add("order", Properties.Settings.Default.xenBootOrder); table.Add("HVM_boot_params", hBoot); Hashtable hPlatform = MakePlatformHash(Properties.Settings.Default.xenPlatformSetting); table.Add("platform", hPlatform); } else { foreach (Xen_ConfigurationSettingData_Type xcsd in system.VirtualSystemOtherConfigurationData) { string key = xcsd.Name.Replace('-', '_'); switch (key.ToLower()) { case "hvm_boot_params": { Hashtable hBoot = new Hashtable(); hBoot.Add("order", xcsd.Value.Value); if (table.ContainsKey(key)) { table[key] = hBoot; } else { table.Add(key, hBoot); } break; } case "hvm_shadow_multiplier": { if (table.ContainsKey(key)) { table[key] = Convert.ToDouble(xcsd.Value.Value); } else { table.Add(key, Convert.ToDouble(xcsd.Value.Value)); } break; } case "platform": { Hashtable hPlatform = MakePlatformHash(xcsd.Value.Value); if (table.ContainsKey(key)) { table[key] = hPlatform; } else { table.Add(key, hPlatform); } break; } default: { if (table.ContainsKey(key)) { table[key] = xcsd.Value.Value; } else { table.Add(key, xcsd.Value.Value); } break; } } } } #endregion try { VM newVM = new VM(table); return VM.create(xenSession, newVM); } catch (Exception ex) { log.ErrorFormat("{0} {1}", Messages.ERROR_CREATE_VM_FAILED, ex.Message); throw new Exception(Messages.ERROR_CREATE_VM_FAILED, ex); } }