@@ -21,9 +21,9 @@ package mount
21
21
22
22
import (
23
23
"fmt"
24
- "strconv"
25
24
"strings"
26
25
26
+ "k8s.io/apimachinery/pkg/util/sets"
27
27
"k8s.io/klog/v2"
28
28
utilexec "k8s.io/utils/exec"
29
29
)
@@ -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,45 +129,13 @@ 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
- 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 )
145
- case "btrfs" :
146
- blockSize , fsSize , err = resizefs .getBtrfsSize (devicePath )
147
- klog .V (5 ).Infof ("Btrfs size: filesystem size=%d, block size=%d, err=%v" , fsSize , blockSize , err )
148
- 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 )
151
- }
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
132
+ supportedFormats := sets .New ("ext3" , "ext4" , "xfs" , "btrfs" )
133
+ if ! supportedFormats .Has (format ) {
134
+ return false , fmt .Errorf ("could not parse fs info of given filesystem format: %s. Supported fs types are: xfs, ext3, ext4" , format )
159
135
}
160
136
return true , nil
161
137
}
162
138
163
- func (resizefs * ResizeFs ) getDeviceSize (devicePath string ) (uint64 , error ) {
164
- output , err := resizefs .exec .Command (blockDev , "--getsize64" , devicePath ).CombinedOutput ()
165
- outStr := strings .TrimSpace (string (output ))
166
- if err != nil {
167
- return 0 , fmt .Errorf ("failed to read size of device %s: %s: %s" , devicePath , err , outStr )
168
- }
169
- size , err := strconv .ParseUint (outStr , 10 , 64 )
170
- if err != nil {
171
- return 0 , fmt .Errorf ("failed to parse size of device %s %s: %s" , devicePath , outStr , err )
172
- }
173
- return size , nil
174
- }
175
-
176
139
func (resizefs * ResizeFs ) getDeviceRO (devicePath string ) (bool , error ) {
177
140
output , err := resizefs .exec .Command (blockDev , "--getro" , devicePath ).CombinedOutput ()
178
141
outStr := strings .TrimSpace (string (output ))
@@ -185,112 +148,6 @@ func (resizefs *ResizeFs) getDeviceRO(devicePath string) (bool, error) {
185
148
case "1" :
186
149
return true , nil
187
150
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
- func (resizefs * ResizeFs ) getBtrfsSize (devicePath string ) (uint64 , uint64 , error ) {
227
- output , err := resizefs .exec .Command ("btrfs" , "inspect-internal" , "dump-super" , "-f" , devicePath ).CombinedOutput ()
228
- if err != nil {
229
- return 0 , 0 , fmt .Errorf ("failed to read size of filesystem on %s: %s: %s" , devicePath , err , string (output ))
230
- }
231
-
232
- blockSize , totalBytes , _ := resizefs .parseBtrfsInfoOutput (string (output ), "sectorsize" , "total_bytes" )
233
-
234
- if blockSize == 0 {
235
- return 0 , 0 , fmt .Errorf ("could not find block size of device %s" , devicePath )
236
- }
237
- if totalBytes == 0 {
238
- return 0 , 0 , fmt .Errorf ("could not find total size of device %s" , devicePath )
239
- }
240
- return blockSize , totalBytes , nil
241
- }
242
-
243
- func (resizefs * ResizeFs ) parseBtrfsInfoOutput (cmdOutput string , blockSizeKey string , totalBytesKey string ) (uint64 , uint64 , error ) {
244
- lines := strings .Split (cmdOutput , "\n " )
245
- var blockSize , blockCount uint64
246
- var err error
247
-
248
- for _ , line := range lines {
249
- tokens := strings .Fields (line )
250
- if len (tokens ) != 2 {
251
- continue
252
- }
253
- key , value := strings .ToLower (strings .TrimSpace (tokens [0 ])), strings .ToLower (strings .TrimSpace (tokens [1 ]))
254
-
255
- if key == blockSizeKey {
256
- blockSize , err = strconv .ParseUint (value , 10 , 64 )
257
- if err != nil {
258
- return 0 , 0 , fmt .Errorf ("failed to parse block size %s: %s" , value , err )
259
- }
260
- }
261
- if key == totalBytesKey {
262
- blockCount , err = strconv .ParseUint (value , 10 , 64 )
263
- if err != nil {
264
- return 0 , 0 , fmt .Errorf ("failed to parse total size %s: %s" , value , err )
265
- }
266
- }
267
- }
268
- return blockSize , blockCount , err
269
- }
270
-
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
- }
151
+ return false , fmt .Errorf ("failed readonly device check. Expected 1 or 0, got '%s'" , outStr )
294
152
}
295
- return blockSize , blockCount , err
296
153
}
0 commit comments