public long CalculateMinSizeHD(string imgName, int hdNumberToGet, string newHDSize) { Image image = new Image(); image.ID = image.GetImageID(imgName); image.Read(image); Image_Physical_Specs ips = new Image_Physical_Specs(); if (!string.IsNullOrEmpty(image.ClientSizeCustom)) { ips = JsonConvert.DeserializeObject <Image_Physical_Specs>(image.ClientSizeCustom); } else { ips = JsonConvert.DeserializeObject <Image_Physical_Specs>(image.ClientSize); } ExtendedPartition EP = image.CalculateMinSizeExtended(imgName, hdNumberToGet); List <VolumeGroup> listVGS = image.CalculateMinSizeVG(imgName, hdNumberToGet); long minHDSizeRequired_BLK = 0; int lbs_BYTE = Convert.ToInt32(ips.hd[hdNumberToGet].lbs); if (Convert.ToInt64(ips.hd[hdNumberToGet].size) * lbs_BYTE == Convert.ToInt64(newHDSize)) { return(Convert.ToInt64(newHDSize)); } foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active != "1") { continue; } long minPartSize_BLK = 0; if (!string.IsNullOrEmpty(part.size_override)) { minPartSize_BLK = Convert.ToInt64(part.size_override); } else if (part.fsid.ToLower() == "8e" || part.fsid.ToLower() == "8e00") { foreach (var VG in listVGS) { if (VG.pv == ips.hd[hdNumberToGet].name + part.number) { minPartSize_BLK = VG.minSize_BLK; } } if (minPartSize_BLK == 0) { minPartSize_BLK = Convert.ToInt64(part.size); } } else if ((string.IsNullOrEmpty(part.resize) && part.type.ToLower() != "extended") || (part.type.ToLower() == "extended" && EP.isOnlySwap)) { minPartSize_BLK = Convert.ToInt64(part.size); } else { if (part.type.ToLower() == "extended") { minPartSize_BLK = EP.minSize_BLK; } else { if (string.IsNullOrEmpty(part.used_mb)) { part.used_mb = "0"; } if (Convert.ToInt64(part.resize) > Convert.ToInt64(part.used_mb)) { minPartSize_BLK = (Convert.ToInt64(part.resize) * 1024 * 1024) / lbs_BYTE; } else { minPartSize_BLK = (Convert.ToInt64(part.used_mb) * 1024 * 1024) / lbs_BYTE; } } } if (part.type.ToLower() != "logical") { minHDSizeRequired_BLK += minPartSize_BLK; } } return(minHDSizeRequired_BLK * lbs_BYTE); }
public ExtendedPartition CalculateMinSizeExtended(string imgName, int hdNumberToGet) { Image image = new Image(); image.ID = image.GetImageID(imgName); image.Read(image); Image_Physical_Specs ips = new Image_Physical_Specs(); if (!string.IsNullOrEmpty(image.ClientSizeCustom)) { ips = JsonConvert.DeserializeObject <Image_Physical_Specs>(image.ClientSizeCustom); } else { ips = JsonConvert.DeserializeObject <Image_Physical_Specs>(image.ClientSize); } int lbs_BYTE = Convert.ToInt32(ips.hd[hdNumberToGet].lbs); ExtendedPartition EP = new ExtendedPartition(); EP.minSize_BLK = 0; EP.isOnlySwap = false; bool hasExtendedPartition = false; bool hasLogicalPartition = false; //Determine if any Extended or Logical Partitions are present. Needed ahead of time correctly calculate sizes. //And calculate minimum needed extended partition size string logicalFSType = null; foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active != "1") { continue; } if (part.type.ToLower() == "extended") { hasExtendedPartition = true; } if (part.type.ToLower() == "logical") { EP.logicalCount++; logicalFSType = part.fstype; hasLogicalPartition = true; EP.hasLogical = true; } } if (EP.logicalCount == 1 && logicalFSType.ToLower() == "swap") { EP.isOnlySwap = true; } if (hasExtendedPartition) { foreach (var partition in ips.hd[hdNumberToGet].partition) { if (partition.active != "1") { continue; } if (hasExtendedPartition && hasLogicalPartition) { if (partition.type.ToLower() == "logical") { if (partition.fsid.ToLower() == "8e") { List <VolumeGroup> listVGS = image.CalculateMinSizeVG(imgName, hdNumberToGet); foreach (var VG in listVGS) { if (VG.pv == ips.hd[hdNumberToGet].name + partition.number) { EP.minSize_BLK = VG.minSize_BLK; } } if (EP.minSize_BLK == 0) { EP.minSize_BLK = Convert.ToInt64(partition.size); } } else { if (!string.IsNullOrEmpty(partition.size_override)) { EP.minSize_BLK += Convert.ToInt64(partition.size_override); } else { if (string.IsNullOrEmpty(partition.resize)) { EP.minSize_BLK += Convert.ToInt64(partition.size); } else { if (Convert.ToInt64(partition.resize) > Convert.ToInt64(partition.used_mb)) { EP.minSize_BLK += (Convert.ToInt64(partition.resize) * 1024 * 1024) / lbs_BYTE; } else { EP.minSize_BLK += (Convert.ToInt64(partition.used_mb) * 1024 * 1024) / lbs_BYTE; } } } } } } //If Hd has extended but no logical, use the extended to calc size else if (hasExtendedPartition && !hasLogicalPartition) { //In this case someone has defined an extended partition but has not created any logical //This could just be for preperation of leaving room for more logical partition later //This should be highly unlikely but should account for it anyway. There is no way of knowing a minimum size required //while still having the partition be resizable. So will set minimum sized required to unless user overrides if (partition.type.ToLower() == "extended") { if (!string.IsNullOrEmpty(partition.size_override)) { EP.minSize_BLK = Convert.ToInt64(partition.size_override); } else { //set arbitary minimum to 100MB EP.minSize_BLK = (Convert.ToInt64("100") * 1024 * 1024) / lbs_BYTE; } } } } } //Logical paritions default to 1MB more than the previous block using fdisk. This needs to be added to extended size so logical parts will fit inside long epPadding = (((1048576 / lbs_BYTE) * EP.logicalCount) + (1048576 / lbs_BYTE)); EP.minSize_BLK += epPadding; return(EP); }