public void getPartLayout(string imgName, string hdToGet, string newHDSize, string clientHD, string taskType) { 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 activeCounter = Convert.ToInt32(hdToGet); int hdNumberToGet = Convert.ToInt32(hdToGet) - 1; //Look for first active hd if (ips.hd[hdNumberToGet].active != "1") { while (activeCounter <= ips.hd.Count()) { if (ips.hd[activeCounter - 1].active == "1") { hdNumberToGet = activeCounter - 1; } activeCounter++; } } newHDSize = (Convert.ToInt64(newHDSize) * .99).ToString("#"); int lbs_BYTE = Convert.ToInt32(ips.hd[hdNumberToGet].lbs); long newHD_BYTES = Convert.ToInt64(newHDSize); long newHD_BLK = Convert.ToInt64(newHDSize) / lbs_BYTE; List<Partition_Resized_Client> listPhysicalAndExtended = new List<Partition_Resized_Client>(); List<Partition_Resized_Client> listLogical = new List<Partition_Resized_Client>(); List<Partition_Resized_Client_LVM> listLVM = new List<Partition_Resized_Client_LVM>(); string bootPart = null; if(ips.hd[hdNumberToGet].boot.Length > 0) bootPart = ips.hd[hdNumberToGet].boot.Substring(ips.hd[hdNumberToGet].boot.Length - 1, 1); ExtendedPartition EP = image.CalculateMinSizeExtended(imgName,hdNumberToGet); List<VolumeGroup> listVGS = new List<VolumeGroup>(); long agreedExtendedSize_BLK = 0; double percentCounter = 0; bool partLayoutVerified = false; bool hasResizableVG = false; int startPart = 0; while (!partLayoutVerified) { double totalPercentage = 0; listPhysicalAndExtended.Clear(); listLogical.Clear(); listVGS.Clear(); startPart = Convert.ToInt32(ips.hd[hdNumberToGet].partition[0].start); int partCounter = 0; hasResizableVG = false; foreach (var part in ips.hd[hdNumberToGet].partition) { if (Convert.ToInt32(part.start) < startPart) startPart = Convert.ToInt32(part.start); if (part.active != "1") continue; if (part.type.ToLower() == "logical") continue; VolumeGroup VG = image.SinglePartMinSizeVG(imgName, hdNumberToGet, partCounter); if (VG.pv != null) { part.resize = (VG.minSize_BLK * lbs_BYTE / 1024 / 1024).ToString(); hasResizableVG = true; } Partition_Resized_Client resizedPart = new Partition_Resized_Client(); if (bootPart == part.number) resizedPart.isBoot = true; else resizedPart.isBoot = false; resizedPart.number = part.number; resizedPart.start = part.start; resizedPart.type = part.type; resizedPart.fsid = part.fsid; resizedPart.uuid = part.uuid; resizedPart.guid = part.guid; resizedPart.fstype = part.fstype; //If not resizable set aside a minimum size based off original partition length long newPartSize_BLK = 0; double tmpTotalPercentage = 0; if (!string.IsNullOrEmpty(part.size_override)) { newPartSize_BLK = Convert.ToInt64(part.size_override); resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / newHD_BLK; resizedPart.partResized = false; if (resizedPart.type.ToLower() == "extended") { resizedPart.size = EP.minSize_BLK.ToString(); agreedExtendedSize_BLK = EP.minSize_BLK; } } else if ((string.IsNullOrEmpty(part.resize) && part.type.ToLower() != "extended") || (part.type.ToLower() == "extended" && EP.isOnlySwap) || Convert.ToInt64(part.size) * lbs_BYTE <= 2097152000) { newPartSize_BLK = Convert.ToInt64(part.size); resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / newHD_BLK; resizedPart.partResized = false; if (resizedPart.type.ToLower() == "extended") { resizedPart.size = EP.minSize_BLK.ToString(); agreedExtendedSize_BLK = EP.minSize_BLK; } } //If resizable determine what percent of drive partition was originally and match that to the new drive //while making sure the min size is still greater than the resized size. else { resizedPart.partResized = true; if (part.type.ToLower() == "extended") newPartSize_BLK = EP.minSize_BLK; else { if (VG.pv != null) { newPartSize_BLK = (Convert.ToInt64(part.resize) * 1024 * 1024) / lbs_BYTE; } else { if (string.IsNullOrEmpty(part.used_mb)) part.used_mb = "0"; if (Convert.ToInt64(part.resize) > Convert.ToInt64(part.used_mb)) newPartSize_BLK = (Convert.ToInt64(part.resize) * 1024 * 1024) / lbs_BYTE; else newPartSize_BLK = (Convert.ToInt64(part.used_mb) * 1024 * 1024) / lbs_BYTE; } } double percentOfOrigDrive = (double)(Convert.ToInt64(part.size) / (double)(Convert.ToInt64(ips.hd[hdNumberToGet].size))); if (percentOfOrigDrive - (percentCounter / 100) <= 0) resizedPart.size = (newHD_BLK * (percentOfOrigDrive)).ToString("#"); else resizedPart.size = (newHD_BLK * (percentOfOrigDrive - (percentCounter / 100))).ToString("#"); if (resizedPart.type.ToLower() == "extended") tmpTotalPercentage = ((double)(Convert.ToInt64(resizedPart.size)) + (((1048576 / lbs_BYTE) * EP.logicalCount) + (1048576 / lbs_BYTE))) / newHD_BLK; else tmpTotalPercentage = (double)(Convert.ToInt64(resizedPart.size)) / newHD_BLK; if (Convert.ToInt64(resizedPart.size) < newPartSize_BLK) { if (resizedPart.type.ToLower() == "extended") { resizedPart.size = (newPartSize_BLK + (((1048576 / lbs_BYTE) * EP.logicalCount) + (1048576 / lbs_BYTE))).ToString(); tmpTotalPercentage = (double)(Convert.ToInt64(resizedPart.size)) / newHD_BLK; } else { resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)(Convert.ToInt64(resizedPart.size)) / newHD_BLK; } } if (resizedPart.type.ToLower() == "extended") agreedExtendedSize_BLK = Convert.ToInt64(resizedPart.size); } if (hasResizableVG) { VG.agreedPVSize_BLK = Convert.ToInt64(resizedPart.size); listVGS.Add(VG); } totalPercentage += tmpTotalPercentage; listPhysicalAndExtended.Add(resizedPart); partCounter++; } percentCounter++; if (totalPercentage <= 1) { long totalAllocated_BLK = 0; long totalUnallocated = 0; int resizablePartsCount = 0; //If totalPercentage is too far below 1 try to increase size of available resizable partitions if (totalPercentage < .95) { foreach (var partition in listPhysicalAndExtended) { totalAllocated_BLK += Convert.ToInt64(partition.size); if (partition.partResized) resizablePartsCount++; } totalUnallocated = newHD_BLK - totalAllocated_BLK; if (resizablePartsCount > 0) { foreach (var partition in listPhysicalAndExtended) { if (partition.partResized) { partition.size = (Convert.ToInt64(partition.size) + (totalUnallocated / resizablePartsCount)).ToString(); if (partition.type.ToLower() == "extended") agreedExtendedSize_BLK = Convert.ToInt64(partition.size); for (int i = 0; i < listVGS.Count(); i++) if (ips.hd[hdNumberToGet].name + partition.number == listVGS[i].pv) listVGS[i].agreedPVSize_BLK = Convert.ToInt64(partition.size); } } } } partLayoutVerified = true; } //Theoretically should never hit this, but added to prevent infinite loop if (percentCounter == 99) { HttpContext.Current.Response.Write("false"); Logger.Log("Error. Could Not Determine A Partition Layout That Fits This HD"); return; } } //Try to resize lv to fit inside newly created lvm if (partLayoutVerified && hasResizableVG) { foreach (var volumeGroup in listVGS) { volumeGroup.agreedPVSize_BLK = Convert.ToInt64(volumeGroup.agreedPVSize_BLK * .99); foreach (var partition in ips.hd[hdNumberToGet].partition) { if (ips.hd[hdNumberToGet].name + partition.number == volumeGroup.pv) { bool singleLvVerified = false; percentCounter = 0; while (!singleLvVerified) { double totalPercentage = 0; listLVM.Clear(); if (partition.active != "1") continue; foreach (var lv in partition.vg.lv) { if (lv.active != "1") continue; Partition_Resized_Client_LVM resizedLV = new Partition_Resized_Client_LVM(); resizedLV.name = lv.name; resizedLV.vg = lv.vg; resizedLV.uuid = lv.uuid; resizedLV.fstype = lv.fstype; //If not resizable set aside a minimum size based off original partition length long newPartSize_BLK = 0; double tmpTotalPercentage = 0; if (!string.IsNullOrEmpty(lv.size_override)) { newPartSize_BLK = Convert.ToInt64(lv.size_override); resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / volumeGroup.agreedPVSize_BLK; resizedLV.partResized = false; } else if (string.IsNullOrEmpty(lv.resize)) { newPartSize_BLK = (Convert.ToInt64(lv.size)); resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / volumeGroup.agreedPVSize_BLK; resizedLV.partResized = false; } //If resizable determine what percent of drive partition was originally and match that to the new drive //while making sure the min size is still greater than the resized size. else { resizedLV.partResized = true; if (Convert.ToInt64(lv.resize) > Convert.ToInt64(lv.used_mb)) newPartSize_BLK = (Convert.ToInt64(lv.resize) * 1024 * 1024) / lbs_BYTE; else newPartSize_BLK = (Convert.ToInt64(lv.used_mb) * 1024 * 1024) / lbs_BYTE; double percentOfOrigDrive = (double)(Convert.ToInt64(lv.size) / (double)(Convert.ToInt64(ips.hd[hdNumberToGet].size))); if (percentOfOrigDrive - (percentCounter / 100) <= 0) resizedLV.size = (volumeGroup.agreedPVSize_BLK * (percentOfOrigDrive)).ToString("#"); else resizedLV.size = (volumeGroup.agreedPVSize_BLK * (percentOfOrigDrive - (percentCounter / 100))).ToString("#"); tmpTotalPercentage = (double)(Convert.ToInt64(resizedLV.size)) / volumeGroup.agreedPVSize_BLK; if (Convert.ToInt64(resizedLV.size) < newPartSize_BLK) { resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)(Convert.ToInt64(resizedLV.size)) / volumeGroup.agreedPVSize_BLK; } } totalPercentage += tmpTotalPercentage; listLVM.Add(resizedLV); } percentCounter++; if (totalPercentage <= 1) { long totalAllocated_BLK = 0; long totalUnallocated = 0; int resizablePartsCount = 0; //If totalPercentage is too far below 1 try to increase size of available resizable partitions if (totalPercentage < .95) { foreach (var lv in listLVM) { totalAllocated_BLK += Convert.ToInt64(lv.size); if (lv.partResized) resizablePartsCount++; } totalUnallocated = volumeGroup.agreedPVSize_BLK - totalAllocated_BLK; if (resizablePartsCount > 0) { foreach (var lv in listLVM) { if (lv.partResized) lv.size = (Convert.ToInt64(lv.size) + (totalUnallocated / resizablePartsCount)).ToString(); } } } singleLvVerified = true; } //Theoretically should never hit this, but added to prevent infinite loop if (percentCounter == 99) { HttpContext.Current.Response.Write("Error"); break; } } } } } } // Try to resize logical to fit inside newly created extended if (partLayoutVerified && EP.hasLogical) { percentCounter = 0; bool logicalPartLayoutVerified = false; while (!logicalPartLayoutVerified) { listLogical.Clear(); double totalPercentage = 0; foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.type.ToLower() != "logical") continue; Partition_Resized_Client resizedPart = new Partition_Resized_Client(); if (bootPart == part.number) resizedPart.isBoot = true; else resizedPart.isBoot = false; resizedPart.number = part.number; resizedPart.start = part.start; resizedPart.type = part.type; resizedPart.fsid = part.fsid; resizedPart.uuid = part.uuid; resizedPart.guid = part.guid; resizedPart.fstype = part.fstype; //If not resizable set aside a minimum size based off original partition length long newPartSize_BLK = 0; double tmpTotalPercentage = 0; //If partition is logical and lvm if (part.fsid.ToLower() == "8e") { //List<VolumeGroup> listVGS_lv = new List<VolumeGroup>(); listVGS = image.CalculateMinSizeVG(imgName, hdNumberToGet); foreach (var VG in listVGS) { if (VG.pv == ips.hd[hdNumberToGet].name + part.number) newPartSize_BLK = VG.minSize_BLK; } if (newPartSize_BLK == 0) newPartSize_BLK = Convert.ToInt64(part.size); part.resize = ((newPartSize_BLK * lbs_BYTE) / 1024 / 1024).ToString(); } if (!string.IsNullOrEmpty(part.size_override)) { newPartSize_BLK = Convert.ToInt64(part.size_override); resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / agreedExtendedSize_BLK; resizedPart.partResized = false; } if (string.IsNullOrEmpty(part.resize)) { newPartSize_BLK = (Convert.ToInt64(part.size)); resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / agreedExtendedSize_BLK; resizedPart.partResized = false; } //If resizable determine what percent of drive partition was originally and match that to the new drive //while making sure the min size is still greater than the resized size. else { resizedPart.partResized = true; if (part.fsid.ToLower() == "8e") { newPartSize_BLK = (Convert.ToInt64(part.resize) * 1024 * 1024) / lbs_BYTE; } else { if (Convert.ToInt64(part.resize) > Convert.ToInt64(part.used_mb)) newPartSize_BLK = (Convert.ToInt64(part.resize) * 1024 * 1024) / lbs_BYTE; else newPartSize_BLK = (Convert.ToInt64(part.used_mb) * 1024 * 1024) / lbs_BYTE; } if (taskType == "upload") resizedPart.size = newPartSize_BLK.ToString(); else { double percentOfOrigDrive = (double)(Convert.ToInt64(part.size) / (double)(Convert.ToInt64(ips.hd[hdNumberToGet].size))); if (percentOfOrigDrive - (percentCounter / 100) <= 0) resizedPart.size = (agreedExtendedSize_BLK * (percentOfOrigDrive)).ToString("#"); else resizedPart.size = (agreedExtendedSize_BLK * (percentOfOrigDrive - (percentCounter / 100))).ToString("#"); tmpTotalPercentage = (double)(Convert.ToInt64(resizedPart.size)) / agreedExtendedSize_BLK; if (Convert.ToInt64(resizedPart.size) < newPartSize_BLK) { resizedPart.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)(Convert.ToInt64(resizedPart.size)) / agreedExtendedSize_BLK; } } } totalPercentage += tmpTotalPercentage; listLogical.Add(resizedPart); } percentCounter++; if (totalPercentage <= 1) { long totalAllocated_BLK = 0; long totalUnallocated = 0; int resizablePartsCount = 0; //If totalPercentage is too far below 1 try to increase size of available resizable partitions if (totalPercentage < .95) { foreach (var partition in listLogical) { totalAllocated_BLK += Convert.ToInt64(partition.size); if (partition.partResized) resizablePartsCount++; } totalUnallocated = agreedExtendedSize_BLK - totalAllocated_BLK; if (resizablePartsCount > 0) { foreach (var partition in listLogical) { if (partition.partResized) partition.size = (Convert.ToInt64(partition.size) + (totalUnallocated / resizablePartsCount)).ToString(); } } } logicalPartLayoutVerified = true; //testing if logical is also lvm foreach (var part in listLogical) { if (part.fsid.ToLower() == "8e") { foreach (var VG in listVGS) { if (VG.pv == ips.hd[hdNumberToGet].name + part.number) { VG.agreedPVSize_BLK = Convert.ToInt64(part.size); } } foreach (var volumeGroup in listVGS) { volumeGroup.agreedPVSize_BLK = Convert.ToInt64(volumeGroup.agreedPVSize_BLK * .99); foreach (var partition in ips.hd[hdNumberToGet].partition) { if (ips.hd[hdNumberToGet].name + partition.number == volumeGroup.pv) { bool singleLvVerified = false; percentCounter = 0; while (!singleLvVerified) { totalPercentage = 0; listLVM.Clear(); if (partition.active != "1") continue; foreach (var lv in partition.vg.lv) { if (lv.active != "1") continue; Partition_Resized_Client_LVM resizedLV = new Partition_Resized_Client_LVM(); resizedLV.name = lv.name; resizedLV.vg = lv.vg; resizedLV.uuid = lv.uuid; resizedLV.fstype = lv.fstype; //If not resizable set aside a minimum size based off original partition length long newPartSize_BLK = 0; double tmpTotalPercentage = 0; if (!string.IsNullOrEmpty(lv.size_override)) { newPartSize_BLK = Convert.ToInt64(lv.size_override); resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / volumeGroup.agreedPVSize_BLK; resizedLV.partResized = false; } else if (string.IsNullOrEmpty(lv.resize)) { newPartSize_BLK = (Convert.ToInt64(lv.size)); resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)newPartSize_BLK / volumeGroup.agreedPVSize_BLK; resizedLV.partResized = false; } //If resizable determine what percent of drive partition was originally and match that to the new drive //while making sure the min size is still greater than the resized size. else { resizedLV.partResized = true; if (Convert.ToInt64(lv.resize) > Convert.ToInt64(lv.used_mb)) newPartSize_BLK = (Convert.ToInt64(lv.resize) * 1024 * 1024) / lbs_BYTE; else newPartSize_BLK = (Convert.ToInt64(lv.used_mb) * 1024 * 1024) / lbs_BYTE; double percentOfOrigDrive = (double)(Convert.ToInt64(lv.size) / (double)(Convert.ToInt64(ips.hd[hdNumberToGet].size))); if (percentOfOrigDrive - (percentCounter / 100) <= 0) resizedLV.size = (volumeGroup.agreedPVSize_BLK * (percentOfOrigDrive)).ToString("#"); else resizedLV.size = (volumeGroup.agreedPVSize_BLK * (percentOfOrigDrive - (percentCounter / 100))).ToString("#"); tmpTotalPercentage = (double)(Convert.ToInt64(resizedLV.size)) / volumeGroup.agreedPVSize_BLK; if (Convert.ToInt64(resizedLV.size) < newPartSize_BLK) { resizedLV.size = newPartSize_BLK.ToString(); tmpTotalPercentage = (double)(Convert.ToInt64(resizedLV.size)) / volumeGroup.agreedPVSize_BLK; } } totalPercentage += tmpTotalPercentage; listLVM.Add(resizedLV); } percentCounter++; if (totalPercentage <= 1) { totalAllocated_BLK = 0; totalUnallocated = 0; resizablePartsCount = 0; //If totalPercentage is too far below 1 try to increase size of available resizable partitions if (totalPercentage < .95) { foreach (var lv in listLVM) { totalAllocated_BLK += Convert.ToInt64(lv.size); if (lv.partResized) resizablePartsCount++; } totalUnallocated = volumeGroup.agreedPVSize_BLK - totalAllocated_BLK; if (resizablePartsCount > 0) { foreach (var lv in listLVM) { if (lv.partResized) lv.size = (Convert.ToInt64(lv.size) + (totalUnallocated / resizablePartsCount)).ToString(); } } } singleLvVerified = true; } //Theoretically should never hit this, but added to prevent infinite loop if (percentCounter == 99) { HttpContext.Current.Response.Write("Error"); break; } } } } } } } } //Theoretically should never hit this, but added to prevent infinite loop if (percentCounter == 99) { HttpContext.Current.Response.Write("Error"); break; } } } //Order partitions based of block start listPhysicalAndExtended = listPhysicalAndExtended.OrderBy(Part => Part.start, new Image.CustomComparer()).ToList(); listLogical = listLogical.OrderBy(Part => Part.start, new Image.CustomComparer()).ToList(); if(taskType == "debug") { try { agreedExtendedSize_BLK = agreedExtendedSize_BLK * 512 / 1024 / 1024; } catch { } foreach(var p in listPhysicalAndExtended) p.size = (Convert.ToInt64(p.size) * 512 / 1024 /1024).ToString(); foreach (var p in listLogical) p.size = (Convert.ToInt64(p.size) * 512 / 1024 / 1024).ToString(); foreach (var p in listLVM) p.size = (Convert.ToInt64(p.size) * 512 / 1024 / 1024).ToString(); } //Create Menu if (ips.hd[hdNumberToGet].table.ToLower() == "mbr") { int counter = 0; int partCount = listPhysicalAndExtended.Count; string partitionCommands = null; if(Convert.ToInt32(listPhysicalAndExtended[0].start) < 2048) partitionCommands = "fdisk -c=dos " + clientHD + " &>>/tmp/clientlog.log <<FDISK\r\n"; else partitionCommands = "fdisk " + clientHD + " &>>/tmp/clientlog.log <<FDISK\r\n"; foreach (var part in listPhysicalAndExtended) { counter++; partitionCommands += "n\r\n"; if (part.type == "primary") partitionCommands += "p\r\n"; else if (part.type == "extended") { partitionCommands += "e\r\n"; } partitionCommands += part.number + "\r\n"; if (counter == 1 || taskType == "upload") partitionCommands += startPart.ToString() + "\r\n"; else partitionCommands += "\r\n"; if(part.type == "extended") partitionCommands += "+" + (Convert.ToInt64(agreedExtendedSize_BLK) - 1).ToString() + "\r\n"; else //FDISK seems to include the starting sector in size so we need to subtract 1 partitionCommands += "+" + (Convert.ToInt64(part.size) -1).ToString() + "\r\n"; partitionCommands += "t\r\n"; if (counter == 1) partitionCommands += part.fsid + "\r\n"; else { partitionCommands += part.number + "\r\n"; partitionCommands += part.fsid + "\r\n"; } if (counter == 1 && part.isBoot) partitionCommands += "a\r\n"; if (counter != 1 && part.isBoot) { partitionCommands += "a\r\n"; partitionCommands += part.number + "\r\n"; } if ((counter == partCount && listLogical.Count == 0)) { partitionCommands += "w\r\n"; partitionCommands += "FDISK\r\n"; } } int logicalCounter = 0; foreach (var logicalPart in listLogical) { logicalCounter++; partitionCommands += "n\r\n"; if (listPhysicalAndExtended.Count < 4) partitionCommands += "l\r\n"; partitionCommands += "\r\n"; if (taskType == "debug") partitionCommands += "+" + (Convert.ToInt64(logicalPart.size) - (logicalCounter * 1)).ToString() + "\r\n"; else partitionCommands += "+" + (Convert.ToInt64(logicalPart.size) - (logicalCounter * 2049)).ToString() + "\r\n"; partitionCommands += "t\r\n"; partitionCommands += logicalPart.number + "\r\n"; partitionCommands += logicalPart.fsid + "\r\n"; if (logicalPart.isBoot) { partitionCommands += "a\r\n"; partitionCommands += logicalPart.number + "\r\n"; } if (logicalCounter == listLogical.Count) { partitionCommands += "w\r\n"; partitionCommands += "FDISK\r\n"; } } HttpContext.Current.Response.Write(partitionCommands); } else { int counter = 0; int partCount = listPhysicalAndExtended.Count; string partitionCommands = "gdisk " + clientHD + " &>>/tmp/clientlog.log <<GDISK\r\n"; foreach (var part in listPhysicalAndExtended) { counter++; partitionCommands += "n\r\n"; partitionCommands += part.number + "\r\n"; if (counter == 1 || taskType == "upload") partitionCommands += startPart.ToString() + "\r\n"; else partitionCommands += "\r\n"; //GDISK seems to NOT include the starting sector in size so don't subtract 1 like in FDISK partitionCommands += "+" + Convert.ToInt64(part.size).ToString() + "\r\n"; partitionCommands += part.fsid + "\r\n"; if ((counter == partCount)) { partitionCommands += "w\r\n"; partitionCommands += "y\r\n"; partitionCommands += "GDISK\r\n"; } } HttpContext.Current.Response.Write(partitionCommands); } foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active != "1") continue; if (part.vg != null) { if (part.vg.lv != null) { HttpContext.Current.Response.Write("echo \"pvcreate -u " + part.uuid + " --norestorefile -yf " + clientHD + part.vg.pv[part.vg.pv.Length - 1] + "\" >>/tmp/lvmcommands \r\n"); HttpContext.Current.Response.Write("echo \"vgcreate " + part.vg.name + " " + clientHD + part.vg.pv[part.vg.pv.Length - 1] + " -yf" + "\" >>/tmp/lvmcommands \r\n"); HttpContext.Current.Response.Write("echo \"" + part.vg.uuid + "\" >>/tmp/vg-" + part.vg.name + " \r\n"); foreach (var lv in part.vg.lv) { foreach(var rlv in listLVM) { if (lv.name == rlv.name && lv.vg == rlv.vg) { HttpContext.Current.Response.Write("echo \"lvcreate -L " + ((Convert.ToInt64(rlv.size) - 8192).ToString()) + "s -n " + rlv.name + " " + rlv.vg + "\" >>/tmp/lvmcommands \r\n"); HttpContext.Current.Response.Write("echo \"" + rlv.uuid + "\" >>/tmp/" + rlv.vg + "-" + rlv.name + "\r\n"); } } } HttpContext.Current.Response.Write("echo \"vgcfgbackup -f /tmp/lvm-" + part.vg.name + "\" >>/tmp/lvmcommands\r\n"); } } } }
public void getOriginalLVM(string imgName, string hdToGet, string clienthd) { 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 hdNumberToGet = Convert.ToInt32(hdToGet) - 1; foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active != "1") continue; if (part.vg != null) { if (part.vg.lv != null) { HttpContext.Current.Response.Write("pvcreate -u " + part.uuid + " --norestorefile -yf " + clienthd+part.vg.pv[part.vg.pv.Length - 1] + "\r\n"); HttpContext.Current.Response.Write("vgcreate " + part.vg.name + " " + clienthd + part.vg.pv[part.vg.pv.Length - 1] + " -yf" + "\r\n"); HttpContext.Current.Response.Write("echo \"" + part.vg.uuid + "\" >>/tmp/vg-" +part.vg.name + "\r\n"); foreach (var lv in part.vg.lv) { if (lv.active == "1") { HttpContext.Current.Response.Write("lvcreate -L " + lv.size + "s -n " + lv.name + " " + lv.vg + "\r\n"); HttpContext.Current.Response.Write("echo \"" + lv.uuid + "\" >>/tmp/" + lv.vg + "-" + lv.name + "\r\n"); } } HttpContext.Current.Response.Write("vgcfgbackup -f /tmp/lvm-" + part.vg.name + "\r\n"); } } } }
public void getMinHDSize(string imgName, string hdToGet, 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); try { ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSizeCustom); } catch { } } else { ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSize); try { ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSize); } catch { } } if(ips.hd == null) { HttpContext.Current.Response.Write("compatibility,0"); return; } if (ips.hd.Count() < Convert.ToInt32(hdToGet)) { HttpContext.Current.Response.Write("notexist,0"); return; } int activeCounter = Convert.ToInt32(hdToGet); bool foundActive = false; int hdNumberToGet = Convert.ToInt32(hdToGet) - 1; //Look for first active hd if (ips.hd[hdNumberToGet].active == "1") { foundActive = true; } else { while(activeCounter <= ips.hd.Count()) { if(ips.hd[activeCounter -1 ].active == "1") { hdNumberToGet = activeCounter - 1; foundActive = true; break; } activeCounter++; } } if(!foundActive) { HttpContext.Current.Response.Write("notactive,0"); return; } long minHDSizeRequired_BYTE = image.CalculateMinSizeHD(imgName, hdNumberToGet, newHDSize); long newHD_BYTES = Convert.ToInt64(newHDSize); if (minHDSizeRequired_BYTE > newHD_BYTES) { HttpContext.Current.Response.Write("false," + (hdNumberToGet + 1)); Logger.Log("Error: " + newHD_BYTES / 1024 / 1024 + " MB Is Less Than The Minimum Required HD Size For This Image(" + minHDSizeRequired_BYTE / 1024 / 1024 + " MB)"); } else if(minHDSizeRequired_BYTE == newHD_BYTES) { HttpContext.Current.Response.Write("original,"+ (hdNumberToGet + 1)); } else HttpContext.Current.Response.Write("true," + (hdNumberToGet + 1)); }
public void getHDParameter(string imgName, string paramName, string hdToGet, string partNumber) { 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 activeCounter = Convert.ToInt32(hdToGet); int hdNumberToGet = Convert.ToInt32(hdToGet) - 1; //Look for first active hd if (ips.hd[hdNumberToGet].active != "1") { while (activeCounter <= ips.hd.Count()) { if (ips.hd[activeCounter - 1].active == "1") { hdNumberToGet = activeCounter - 1; } activeCounter++; } } if (paramName == "uuid") { foreach (var partition in ips.hd[hdNumberToGet].partition) if (partition.number == partNumber) { HttpContext.Current.Response.Write(partition.uuid); break; } } else if (paramName == "lvmswap") { foreach (var partition in ips.hd[hdNumberToGet].partition) { if (partition.vg != null) { if (partition.vg.lv != null) { foreach (var lv in partition.vg.lv) { if (lv.fstype.ToLower() == "swap" && lv.active == "1") { HttpContext.Current.Response.Write(lv.vg.Replace("-","--") + "-" + lv.name.Replace("-","--") + "," + lv.uuid); break; } } } } } } else if (paramName == "guid") { foreach (var partition in ips.hd[hdNumberToGet].partition) if (partition.number == partNumber) { HttpContext.Current.Response.Write(partition.guid); break; } } else if (paramName == "table") { HttpContext.Current.Response.Write(ips.hd[hdNumberToGet].table); } else if (paramName == "partCount") { int activePartsCounter = 0; foreach(var part in ips.hd[hdNumberToGet].partition) { if (part.active == "1") activePartsCounter++; } HttpContext.Current.Response.Write(activePartsCounter.ToString()); } else if (paramName == "activeParts") { Utility settings = new Utility(); string parts = null; string imageFiles = null; String[] partFiles = null; string compExt = null; foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active == "1") { if (hdNumberToGet == 0) imageFiles = settings.GetSettings("Image Store Path") + imgName; else imageFiles = settings.GetSettings("Image Store Path") + imgName + Path.DirectorySeparatorChar + "hd" + (hdNumberToGet + 1).ToString(); try { // modified by cocoon^M foreach (var ext in ImageExtensions) { partFiles = Directory.GetFiles(imageFiles + Path.DirectorySeparatorChar, "*" + ext); if (partFiles != null && partFiles.Length > 0) { compExt = ext; break; } } if (partFiles.Length == 0) { Logger.Log("Image Files Could Not Be Located"); } } catch { Logger.Log("Image Files Could Not Be Located"); } // modified by cocoon^M foreach (var pType in ImagePartTypes) { if (File.Exists(imageFiles + Path.DirectorySeparatorChar + "part" + part.number + pType + compExt)) { parts += part.number + " "; break; } } } } HttpContext.Current.Response.Write(parts); } else if (paramName == "lvmactiveParts") { Utility settings = new Utility(); string parts = null; string imageFiles = null; String[] partFiles = null; string compExt = null; foreach (var part in ips.hd[hdNumberToGet].partition) { if (part.active != "1") continue; if (hdNumberToGet == 0) imageFiles = settings.GetSettings("Image Store Path") + imgName; else imageFiles = settings.GetSettings("Image Store Path") + imgName + Path.DirectorySeparatorChar + "hd" + (hdNumberToGet + 1).ToString(); try { // modified by cocoon foreach (var ext in ImageExtensions) { partFiles = Directory.GetFiles(imageFiles + Path.DirectorySeparatorChar, "*" + ext); if (partFiles != null && partFiles.Length > 0) { compExt = ext; break; } } if (partFiles.Length == 0) { Logger.Log("Image Files Could Not Be Located"); } } catch { Logger.Log("Image Files Could Not Be Located"); } if (part.vg != null) { if (part.vg.lv != null) { foreach (var lv in part.vg.lv) { if (lv.active == "1") { // modified by cocoon foreach (var pType in ImagePartTypes) { if (File.Exists(imageFiles + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + pType + compExt)) { parts += lv.vg + "-" + lv.name + " "; break; } } } } } } } HttpContext.Current.Response.Write(parts); } else if (paramName == "HDguid") { HttpContext.Current.Response.Write(ips.hd[hdNumberToGet].guid); } else if (paramName == "originalHDSize") { string originalSize = (Convert.ToInt64(ips.hd[hdNumberToGet].size) * Convert.ToInt32(ips.hd[hdNumberToGet].lbs)).ToString(); HttpContext.Current.Response.Write(originalSize); } else if (paramName == "isKnownLayout") { if(ips.hd[hdNumberToGet].table == "mbr") { if(ips.hd[hdNumberToGet].partition.Count() == 1) if(ips.hd[hdNumberToGet].partition[0].start == "63") HttpContext.Current.Response.Write("winxp"); else if (ips.hd[hdNumberToGet].partition[0].start == "2048") HttpContext.Current.Response.Write("winvista"); else HttpContext.Current.Response.Write("false"); else if(ips.hd[hdNumberToGet].partition.Count() == 2) { string part1Size = ((Convert.ToInt64(ips.hd[hdNumberToGet].partition[0].size) * Convert.ToInt32(ips.hd[hdNumberToGet].lbs) / 1024 /1024)).ToString(); if(ips.hd[hdNumberToGet].partition[0].start == "2048" && part1Size == "100") HttpContext.Current.Response.Write("win7"); else if(ips.hd[hdNumberToGet].partition[0].start == "2048" && part1Size == "350") HttpContext.Current.Response.Write("win8"); else HttpContext.Current.Response.Write("false"); } else HttpContext.Current.Response.Write("false"); } else if (ips.hd[hdNumberToGet].table == "gpt") { if (ips.hd[hdNumberToGet].partition.Count() == 3) if (ips.hd[hdNumberToGet].partition[0].start == "2048" && ips.hd[hdNumberToGet].partition[1].start == "206848" && ips.hd[hdNumberToGet].partition[2].start == "468992") HttpContext.Current.Response.Write("win7gpt"); else HttpContext.Current.Response.Write("false"); else if (ips.hd[hdNumberToGet].partition.Count() == 4) { if (ips.hd[hdNumberToGet].partition[0].start == "2048" && ips.hd[hdNumberToGet].partition[1].start == "616448" && ips.hd[hdNumberToGet].partition[2].start == "821248" && ips.hd[hdNumberToGet].partition[3].start == "1083392") HttpContext.Current.Response.Write("win8gpt"); else if (ips.hd[hdNumberToGet].partition[0].start == "2048" && ips.hd[hdNumberToGet].partition[1].start == "616448" && ips.hd[hdNumberToGet].partition[2].start == "819200" && ips.hd[hdNumberToGet].partition[3].start == "1081344") HttpContext.Current.Response.Write("win8gpt"); else HttpContext.Current.Response.Write("false"); } else HttpContext.Current.Response.Write("false"); } else HttpContext.Current.Response.Write("Error: Could Not Determine Partition Table Type"); } }
public void AddImage(Image image) { image.Create(image); string imageID = image.GetImageID(image.Name); HttpContext.Current.Response.Write(imageID + "," + Utility.Message); }
public void imagesize() { Utility utility = new Utility(); HttpContext postedContext = HttpContext.Current; HttpFileCollection Files = postedContext.Request.Files; string imagename = utility.Decode((string)postedContext.Request.Form["imgName"]); string imagesize = utility.Decode((string)postedContext.Request.Form["imageSize"]); try { using (NpgsqlConnection conn = new NpgsqlConnection(Utility.DBString)) { NpgsqlCommand cmd = new NpgsqlCommand("images_updatesize", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new NpgsqlParameter("@imagename", imagename)); cmd.Parameters.Add(new NpgsqlParameter("@imagesize", imagesize)); conn.Open(); cmd.ExecuteNonQuery(); } } catch (Exception ex) { Logger.Log(ex.ToString()); HttpContext.Current.Response.Write("false"); return; } //Validate image specifications were recorded successfully Image image = new Image(); image.ID = image.GetImageID(imagename); image.Read(image); Image_Physical_Specs existingips = new Image_Physical_Specs(); try { existingips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSize); } catch(Exception ex) { Logger.Log(ex.ToString()); HttpContext.Current.Response.Write("false"); return; } if (existingips.hd == null) { HttpContext.Current.Response.Write("false"); return; } //Reset Custom Specs image.UpdateSpecs(image.Name, ""); HttpContext.Current.Response.Write("true"); }