Skip to content

Commit 278ece3

Browse files
authoredMay 20, 2020
Merge pull request #166 from jingxu97/May/drivename
Remove driver letter assignment during volume format
2 parents 2df71eb + a18ed37 commit 278ece3

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed
 

‎mount/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
utilpath "k8s.io/utils/path"
3231
)
@@ -230,17 +229,17 @@ func (mounter *SafeFormatAndMount) formatAndMountSensitive(source string, target
230229

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

239-
driveLetter, err := getDriveLetterByDiskNumber(source, mounter.Exec)
238+
volumeIds, err := listVolumesOnDisk(source)
240239
if err != nil {
241240
return err
242241
}
243-
driverPath := driveLetter + ":"
242+
driverPath := volumeIds[0]
244243
target = NormalizeWindowsPath(target)
245244
klog.V(4).Infof("Attempting to formatAndMount disk: %s %s %s", fstype, driverPath, target)
246245
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
250249
return nil
251250
}
252251

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)
256255
output, err := exec.Command("powershell", "/c", cmd).CombinedOutput()
256+
klog.V(4).Infof("listVolumesOnDisk id from %s: %s", diskID, string(output))
257257
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)
259259
}
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
264263
}
265264

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

0 commit comments

Comments
 (0)
Please sign in to comment.