@@ -5,11 +5,13 @@ package logical_disk
55import (
66 "encoding/binary"
77 "fmt"
8- "golang.org/x/sys/windows"
98 "regexp"
9+ "slices"
1010 "strconv"
1111 "strings"
1212
13+ "golang.org/x/sys/windows"
14+
1315 "github.com/alecthomas/kingpin/v2"
1416 "github.com/go-kit/log"
1517 "github.com/go-kit/log/level"
@@ -463,6 +465,9 @@ func getDriveType(driveType uint32) string {
463465 }
464466}
465467
468+ // diskExtentSize Size of the DiskExtent structure in bytes.
469+ const diskExtentSize = 24
470+
466471// getDiskIDByVolume returns the disk ID for a given volume.
467472func getDiskIDByVolume (rootDrive string ) (string , error ) {
468473 // Open a volume handle to the Disk Root.
@@ -488,16 +493,24 @@ func getDiskIDByVolume(rootDrive string) (string, error) {
488493 var bytesReturned uint32
489494 err = windows .DeviceIoControl (f , controlCode , nil , 0 , & volumeDiskExtents [0 ], uint32 (len (volumeDiskExtents )), & bytesReturned , nil )
490495 if err != nil {
491- return "" , err
496+ return "" , fmt .Errorf ("could not identify physical drive for %s: %w" , rootDrive , err )
497+ }
498+
499+ numDiskIDs := uint (binary .LittleEndian .Uint32 (volumeDiskExtents ))
500+ if numDiskIDs < 1 {
501+ return "" , fmt .Errorf ("could not identify physical drive for %s: no disk IDs returned" , rootDrive )
492502 }
493503
494- if uint (binary .LittleEndian .Uint32 (volumeDiskExtents )) != 1 {
495- return "" , fmt .Errorf ("could not identify physical drive for %s" , rootDrive )
504+ diskIDs := make ([]string , numDiskIDs )
505+
506+ for i := range numDiskIDs {
507+ diskIDs [i ] = strconv .FormatUint (uint64 (binary .LittleEndian .Uint32 (volumeDiskExtents [8 + i * diskExtentSize :])), 10 )
496508 }
497509
498- diskId := strconv .FormatUint (uint64 (binary .LittleEndian .Uint32 (volumeDiskExtents [8 :])), 10 )
510+ slices .Sort (diskIDs )
511+ diskIDs = slices .Compact (diskIDs )
499512
500- return diskId , nil
513+ return strings . Join ( diskIDs , ";" ) , nil
501514}
502515
503516func getVolumeInfo (rootDrive string ) (volumeInfo , error ) {
0 commit comments