示例#1
0
        public static MountedDiskInfo GetMountedDiskInfo(string serverName, int driveNumber)
        {
            MountedDiskInfo diskInfo = new MountedDiskInfo {
                DiskNumber = driveNumber
            };

            // find mounted disk using VDS
            AdvancedDisk advancedDisk = null;
            Pack         diskPack     = null;

            // first attempt
            Thread.Sleep(3000);
            HostedSolutionLog.LogInfo("Trying to find mounted disk - first attempt");
            FindVdsDisk(serverName, diskInfo.DiskNumber, out advancedDisk, out diskPack);

            // second attempt
            if (advancedDisk == null)
            {
                Thread.Sleep(20000);
                HostedSolutionLog.LogInfo("Trying to find mounted disk - second attempt");
                FindVdsDisk(serverName, diskInfo.DiskNumber, out advancedDisk, out diskPack);
            }

            if (advancedDisk == null)
            {
                throw new Exception("Could not find mounted disk");
            }

            // Set disk address
            diskInfo.DiskAddress = advancedDisk.DiskAddress;
            var addressParts = diskInfo.DiskAddress.ParseExact("Port{0}Path{1}Target{2}Lun{3}");
            var portNumber   = addressParts[0];
            var targetId     = addressParts[2];
            var lun          = addressParts[3];

            // check if DiskPart must be used to bring disk online and clear read-only flag
            bool useDiskPartToClearReadOnly = false;

            if (ConfigurationManager.AppSettings[Constants.CONFIG_USE_DISKPART_TO_CLEAR_READONLY_FLAG] != null)
            {
                useDiskPartToClearReadOnly = Boolean.Parse(ConfigurationManager.AppSettings[Constants.CONFIG_USE_DISKPART_TO_CLEAR_READONLY_FLAG]);
            }

            // determine disk index for DiskPart
            Wmi cimv2 = new Wmi(serverName, Constants.WMI_CIMV2_NAMESPACE);
            ManagementObject objDisk = cimv2.GetWmiObject("win32_diskdrive",
                                                          "Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID={0} and ScsiLogicalUnit={1} and scsiPort={2}",
                                                          targetId, lun, portNumber);

            if (useDiskPartToClearReadOnly)
            {
                // *** Clear Read-Only and bring disk online with DiskPart ***
                HostedSolutionLog.LogInfo("Clearing disk Read-only flag and bringing disk online");

                if (objDisk != null)
                {
                    // disk found
                    // run DiskPart
                    string diskPartResult = RunDiskPart(serverName, String.Format(@"select disk {0}
attributes disk clear readonly
online disk
exit", Convert.ToInt32(objDisk["Index"])));

                    HostedSolutionLog.LogInfo("DiskPart Result: " + diskPartResult);
                }
            }
            else
            {
                // *** Clear Read-Only and bring disk online with VDS ***
                // clear Read-Only
                if ((advancedDisk.Flags & DiskFlags.ReadOnly) == DiskFlags.ReadOnly)
                {
                    HostedSolutionLog.LogInfo("Clearing disk Read-only flag");
                    advancedDisk.ClearFlags(DiskFlags.ReadOnly);
                    while ((advancedDisk.Flags & DiskFlags.ReadOnly) == DiskFlags.ReadOnly)
                    {
                        Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }
                }

                // bring disk ONLINE
                if (advancedDisk.Status == DiskStatus.Offline)
                {
                    HostedSolutionLog.LogInfo("Bringing disk online");
                    advancedDisk.Online();
                    while (advancedDisk.Status == DiskStatus.Offline)
                    {
                        Thread.Sleep(100);
                        advancedDisk.Refresh();
                    }
                }
            }

            // small pause after getting disk online
            Thread.Sleep(3000);

            // get disk again
            FindVdsDisk(serverName, diskInfo.DiskNumber, out advancedDisk, out diskPack);

            // find volumes using VDS
            List <string> volumes = new List <string>();

            HostedSolutionLog.LogInfo("Querying disk volumes with VDS");
            foreach (Volume volume in diskPack.Volumes)
            {
                string letter = volume.DriveLetter.ToString();
                if (letter != "")
                {
                    volumes.Add(letter);
                }
            }

            // find volumes using WMI
            if (volumes.Count == 0 && objDisk != null)
            {
                HostedSolutionLog.LogInfo("Querying disk volumes with WMI");
                foreach (ManagementObject objPartition in objDisk.GetRelated("Win32_DiskPartition"))
                {
                    foreach (ManagementObject objVolume in objPartition.GetRelated("Win32_LogicalDisk"))
                    {
                        volumes.Add(objVolume["Name"].ToString().TrimEnd(':'));
                    }
                }
            }

            HostedSolutionLog.LogInfo("Volumes found: " + volumes.Count);

            // Set volumes
            diskInfo.DiskVolumes = volumes.ToArray();

            return(diskInfo);
        }