Skip to content

Commit

Permalink
Implement disk.GetDiskSerialNumber for Windows via WMI
Browse files Browse the repository at this point in the history
shirou#435. Parameter should be \\.\PHYSICALDRIVEX as obtained
via `wmic diskdrive`. See also https://superuser.com/a/498109

The serial is currently returned as found via WMI, testing would be needed
by booting on Linux to check if we have the same results and probably
sort them as per https://stackoverflow.com/q/27297348/
  • Loading branch information
Lomanic committed Dec 5, 2017
1 parent 5c46042 commit 8905b92
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions disk/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"unsafe"

"github.com/StackExchange/wmi"
"github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/windows"
)
Expand All @@ -33,6 +34,11 @@ type Win32_PerfFormattedData struct {
AvgDisksecPerWrite uint64
}

type Win32_DiskDrive struct {
Name string
SerialNumber string
}

const WaitMSec = 500

func Usage(path string) (*UsageStat, error) {
Expand Down Expand Up @@ -159,3 +165,20 @@ func IOCounters(names ...string) (map[string]IOCountersStat, error) {
}
return ret, nil
}

func GetDiskSerialNumber(name string) string {
var dst []Win32_DiskDrive
q := wmi.CreateQuery(&dst, "")
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
defer cancel()
err := common.WMIQueryWithContext(ctx, q, &dst)
if err != nil {
return ""
}
for _, v := range dst {
if v.Name == name {
return v.SerialNumber
}
}
return ""
}

0 comments on commit 8905b92

Please sign in to comment.