Skip to content

Commit 2b4087a

Browse files
committed
Use WMI to implement Disk API for reduce PowerShell overhead
1 parent f9750e0 commit 2b4087a

File tree

2 files changed

+197
-115
lines changed

2 files changed

+197
-115
lines changed

pkg/cim/disk.go

+42
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ import (
1111
"github.com/microsoft/wmi/server2019/root/microsoft/windows/storage"
1212
)
1313

14+
const (
15+
// PartitionStyleUnknown indicates an unknown partition table format
16+
PartitionStyleUnknown = 0
17+
// PartitionStyleGPT indicates the disk uses GUID Partition Table (GPT) format
18+
PartitionStyleGPT = 2
19+
20+
// GPTPartitionTypeBasicData is the GUID for basic data partitions in GPT
21+
// Used for general purpose storage partitions
22+
GPTPartitionTypeBasicData = "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}"
23+
// GPTPartitionTypeMicrosoftReserved is the GUID for Microsoft Reserved Partition (MSR)
24+
// Reserved by Windows for system use
25+
GPTPartitionTypeMicrosoftReserved = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"
26+
)
27+
1428
// QueryDiskByNumber retrieves disk information for a specific disk identified by its number.
1529
//
1630
// The equivalent WMI query is:
@@ -34,3 +48,31 @@ func QueryDiskByNumber(diskNumber uint32, selectorList []string) (*storage.MSFT_
3448

3549
return disk, nil
3650
}
51+
52+
// ListDisks retrieves information about all available disks.
53+
//
54+
// The equivalent WMI query is:
55+
//
56+
// SELECT [selectors] FROM MSFT_Disk
57+
//
58+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk
59+
// for the WMI class definition.
60+
func ListDisks(selectorList []string) ([]*storage.MSFT_Disk, error) {
61+
diskQuery := query.NewWmiQueryWithSelectList("MSFT_Disk", selectorList)
62+
instances, err := QueryInstances(WMINamespaceStorage, diskQuery)
63+
if IgnoreNotFound(err) != nil {
64+
return nil, err
65+
}
66+
67+
var disks []*storage.MSFT_Disk
68+
for _, instance := range instances {
69+
disk, err := storage.NewMSFT_DiskEx1(instance)
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to query disk %v. error: %v", instance, err)
72+
}
73+
74+
disks = append(disks, disk)
75+
}
76+
77+
return disks, nil
78+
}

0 commit comments

Comments
 (0)