Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.31] cleanup: refine Windows Resizevolume func #2731

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading