From 28c1105d6b7170acb67f1894fd7951a4ab9f0292 Mon Sep 17 00:00:00 2001 From: Aleksandr Rigachnyi Date: Fri, 14 Jun 2024 17:58:20 +0200 Subject: [PATCH] NBSNEBIUS-346: don't recreate filesystem while pvc mount (#1413) --- .../tools/csi_driver/internal/driver/node.go | 6 ++-- .../csi_driver/internal/driver/node_test.go | 2 +- .../csi_driver/internal/mounter/iface.go | 2 +- .../tools/csi_driver/internal/mounter/mock.go | 4 +-- .../csi_driver/internal/mounter/mounter.go | 31 ++++++++++++++----- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cloud/blockstore/tools/csi_driver/internal/driver/node.go b/cloud/blockstore/tools/csi_driver/internal/driver/node.go index 5e87e84a20e..d51c7daf228 100644 --- a/cloud/blockstore/tools/csi_driver/internal/driver/node.go +++ b/cloud/blockstore/tools/csi_driver/internal/driver/node.go @@ -576,12 +576,12 @@ func (s *nodeService) makeFilesystemIfNeeded( } logVolume(volumeId, "making filesystem %q on device %q", fsType, deviceName) - err = s.mounter.MakeFilesystem(deviceName, fsType) + out, err := s.mounter.MakeFilesystem(deviceName, fsType) if err != nil { - return err + return fmt.Errorf("failed to make filesystem: %w, output %q", err, out) } - logVolume(volumeId, "succeeded making filesystem") + logVolume(volumeId, "succeeded making filesystem: %q", out) return nil } diff --git a/cloud/blockstore/tools/csi_driver/internal/driver/node_test.go b/cloud/blockstore/tools/csi_driver/internal/driver/node_test.go index b95a0862c87..87a7b5beedc 100644 --- a/cloud/blockstore/tools/csi_driver/internal/driver/node_test.go +++ b/cloud/blockstore/tools/csi_driver/internal/driver/node_test.go @@ -236,7 +236,7 @@ func TestPublishUnpublishDiskForInfrakuber(t *testing.T) { mounter.On("IsFilesystemExisted", nbdDeviceFile).Return(false, nil) - mounter.On("MakeFilesystem", nbdDeviceFile, "ext4").Return(nil) + mounter.On("MakeFilesystem", nbdDeviceFile, "ext4").Return([]byte{}, nil) mounter.On("IsMountPoint", targetPath).Return(false, nil) diff --git a/cloud/blockstore/tools/csi_driver/internal/mounter/iface.go b/cloud/blockstore/tools/csi_driver/internal/mounter/iface.go index 00b03789d24..ba767ee3753 100644 --- a/cloud/blockstore/tools/csi_driver/internal/mounter/iface.go +++ b/cloud/blockstore/tools/csi_driver/internal/mounter/iface.go @@ -8,5 +8,5 @@ type Interface interface { CleanupMountPoint(target string) error IsFilesystemExisted(device string) (bool, error) - MakeFilesystem(device string, fsType string) error + MakeFilesystem(device string, fsType string) ([]byte, error) } diff --git a/cloud/blockstore/tools/csi_driver/internal/mounter/mock.go b/cloud/blockstore/tools/csi_driver/internal/mounter/mock.go index e7ff1d92b04..3bb4e1440cc 100644 --- a/cloud/blockstore/tools/csi_driver/internal/mounter/mock.go +++ b/cloud/blockstore/tools/csi_driver/internal/mounter/mock.go @@ -35,9 +35,9 @@ func (c *Mock) IsFilesystemExisted(device string) (bool, error) { return args.Get(0).(bool), args.Error(1) } -func (c *Mock) MakeFilesystem(device string, fsType string) error { +func (c *Mock) MakeFilesystem(device string, fsType string) ([]byte, error) { args := c.Called(device, fsType) - return args.Error(0) + return args.Get(0).([]byte), args.Error(1) } //////////////////////////////////////////////////////////////////////////////// diff --git a/cloud/blockstore/tools/csi_driver/internal/mounter/mounter.go b/cloud/blockstore/tools/csi_driver/internal/mounter/mounter.go index 2623780ca1d..bb62f43702a 100644 --- a/cloud/blockstore/tools/csi_driver/internal/mounter/mounter.go +++ b/cloud/blockstore/tools/csi_driver/internal/mounter/mounter.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "os/exec" + "strconv" + "strings" "k8s.io/mount-utils" ) @@ -37,15 +39,33 @@ func (m *mounter) IsFilesystemExisted(device string) (bool, error) { return false, fmt.Errorf("failed to find 'blkid' tool: %w", err) } + if _, err := exec.LookPath("blockdev"); err != nil { + return false, fmt.Errorf("failed to find 'blockdev' tool: %w", err) + } + if _, err := os.Stat(device); os.IsNotExist(err) { return false, fmt.Errorf("failed to find device %q: %w", device, err) } - out, err := exec.Command("blkid", device).CombinedOutput() + out, err := exec.Command("blockdev", "--getsize64", device).CombinedOutput() + if err != nil { + return false, fmt.Errorf("failed to get size of device %q: %w", device, err) + } + + deviceSize, err := strconv.ParseUint(strings.TrimSpace(string(out)), 10, 64) + if err != nil { + return false, fmt.Errorf("failed to convert %q to number: %w", out, err) + } + + if deviceSize == 0 { + return false, fmt.Errorf("size of device %q is empty", device) + } + + out, err = exec.Command("blkid", device).CombinedOutput() return err == nil && string(out) != "", nil } -func (m *mounter) MakeFilesystem(device string, fsType string) error { +func (m *mounter) MakeFilesystem(device string, fsType string) ([]byte, error) { options := []string{"-t", fsType} if fsType == "ext4" { options = append(options, "-E", "nodiscard") @@ -55,10 +75,5 @@ func (m *mounter) MakeFilesystem(device string, fsType string) error { } options = append(options, device) - out, err := exec.Command("mkfs", options...).CombinedOutput() - if err != nil { - return fmt.Errorf("failed to make filesystem: %w, output %q", err, out) - } - - return nil + return exec.Command("mkfs", options...).CombinedOutput() }