public VHDInfo GetBaseCfg() { VHDInfo vhdInfo; try { vhdInfo = this.GetCfg(); while (!string.IsNullOrEmpty(vhdInfo.ParentPath)) { using (IVirtualDisk virtualDisk = (IVirtualDisk) new VirtualDisk(this._Host, vhdInfo.ParentPath, this._Logger)) vhdInfo = virtualDisk.GetCfg(); } } catch (Exception ex) { vhdInfo = new VHDInfo(); this._Logger.FormatErrorWithException(ex, "Failed to get the base vhd cfg information due to exception: "); } return(vhdInfo); }
public void CreateVirtualDisk(string path, ulong size, string virtualDiskType, ushort format) { if (File.Exists(path)) { this._Logger.Information(string.Format("Validating existing disk {0}...", (object)path)); try { using (IVirtualDisk virtualDisk = this.ValidateVirtualDisk(path)) { if (virtualDisk != null) { if (virtualDisk.GetCfg().MaxInternalSize >= (long)size) { this._Logger.Information("Existing disk validated."); return; } } } } catch (Exception ex) { if (this.HandleException(ex)) { throw; } else { this._Logger.Warning(ex, "Existing disk validation failed"); } } this._Logger.Information(string.Format("Deleting existing disk {0}...", (object)path)); File.Delete(path); } if ((long)(size % 4096UL) != 0L) { size += 4096UL - size % 4096UL; } using (IImageManagementService managementService = ImageManagementService.GetImageManagementService(this._Host)) { using (IVirtualHardDiskSettingData hardDiskSettingData = VirtualHardDiskSettingData.CreateVirtualHardDiskSettingData(this._Host)) { hardDiskSettingData.Path = path; hardDiskSettingData.MaxInternalSize = size; if (!(virtualDiskType == "Fixed")) { if (!(virtualDiskType == "Dynamic")) { throw new HyperVException("Don't know how to create a disk of type " + virtualDiskType); } hardDiskSettingData.Type = (ushort)3; } else { hardDiskSettingData.Type = (ushort)2; } hardDiskSettingData.Format = format; hardDiskSettingData.PhysicalSectorSize = 512U; managementService.CreateVirtualHardDisk(hardDiskSettingData); } } }