@@ -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).
2845func (VolAPIImplementor ) ListVolumesOnDisk (diskID string ) (volumeIDs []string , err error ) {
2946 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 {
95112 var out []byte
96113 var err error
97114 var finalSize int64
115+ var outString string
98116 if size == 0 {
99117 cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" , volumeID )
100118 out , err = runExec (cmd )
@@ -104,7 +122,7 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
104122 }
105123
106124 var getVolumeSizing map [string ]int64
107- outString : = string (out )
125+ outString = string (out )
108126 err = json .Unmarshal ([]byte (outString ), & getVolumeSizing )
109127 if err != nil {
110128 return fmt .Errorf ("out %v outstring %v err %v" , out , outString , err )
@@ -117,6 +135,16 @@ func (VolAPIImplementor) ResizeVolume(volumeID string, size int64) error {
117135 finalSize = size
118136 }
119137
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+
120148 cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Resize-Partition -Size %d" , volumeID , finalSize )
121149 out , err = runExec (cmd )
122150 if err != nil {
0 commit comments