Skip to content

Commit 4dd7e4a

Browse files
committed
Move WMI disk functions to cim package
1 parent d6cd0f5 commit 4dd7e4a

File tree

3 files changed

+170
-50
lines changed

3 files changed

+170
-50
lines changed

pkg/cim/disk.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ const (
2323
// GPTPartitionTypeMicrosoftReserved is the GUID for Microsoft Reserved Partition (MSR)
2424
// Reserved by Windows for system use
2525
GPTPartitionTypeMicrosoftReserved = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"
26+
27+
// ErrorCodeCreatePartitionAccessPathAlreadyInUse is the error code (42002) returned when the driver letter failed to assign after partition created
28+
ErrorCodeCreatePartitionAccessPathAlreadyInUse = 42002
29+
)
30+
31+
var (
32+
DiskSelectorListForNumberAndLocation = []string{"Number", "Location"}
33+
DiskSelectorListForPartitionStyle = []string{"PartitionStyle"}
34+
DiskSelectorListForPathAndSerialNumber = []string{"Path", "SerialNumber"}
35+
DiskSelectorListForIsOffline = []string{"IsOffline"}
36+
DiskSelectorListForSize = []string{"Size"}
2637
)
2738

2839
// QueryDiskByNumber retrieves disk information for a specific disk identified by its number.
@@ -76,3 +87,106 @@ func ListDisks(selectorList []string) ([]*storage.MSFT_Disk, error) {
7687

7788
return disks, nil
7889
}
90+
91+
// InitializeDisk initializes a RAW disk with a particular partition style.
92+
//
93+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/initialize-msft-disk
94+
// for the WMI method definition.
95+
func InitializeDisk(disk *storage.MSFT_Disk, partitionStyle int) (int, error) {
96+
result, err := disk.InvokeMethodWithReturn("Initialize", int32(partitionStyle))
97+
return int(result), err
98+
}
99+
100+
// RefreshDisk Refreshes the cached disk layout information.
101+
//
102+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk-refresh
103+
// for the WMI method definition.
104+
func RefreshDisk(disk *storage.MSFT_Disk) (int, string, error) {
105+
var status string
106+
result, err := disk.InvokeMethodWithReturn("Refresh", &status)
107+
return int(result), status, err
108+
}
109+
110+
// CreatePartitionOnDisk creates a partition on a disk.
111+
//
112+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/createpartition-msft-disk
113+
// for the WMI method definition.
114+
func CreatePartitionOnDisk(disk *storage.MSFT_Disk, params ...interface{}) (int, error) {
115+
result, err := disk.InvokeMethodWithReturn(
116+
"CreatePartition",
117+
params...)
118+
return int(result), err
119+
}
120+
121+
// SetDiskState takes a disk online or offline.
122+
//
123+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk-online and
124+
// https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-disk-offline
125+
// for the WMI method definition.
126+
func SetDiskState(disk *storage.MSFT_Disk, online bool) (int, string, error) {
127+
method := "Offline"
128+
if online {
129+
method = "Online"
130+
}
131+
132+
var status string
133+
result, err := disk.InvokeMethodWithReturn(method, &status)
134+
return int(result), status, err
135+
}
136+
137+
// RescanDisks rescans all changes by updating the internal cache of software objects (that is, Disks, Partitions, Volumes)
138+
// for the storage setting.
139+
//
140+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-storagesetting-updatehoststoragecache
141+
// for the WMI method definition.
142+
func RescanDisks() (int, error) {
143+
result, _, err := InvokeCimMethod(WMINamespaceStorage, "MSFT_StorageSetting", "UpdateHostStorageCache", nil)
144+
return result, err
145+
}
146+
147+
// GetDiskNumber returns the number of a disk.
148+
func GetDiskNumber(disk *storage.MSFT_Disk) (uint32, error) {
149+
number, err := disk.GetProperty("Number")
150+
if err != nil {
151+
return 0, err
152+
}
153+
return uint32(number.(int32)), err
154+
}
155+
156+
// GetDiskLocation returns the location of a disk.
157+
func GetDiskLocation(disk *storage.MSFT_Disk) (string, error) {
158+
return disk.GetPropertyLocation()
159+
}
160+
161+
// GetDiskPartitionStyle returns the partition style of a disk.
162+
func GetDiskPartitionStyle(disk *storage.MSFT_Disk) (int32, error) {
163+
retValue, err := disk.GetProperty("PartitionStyle")
164+
if err != nil {
165+
return 0, err
166+
}
167+
return retValue.(int32), err
168+
}
169+
170+
// IsDiskOffline returns whether a disk is offline.
171+
func IsDiskOffline(disk *storage.MSFT_Disk) (bool, error) {
172+
return disk.GetPropertyIsOffline()
173+
}
174+
175+
// GetDiskSize returns the size of a disk.
176+
func GetDiskSize(disk *storage.MSFT_Disk) (int64, error) {
177+
sz, err := disk.GetProperty("Size")
178+
if err != nil {
179+
return -1, err
180+
}
181+
return strconv.ParseInt(sz.(string), 10, 64)
182+
}
183+
184+
// GetDiskPath returns the path of a disk.
185+
func GetDiskPath(disk *storage.MSFT_Disk) (string, error) {
186+
return disk.GetPropertyPath()
187+
}
188+
189+
// GetDiskSerialNumber returns the serial number of a disk.
190+
func GetDiskSerialNumber(disk *storage.MSFT_Disk) (string, error) {
191+
return disk.GetPropertySerialNumber()
192+
}

pkg/cim/volume.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,27 @@ func GetPartitionDiskNumber(part *storage.MSFT_Partition) (uint32, error) {
245245

246246
return uint32(diskNumber.(int32)), nil
247247
}
248+
249+
// SetPartitionState takes a partition online or offline.
250+
//
251+
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-online and
252+
// https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partition-offline
253+
// for the WMI method definition.
254+
func SetPartitionState(part *storage.MSFT_Partition, online bool) (int, string, error) {
255+
method := "Offline"
256+
if online {
257+
method = "Online"
258+
}
259+
260+
var status string
261+
result, err := part.InvokeMethodWithReturn(method, &status)
262+
return int(result), status, err
263+
}
264+
265+
func FilterForPartitionOnDisk(diskNumber uint32) *query.WmiQueryFilter {
266+
return query.NewWmiQueryFilter("DiskNumber", strconv.Itoa(int(diskNumber)), query.Equals)
267+
}
268+
269+
func FilterForPartitionsOfTypeNormal() *query.WmiQueryFilter {
270+
return query.NewWmiQueryFilter("GptType", GPTPartitionTypeMicrosoftReserved, query.NotEquals)
271+
}

0 commit comments

Comments
 (0)