@@ -24,6 +24,23 @@ func runExec(cmd string) ([]byte, error) {
24
24
return out , err
25
25
}
26
26
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
+
27
44
// ListVolumesOnDisk - returns back list of volumes(volumeIDs) in the disk (requested in diskID).
28
45
func (VolAPIImplementor ) ListVolumesOnDisk (diskID string ) (volumeIDs []string , err error ) {
29
46
cmd := fmt .Sprintf ("(Get-Disk -DeviceId %s |Get-Partition | Get-Volume).UniqueId" , diskID )
@@ -95,6 +112,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
95
112
var out []byte
96
113
var err error
97
114
var finalSize int64
115
+ var outString string
98
116
if size == 0 {
99
117
cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" , volumeID )
100
118
out , err = runExec (cmd )
@@ -104,7 +122,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
104
122
}
105
123
106
124
var getVolumeSizing map [string ]int64
107
- outString : = string (out )
125
+ outString = string (out )
108
126
err = json .Unmarshal ([]byte (outString ), & getVolumeSizing )
109
127
if err != nil {
110
128
return fmt .Errorf ("out %v outstring %v err %v" , out , outString , err )
@@ -117,6 +135,16 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
117
135
finalSize = size
118
136
}
119
137
138
+ currentSize , err := getVolumeSize (volumeID )
139
+ if err != nil {
140
+ return fmt .Errorf ("error getting the current size of volume (%s) with error (%v)" , volumeID , err )
141
+ }
142
+
143
+ //if the partition's size is already the size we want this is a noop, just return
144
+ if currentSize >= finalSize {
145
+ return nil
146
+ }
147
+
120
148
cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Resize-Partition -Size %d" , volumeID , finalSize )
121
149
out , err = runExec (cmd )
122
150
if err != nil {
0 commit comments