Skip to content

Commit 09283a5

Browse files
committed
noop when resize is called and size of partition is >= requested size
1 parent 46d1981 commit 09283a5

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

internal/os/volume/api.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ func runExec(cmd string) ([]byte, error) {
2424
return out, err
2525
}
2626

27+
func getVolumeSize(volumeID string) (int64, error) {
28+
cmd := fmt.Sprintf("(Get-Volume -UniqueId \"%s\" | Get-partition).Size", volumeID)
29+
out, err := runExec(cmd)
30+
31+
if err != nil || len(out) == 0 {
32+
return -1, fmt.Errorf("error getting size of the partition from mount. cmd %s, output: %s, error: %v", cmd, string(out), err)
33+
}
34+
35+
outString := strings.TrimSpace(string(out))
36+
volumeSize, err := strconv.ParseInt(outString, 10, 64)
37+
if err != nil {
38+
return -1, fmt.Errorf("error parsing size of volume %s received %v trimmed to %v err %v", volumeID, out, outString, err)
39+
}
40+
41+
return volumeSize, nil
42+
}
43+
2744
// ListVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID).
2845
func (VolAPIImplementor) ListVolumesOnDisk(diskID string) (volumeIDs []string, err error) {
2946
cmd := fmt.Sprintf("(Get-Disk -DeviceId %s |Get-Partition | Get-Volume).UniqueId", diskID)
@@ -87,6 +104,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
87104
var out []byte
88105
var err error
89106
var finalSize int64
107+
var outString string
90108
if size == 0 {
91109
cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json", volumeID)
92110
out, err = runExec(cmd)
@@ -96,7 +114,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
96114
}
97115

98116
var getVolumeSizing map[string]int64
99-
outString := string(out)
117+
outString = string(out)
100118
err = json.Unmarshal([]byte(outString), &getVolumeSizing)
101119
if err != nil {
102120
return fmt.Errorf("out %v outstring %v err %v", out, outString, err)
@@ -109,6 +127,16 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
109127
finalSize = size
110128
}
111129

130+
currentSize, err := getVolumeSize(volumeID)
131+
if err != nil {
132+
return fmt.Errorf("error getting the current size of volume (%s) with error (%v)", volumeID, err)
133+
}
134+
135+
//if the partition's size is already the size we want this is a noop, just return
136+
if currentSize >= finalSize {
137+
return nil
138+
}
139+
112140
cmd = fmt.Sprintf("Get-Volume -UniqueId \"%s\" | Get-partition | Resize-Partition -Size %d", volumeID, finalSize)
113141
out, err = runExec(cmd)
114142
if err != nil {

0 commit comments

Comments
 (0)