Beispiel #1
0
        public static string GetVolumeInformation(Volume volume)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("Volume size: {0} bytes\n", volume.Size.ToString("###,###,###,###,##0"));
            builder.AppendFormat("Volume type: {0}\n", VolumeHelper.GetVolumeTypeString(volume));
            if (volume is GPTPartition)
            {
                builder.AppendFormat("Partition name: {0}\n", ((GPTPartition)volume).PartitionName);
            }
            else if (volume is DynamicVolume)
            {
                builder.AppendFormat("Volume name: {0}\n", ((DynamicVolume)volume).Name);
                builder.AppendFormat("Volume status: {0}\n", VolumeHelper.GetVolumeStatusString(volume));
            }

            Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(volume);

            if (windowsVolumeGuid.HasValue)
            {
                List <string> mountPoints = WindowsVolumeManager.GetMountPoints(windowsVolumeGuid.Value);
                foreach (string volumePath in mountPoints)
                {
                    builder.AppendFormat("Volume path: {0}\n", volumePath);
                }
                bool isMounted = WindowsVolumeManager.IsMounted(windowsVolumeGuid.Value);
                builder.AppendFormat("Mounted: {0}\n", isMounted);
            }
            builder.AppendLine();

            if (volume is MirroredVolume)
            {
                builder.AppendLine("Extents:");
                List <DynamicVolume> components = ((MirroredVolume)volume).Components;
                for (int componentIndex = 0; componentIndex < components.Count; componentIndex++)
                {
                    if (componentIndex != 0)
                    {
                        builder.AppendLine();
                    }
                    DynamicVolume component = components[componentIndex];
                    builder.AppendFormat("Component {0}:\n", componentIndex);
                    builder.Append(GetExtentsInformation(component));
                }
            }
            else if (volume is DynamicVolume)
            {
                builder.AppendLine("Extents:");
                builder.Append(GetExtentsInformation((DynamicVolume)volume));
            }
            else if (volume is Partition)
            {
                Partition partition             = (Partition)volume;
                long      partitionOffset       = partition.FirstSector * partition.BytesPerSector;
                string    partitionOffsetString = FormattingHelper.GetStandardSizeString(partitionOffset);
                builder.AppendFormat("Partiton Offset: {0}, Start Sector: {1}\n", partitionOffsetString, partition.FirstSector);
            }

            return(builder.ToString());
        }
Beispiel #2
0
        public static string GetExtentLabel(Volume volume, DiskExtent extent, int width)
        {
            StringBuilder builder = new StringBuilder();

            if (volume != null)
            {
                Guid?volumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(volume);
                if (volumeGuid.HasValue)
                {
                    List <string> mountPoints = WindowsVolumeManager.GetMountPoints(volumeGuid.Value);
                    if (mountPoints.Count > 0)
                    {
                        builder.AppendLine(mountPoints[0]);
                    }
                }
            }
            long size = extent.Size;

            if (width <= 60)
            {
                builder.AppendLine(FormattingHelper.GetCompactSizeString(extent.Size));
            }
            else
            {
                builder.AppendLine(FormattingHelper.GetStandardSizeString(extent.Size));
            }
            if (volume != null)
            {
                string statusString = VolumeHelper.GetVolumeStatusString(volume);
                builder.AppendLine(statusString);
            }

            return(builder.ToString());
        }
        private static ExtendFileSystemResult ExtendMountedFileSystem(Volume volume, Guid windowsVolumeGuid, long numberOfAdditionalSectors)
        {
            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
            // We either have to take the disk(s) offline or use the OS volume handle for write operations.
            // http://msdn.microsoft.com/en-us/library/ff551353%28v=vs.85%29.aspx
            OperatingSystemVolume osVolume   = new OperatingSystemVolume(windowsVolumeGuid, volume.BytesPerSector, volume.Size);
            IExtendableFileSystem fileSystem = FileSystemHelper.ReadFileSystem(osVolume) as IExtendableFileSystem;

            if (fileSystem == null)
            {
                return(ExtendFileSystemResult.UnsupportedFileSystem);
            }

            bool success = WindowsVolumeManager.ExclusiveLock(windowsVolumeGuid, FileAccess.ReadWrite);

            if (!success)
            {
                return(ExtendFileSystemResult.CannotLockVolume);
            }

            // Dismounting the volume will make sure the OS have the correct filesystem size. (locking the volume is not enough)
            success = WindowsVolumeManager.DismountVolume(windowsVolumeGuid);
            if (!success)
            {
                return(ExtendFileSystemResult.CannotDismountVolume);
            }

            fileSystem.Extend(numberOfAdditionalSectors);

            WindowsVolumeManager.ReleaseLock(windowsVolumeGuid);
            return(ExtendFileSystemResult.Success);
        }
