@@ -117,11 +117,6 @@ func (resizefs *ResizeFs) NeedResize(devicePath string, deviceMountPath string)
117
117
return false , nil
118
118
}
119
119
120
- deviceSize , err := resizefs .getDeviceSize (devicePath )
121
- if err != nil {
122
- return false , err
123
- }
124
- var fsSize , blockSize uint64
125
120
format , err := getDiskFormat (resizefs .exec , devicePath )
126
121
if err != nil {
127
122
formatErr := fmt .Errorf ("ResizeFS.Resize - error checking format for device %s: %v" , devicePath , err )
@@ -134,30 +129,28 @@ func (resizefs *ResizeFs) NeedResize(devicePath string, deviceMountPath string)
134
129
return false , nil
135
130
}
136
131
137
- klog .V (3 ).Infof ("ResizeFs.needResize - checking mounted volume %s" , devicePath )
138
132
switch format {
139
- case "ext3" , "ext4" :
140
- blockSize , fsSize , err = resizefs .getExtSize (devicePath )
141
- klog .V (5 ).Infof ("Ext size: filesystem size=%d, block size=%d" , fsSize , blockSize )
142
- case "xfs" :
143
- blockSize , fsSize , err = resizefs .getXFSSize (deviceMountPath )
144
- klog .V (5 ).Infof ("Xfs size: filesystem size=%d, block size=%d, err=%v" , fsSize , blockSize , err )
133
+ case "ext3" , "ext4" , "xfs" :
134
+ // For ext3/ext4/xfs, recommendation received from linux filesystem folks is to let
135
+ // resize2fs/xfs_growfs do the check for us. So we will not do any check here.
136
+ return true , nil
145
137
case "btrfs" :
146
- blockSize , fsSize , err = resizefs .getBtrfsSize (devicePath )
138
+ deviceSize , err := resizefs .getDeviceSize (devicePath )
139
+ if err != nil {
140
+ return false , err
141
+ }
142
+ blockSize , fsSize , err := resizefs .getBtrfsSize (devicePath )
147
143
klog .V (5 ).Infof ("Btrfs size: filesystem size=%d, block size=%d, err=%v" , fsSize , blockSize , err )
144
+ if err != nil {
145
+ return false , err
146
+ }
147
+ if deviceSize <= fsSize + blockSize {
148
+ return false , nil
149
+ }
150
+ return true , nil
148
151
default :
149
- klog .Errorf ("Not able to parse given filesystem info. fsType: %s, will not resize" , format )
150
- return false , fmt .Errorf ("Could not parse fs info on given filesystem format: %s. Supported fs types are: xfs, ext3, ext4" , format )
152
+ return false , fmt .Errorf ("could not parse fs info of given filesystem format: %s. Supported fs types are: xfs, ext3, ext4" , format )
151
153
}
152
- if err != nil {
153
- return false , err
154
- }
155
- // Tolerate one block difference, just in case of rounding errors somewhere.
156
- klog .V (5 ).Infof ("Volume %s: device size=%d, filesystem size=%d, block size=%d" , devicePath , deviceSize , fsSize , blockSize )
157
- if deviceSize <= fsSize + blockSize {
158
- return false , nil
159
- }
160
- return true , nil
161
154
}
162
155
163
156
func (resizefs * ResizeFs ) getDeviceSize (devicePath string ) (uint64 , error ) {
@@ -173,56 +166,6 @@ func (resizefs *ResizeFs) getDeviceSize(devicePath string) (uint64, error) {
173
166
return size , nil
174
167
}
175
168
176
- func (resizefs * ResizeFs ) getDeviceRO (devicePath string ) (bool , error ) {
177
- output , err := resizefs .exec .Command (blockDev , "--getro" , devicePath ).CombinedOutput ()
178
- outStr := strings .TrimSpace (string (output ))
179
- if err != nil {
180
- return false , fmt .Errorf ("failed to get readonly bit from device %s: %s: %s" , devicePath , err , outStr )
181
- }
182
- switch outStr {
183
- case "0" :
184
- return false , nil
185
- case "1" :
186
- return true , nil
187
- default :
188
- return false , fmt .Errorf ("Failed readonly device check. Expected 1 or 0, got '%s'" , outStr )
189
- }
190
- }
191
-
192
- func (resizefs * ResizeFs ) getExtSize (devicePath string ) (uint64 , uint64 , error ) {
193
- output , err := resizefs .exec .Command ("dumpe2fs" , "-h" , devicePath ).CombinedOutput ()
194
- if err != nil {
195
- return 0 , 0 , fmt .Errorf ("failed to read size of filesystem on %s: %s: %s" , devicePath , err , string (output ))
196
- }
197
-
198
- blockSize , blockCount , _ := resizefs .parseFsInfoOutput (string (output ), ":" , "block size" , "block count" )
199
-
200
- if blockSize == 0 {
201
- return 0 , 0 , fmt .Errorf ("could not find block size of device %s" , devicePath )
202
- }
203
- if blockCount == 0 {
204
- return 0 , 0 , fmt .Errorf ("could not find block count of device %s" , devicePath )
205
- }
206
- return blockSize , blockSize * blockCount , nil
207
- }
208
-
209
- func (resizefs * ResizeFs ) getXFSSize (devicePath string ) (uint64 , uint64 , error ) {
210
- output , err := resizefs .exec .Command ("xfs_io" , "-c" , "statfs" , devicePath ).CombinedOutput ()
211
- if err != nil {
212
- return 0 , 0 , fmt .Errorf ("failed to read size of filesystem on %s: %s: %s" , devicePath , err , string (output ))
213
- }
214
-
215
- blockSize , blockCount , _ := resizefs .parseFsInfoOutput (string (output ), "=" , "geom.bsize" , "geom.datablocks" )
216
-
217
- if blockSize == 0 {
218
- return 0 , 0 , fmt .Errorf ("could not find block size of device %s" , devicePath )
219
- }
220
- if blockCount == 0 {
221
- return 0 , 0 , fmt .Errorf ("could not find block count of device %s" , devicePath )
222
- }
223
- return blockSize , blockSize * blockCount , nil
224
- }
225
-
226
169
func (resizefs * ResizeFs ) getBtrfsSize (devicePath string ) (uint64 , uint64 , error ) {
227
170
output , err := resizefs .exec .Command ("btrfs" , "inspect-internal" , "dump-super" , "-f" , devicePath ).CombinedOutput ()
228
171
if err != nil {
@@ -268,29 +211,18 @@ func (resizefs *ResizeFs) parseBtrfsInfoOutput(cmdOutput string, blockSizeKey st
268
211
return blockSize , blockCount , err
269
212
}
270
213
271
- func (resizefs * ResizeFs ) parseFsInfoOutput (cmdOutput string , spliter string , blockSizeKey string , blockCountKey string ) (uint64 , uint64 , error ) {
272
- lines := strings .Split (cmdOutput , "\n " )
273
- var blockSize , blockCount uint64
274
- var err error
275
-
276
- for _ , line := range lines {
277
- tokens := strings .Split (line , spliter )
278
- if len (tokens ) != 2 {
279
- continue
280
- }
281
- key , value := strings .ToLower (strings .TrimSpace (tokens [0 ])), strings .ToLower (strings .TrimSpace (tokens [1 ]))
282
- if key == blockSizeKey {
283
- blockSize , err = strconv .ParseUint (value , 10 , 64 )
284
- if err != nil {
285
- return 0 , 0 , fmt .Errorf ("failed to parse block size %s: %s" , value , err )
286
- }
287
- }
288
- if key == blockCountKey {
289
- blockCount , err = strconv .ParseUint (value , 10 , 64 )
290
- if err != nil {
291
- return 0 , 0 , fmt .Errorf ("failed to parse block count %s: %s" , value , err )
292
- }
293
- }
214
+ func (resizefs * ResizeFs ) getDeviceRO (devicePath string ) (bool , error ) {
215
+ output , err := resizefs .exec .Command (blockDev , "--getro" , devicePath ).CombinedOutput ()
216
+ outStr := strings .TrimSpace (string (output ))
217
+ if err != nil {
218
+ return false , fmt .Errorf ("failed to get readonly bit from device %s: %w: %s" , devicePath , err , outStr )
219
+ }
220
+ switch outStr {
221
+ case "0" :
222
+ return false , nil
223
+ case "1" :
224
+ return true , nil
225
+ default :
226
+ return false , fmt .Errorf ("failed readonly device check. Expected 1 or 0, got '%s'" , outStr )
294
227
}
295
- return blockSize , blockCount , err
296
228
}
0 commit comments