@@ -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 )
@@ -87,6 +104,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
87
104
var out []byte
88
105
var err error
89
106
var finalSize int64
107
+ var outString string
90
108
if size == 0 {
91
109
cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" , volumeID )
92
110
out , err = runExec (cmd )
@@ -96,7 +114,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
96
114
}
97
115
98
116
var getVolumeSizing map [string ]int64
99
- outString : = string (out )
117
+ outString = string (out )
100
118
err = json .Unmarshal ([]byte (outString ), & getVolumeSizing )
101
119
if err != nil {
102
120
return fmt .Errorf ("out %v outstring %v err %v" , out , outString , err )
@@ -109,6 +127,16 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
109
127
finalSize = size
110
128
}
111
129
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
+
112
140
cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Resize-Partition -Size %d" , volumeID , finalSize )
113
141
out , err = runExec (cmd )
114
142
if err != nil {
0 commit comments