Skip to content

Commit

Permalink
NBSNEBIUS-346: don't recreate filesystem while pvc mount (#1413)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanek325 authored Jun 14, 2024
1 parent 1881b1a commit 28c1105
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
6 changes: 3 additions & 3 deletions cloud/blockstore/tools/csi_driver/internal/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cloud/blockstore/tools/csi_driver/internal/mounter/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
31 changes: 23 additions & 8 deletions cloud/blockstore/tools/csi_driver/internal/mounter/mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"

"k8s.io/mount-utils"
)
Expand Down Expand Up @@ -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")
Expand All @@ -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()
}

0 comments on commit 28c1105

Please sign in to comment.