123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using DiskAccessLibrary;
- using Utilities;
- namespace DiskAccessLibrary.LogicalDiskManager
- {
- public class RetainHelper
- {
- public static bool IsVolumeRetained(DynamicVolume volume, out bool isBootVolume)
- {
- if (volume is SimpleVolume)
- {
- return IsVolumeRetained((SimpleVolume)volume, out isBootVolume);
- }
- else if (volume is MirroredVolume)
- {
- foreach (DynamicVolume component in ((MirroredVolume)volume).Components)
- {
- if (component.IsOperational && component is SimpleVolume)
- {
- if (IsVolumeRetained((SimpleVolume)component, out isBootVolume))
- {
- return true;
- }
- }
- }
- }
- isBootVolume = false;
- return false;
- }
- public static bool IsVolumeRetained(SimpleVolume volume, out bool isBootVolume)
- {
- isBootVolume = false;
- DynamicDisk disk = DynamicDisk.ReadFromDisk(volume.Disk);
- long bootPartitionStartLBA;
- List<ExtentRecord> retained = GetRetainedExtentsOnDisk(disk, out bootPartitionStartLBA);
- foreach(ExtentRecord record in retained)
- {
- if (record.ExtentId == volume.DiskExtent.ExtentID)
- {
- if ((long)(disk.PrivateHeader.PublicRegionStartLBA + record.DiskOffsetLBA) == bootPartitionStartLBA)
- {
- isBootVolume = true;
- }
- return true;
- }
- }
- return false;
- }
- private static List<ExtentRecord> GetRetainedExtentsOnDisk(DynamicDisk disk, out long bootPartitionStartLBA)
- {
- VolumeManagerDatabase database = VolumeManagerDatabase.ReadFromDisk(disk);
- DiskRecord diskRecord = database.FindDiskByDiskGuid(disk.DiskGuid);
- bootPartitionStartLBA = -1;
- List<ExtentRecord> result = new List<ExtentRecord>();
- foreach (VolumeRecord volume in database.VolumeRecords)
- {
- if ((volume.VolumeFlags & VolumeFlags.RetainPartition) > 0)
- {
- List<ComponentRecord> components = database.FindComponentsByVolumeID(volume.VolumeId);
- foreach (ComponentRecord componentRecord in components)
- {
- if (componentRecord.ExtentLayout == ExtentLayoutName.Concatenated)
- {
- if (componentRecord.NumberOfExtents == 1)
- {
- List<ExtentRecord> extents = database.FindExtentsByComponentID(componentRecord.ComponentId);
- if (extents.Count == 1)
- {
- ExtentRecord extent = extents[0];
- if (extent.DiskId == diskRecord.DiskId)
- {
- result.Add(extent);
- if ((volume.VolumeFlags & VolumeFlags.BootVolume) > 0)
- {
- bootPartitionStartLBA = (long)(disk.PrivateHeader.PublicRegionStartLBA + extent.DiskOffsetLBA);
- }
- }
- }
- }
- }
- }
- }
- }
- return result;
- }
- }
- }
|