Beispiel #4
0
        public static void UnlockAllDisksAndVolumes()
        {
            DiskLockHelper.ReleaseLock(m_lockedDisks);

            foreach (DynamicVolume volumeToUnlock in m_lockedVolumes)
            {
                WindowsVolumeManager.ReleaseLock(volumeToUnlock.VolumeGuid);
            }
            m_lockedDisks.Clear();
            m_lockedVolumes.Clear();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (listVolumes.SelectedItems.Count > 0)
            {
                Volume selectedVolume = (Volume)listVolumes.SelectedItems[0].Tag;
                if (selectedVolume is DynamicVolume && !((DynamicVolume)selectedVolume).IsOperational)
                {
                    MessageBox.Show("The selected volume is not operational", "Error");
                    return;
                }
                if (!chkReadOnly.Checked)
                {
                    bool skipLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(selectedVolume));
                    if (!skipLock)
                    {
                        Guid?volumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(selectedVolume);
                        if (Environment.OSVersion.Version.Major >= 6)
                        {
                            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
                            // We either have to take the disk(s) offline or use the OS volume handle for write operations.
                            if (!volumeGuid.HasValue)
                            {
                                MessageBox.Show("The selected volume is not recognized by your operating system");
                                return;
                            }
                            selectedVolume = new OperatingSystemVolume(volumeGuid.Value, selectedVolume.BytesPerSector, selectedVolume.Size, chkReadOnly.Checked);
                        }

                        bool isLocked = false;
                        if (volumeGuid.HasValue)
                        {
                            isLocked = WindowsVolumeManager.ExclusiveLock(volumeGuid.Value);
                        }
                        if (!isLocked)
                        {
                            MessageBox.Show("Unable to lock the volume", "Error");
                            return;
                        }
                    }
                }
                m_selectedVolume  = selectedVolume;
                m_isReadOnly      = chkReadOnly.Checked;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("No volume was selected", "Error");
                return;
            }
        }
        /// <param name="diskGroup">Can be set to null when volume is partition</param>
        public static ExtendFileSystemResult ExtendFileSystem(List <DynamicDisk> diskGroup, Volume volume, long numberOfAdditionalSectors)
        {
            Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(volume);
            bool isMounted         = windowsVolumeGuid.HasValue && WindowsVolumeManager.IsMounted(windowsVolumeGuid.Value);

            if (isMounted)
            {
                return(ExtendMountedFileSystem(volume, windowsVolumeGuid.Value, numberOfAdditionalSectors));
            }
            else
            {
                if (volume is Partition)
                {
                    return(ExtendUnmountedFileSystem((Partition)volume, numberOfAdditionalSectors));
                }
                else
                {
                    return(ExtendUnmountedFileSystem(diskGroup, (DynamicVolume)volume, numberOfAdditionalSectors));
                }
            }
        }
        public static void ReleaseDisk(Disk disk)
        {
            if (disk is DiskImage)
            {
                ((DiskImage)disk).ReleaseLock();
            }
            else if (disk is RAMDisk)
            {
                ((RAMDisk)disk).Free();
            }
#if Win32
            else if (disk is PhysicalDisk)
            {
                if (!DiskAccessLibrary.LogicalDiskManager.DynamicDisk.IsDynamicDisk(disk))
                {
                    LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)disk);
                    try
                    {
                        ((PhysicalDisk)disk).UpdateProperties();
                    }
                    catch (System.IO.IOException)
                    {
                    }
                }
            }
            else if (disk is VolumeDisk)
            {
                bool skippedLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(((VolumeDisk)disk).Volume));
                if (!skippedLock)
                {
                    Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(((VolumeDisk)disk).Volume);
                    if (windowsVolumeGuid.HasValue)
                    {
                        WindowsVolumeManager.ReleaseLock(windowsVolumeGuid.Value);
                    }
                }
            }
#endif
        }
        public static void DetailCommand(string[] args)
        {
            if (args.Length == 2)
            {
                switch (args[1].ToLower())
                {
                case "disk":
                {
                    Console.WriteLine();
                    if (m_selectedDisk != null)
                    {
                        Console.WriteLine("Size: {0} bytes", m_selectedDisk.Size.ToString("###,###,###,###,##0"));
                        if (m_selectedDisk is PhysicalDisk)
                        {
                            PhysicalDisk disk = (PhysicalDisk)m_selectedDisk;
                            Console.WriteLine("Geometry: Heads: {0}, Cylinders: {1}, Sectors Per Track: {2}", disk.TracksPerCylinder, disk.Cylinders, disk.SectorsPerTrack);
                            Console.WriteLine();
                        }
                        else if (m_selectedDisk is DiskImage)
                        {
                            DiskImage disk = (DiskImage)m_selectedDisk;
                            Console.WriteLine("Disk image path: {0}", disk.Path);
                            Console.WriteLine();
                        }

                        MasterBootRecord mbr = MasterBootRecord.ReadFromDisk(m_selectedDisk);
                        if (mbr != null)
                        {
                            Console.WriteLine("Partitioning scheme: " + (mbr.IsGPTBasedDisk ? "GPT" : "MBR"));
                        }
                        DynamicDisk dynamicDisk = DynamicDisk.ReadFromDisk(m_selectedDisk);
                        Console.WriteLine("Disk type: " + ((dynamicDisk != null) ? "Dynamic Disk" : "Basic Disk"));
                    }
                    else
                    {
                        Console.WriteLine("No disk has been selected.");
                    }
                    break;
                }

                case "volume":
                case "partition":
                {
                    Console.WriteLine();
                    if (m_selectedVolume != null)
                    {
                        Console.WriteLine("Volume size: {0} bytes", m_selectedVolume.Size.ToString("###,###,###,###,##0"));
                        if (m_selectedVolume is GPTPartition)
                        {
                            Console.WriteLine("Partition name: {0}", ((GPTPartition)m_selectedVolume).PartitionName);
                        }

                        Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(m_selectedVolume);
                        if (windowsVolumeGuid.HasValue)
                        {
                            List <string> mountPoints = WindowsVolumeManager.GetMountPoints(windowsVolumeGuid.Value);
                            foreach (string volumePath in mountPoints)
                            {
                                Console.WriteLine("Volume path: {0}", volumePath);
                            }
                            bool isMounted = WindowsVolumeManager.IsMounted(windowsVolumeGuid.Value);
                            Console.WriteLine("Mounted: {0}", isMounted);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No volume has been selected.");
                    }
                    break;
                }

                default:
                    Console.WriteLine("Invalid argument.");
                    HelpDetail();
                    break;
                }
            }
            else if (args.Length > 2)
            {
                Console.WriteLine("Too many arguments.");
                HelpDetail();
            }
            else
            {
                HelpDetail();
            }
        }