Skip to content

Commit c5ae6f5

Browse files
Merge pull request kubernetes-sigs#32 from RomanBednar/cherry-pr-972
Bug 1877261: UPSTREAM: 973: filesystem is not resized when restoring
2 parents f0879a6 + f902ca7 commit c5ae6f5

File tree

4 files changed

+92
-51
lines changed

4 files changed

+92
-51
lines changed

pkg/gce-pd-csi-driver/node.go

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

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

pkg/gce-pd-csi-driver/node_test.go

+67-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ package gceGCEDriver
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"io/ioutil"
22+
"k8s.io/utils/exec"
23+
testingexec "k8s.io/utils/exec/testing"
2124
"os"
2225
"path/filepath"
2326
"testing"
@@ -63,6 +66,15 @@ func getTestBlockingGCEDriver(t *testing.T, readyToExecute chan chan struct{}) *
6366
return gceDriver
6467
}
6568

69+
func makeFakeCmd(fakeCmd *testingexec.FakeCmd, cmd string, args ...string) testingexec.FakeCommandAction {
70+
c := cmd
71+
a := args
72+
return func(cmd string, args ...string) exec.Cmd {
73+
command := testingexec.InitFakeCmd(fakeCmd, c, a...)
74+
return command
75+
}
76+
}
77+
6678
func TestNodeGetVolumeStats(t *testing.T) {
6779
gceDriver := getTestGCEDriver(t)
6880
ns := gceDriver.ns
@@ -351,8 +363,6 @@ func TestNodeUnpublishVolume(t *testing.T) {
351363
}
352364

353365
func TestNodeStageVolume(t *testing.T) {
354-
gceDriver := getTestGCEDriver(t)
355-
ns := gceDriver.ns
356366
volumeID := "project/test001/zones/c1/disks/testDisk"
357367
blockCap := &csi.VolumeCapability_Block{
358368
Block: &csi.VolumeCapability_BlockVolume{},
@@ -436,6 +446,61 @@ func TestNodeStageVolume(t *testing.T) {
436446
}
437447
for _, tc := range testCases {
438448
t.Logf("Test case: %s", tc.name)
449+
actionList := []testingexec.FakeCommandAction{
450+
makeFakeCmd(
451+
&testingexec.FakeCmd{
452+
CombinedOutputScript: []testingexec.FakeAction{
453+
func() ([]byte, []byte, error) {
454+
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
455+
},
456+
},
457+
},
458+
"blkid",
459+
),
460+
makeFakeCmd(
461+
&testingexec.FakeCmd{
462+
CombinedOutputScript: []testingexec.FakeAction{
463+
func() ([]byte, []byte, error) {
464+
return []byte("1"), nil, nil
465+
},
466+
},
467+
},
468+
"blockdev",
469+
),
470+
makeFakeCmd(
471+
&testingexec.FakeCmd{
472+
CombinedOutputScript: []testingexec.FakeAction{
473+
func() ([]byte, []byte, error) {
474+
return []byte("1"), nil, nil
475+
},
476+
},
477+
},
478+
"blockdev",
479+
),
480+
makeFakeCmd(
481+
&testingexec.FakeCmd{
482+
CombinedOutputScript: []testingexec.FakeAction{
483+
func() ([]byte, []byte, error) {
484+
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
485+
},
486+
},
487+
},
488+
"blkid",
489+
),
490+
makeFakeCmd(
491+
&testingexec.FakeCmd{
492+
CombinedOutputScript: []testingexec.FakeAction{
493+
func() ([]byte, []byte, error) {
494+
return []byte(fmt.Sprintf("block size: 1\nblock count: 1")), nil, nil
495+
},
496+
},
497+
},
498+
"dumpe2fs",
499+
),
500+
}
501+
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList})
502+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
503+
ns := gceDriver.ns
439504
_, err := ns.NodeStageVolume(context.Background(), tc.req)
440505
if err != nil {
441506
serverError, ok := status.FromError(err)

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/v2"
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
}

test/e2e/tests/resize_e2e_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,12 @@ var _ = Describe("GCE PD CSI Driver", func() {
232232
}
233233
}()
234234

235-
// Verify pre-resize fs size
236-
sizeGb, err := testutils.GetFSSizeInGb(instance, publishDir)
237-
Expect(err).To(BeNil(), "Failed to get FSSize in GB")
238-
Expect(sizeGb).To(Equal(defaultSizeGb))
239-
240235
// Resize node
241236
_, err = client.NodeExpandVolume(volID, publishDir, newSizeGb)
242237
Expect(err).To(BeNil(), "Node expand volume failed")
243238

244239
// Verify disk size
245-
sizeGb, err = testutils.GetFSSizeInGb(instance, publishDir)
240+
sizeGb, err := testutils.GetFSSizeInGb(instance, publishDir)
246241
Expect(err).To(BeNil(), "Failed to get FSSize in GB")
247242
Expect(sizeGb).To(Equal(newSizeGb))
248243

0 commit comments

Comments
 (0)