Skip to content

Commit

Permalink
Merge pull request #340 from williambrode/ISSUE-339
Browse files Browse the repository at this point in the history
Fix #339 by getting the physical disk info which properly shows SSD vs HDD
  • Loading branch information
jaypipes authored May 4, 2023
2 parents d89a781 + 0da852b commit 3ff88ab
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions pkg/block/block_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,37 @@
package block

import (
"strconv"
"strings"

"github.com/StackExchange/wmi"

"github.com/jaypipes/ghw/pkg/util"
)

type physicalDiskMediaType int

const (
PHYSICAL_DISK_MEDIA_TYPE_UNSPECIFIED physicalDiskMediaType = 0
PHYSICAL_DISK_MEDIA_TYPE_HDD physicalDiskMediaType = 3
PHYSICAL_DISK_MEDIA_TYPE_SSD physicalDiskMediaType = 4
PHYSICAL_DISK_MEDIA_TYPE_SCM physicalDiskMediaType = 5
)

func (dt physicalDiskMediaType) ToDriveType() DriveType {
switch dt {
case PHYSICAL_DISK_MEDIA_TYPE_UNSPECIFIED:
return DRIVE_TYPE_UNKNOWN
case PHYSICAL_DISK_MEDIA_TYPE_HDD:
return DRIVE_TYPE_HDD
case PHYSICAL_DISK_MEDIA_TYPE_SSD:
return DRIVE_TYPE_SSD
case PHYSICAL_DISK_MEDIA_TYPE_SCM:
return DRIVE_TYPE_UNKNOWN
}
return DRIVE_TYPE_UNKNOWN
}

const wqlDiskDrive = "SELECT Caption, CreationClassName, DefaultBlockSize, Description, DeviceID, Index, InterfaceType, Manufacturer, MediaType, Model, Name, Partitions, SerialNumber, Size, TotalCylinders, TotalHeads, TotalSectors, TotalTracks, TracksPerCylinder FROM Win32_DiskDrive"

type win32DiskDrive struct {
Expand Down Expand Up @@ -75,6 +99,13 @@ type win32LogicalDisk struct {
SystemName *string
}

const wqlPhysicalDisk = "SELECT DeviceId, MediaType FROM MSFT_PhysicalDisk"

type win32PhysicalDisk struct {
DeviceId string
MediaType physicalDiskMediaType
}

func (i *Info) load() error {
win32DiskDriveDescriptions, err := getDiskDrives()
if err != nil {
Expand All @@ -96,14 +127,27 @@ func (i *Info) load() error {
return err
}

win32PhysicalDisks, err := getPhysicalDisks()
if err != nil {
return err
}

// Converting into standard structures
disks := make([]*Disk, 0)
for _, diskdrive := range win32DiskDriveDescriptions {
var physicalDiskMediaType physicalDiskMediaType
for _, physicalDisk := range win32PhysicalDisks {
if id, err := strconv.Atoi(physicalDisk.DeviceId); err != nil {
return err
} else if uint32(id) == *diskdrive.Index {
physicalDiskMediaType = physicalDisk.MediaType
}
}
disk := &Disk{
Name: strings.TrimSpace(*diskdrive.DeviceID),
SizeBytes: *diskdrive.Size,
PhysicalBlockSizeBytes: *diskdrive.DefaultBlockSize,
DriveType: toDriveType(*diskdrive.MediaType, *diskdrive.Caption),
DriveType: toDriveType(physicalDiskMediaType, *diskdrive.MediaType, *diskdrive.Caption),
StorageController: toStorageController(*diskdrive.InterfaceType),
BusPath: util.UNKNOWN, // TODO: add information
NUMANodeID: -1,
Expand Down Expand Up @@ -191,7 +235,20 @@ func getLogicalDisks() ([]win32LogicalDisk, error) {
return win32LogicalDiskDescriptions, nil
}

func toDriveType(mediaType string, caption string) DriveType {
func getPhysicalDisks() ([]win32PhysicalDisk, error) {
// Getting physical disks from WMI
var win32PhysicalDisks []win32PhysicalDisk
if err := wmi.QueryNamespace(wqlPhysicalDisk, &win32PhysicalDisks, "root\\Microsoft\\Windows\\Storage"); err != nil {
return nil, err
}
return win32PhysicalDisks, nil
}

func toDriveType(physicalDiskMediaType physicalDiskMediaType, mediaType string, caption string) DriveType {
if driveType := physicalDiskMediaType.ToDriveType(); driveType != DRIVE_TYPE_UNKNOWN {
return driveType
}

mediaType = strings.ToLower(mediaType)
caption = strings.ToLower(caption)
if strings.Contains(mediaType, "fixed") || strings.Contains(mediaType, "ssd") || strings.Contains(caption, "ssd") {
Expand Down

0 comments on commit 3ff88ab

Please sign in to comment.