Skip to content

Commit 37cdee1

Browse files
jingxu97brahmaroutu
authored andcommitted
Remove driver letter assignment during volume format
This PR removes the driver letter assignment during volume format and mount because driver letter might run out and cause issues during mount. Intead, it uses volume id to mount the target dir.
1 parent a9ea36e commit 37cdee1

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

mount_windows.go

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

2828
"k8s.io/klog/v2"
29-
utilexec "k8s.io/utils/exec"
3029
"k8s.io/utils/keymutex"
3130
)
3231

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

223222
// format disk if it is unformatted(raw)
224223
cmd := fmt.Sprintf("Get-Disk -Number %s | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru"+
225-
" | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", source, fstype)
224+
" | New-Partition -UseMaximumSize | Format-Volume -FileSystem %s -Confirm:$false", source, fstype)
226225
if output, err := mounter.Exec.Command("powershell", "/c", cmd).CombinedOutput(); err != nil {
227226
return fmt.Errorf("diskMount: format disk failed, error: %v, output: %q", err, string(output))
228227
}
229228
klog.V(4).Infof("diskMount: Disk successfully formatted, disk: %q, fstype: %q", source, fstype)
230229

231-
driveLetter, err := getDriveLetterByDiskNumber(source, mounter.Exec)
230+
volumeIds, err := listVolumesOnDisk(source)
232231
if err != nil {
233232
return err
234233
}
235-
driverPath := driveLetter + ":"
234+
driverPath := volumeIds[0]
236235
target = NormalizeWindowsPath(target)
237236
output, err := mounter.Exec.Command("cmd", "/c", "mklink", "/D", target, driverPath).CombinedOutput()
238237
if err != nil {
@@ -243,17 +242,17 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
243242
return nil
244243
}
245244

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)
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)
249248
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 "", fmt.Errorf("azureMount: Get Drive Letter failed: %v, output: %q", err, string(output))
251+
return []string{}, fmt.Errorf("error list volumes on disk. cmd: %s, output: %s, error: %v", cmd, string(output), err)
252252
}
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
253+
254+
volumeIds := strings.Split(strings.TrimSpace(string(output)), "\r\n")
255+
return volumeIds, nil
257256
}
258257

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

0 commit comments

Comments
 (0)