9
9
10
10
"github.com/microsoft/wmi/pkg/base/query"
11
11
"github.com/microsoft/wmi/pkg/errors"
12
- cim "github.com/microsoft/wmi/pkg/wmiinstance"
13
12
"github.com/microsoft/wmi/server2019/root/microsoft/windows/storage"
14
13
)
15
14
@@ -129,131 +128,84 @@ func ListPartitionsWithFilters(selectorList []string, filters ...*query.WmiQuery
129
128
return partitions , nil
130
129
}
131
130
132
- // ListPartitionToVolumeMappings builds a mapping between partition and volume with partition Object ID as the key.
133
- //
134
- // The equivalent WMI query is:
135
- //
136
- // SELECT [selectors] FROM MSFT_PartitionToVolume
137
- //
138
- // Partition | Volume
139
- // --------- | ------
140
- // MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...
141
- //
142
- // Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume
143
- // for the WMI class definition.
144
- func ListPartitionToVolumeMappings () (map [string ]string , error ) {
145
- return ListWMIInstanceMappings (WMINamespaceStorage , "MSFT_PartitionToVolume" , nil ,
146
- mappingObjectRefIndexer ("Partition" , "MSFT_Partition" , "ObjectId" ),
147
- mappingObjectRefIndexer ("Volume" , "MSFT_Volume" , "ObjectId" ),
148
- )
149
- }
150
-
151
- // ListVolumeToPartitionMappings builds a mapping between volume and partition with volume Object ID as the key.
152
- //
153
- // The equivalent WMI query is:
131
+ // FindPartitionsByVolume finds all partitions associated with the given volumes
132
+ // using MSFT_PartitionToVolume association.
154
133
//
155
- // SELECT [selectors] FROM MSFT_PartitionToVolume
134
+ // WMI association MSFT_PartitionToVolume:
156
135
//
157
- // Partition | Volume
158
- // --------- | ------
159
- // MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...
136
+ // Partition | Volume
137
+ // --------- | ------
138
+ // MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...
160
139
//
161
140
// Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume
162
141
// for the WMI class definition.
163
- func ListVolumeToPartitionMappings () (map [string ]string , error ) {
164
- return ListWMIInstanceMappings (WMINamespaceStorage , "MSFT_PartitionToVolume" , nil ,
165
- mappingObjectRefIndexer ("Volume" , "MSFT_Volume" , "ObjectId" ),
166
- mappingObjectRefIndexer ("Partition" , "MSFT_Partition" , "ObjectId" ),
167
- )
168
- }
169
-
170
- // FindPartitionsByVolume finds all partitions associated with the given volumes
171
- // using partition-to-volume mapping.
172
- func FindPartitionsByVolume (partitions []* storage.MSFT_Partition , volumes []* storage.MSFT_Volume ) ([]* storage.MSFT_Partition , error ) {
173
- var partitionInstances []* cim.WmiInstance
174
- for _ , part := range partitions {
175
- partitionInstances = append (partitionInstances , part .WmiInstance )
176
- }
177
-
178
- var volumeInstances []* cim.WmiInstance
179
- for _ , volume := range volumes {
180
- volumeInstances = append (volumeInstances , volume .WmiInstance )
181
- }
182
-
183
- partitionToVolumeMappings , err := ListPartitionToVolumeMappings ()
184
- if err != nil {
185
- return nil , err
186
- }
187
-
188
- filtered , err := FindInstancesByObjectIDMapping (partitionInstances , volumeInstances , partitionToVolumeMappings )
189
- if err != nil {
190
- return nil , err
191
- }
192
-
142
+ func FindPartitionsByVolume (volumes []* storage.MSFT_Volume ) ([]* storage.MSFT_Partition , error ) {
193
143
var result []* storage.MSFT_Partition
194
- for _ , instance := range filtered {
195
- part , err := storage . NewMSFT_PartitionEx1 ( instance )
144
+ for _ , vol := range volumes {
145
+ collection , err := vol . GetAssociated ( "MSFT_PartitionToVolume" , "MSFT_Partition" , "Partition" , "Volume" )
196
146
if err != nil {
197
- return nil , fmt .Errorf ("failed to query partition %v. error: %v" , instance , err )
147
+ return nil , fmt .Errorf ("failed to query associated partition for %v. error: %v" , vol , err )
198
148
}
199
149
200
- result = append (result , part )
150
+ for _ , instance := range collection {
151
+ part , err := storage .NewMSFT_PartitionEx1 (instance )
152
+ if err != nil {
153
+ return nil , fmt .Errorf ("failed to query partition %v. error: %v" , instance , err )
154
+ }
155
+
156
+ result = append (result , part )
157
+ }
201
158
}
202
159
203
160
return result , nil
204
161
}
205
162
206
163
// FindVolumesByPartition finds all volumes associated with the given partitions
207
- // using volume-to-partition mapping.
208
- func FindVolumesByPartition (volumes []* storage.MSFT_Volume , partitions []* storage.MSFT_Partition ) ([]* storage.MSFT_Volume , error ) {
209
- var volumeInstances []* cim.WmiInstance
210
- for _ , volume := range volumes {
211
- volumeInstances = append (volumeInstances , volume .WmiInstance )
212
- }
213
-
214
- var partitionInstances []* cim.WmiInstance
215
- for _ , part := range partitions {
216
- partitionInstances = append (partitionInstances , part .WmiInstance )
217
- }
218
-
219
- volumeToPartitionMappings , err := ListVolumeToPartitionMappings ()
220
- if err != nil {
221
- return nil , err
222
- }
223
-
224
- filtered , err := FindInstancesByObjectIDMapping (volumeInstances , partitionInstances , volumeToPartitionMappings )
225
- if err != nil {
226
- return nil , err
227
- }
228
-
164
+ // using MSFT_PartitionToVolume association.
165
+ //
166
+ // WMI association MSFT_PartitionToVolume:
167
+ //
168
+ // Partition | Volume
169
+ // --------- | ------
170
+ // MSFT_Partition (ObjectId = "{1}\\WIN-8E2EVAQ9QSB\ROOT/Microsoft/Win...) | MSFT_Volume (ObjectId = "{1}\\WIN-8E2EVAQ9QS...
171
+ //
172
+ // Refer to https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-partitiontovolume
173
+ // for the WMI class definition.
174
+ func FindVolumesByPartition (partitions []* storage.MSFT_Partition ) ([]* storage.MSFT_Volume , error ) {
229
175
var result []* storage.MSFT_Volume
230
- for _ , instance := range filtered {
231
- volume , err := storage . NewMSFT_VolumeEx1 ( instance )
176
+ for _ , part := range partitions {
177
+ collection , err := part . GetAssociated ( "MSFT_PartitionToVolume" , "MSFT_Volume" , "Volume" , "Partition" )
232
178
if err != nil {
233
- return nil , fmt .Errorf ("failed to query volume %v. error: %v" , instance , err )
179
+ return nil , fmt .Errorf ("failed to query associated volumes for %v. error: %v" , part , err )
234
180
}
235
181
236
- result = append (result , volume )
182
+ for _ , instance := range collection {
183
+ volume , err := storage .NewMSFT_VolumeEx1 (instance )
184
+ if err != nil {
185
+ return nil , fmt .Errorf ("failed to query volume %v. error: %v" , instance , err )
186
+ }
187
+
188
+ result = append (result , volume )
189
+ }
237
190
}
238
191
239
192
return result , nil
240
193
}
241
194
242
195
// GetPartitionByVolumeUniqueID retrieves a specific partition from a volume identified by its unique ID.
243
- func GetPartitionByVolumeUniqueID (volumeID string , partitionSelectorList [] string ) (* storage.MSFT_Partition , error ) {
196
+ func GetPartitionByVolumeUniqueID (volumeID string ) (* storage.MSFT_Partition , error ) {
244
197
volume , err := QueryVolumeByUniqueID (volumeID , []string {"ObjectId" })
245
198
if err != nil {
246
199
return nil , err
247
200
}
248
201
249
- partitions , err := ListPartitionsWithFilters ( partitionSelectorList )
202
+ result , err := FindPartitionsByVolume ([] * storage. MSFT_Volume { volume } )
250
203
if err != nil {
251
204
return nil , err
252
205
}
253
206
254
- result , err := FindPartitionsByVolume (partitions , []* storage.MSFT_Volume {volume })
255
- if err != nil {
256
- return nil , err
207
+ if len (result ) == 0 {
208
+ return nil , errors .NotFound
257
209
}
258
210
259
211
return result [0 ], nil
@@ -269,12 +221,7 @@ func GetVolumeByDriveLetter(driveLetter string, partitionSelectorList []string)
269
221
return nil , err
270
222
}
271
223
272
- volumes , err := ListVolumes (partitionSelectorList )
273
- if err != nil {
274
- return nil , err
275
- }
276
-
277
- result , err := FindVolumesByPartition (volumes , partitions )
224
+ result , err := FindVolumesByPartition (partitions )
278
225
if err != nil {
279
226
return nil , err
280
227
}
0 commit comments