Пример #1
0
        public static bool IsPhysicalDiskSystem(PhysicalDisk pd)
        {
            bool result = false;

            try
            {
                string text  = null;
                string text2 = null;
                Regex  regex = new Regex("PhysicalDisk([0-9]+)");
                Match  match = regex.Match(pd.FriendlyName);
                if (match != null && match.Success && match.Groups.Count > 1)
                {
                    text = match.Groups[1].Value;
                }
                if (text != null)
                {
                    ManagementObjectCollection managementObjectCollection = null;
                    if (SpacesApi.GetWin32ObjectsByQuery("select * from Win32_DiskPartition where BootPartition=True and PrimaryPartition=True", ref managementObjectCollection) == SpacesApiError.Success)
                    {
                        foreach (ManagementBaseObject managementBaseObject in managementObjectCollection)
                        {
                            ManagementObject managementObject = (ManagementObject)managementBaseObject;
                            if (managementObject["DeviceId"] != null)
                            {
                                string input  = (string)managementObject["DeviceId"];
                                Regex  regex2 = new Regex("Disk #([0-9]+), Partition #([0-9])");
                                Match  match2 = regex2.Match(input);
                                if (match2 != null && match2.Success && match2.Groups.Count > 1)
                                {
                                    text2 = match2.Groups[1].Value;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (text != null && text2 != null)
                {
                    result = (text == text2);
                }
            }
            catch (Exception ex)
            {
                if (SpacesApi.DebugOn)
                {
                    SpacesApi.Debug("Failed to determine if physial disk {0} is system disk: {1}", new object[]
                    {
                        pd.FriendlyName,
                        ex
                    });
                }
            }
            return(result);
        }
Пример #2
0
        public static SpacesApiError GetPhysicalDisk(string serial, ref PhysicalDisk disk)
        {
            SpacesApiError spacesApiError = SpacesApiError.Success;

            try
            {
                ManagementObjectCollection managementObjectCollection = null;
                disk = null;
                string query = string.Format("Select * From MSFT_PhysicalDisk Where SerialNumber like '%{0}'", serial);
                spacesApiError = SpacesApi.GetStorageObjectsByQuery(query, ref managementObjectCollection);
                if (spacesApiError != SpacesApiError.Success)
                {
                    return(spacesApiError);
                }
                foreach (ManagementBaseObject managementBaseObject in managementObjectCollection)
                {
                    ManagementObject m = (ManagementObject)managementBaseObject;
                    string           b = SpacesApiUtil.GetManagementObjectValue <string>(m, "SerialNumber").Trim();
                    if (serial == b)
                    {
                        disk = new PhysicalDisk();
                        disk.FromManagementObject(m);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                spacesApiError = SpacesApiError.Failed;
                if (SpacesApi.DebugOn)
                {
                    SpacesApi.Debug("Failed to get physical disk: {0}", new object[]
                    {
                        ex
                    });
                }
            }
            return(spacesApiError);
        }
Пример #3
0
        public static SpacesApiError GetPhysicalDisksForVirtualDisk(string vdid, ref List <PhysicalDisk> disks)
        {
            SpacesApiError spacesApiError = SpacesApiError.Success;

            try
            {
                Regex regex  = new Regex("(.+)MSFT_VirtualDisk.ObjectId=\"([{}0-9a-zA-Z-]+)\"");
                Regex regex2 = new Regex("(.+)MSFT_PhysicalDisk.ObjectId=\"([{}0-9a-zA-Z-]+)\"");
                ManagementObjectCollection managementObjectCollection = null;
                spacesApiError = SpacesApi.GetStorageObjectsByQuery("Select * from MSFT_VirtualDiskToPhysicalDisk", ref managementObjectCollection);
                if (spacesApiError != SpacesApiError.Success)
                {
                    return(spacesApiError);
                }
                foreach (ManagementBaseObject managementBaseObject in managementObjectCollection)
                {
                    ManagementObject managementObject = (ManagementObject)managementBaseObject;
                    string           text             = null;
                    string           text2            = null;
                    if (managementObject["VirtualDisk"] != null)
                    {
                        Match match = regex.Match((string)managementObject["VirtualDisk"]);
                        if (match.Success && match.Groups.Count > 2)
                        {
                            text = match.Groups[2].Value;
                            if (text != vdid)
                            {
                                continue;
                            }
                        }
                    }
                    if (text != null && managementObject["PhysicalDisk"] != null)
                    {
                        Match match2 = regex2.Match((string)managementObject["PhysicalDisk"]);
                        if (match2.Success && match2.Groups.Count > 2)
                        {
                            text2 = match2.Groups[2].Value;
                        }
                    }
                    if (text2 != null)
                    {
                        ManagementObjectCollection managementObjectCollection2 = null;
                        spacesApiError = SpacesApi.GetStorageObjectsByQuery("Select * From MSFT_PhysicalDisk where ObjectId = '" + text2 + "'", ref managementObjectCollection2);
                        if (spacesApiError != SpacesApiError.Success)
                        {
                            return(spacesApiError);
                        }
                        foreach (ManagementBaseObject managementBaseObject2 in managementObjectCollection2)
                        {
                            ManagementObject m            = (ManagementObject)managementBaseObject2;
                            PhysicalDisk     physicalDisk = new PhysicalDisk();
                            physicalDisk.FromManagementObject(m);
                            if (disks == null)
                            {
                                disks = new List <PhysicalDisk>();
                            }
                            disks.Add(physicalDisk);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                spacesApiError = SpacesApiError.Failed;
                if (SpacesApi.DebugOn)
                {
                    SpacesApi.Debug("Failed to get physical disks for virtual disk {1}: {0}", new object[]
                    {
                        ex,
                        vdid
                    });
                }
            }
            return(spacesApiError);
        }