@@ -23,6 +23,17 @@ const (
23
23
// GPTPartitionTypeMicrosoftReserved is the GUID for Microsoft Reserved Partition (MSR)
24
24
// Reserved by Windows for system use
25
25
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" }
26
37
)
27
38
28
39
// QueryDiskByNumber retrieves disk information for a specific disk identified by its number.
@@ -76,3 +87,106 @@ func ListDisks(selectorList []string) ([]*storage.MSFT_Disk, error) {
76
87
77
88
return disks , nil
78
89
}
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
+ }
0 commit comments