public static DiskGroupLockResult ExtendPartition(Partition volume, long numberOfAdditionalExtentSectors)
        {
            if (volume.Disk is PhysicalDisk)
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool isReadOnly;
                    bool isOnline = ((PhysicalDisk)volume.Disk).GetOnlineStatus(out isReadOnly);
                    if (!isOnline || isReadOnly)
                    {
                        return(DiskGroupLockResult.OneOrMoreDisksAreOfflineOrReadonly);
                    }
                }

                LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(((PhysicalDisk)volume.Disk));
                if (status == LockStatus.CannotLockDisk)
                {
                    return(DiskGroupLockResult.CannotLockDisk);
                }
                else if (status == LockStatus.CannotLockVolume)
                {
                    return(DiskGroupLockResult.CannotLockVolume);
                }

                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(false);
                    if (!success)
                    {
                        return(DiskGroupLockResult.CannotTakeDiskOffline);
                    }
                }
            }

            ExtendHelper.ExtendPartition(volume, numberOfAdditionalExtentSectors);

            if (volume.Disk is PhysicalDisk)
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(true);
                }
                LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)volume.Disk);
                ((PhysicalDisk)volume.Disk).UpdateProperties();
            }

            return(DiskGroupLockResult.Success);
        }
        private static ExtendFileSystemResult ExtendUnmountedFileSystem(Partition volume, long numberOfAdditionalSectors)
        {
            IExtendableFileSystem fileSystem = FileSystemHelper.ReadFileSystem(volume) as IExtendableFileSystem;

            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
            // Basic disks are not needed to be taken offline for direct write operations within volume region. Only dynamic disks have to.
            if (volume.Disk is PhysicalDisk && Environment.OSVersion.Version.Major >= 6)
            {
                bool isReadOnly;
                bool isOnline = ((PhysicalDisk)volume.Disk).GetOnlineStatus(out isReadOnly);
                if (!isOnline || isReadOnly)
                {
                    return(ExtendFileSystemResult.OneOrMoreDisksAreOfflineOrReadonly);
                }

                LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(((PhysicalDisk)volume.Disk));
                if (status == LockStatus.CannotLockDisk)
                {
                    return(ExtendFileSystemResult.CannotLockDisk);
                }
                else if (status == LockStatus.CannotLockVolume)
                {
                    return(ExtendFileSystemResult.CannotLockVolume);
                }

                bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(false);
                if (!success)
                {
                    return(ExtendFileSystemResult.CannotTakeDiskOffline);
                }
            }

            fileSystem.Extend(numberOfAdditionalSectors);

            if (volume.Disk is PhysicalDisk && (Environment.OSVersion.Version.Major >= 6))
            {
                bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(true);
                LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)volume.Disk);
            }

            return(ExtendFileSystemResult.Success);
        }
예제 #3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            PhysicalDisk selectedDisk;

            if (listPhysicalDisks.SelectedItems.Count > 0)
            {
                selectedDisk = (PhysicalDisk)listPhysicalDisks.SelectedItems[0].Tag;
            }
            else
            {
                MessageBox.Show("No disk was selected", "Error");
                return;
            }
            if (!chkReadOnly.Checked)
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool isDiskReadOnly;
                    bool isOnline = selectedDisk.GetOnlineStatus(out isDiskReadOnly);
                    if (isDiskReadOnly)
                    {
                        MessageBox.Show("The selected disk is set to readonly", "Error");
                        return;
                    }

                    if (isOnline)
                    {
                        DialogResult result = MessageBox.Show("The selected disk will now be taken offline. OK?", String.Empty, MessageBoxButtons.OKCancel);
                        if (result == DialogResult.OK)
                        {
                            bool success = selectedDisk.SetOnlineStatus(false);
                            if (!success)
                            {
                                MessageBox.Show("Was not able to take the disk offline", "Error");
                                return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (DynamicDisk.IsDynamicDisk(selectedDisk))
                    {
                        // The user will probably want to stop the Logical Disk Manager services (vds, dmadmin, dmserver)
                        // and lock all dynamic disks and dynamic volumes before whatever he's doing.
                        // Modifications the the LDM database should be applied to all dynamic disks.
                        DialogResult result = MessageBox.Show("The dynamic disk database will likely be corrupted, Continue?", "Warning", MessageBoxButtons.YesNo);
                        if (result != DialogResult.Yes)
                        {
                            return;
                        }
                    }
                    else
                    {
                        // Locking a disk does not prevent Windows from accessing mounted volumes on it. (it does prevent creation of new volumes).
                        // For basic disks we need to lock the Disk and Volumes, and we should also call UpdateDiskProperties() after releasing the lock.
                        LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(selectedDisk);
                        if (status == LockStatus.CannotLockDisk)
                        {
                            MessageBox.Show("Unable to lock the disk", "Error");
                            return;
                        }
                        else if (status == LockStatus.CannotLockVolume)
                        {
                            MessageBox.Show("Unable to lock one of the volumes on the disk", "Error");
                            return;
                        }
                    }
                }
            }
            if (chkReadOnly.Checked)
            {
                selectedDisk = new PhysicalDisk(selectedDisk.PhysicalDiskIndex, true);
            }
            m_selectedDisk    = selectedDisk;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }