@@ -80,47 +80,44 @@ func (VolAPIImplementor) DismountVolume(volumeID, path string) error {
8080 return nil
8181}
8282
83- // ResizeVolume - resize the volume to the size specified as parameter.
83+ // ResizeVolume - resizes a volume with the given size, if size == 0 then max supported size is used
8484func (VolAPIImplementor ) ResizeVolume (volumeID string , size int64 ) error {
85- // TODO: Check the size of the resize
86- // TODO: We have to get the right partition.
87- cmd := fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMin,SizeMax | ConvertTo-Json" , volumeID )
88- out , err := runExec (cmd )
89-
90- if err != nil || len (out ) == 0 {
91- return fmt .Errorf ("error getting sizemin,sizemax from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
92- }
93-
94- var getVolumeSizing map [string ]int64
95- outString := string (out )
96- err = json .Unmarshal ([]byte (outString ), & getVolumeSizing )
97- if err != nil {
98- return fmt .Errorf ("out %v outstring %v err %v" , out , outString , err )
99- }
100-
101- sizeMin := getVolumeSizing ["SizeMin" ]
102- sizeMax := getVolumeSizing ["SizeMax" ]
103-
104- //if the size is too small then fail here
105- if size < sizeMin {
106- return fmt .Errorf ("size %v is below the minimum %v allowed for the volume" , size , sizeMin )
107- }
108-
109- //if the size is greater than sizeMax but within 10% it might be due to overhead, change size to sizeMax
110- //otherwise let size go through, it will fail the resize operation
111- if (size > sizeMax ) && (float64 (size ) < float64 (float64 (sizeMax )* float64 (1.1 ))) {
112- size = sizeMax
113- }
114-
115- cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Resize-Partition -Size %d" , volumeID , size )
85+ // If size is 0 then we will resize to the maximum size possible, otherwise just resize to size
86+ var cmd string
87+ var out []byte
88+ var err error
89+ var finalSize int64
90+ if size == 0 {
91+ cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" , volumeID )
92+ out , err = runExec (cmd )
93+
94+ if err != nil || len (out ) == 0 {
95+ return fmt .Errorf ("error getting sizemin,sizemax from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
96+ }
97+
98+ var getVolumeSizing map [string ]int64
99+ outString := string (out )
100+ err = json .Unmarshal ([]byte (outString ), & getVolumeSizing )
101+ if err != nil {
102+ return fmt .Errorf ("out %v outstring %v err %v" , out , outString , err )
103+ }
104+
105+ sizeMax := getVolumeSizing ["SizeMax" ]
106+
107+ finalSize = sizeMax
108+ } else {
109+ finalSize = size
110+ }
111+
112+ cmd = fmt .Sprintf ("Get-Volume -UniqueId \" %s\" | Get-partition | Resize-Partition -Size %d" , volumeID , finalSize )
116113 out , err = runExec (cmd )
117114 if err != nil {
118- return fmt .Errorf ("error resizing volume. cmd: %s, output: %s size:%v, sizeMax %v, error: %v" , cmd , string (out ), size , sizeMax , err )
115+ return fmt .Errorf ("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v" , cmd , string (out ), size , finalSize , err )
119116 }
120117 return nil
121118}
122119
123- // VolumeStats - resize the volume to the size specified as parameter.
120+ // VolumeStats - retrieves the volume stats for a given volume
124121func (VolAPIImplementor ) VolumeStats (volumeID string ) (int64 , int64 , error ) {
125122 // get the size and sizeRemaining for the volume
126123 cmd := fmt .Sprintf ("(Get-Volume -UniqueId \" %s\" | Select SizeRemaining,Size) | ConvertTo-Json" , volumeID )
@@ -171,9 +168,18 @@ func (VolAPIImplementor) GetVolumeDiskNumber(volumeID string) (int64, error) {
171168 return diskNumber , nil
172169}
173170
174- // GetVolumeIDFromMount - gets the volume ID given a mount point
171+ // GetVolumeIDFromMount - gets the volume ID given a mount point, the function is recursive until it find a volume or errors out
175172func (VolAPIImplementor ) GetVolumeIDFromMount (mount string ) (string , error ) {
176- // get the size and sizeRemaining for the volume
173+ volumeString , err := getTarget (mount )
174+
175+ if err != nil {
176+ return "" , fmt .Errorf ("error getting the volume for the mount %s, internal error %v" , mount , err )
177+ }
178+
179+ return volumeString , nil
180+ }
181+
182+ func getTarget (mount string ) (string , error ) {
177183 cmd := fmt .Sprintf ("Get-Item -LiteralPath \" %s\" | Select Target | ConvertTo-Json" , mount )
178184 out , err := runExec (cmd )
179185
@@ -194,7 +200,7 @@ func (VolAPIImplementor) GetVolumeIDFromMount(mount string) (string, error) {
194200 volumeString = strings .TrimSuffix (volumeString , "\n " )
195201
196202 if ! strings .HasPrefix (volumeString , "Volume" ) {
197- return "" , fmt . Errorf ( "error getting the volume for the mount %s, received (%s), extracted %s" , mount , outString , volumeString )
203+ return getTarget ( volumeString )
198204 }
199205
200206 volumeString = "\\ \\ ?\\ " + volumeString
0 commit comments