public ServiceClientPartition(ImageProfileWithImage imageProfile) { string schema = null; if (imageProfile != null) { _imageProfile = imageProfile; if ((imageProfile.PartitionMethod == "Dynamic" || imageProfile.PartitionMethod == "Standard" || imageProfile.PartitionMethod == "Standard Core Storage") && !string.IsNullOrEmpty(imageProfile.CustomSchema)) { schema = imageProfile.CustomSchema; } else { schema = new FilesystemServices().ReadSchemaFile(imageProfile.Image.Name); } } if (!string.IsNullOrEmpty(schema)) { _imageSchema = JsonConvert.DeserializeObject <DtoImageSchema>(schema); } }
/// <summary> /// Calculates the minimum block size required for a single partition, taking into account any children partitions. /// </summary> public PartitionHelper Partition(int hdNumberToGet, int partNumberToGet, long newHdSize) { var partition = _imageSchema.HardDrives[hdNumberToGet].Partitions[partNumberToGet]; var partitionHelper = new PartitionHelper { MinSizeBlk = 0 }; var extendedPartitionHelper = new ExtendedPartitionHelper(); if (partition.Type.ToLower() == "extended") { extendedPartitionHelper = ExtendedPartition(hdNumberToGet, newHdSize); } partitionHelper.VolumeGroupHelper = VolumeGroup(hdNumberToGet, partNumberToGet, newHdSize); var lbsByte = _imageSchema.HardDrives[hdNumberToGet].Lbs; //Look if any volume groups are present for this partition. If so set the volumesize for the volume group to the minimum size //required for the volume group. Volume groups are always treated as resizable even if none of the individual //logical volumes are resizable if (partitionHelper.VolumeGroupHelper.Pv != null) { partitionHelper.PartitionHasVolumeGroup = true; partition.VolumeSize = partitionHelper.VolumeGroupHelper.MinSizeBlk * lbsByte / 1024 / 1024; } if (partition.ForceFixedSize) { partitionHelper.MinSizeBlk = partition.Size; partitionHelper.IsDynamicSize = false; } //Use partition size that user has set for the partition, if it is set. else if (!string.IsNullOrEmpty(partition.CustomSize) && !string.IsNullOrEmpty(partition.CustomSizeUnit)) { long customSizeBytes = 0; switch (partition.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(partition.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(partition.CustomSize) * 1024 * 1024 * 1024; break; case "%": var hdPercent = Convert.ToDouble(partition.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } partitionHelper.MinSizeBlk = customSizeBytes / lbsByte; partitionHelper.IsDynamicSize = false; } //If partition is not resizable. Determine partition size. Also if the partition is less than 5 gigs assume it is that // size for a reason, do not resize it even if it is marked as a resizable partition else if ((partition.VolumeSize == 0 && partition.Type.ToLower() != "extended") || (partition.Type.ToLower() == "extended" && extendedPartitionHelper.IsOnlySwap) || partition.Size * lbsByte <= 5368709120 || partition.FsType == "swap") { partitionHelper.MinSizeBlk = partition.Size; partitionHelper.IsDynamicSize = 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 less than the resized size. else { partitionHelper.IsDynamicSize = true; if (partition.Type.ToLower() == "extended") { partitionHelper.MinSizeBlk = extendedPartitionHelper.MinSizeBlk; } else if (partitionHelper.VolumeGroupHelper.Pv != null) { partitionHelper.MinSizeBlk = partitionHelper.VolumeGroupHelper.MinSizeBlk; } else { string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "winpe", "xfs" }) { imageFile = new FilesystemServices().GetFileNameWithFullPath(_imageProfile.Image.Name, hdNumberToGet.ToString(), partition.Number, ext); if (!string.IsNullOrEmpty(imageFile)) { break; } } if (Path.GetExtension(imageFile) == ".wim") { partitionHelper.MinSizeBlk = partition.UsedMb * 1024 * 1024 / lbsByte; } else { //The resize value and used_mb value are calculated during upload by two different methods //Use the one that is bigger just in case. if (partition.VolumeSize > partition.UsedMb) { partitionHelper.MinSizeBlk = partition.VolumeSize * 1024 * 1024 / lbsByte; } else { partitionHelper.MinSizeBlk = partition.UsedMb * 1024 * 1024 / lbsByte; } } } } return(partitionHelper); }
/// <summary> /// Calculates the minimum block size required for a single logical volume, assuming the logical volume cannot have any /// children. /// </summary> public PartitionHelper LogicalVolume(LogicalVolume lv, int lbsByte, long newHdSize, int hdNumberToGet) { var logicalVolumeHelper = new PartitionHelper { MinSizeBlk = 0 }; if (lv.ForceFixedSize) { logicalVolumeHelper.MinSizeBlk = lv.Size; logicalVolumeHelper.IsDynamicSize = false; } else if (!string.IsNullOrEmpty(lv.CustomSize) && !string.IsNullOrEmpty(lv.CustomSizeUnit)) { long customSizeBytes = 0; switch (lv.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024 * 1024; break; case "%": var hdPercent = Convert.ToDouble(lv.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } logicalVolumeHelper.MinSizeBlk = customSizeBytes / lbsByte; logicalVolumeHelper.IsDynamicSize = false; } else { logicalVolumeHelper.IsDynamicSize = true; string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { imageFile = new FilesystemServices().GetLVMFileNameWithFullPath(_imageProfile.Image.Name, hdNumberToGet.ToString(), lv.VolumeGroup, lv.Name, ext); if (!string.IsNullOrEmpty(imageFile)) { break; } } if (Path.GetExtension(imageFile) == ".wim") { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } else { if (lv.VolumeSize > lv.UsedMb) { logicalVolumeHelper.MinSizeBlk = lv.VolumeSize * 1024 * 1024 / lbsByte; } else { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } } } return(logicalVolumeHelper); }
public List <PhysicalPartition> GetActivePartitions(int schemaHdNumber, ImageProfileWithImage imageProfile) { var listPhysicalPartition = new List <PhysicalPartition>(); foreach ( var partition in _imageSchema.HardDrives[schemaHdNumber].Partitions.Where(partition => partition.Active) ) { var physicalPartition = new PhysicalPartition(); physicalPartition.Number = partition.Number; physicalPartition.Guid = partition.Guid; physicalPartition.Uuid = partition.Uuid; physicalPartition.FileSystem = partition.FsType; physicalPartition.Type = partition.Type; physicalPartition.EfiBootLoader = partition.EfiBootLoader; string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { imageFile = new FilesystemServices().GetFileNameWithFullPath(imageProfile.Image.Name, schemaHdNumber.ToString(), partition.Number, ext); if (!string.IsNullOrEmpty(imageFile)) { physicalPartition.PartcloneFileSystem = ext; break; } } switch (Path.GetExtension(imageFile)) { case ".lz4": physicalPartition.Compression = "lz4"; physicalPartition.ImageType = "Block"; break; case ".gz": physicalPartition.Compression = "gz"; physicalPartition.ImageType = "Block"; break; case ".uncp": physicalPartition.Compression = "uncp"; physicalPartition.ImageType = "Block"; break; case ".wim": physicalPartition.ImageType = "File"; break; } if (partition.VolumeGroup.Name != null) { physicalPartition.VolumeGroup = new VolumeGroup(); physicalPartition.VolumeGroup.Name = partition.VolumeGroup.Name; var listLogicalVolumes = new List <Toems_Common.Dto.clientimaging.LogicalVolume>(); var logicalVolumeCounter = 0; foreach (var logicalVolume in partition.VolumeGroup.LogicalVolumes.Where(x => x.Active)) { logicalVolumeCounter++; var clientLogicalVolume = new Toems_Common.Dto.clientimaging.LogicalVolume(); clientLogicalVolume.Name = logicalVolume.Name; clientLogicalVolume.FileSystem = logicalVolume.FsType; clientLogicalVolume.Uuid = logicalVolume.Uuid; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { imageFile = new FilesystemServices().GetLVMFileNameWithFullPath(imageProfile.Image.Name, schemaHdNumber.ToString(), partition.VolumeGroup.Name, logicalVolume.Name, ext); if (!string.IsNullOrEmpty(imageFile)) { clientLogicalVolume.PartcloneFileSystem = ext; break; } } switch (Path.GetExtension(imageFile)) { case ".lz4": clientLogicalVolume.Compression = "lz4"; clientLogicalVolume.ImageType = "Block"; break; case ".gz": clientLogicalVolume.Compression = "gz"; clientLogicalVolume.ImageType = "Block"; break; case ".uncp": clientLogicalVolume.Compression = "uncp"; clientLogicalVolume.ImageType = "Block"; break; case ".wim": clientLogicalVolume.ImageType = "File"; break; } listLogicalVolumes.Add(clientLogicalVolume); } physicalPartition.VolumeGroup.LogicalVolumeCount = logicalVolumeCounter; physicalPartition.VolumeGroup.LogicalVolumes = listLogicalVolumes; } listPhysicalPartition.Add(physicalPartition); } return(listPhysicalPartition); }