private void _AddDisksTo(IVimVm replicaVm, VmDiskInfo[] disks) { ScsiControllerInfo[] scsiControllersInfo = this._GetScsiControllersInfo(replicaVm); this._Logger.Verbose("Creating and adding drives to the replica vm"); this._Context.SetLowLevelState("CreatingDisks"); this._GetDatastore(); foreach (VmDiskInfo disk in disks) { this._CheckStopping(); bool flag = false; string file = disk.File; disk.CtrlKey = scsiControllersInfo[disk.CtrlKey - 1].CtrlKey; if (!string.IsNullOrEmpty(disk.PreExistingDiskPath)) { try { IVimDatastore location = disk.Location; this._Logger.Verbose("Getting Vmdks Info on datastore: " + location.Name); string fileName = Path.GetFileName(disk.PreExistingDiskPath.Replace("\\", "/")); Dictionary <string, VmdkFileInfo> vmdksFileInfo = location.GetVmdksFileInfo(disk.PreExistingDiskPath, this._Context.ESXHost.ClientCtx); if (vmdksFileInfo == null && vmdksFileInfo.Count < 2) { throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " could not be found.", false); } VmdkFileInfo fileInfo1 = vmdksFileInfo[fileName]; VmdkFileInfo fileInfo2 = vmdksFileInfo[fileInfo1.DataFileName]; if (fileInfo1.Size + fileInfo2.Size <= 0UL) { throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " does not have a valid configuration.", false); } this._EnsureVmFolderExists(replicaVm, disk); string targetFile1 = this._VimUtils_GetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/" + fileInfo1.Name; this._Logger.Verbose(string.Format("Pre-stage folder vmdks \"{0}\" moving to destination folder \"{1}\"", (object)fileInfo1.FullName, (object)targetFile1)); this._VimDatastore_MoveFilesByFullName(location, fileInfo1, targetFile1); string targetFile2 = this._VimUtils_GetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/" + fileInfo2.DataFileName; this._Logger.Verbose(string.Format("Pre-stage folder vmdks \"{0}\" moving to destination folder \"{1}\"", (object)fileInfo2.FullName, (object)targetFile2)); this._VimDatastore_MoveFilesByFullName(location, fileInfo2, targetFile2); this._Logger.Verbose(string.Format("Pre-stage Disk found and trying to add \"{0}\"", (object)targetFile1), "CreateESXVm"); disk.File = targetFile1; this._AddVirtualDisk(replicaVm, disk); flag = true; } catch (Exception ex) { this._Logger.Error(ex, "Exception thrown on attaching a disk:"); throw ex; } } if (!flag) { this._Logger.Verbose(string.Format("Trying to create disk \"{0}\"", (object)file)); this._CreateVirtualDisk(replicaVm, disk); } this._Logger.Verbose(string.Format("disk \"{0}\" created/added. Actual file used is \"{1}\"", (object)file, (object)disk.File)); } }
public VmDiskInfo(bool exists, string file, int ctrlKey, int unitNumber, long sizeMB, string mode, IVimDatastore location, string preexistingDiskPath) { this.Exists = exists; this.File = file; this.CtrlKey = ctrlKey; this.UnitNumber = unitNumber; this.SizeMB = sizeMB; this.Mode = mode; this.Location = location; this.PreExistingDiskPath = preexistingDiskPath; }
public VmCreationInfo(string name, string guestId, int numCPU, int numCoresPerProcessor, long memoryMB, ScsiControllerType scsiControllerType, VirtualNicType nicType, string[] nicMapping, VmDiskInfo[] disks, IVimDatastore location) { this.Name = name; this.GuestId = guestId; this.NumCPU = numCPU; this.NumCoresPerProcessor = numCoresPerProcessor; this.MemoryMB = memoryMB; this.ScsiControllerType = scsiControllerType; this.NicType = nicType; this.NICMapping = nicMapping; this.Disks = disks; this.Location = location; }
public bool Invoke() { if (this._GetAllVmsIndexedByName().ContainsKey(this._Context.JobInfoWrapper.VmName)) { return(true); } IVimDatastore replicaVmDatastore = this._GetVmDatastore(); VimClientlContext vimClientlContext = (VimClientlContext) new OculiServiceVimCallContext(); string replicaVmName = this._Context.JobInfoWrapper.VmName; VimClientlContext ctx = vimClientlContext; return(replicaVmDatastore.IsFolderOnRootExist(replicaVmName, ctx)); }
private VmDiskInfo[] _BuildDiskList(OculiServiceVolumePersistedState[] internalVolumeInfos) { return(((IEnumerable <OculiServiceVolumePersistedState>)internalVolumeInfos).Select <OculiServiceVolumePersistedState, VmDiskInfo>((Func <OculiServiceVolumePersistedState, VmDiskInfo>)(vi => { if (string.IsNullOrEmpty(vi.VirtualDiskPath)) { this._Logger.FormatWarning("Volume missing Data Storage URL for vm {0} will use {1}", (object)this._Context.JobInfoWrapper.VmName, (object)this._Context.JobInfoWrapper.DataStoreUrl); vi.VirtualDiskPath = this._Context.JobInfoWrapper.DataStoreUrl; } string file = this._BuildESXDiskName(vi); IVimDatastore datastoreByUrl = this._ESXHost.GetDatastoreByUrl(vi.VirtualDiskPath); long datastoreMaxVmdkSizeMb = this._ESXHost.GetDatastoreMaxVmdkSizeMB(datastoreByUrl); VmDiskInfo disk = new VmDiskInfo(false, file, vi.VmSCSIBus + 1, vi.VmSCSIUnitNumber, Math.Min(vi.DesiredSize / 1048576L, datastoreMaxVmdkSizeMb), "persistent", datastoreByUrl, this._DiskProvisioningType(vi.DiskProvisioningType), vi.PreexistingDiskPath); this._ReportOnDisk(disk, vi); return disk; })).ToArray <VmDiskInfo>()); }
private VmDiskInfo[] _BuildDiskList(OculiServiceVolumePersistedState[] volumes) { return(((IEnumerable <OculiServiceVolumePersistedState>)volumes).Select <OculiServiceVolumePersistedState, VmDiskInfo>((Func <OculiServiceVolumePersistedState, VmDiskInfo>)(volume => { if (string.IsNullOrEmpty(volume.VirtualDiskPath)) { this._Logger.Error("Volume missing Data Storage URL for job " + this._Context.JobInfoWrapper.Name); throw new OculiServiceServiceException(0, "Volume missing Data Storage URL"); } string file = ESXHost.BuildDiskName(this._Context.JobInfoWrapper.SourceHostName, volume.Name); IVimDatastore datastoreByUrl = this._GetDatastoreByUrl(volume); long datastoreMaxVmdkSizeMb = this._ESXHost.GetDatastoreMaxVmdkSizeMB(datastoreByUrl); this._CheckDiskProvisioningType(volume); VmDiskInfo disk = new VmDiskInfo(false, file, volume.VmSCSIBus + 1, volume.VmSCSIUnitNumber, Math.Min(volume.DesiredSize / 1048576L, datastoreMaxVmdkSizeMb), "persistent", datastoreByUrl, volume.DiskProvisioningType, volume.PreexistingDiskPath); this._ReportOnDisk(disk, volume); return disk; })).ToArray <VmDiskInfo>()); }
private VmCreationInfo _BuildCreationInfo() { this._Logger.Verbose("Setting up VMDK file information"); this._AssignControllersToVolumes(); VmDiskInfo[] disks = this._BuildDiskList(this._Context.JobInfoWrapper.VolumePersistedState); if (disks.Length == 0) { throw new OculiServiceServiceException(0, "There are no disks to create"); } this._Logger.Verbose("Setting up NIC information"); this._AssignDeviceAndBusNumbers(this._Context.JobInfoWrapper.Nics); OculiServiceInternalVirtualNetworkInterfaceInfo[] replicaNics = this._Context.JobInfoWrapper.Nics; string[] nicMapping = this._VirtualNetworkList(replicaNics); this._Logger.Verbose("Added information for " + replicaNics.Length.ToString() + " nics."); VirtualNicType nicType = this._VirtualNicType(); this._CheckStopping(); IVimDatastore replicaDatastore = this._GetDatastore(); return(new VmCreationInfo(this._Context.JobInfoWrapper.VmName, this._Context.JobInfoWrapper.GuestOS, this._Context.JobInfoWrapper.NumberOfCPUs, this._Context.JobInfoWrapper.NumberOfCoresPerProcessor, this._Context.JobInfoWrapper.RamSizeMB, this._ScsiControllerType(), nicType, nicMapping, disks, replicaDatastore)); }
protected virtual IVimVm _CreateVm() { this._Context.SetLowLevelState("CreatingVm"); IVimDatastore datastoreByUrl = this._ESXHost.GetDatastoreByUrl(this._Context.JobInfoWrapper.DataStoreUrl); string replicaVmName = this._Context.JobInfoWrapper.VmName; if (datastoreByUrl.IsFolderOnRootExist(replicaVmName, this._Context.ESXHost.ClientCtx)) { throw new OculiServiceServiceException(0, string.Format("The folder for the vm {0} already exists on the datastore {1}", (object)replicaVmName, (object)datastoreByUrl.BracketedName)); } VirtualMachineConfigSpec compatibleConfigSpec = this._Context.ESXHost.SourceVm().GetCompatibleConfigSpec(datastoreByUrl.Name, this._Context.JobInfoWrapper.VmName); compatibleConfigSpec.numCPUs = Math.Min((int)this._ESXHost.GetNumberCPU(), compatibleConfigSpec.numCPUs); long num = Math.Min(this._ESXHost.GetMemory() / 1048576L, compatibleConfigSpec.memoryMB); compatibleConfigSpec.memoryMB = num - num % 4L; this._Logger.FormatInformation(" VM will have {0} CPUs and {1}MB memory.", (object)compatibleConfigSpec.numCPUs, (object)compatibleConfigSpec.memoryMB); Dictionary <string, string> networkMap = new Dictionary <string, string>(); ((IEnumerable <VirtualSwitchMapping>) this._Context.JobInfoWrapper.VirtualSwitchMapping).ForEach <VirtualSwitchMapping>((System.Action <VirtualSwitchMapping>)(vsm => networkMap.Add(vsm.SourceVirtualSwitch.Label, vsm.TargetVirtualSwitch.Label))); Dictionary <string, string> distributedPortGroupMap = this._Context.ESXHost.SourceVm().GetHostAndProperties().GetDistributedVirtualPortgroups(); ((IEnumerable <VirtualDeviceConfigSpec>)compatibleConfigSpec.deviceChange).Where <VirtualDeviceConfigSpec>((Func <VirtualDeviceConfigSpec, bool>)(vdcs => { if (vdcs.device is VirtualEthernetCard) { return(vdcs.device.backing is VirtualEthernetCardDistributedVirtualPortBackingInfo); } return(false); })).ForEach <VirtualDeviceConfigSpec>((System.Action <VirtualDeviceConfigSpec>)(vdcs => { string str1 = distributedPortGroupMap[((VirtualEthernetCardDistributedVirtualPortBackingInfo)vdcs.device.backing).port.portgroupKey]; int length = str1.IndexOf(" (", 0); string str2 = str1.Substring(0, length); vdcs.device.deviceInfo.summary = str2; })); return(this._ESXHost.CreateVmWithNetworkMapping(compatibleConfigSpec, networkMap, this._Context.ESXHost.ClientCtx)); }
private void _ProcessPrestagedDisk(IVimVm replicaVm, VmDiskInfo disk) { try { IVimDatastore location = disk.Location; this._Logger.Information("Getting Vmdks Info on datastore: " + location.Name); string fileName = Path.GetFileName(disk.PreExistingDiskPath.Replace("\\", "/")); Dictionary <string, VmdkFileInfo> vmdksFileInfo = location.GetVmdksFileInfo(disk.PreExistingDiskPath, this._Context.ESXHost.ClientCtx); if (vmdksFileInfo == null && vmdksFileInfo.Count < 2) { throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " could not be found.", false); } VmdkFileInfo vmdkFileInfo1 = vmdksFileInfo[fileName]; VmdkFileInfo vmdkFileInfo2 = vmdksFileInfo[vmdkFileInfo1.DataFileName]; if (vmdkFileInfo1.Size + vmdkFileInfo2.Size <= 0UL) { throw new EsxException("The pre-existing disk " + disk.PreExistingDiskPath + " does not have a valid configuration.", false); } string str = this._VimUtilsGetVolumeName(location) + this._Context.JobInfoWrapper.VmName + "/"; string name = vmdkFileInfo1.Name; string target1 = str + name; this._Logger.Information(string.Format("Pre-stage folder vmdk-s \"{0}\" moving to destination folder \"{1}\"", (object)vmdkFileInfo1.FullName, (object)target1)); location.MoveFilesByFullName(vmdkFileInfo1.FullName, target1, this._Context.JobInfoWrapper.VmName, true, this._Context.ESXHost.ClientCtx); string dataFileName = vmdkFileInfo1.DataFileName; string target2 = str + dataFileName; this._Logger.Information(string.Format("Pre-stage folder vmdk-s \"{0}\" moving to destination folder \"{1}\"", (object)vmdkFileInfo2.FullName, (object)target2)); location.MoveFilesByFullName(vmdkFileInfo2.FullName, target2, this._Context.JobInfoWrapper.VmName, true, this._Context.ESXHost.ClientCtx); this._Logger.Information(string.Format("Pre-stage Disk found and trying to add \"{0}\"", (object)target1)); disk.File = target1; this._AddVirtualDisk(replicaVm, disk); } catch (Exception ex) { this._Logger.Error("Exception thrown on attaching a disk" + ex.ToString()); throw ex; } }
protected virtual string _VimUtilsGetVolumeName(IVimDatastore diskDatastore) { return(VimUtils.GetVolumeName(diskDatastore.Name)); }
public static VmdkMapping[] CreateTargetVmdkMappings(VmdkMapping[] srcVmdkMappings, IVimDatastore ds, string vmxPath) { List <VmdkMapping> vmdkMappingList = new List <VmdkMapping>(); List <string> vmdkBaseNames = new List <string>(); foreach (VmdkMapping srcVmdkMapping in srcVmdkMappings) { VmdkMapping vmdkMapping = new VmdkMapping(); vmdkMapping.IsSnapshotable = srcVmdkMapping.IsSnapshotable; vmdkMapping.Key = srcVmdkMapping.Key; vmdkMapping.Label = srcVmdkMapping.Label; vmdkMapping.VmdkSize = 0L; vmdkMapping.DatastoreName = ds.DsProperties.Name; vmdkMapping.DatastoreUrl = ds.DsProperties.Url; string str1 = VmxConfiguration.ResolveBaseName(srcVmdkMapping.VmdkBaseName, vmdkBaseNames); string extension = Path.GetExtension(srcVmdkMapping.VmdkShortName); string str2 = str1 + extension; string str3 = str1 + "-flat" + extension; vmdkMapping.VmdkDir = vmxPath; vmdkMapping.VmdkBaseName = str1; vmdkMapping.VmdkShortName = str2; vmdkMapping.VmdkShortNameBin = str3; vmdkMapping.VmdkFullName = vmxPath + "/" + str2; vmdkMapping.VmdkFullNameBin = vmxPath + "/" + str3; vmdkMappingList.Add(vmdkMapping); } return(vmdkMappingList.ToArray()); }
protected virtual void _VimDatastore_MoveFilesByFullName(IVimDatastore diskDatastore, VmdkFileInfo fileInfo, string targetFile) { diskDatastore.MoveFilesByFullName(fileInfo.FullName, targetFile, this._Context.JobInfoWrapper.VmName, true, this._Context.ESXHost.ClientCtx); }