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)); } }
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; } }