@@ -26,7 +26,6 @@ import (
26
26
"strings"
27
27
28
28
"k8s.io/klog/v2"
29
- utilexec "k8s.io/utils/exec"
30
29
"k8s.io/utils/keymutex"
31
30
utilpath "k8s.io/utils/path"
32
31
)
@@ -230,17 +229,17 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
230
229
231
230
// format disk if it is unformatted(raw)
232
231
cmd := fmt .Sprintf ("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru" +
233
- " | New-Partition -AssignDriveLetter - UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false" , source , fstype )
232
+ " | New-Partition -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false" , source , fstype )
234
233
if output , err := mounter .Exec .Command ("powershell" , "/c" , cmd ).CombinedOutput (); err != nil {
235
234
return fmt .Errorf ("diskMount: format disk failed, error: %v, output: %q" , err , string (output ))
236
235
}
237
236
klog .V (4 ).Infof ("diskMount: Disk successfully formatted, disk: %q, fstype: %q" , source , fstype )
238
237
239
- driveLetter , err := getDriveLetterByDiskNumber (source , mounter . Exec )
238
+ volumeIds , err := listVolumesOnDisk (source )
240
239
if err != nil {
241
240
return err
242
241
}
243
- driverPath := driveLetter + ":"
242
+ driverPath := volumeIds [ 0 ]
244
243
target = NormalizeWindowsPath (target )
245
244
klog .V (4 ).Infof ("Attempting to formatAndMount disk: %s %s %s" , fstype , driverPath , target )
246
245
if output , err := mounter .Exec .Command ("cmd" , "/c" , "mklink" , "/D" , target , driverPath ).CombinedOutput (); err != nil {
@@ -250,17 +249,17 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
250
249
return nil
251
250
}
252
251
253
- // Get drive letter according to windows disk number
254
- func getDriveLetterByDiskNumber ( diskNum string , exec utilexec. Interface ) (string , error ) {
255
- cmd := fmt .Sprintf ("(Get-Partition -DiskNumber %s).DriveLetter " , diskNum )
252
+ // ListVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID).
253
+ func listVolumesOnDisk ( diskID string ) (volumeIDs [] string , err error ) {
254
+ cmd := fmt .Sprintf ("(Get-Disk -DeviceId %s | Get-Partition | Get-Volume).UniqueId " , diskID )
256
255
output , err := exec .Command ("powershell" , "/c" , cmd ).CombinedOutput ()
256
+ klog .V (4 ).Infof ("listVolumesOnDisk id from %s: %s" , diskID , string (output ))
257
257
if err != nil {
258
- return "" , fmt .Errorf ("azureMount: Get Drive Letter failed : %v , output: %q " , err , string (output ))
258
+ return [] string {} , fmt .Errorf ("error list volumes on disk. cmd : %s , output: %s, error: %v " , cmd , string (output ), err )
259
259
}
260
- if len (string (output )) < 1 {
261
- return "" , fmt .Errorf ("azureMount: Get Drive Letter failed, output is empty" )
262
- }
263
- return string (output )[:1 ], nil
260
+
261
+ volumeIds := strings .Split (strings .TrimSpace (string (output )), "\r \n " )
262
+ return volumeIds , nil
264
263
}
265
264
266
265
// getAllParentLinks walks all symbolic links and return all the parent targets recursively
0 commit comments