Skip to content

Commit a9ea36e

Browse files
jingxu97brahmaroutu
authored andcommitted
Revert "Merge pull request kubernetes#166 from jingxu97/May/drivename"
This reverts commit 278ece378a5054f16a07622b51ddaf82b328d6e6, reversing changes made to 2df71ebbae66f39338aed4cd0bb82d2212ee33cc.
1 parent 029074e commit a9ea36e

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

mount_windows.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727

2828
"k8s.io/klog/v2"
29+
utilexec "k8s.io/utils/exec"
2930
"k8s.io/utils/keymutex"
3031
)
3132

@@ -221,38 +222,38 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
221222

222223
// format disk if it is unformatted(raw)
223224
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)
225226
if output, err := mounter.Exec.Command("powershell", "/c", cmd).CombinedOutput(); err != nil {
226227
return fmt.Errorf("diskMount: format disk failed, error: %v, output: %q", err, string(output))
227228
}
228229
klog.V(4).Infof("diskMount: Disk successfully formatted, disk: %q, fstype: %q", source, fstype)
229230

230-
volumeIds, err := listVolumesOnDisk(source)
231+
driveLetter, err := getDriveLetterByDiskNumber(source, mounter.Exec)
231232
if err != nil {
232233
return err
233234
}
234-
volumeID := volumeIds[0]
235+
driverPath := driveLetter + ":"
235236
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()
237238
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))
239240
return err
240241
}
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))
242243
return nil
243244
}
244245

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)
248249
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
249-
klog.V(4).Infof("listVolumesOnDisk id from %s: %s", diskID, string(output))
250250
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))
252252
}
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
256257
}
257258

258259
// getAllParentLinks walks all symbolic links and return all the parent targets recursively

0 commit comments

Comments
 (0)