Skip to content

Commit febd972

Browse files
committed
use mount-utils for resizing volumes
1 parent 8048b98 commit febd972

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

Diff for: pkg/gce-pd-csi-driver/node.go

+9
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,15 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
340340
devicePath, stagingTargetPath, fstype, options, err))
341341
}
342342

343+
// Part 4: Resize filesystem.
344+
// https://github.com/kubernetes/kubernetes/issues/94929
345+
resizer := resizefs.NewResizeFs(ns.Mounter)
346+
_, err = resizer.Resize(devicePath, stagingTargetPath)
347+
if err != nil {
348+
return nil, status.Error(codes.Internal, fmt.Sprintf("error when resizing volume %s: %v", volumeID, err))
349+
350+
}
351+
343352
klog.V(4).Infof("NodeStageVolume succeeded on %v to %s", volumeID, stagingTargetPath)
344353
return &csi.NodeStageVolumeResponse{}, nil
345354
}

Diff for: pkg/resizefs/resizefs_linux.go

+15-43
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ limitations under the License.
1919
package resizefs
2020

2121
import (
22-
"fmt"
22+
"google.golang.org/grpc/codes"
23+
"google.golang.org/grpc/status"
2324

2425
"k8s.io/klog"
2526
"k8s.io/mount-utils"
@@ -38,51 +39,22 @@ func NewResizeFs(mounter *mount.SafeFormatAndMount) *resizeFs {
3839
}
3940

4041
// Resize perform resize of file system
41-
func (resizefs *resizeFs) Resize(devicePath, deviceMountPath string) (bool, error) {
42-
format, err := resizefs.mounter.GetDiskFormat(devicePath)
42+
func (resizefs *resizeFs) Resize(devicePath, deviceMountPath string) (needResize bool, err error) {
43+
resizer := mount.NewResizeFs(resizefs.mounter.Exec)
4344

44-
if err != nil {
45-
formatErr := fmt.Errorf("ResizeFS.Resize - error checking format for device %s: %v", devicePath, err)
46-
return false, formatErr
45+
klog.V(4).Infof("Checking if filesystem needs to be resized. Device: %s Mountpoint: %s", devicePath, deviceMountPath)
46+
if needResize, err = resizer.NeedResize(devicePath, deviceMountPath); err != nil {
47+
err = status.Errorf(codes.Internal, "Could not determine if filesystem %q needs to be resized: %v", deviceMountPath, err)
48+
return
4749
}
4850

49-
// If disk has no format, there is no need to resize the disk because mkfs.*
50-
// by default will use whole disk anyways.
51-
if format == "" {
52-
return false, nil
51+
if needResize {
52+
klog.V(4).Infof("Resizing filesystem. Device: %s Mountpoint: %s", devicePath, deviceMountPath)
53+
if _, err = resizer.Resize(devicePath, deviceMountPath); err != nil {
54+
err = status.Errorf(codes.Internal, "Failed to resize filesystem %q: %v", deviceMountPath, err)
55+
return
56+
}
5357
}
5458

55-
klog.V(3).Infof("ResizeFS.Resize - Expanding mounted volume %s", devicePath)
56-
switch format {
57-
case "ext3", "ext4":
58-
return resizefs.extResize(devicePath)
59-
case "xfs":
60-
return resizefs.xfsResize(deviceMountPath)
61-
}
62-
return false, fmt.Errorf("ResizeFS.Resize - resize of format %s is not supported for device %s mounted at %s", format, devicePath, deviceMountPath)
63-
}
64-
65-
func (resizefs *resizeFs) extResize(devicePath string) (bool, error) {
66-
output, err := resizefs.mounter.Exec.Command("resize2fs", devicePath).CombinedOutput()
67-
if err == nil {
68-
klog.V(2).Infof("Device %s resized successfully", devicePath)
69-
return true, nil
70-
}
71-
72-
resizeError := fmt.Errorf("resize of device %s failed: %v. resize2fs output: %s", devicePath, err, string(output))
73-
return false, resizeError
74-
75-
}
76-
77-
func (resizefs *resizeFs) xfsResize(deviceMountPath string) (bool, error) {
78-
args := []string{"-d", deviceMountPath}
79-
output, err := resizefs.mounter.Exec.Command("xfs_growfs", args...).CombinedOutput()
80-
81-
if err == nil {
82-
klog.V(2).Infof("Device %s resized successfully", deviceMountPath)
83-
return true, nil
84-
}
85-
86-
resizeError := fmt.Errorf("resize of device %s failed: %v. xfs_growfs output: %s", deviceMountPath, err, string(output))
87-
return false, resizeError
59+
return
8860
}

0 commit comments

Comments
 (0)