Exemple #1
0
        /// <summary>
        /// Will lock physical basic disk and all volumes on it.
        /// If the operation is not completed successfully, all locks will be releases.
        /// </summary>
        public static LockStatus LockBasicDiskAndVolumesOrNone(PhysicalDisk disk)
        {
            bool success = disk.ExclusiveLock();

            if (!success)
            {
                return(LockStatus.CannotLockDisk);
            }
            List <Partition> partitions  = BasicDiskHelper.GetPartitions(disk);
            List <Guid>      volumeGuids = new List <Guid>();

            foreach (Partition partition in partitions)
            {
                Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(partition);
                if (windowsVolumeGuid.HasValue)
                {
                    volumeGuids.Add(windowsVolumeGuid.Value);
                }
                else
                {
                    return(LockStatus.CannotLockVolume);
                }
            }

            success = LockAllVolumesOrNone(volumeGuids);
            if (!success)
            {
                disk.ReleaseLock();
                return(LockStatus.CannotLockVolume);
            }
            return(LockStatus.Success);
        }
Exemple #2
0
 public static void ListPartitions()
 {
     if (m_selectedDisk != null)
     {
         List <Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
         Console.WriteLine("Partition #  Type              Size     Offset   Start Sector");
         Console.WriteLine("-----------  ----------------  -------  -------  ------------");
         for (int index = 0; index < partitions.Count; index++)
         {
             Partition partition = partitions[index];
             long      offset    = partition.FirstSector * m_selectedDisk.BytesPerSector;
             long      size      = partition.Size;
             string    partitionType;
             if (partition is GPTPartition)
             {
                 partitionType = ((GPTPartition)partition).PartitionTypeName;
             }
             else // partition is MBRPartition
             {
                 partitionType = ((MBRPartition)partition).PartitionTypeName.ToString();
             }
             partitionType = partitionType.PadRight(16);
             string startSector = partition.FirstSector.ToString().PadLeft(12);
             Console.WriteLine("Partition {0}  {1}  {2}  {3}  {4}", index.ToString(), partitionType, GetStandardSizeString(size), GetStandardSizeString(offset), startSector);
         }
     }
     else
     {
         Console.WriteLine("No disk has been selected");
     }
 }
Exemple #3
0
        public static List <Volume> GetVolumes()
        {
            List <PhysicalDisk> disks  = PhysicalDiskHelper.GetPhysicalDisks();
            List <Volume>       result = new List <Volume>();

            // Get partitions:
            foreach (Disk disk in disks)
            {
                if (!DynamicDisk.IsDynamicDisk(disk))
                {
                    List <Partition> partitions = BasicDiskHelper.GetPartitions(disk);
                    foreach (Partition partition in partitions)
                    {
                        result.Add(partition);
                    }
                }
            }

            // Get dynamic volumes
            List <DynamicVolume> dynamicVolumes = WindowsDynamicVolumeHelper.GetDynamicVolumes();

            foreach (DynamicVolume volume in dynamicVolumes)
            {
                result.Add(volume);
            }

            return(result);
        }
        public static Partition CreatePrimaryPartition(Disk disk)
        {
            MasterBootRecord mbr = new MasterBootRecord();

            mbr.DiskSignature = (uint)new Random().Next(Int32.MaxValue);
            mbr.PartitionTable[0].PartitionTypeName = PartitionTypeName.Primary;
            mbr.PartitionTable[0].FirstSectorLBA    = 63;
            mbr.PartitionTable[0].SectorCountLBA    = (uint)Math.Min(disk.TotalSectors - 63, UInt32.MaxValue);
            MasterBootRecord.WriteToDisk(disk, mbr);
            return(BasicDiskHelper.GetPartitions(disk)[0]);
        }
