Skip to content

Commit

Permalink
Merge pull request #2731 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…2730-to-release-1.31

[release-1.31] cleanup: refine Windows Resizevolume func
  • Loading branch information
andyzhangx authored Dec 14, 2024
2 parents 193b64e + 3f28f78 commit 6d7d669
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions pkg/os/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,22 @@ func UnmountVolume(volumeID, path string) error {

// ResizeVolume - resizes a volume with the given size, if size == 0 then max supported size is used
func ResizeVolume(volumeID string, size int64) error {
// If size is 0 then we will resize to the maximum size possible, otherwise just resize to size
var cmd string
var out []byte
var err error
// if size is 0 then we will resize to the maximum size possible, otherwise just resize to size
var finalSize int64
var outString string
if size == 0 {
cmd = "Get-Volume -UniqueId \"$Env:volumeID\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json"
cmd := "Get-Volume -UniqueId \"$Env:volumeID\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json"
out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))

if err != nil || len(out) == 0 {
return fmt.Errorf("error getting sizemin,sizemax from mount. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}

var getVolumeSizing map[string]int64
outString = string(out)
err = json.Unmarshal([]byte(outString), &getVolumeSizing)
if err != nil {
outString := string(out)
if err = json.Unmarshal([]byte(outString), &getVolumeSizing); err != nil {
return fmt.Errorf("out %v outstring %v err %v", out, outString, err)
}

sizeMax := getVolumeSizing["SizeMax"]

finalSize = sizeMax
finalSize = getVolumeSizing["SizeMax"]
} else {
finalSize = size
}
Expand All @@ -151,14 +143,13 @@ func ResizeVolume(volumeID string, size int64) error {
return fmt.Errorf("error getting the current size of volume (%s) with error (%v)", volumeID, err)
}

//if the partition's size is already the size we want this is a noop, just return
if currentSize >= finalSize {
klog.V(2).Infof("Attempted to resize volume %s to a lower size, from currentBytes=%d wantedBytes=%d", volumeID, currentSize, finalSize)
return nil
}

cmd = fmt.Sprintf("Get-Volume -UniqueId \"$Env:volumeID\" | Get-Partition | Resize-Partition -Size %d", finalSize)
out, err = azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))
cmd := fmt.Sprintf("Get-Volume -UniqueId \"$Env:volumeID\" | Get-Partition | Resize-Partition -Size %d", finalSize)
out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))
if err != nil {
return fmt.Errorf("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v", cmd, string(out), size, finalSize, err)
}
Expand Down

0 comments on commit 6d7d669

Please sign in to comment.