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