Skip to content

Commit 6cecc73

Browse files
authored
logical_disk: Fix failed to get disk ID for dynamic volumes (#1544)
1 parent 4635be8 commit 6cecc73

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

.github/workflows/pr-check.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ jobs:
2828
bug
2929
docs
3030
chore
31-
&& exit 1
31+
&& exit 1
32+
title:
33+
name: check title prefix
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v3
37+
- name: check
38+
run: |
39+
PR_TITLE_PREFIX=$(echo "$PR_TITLE" | cut -d':' -f1)
40+
if [[ ! -d "pkg/collector/$PR_TITLE_PREFIX" ]] || [[ "$PR_TITLE_PREFIX" == "chore" ]] || [[ "$PR_TITLE_PREFIX" == "*" ]]; then
41+
echo "PR title must start with an name of an collector package"
42+
echo "Example: 'logical_disk: description'"
43+
exit 1
44+
fi
45+
env:
46+
PR_TITLE: ${{ github.event.pull_request.title }}

pkg/collector/logical_disk/logical_disk.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ package logical_disk
55
import (
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.
467472
func 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

503516
func getVolumeInfo(rootDrive string) (volumeInfo, error) {

0 commit comments

Comments
 (0)