Exemple #5
0
        public static void UnlockBasicDiskAndVolumes(PhysicalDisk disk)
        {
            List <Partition> partitions = BasicDiskHelper.GetPartitions(disk);

            foreach (Partition partition in partitions)
            {
                Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(partition);
                if (windowsVolumeGuid.HasValue)
                {
                    WindowsVolumeManager.ReleaseLock(windowsVolumeGuid.Value);
                }
            }

            disk.ReleaseLock();
        }
Exemple #6
0
        /// <summary>
        /// If a disk has more than one partition, the last one is the only one we'll be able to resize without moving partitions around
        /// </summary>
        public static Partition GetLastPartition(Disk disk)
        {
            MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(disk);

            if (mbr == null)
            {
                return(new RemovableVolume(disk));
            }
            else
            {
                if (!DynamicDisk.IsDynamicDisk(disk))
                {
                    List <Partition> partitions = BasicDiskHelper.GetPartitions(disk);
                    if (partitions.Count > 0)
                    {
                        return(partitions[partitions.Count - 1]);
                    }
                }
                return(null);
            }
        }
Exemple #7
0
        public static void SelectCommand(string[] args)
        {
            if (args.Length == 1)
            {
                HelpSelect();
                return;
            }

            switch (args[1].ToLower())
            {
            case "disk":
            {
                if (args.Length == 3)
                {
                    int diskIndex = Conversion.ToInt32(args[2], -1);
                    if (diskIndex >= 0)
                    {
                        PhysicalDisk disk = null;
                        try
                        {
                            disk = new PhysicalDisk(diskIndex);
                        }
                        catch
                        {
                            Console.WriteLine();
                            Console.WriteLine("Error: Invalid disk number");
                        }

                        if (disk != null)
                        {
                            m_selectedDisk = disk;
                        }
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Error: Invalid number of arguments");
                }
                break;
            }

            case "vdisk":
            {
                if (args.Length == 3)
                {
                    KeyValuePairList <string, string> parameters = ParseParameters(args, 2);
                    if (parameters.ContainsKey("file"))
                    {
                        string path = parameters.ValueOf("file");
                        if (new FileInfo(path).Exists)
                        {
                            try
                            {
                                m_selectedDisk = DiskImage.GetDiskImage(path);
                            }
                            catch (InvalidDataException)
                            {
                                Console.WriteLine("Invalid virtual disk format");
                            }
                            catch (NotImplementedException)
                            {
                                Console.WriteLine("Unsupported virtual disk format");
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine("Cannot read file: " + ex.Message);
                            }
                        }
                        else
                        {
                            Console.WriteLine("File not found: \"{0}\"", path);
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Invalid argument");
                    }
                }
                else
                {
                    Console.WriteLine("Error: Invalid number of arguments");
                }
                break;
            }

            case "partition":
            {
                if (m_selectedDisk != null)
                {
                    if (args.Length == 3)
                    {
                        int partitionIndex          = Conversion.ToInt32(args[2], -1);
                        List <Partition> partitions = BasicDiskHelper.GetPartitions(m_selectedDisk);
                        if (partitionIndex >= 0 && partitionIndex < partitions.Count)
                        {
                            m_selectedVolume = partitions[partitionIndex];
                        }
                        else
                        {
                            Console.WriteLine("Error: Invalid partition number");
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Partition number was not specified");
                    }
                }
                else
                {
                    Console.WriteLine("No disk has been selected");
                }
                break;
            }

            case "volume":
            {
                if (args.Length == 3)
                {
                    List <Volume> volumes;
                    try
                    {
                        volumes = WindowsVolumeHelper.GetVolumes();
                    }
                    catch
                    {
                        volumes = new List <Volume>();
                    }

                    int volumeIndex = Conversion.ToInt32(args[2], -1);
                    if (volumeIndex >= 0 && volumeIndex < volumes.Count)
                    {
                        m_selectedVolume = volumes[volumeIndex];
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Error: Invalid volume number");
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Error: Volume number was not specified");
                }
                break;
            }

            default:
                HelpSelect();
                break;
            }
        }