From 6130ea0e3104c1e10bd33befcf0684ba2f60f7f9 Mon Sep 17 00:00:00 2001 From: zhou Date: Sat, 16 Jul 2022 20:46:47 +0800 Subject: [PATCH] Update block_windows.go wmi report inaccurate disk size, refer : https://stackoverflow.com/questions/9901792/wmi-win32-diskdrive-to-get-total-sector-on-the-physical-disk-drive replace Win32_DiskDrive with DeviceIOControl. --- pkg/block/block_windows.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/block/block_windows.go b/pkg/block/block_windows.go index 75c6f04c..73d33e85 100644 --- a/pkg/block/block_windows.go +++ b/pkg/block/block_windows.go @@ -6,7 +6,10 @@ package block import ( + "os" "strings" + "syscall" + "unsafe" "github.com/StackExchange/wmi" @@ -158,6 +161,26 @@ func getDiskDrives() ([]win32DiskDrive, error) { if err := wmi.Query(wqlDiskDrive, &win3232DiskDriveDescriptions); err != nil { return nil, err } + for _, disk := range win3232DiskDriveDescriptions { + if disk.DeviceID == nil { + continue + } + volume, err := os.OpenFile(*disk.DeviceID, os.O_RDWR, 0) + if err != nil { + continue + } + // Get the volume's underlying partition size in NTFS clusters. + var ( + partitionSize int64 + bytes uint32 + ) + const _IOCTL_DISK_GET_LENGTH_INFO = 0x0007405C + err = syscall.DeviceIoControl(syscall.Handle(volume.Fd()), _IOCTL_DISK_GET_LENGTH_INFO, nil, 0, (*byte)(unsafe.Pointer(&partitionSize)), 8, &bytes, nil) + volume.Close() + if err == nil { + *disk.Size = uint64(partitionSize) + } + } return win3232DiskDriveDescriptions, nil }