Skip to content

Commit

Permalink
refactor: Switched to k8s.io/mount-utils
Browse files Browse the repository at this point in the history
Replaced our code for formatting and other disk related functions to k8s.io/mount-utils. This leaves only code that is related to LUKS.
  • Loading branch information
lukasmetzner committed Oct 17, 2024
1 parent f908f5d commit 8147b51
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 193 deletions.
8 changes: 0 additions & 8 deletions internal/driver/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ func (s *sanityMountService) PathExists(path string) (bool, error) {
return true, nil
}

func (s *sanityMountService) FormatDisk(_ string, _ string) error {
return nil
}

func (s *sanityMountService) DetectDiskFormat(_ string) (string, error) {
return "ext4", nil
}

type sanityResizeService struct{}

func (s *sanityResizeService) Resize(volumePath string) error {
Expand Down
28 changes: 6 additions & 22 deletions internal/mock/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ func (s *VolumeService) Resize(ctx context.Context, volume *csi.Volume, size int
}

type VolumeMountService struct {
PublishFunc func(targetPath string, devicePath string, opts volumes.MountOpts) error
UnpublishFunc func(targetPath string) error
PathExistsFunc func(path string) (bool, error)
FormatDiskFunc func(disk string, fstype string) error
DetectDiskFormatFunc func(disk string) (string, error)
PublishFunc func(targetPath string, devicePath string, opts volumes.MountOpts) error
UnpublishFunc func(targetPath string) error
PathExistsFunc func(path string) (bool, error)
}

func (s *VolumeMountService) Publish(targetPath string, devicePath string, opts volumes.MountOpts) error {
Expand All @@ -91,32 +89,18 @@ func (s *VolumeMountService) Publish(targetPath string, devicePath string, opts
return s.PublishFunc(targetPath, devicePath, opts)
}

func (s *VolumeMountService) PathExists(path string) (bool, error) {
if s.PathExistsFunc == nil {
panic("not implemented")
}
return s.PathExistsFunc(path)
}

func (s *VolumeMountService) Unpublish(targetPath string) error {
if s.UnpublishFunc == nil {
panic("not implemented")
}
return s.UnpublishFunc(targetPath)
}

func (s *VolumeMountService) FormatDisk(disk string, fstype string) error {
if s.FormatDiskFunc == nil {
panic("not implemented")
}
return s.FormatDiskFunc(disk, fstype)
}

func (s *VolumeMountService) DetectDiskFormat(disk string) (string, error) {
if s.DetectDiskFormatFunc == nil {
func (s *VolumeMountService) PathExists(path string) (bool, error) {
if s.PathExistsFunc == nil {
panic("not implemented")
}
return s.DetectDiskFormatFunc(disk)
return s.PathExistsFunc(path)
}

type VolumeResizeService struct {
Expand Down
99 changes: 7 additions & 92 deletions internal/volumes/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/go-kit/log"
Expand All @@ -29,8 +28,6 @@ type MountService interface {
Publish(targetPath string, devicePath string, opts MountOpts) error
Unpublish(targetPath string) error
PathExists(path string) (bool, error)
FormatDisk(disk string, fstype string) error
DetectDiskFormat(disk string) (string, error)
}

// LinuxMountService mounts volumes on a Linux system.
Expand Down Expand Up @@ -100,7 +97,7 @@ func (s *LinuxMountService) Publish(targetPath string, devicePath string, opts M
mountOptions = append(mountOptions, opts.Additional...)

if opts.EncryptionPassphrase != "" {
existingFSType, err := s.DetectDiskFormat(devicePath)
existingFSType, err := s.mounter.GetDiskFormat(devicePath)
if err != nil {
return fmt.Errorf("unable to detect existing disk format of %s: %w", devicePath, err)
}
Expand All @@ -122,26 +119,6 @@ func (s *LinuxMountService) Publish(targetPath string, devicePath string, opts M
devicePath = luksDevicePath
}

// Format disk if requested (/skip formatting for block devices)
if opts.FSType != "" {
existingFSType, err := s.DetectDiskFormat(devicePath)
if err != nil {
return fmt.Errorf("unable to detect existing disk format of %s: %w", devicePath, err)
}

if existingFSType == "" {
if opts.Readonly {
return fmt.Errorf("cannot publish unformatted disk %s in read-only mode", devicePath)
}

if err = s.FormatDisk(devicePath, opts.FSType); err != nil {
return err
}
} else if existingFSType != opts.FSType {
return fmt.Errorf("requested %s volume, but disk %s already is formatted with %s", opts.FSType, devicePath, existingFSType)
}
}

level.Info(s.logger).Log(
"msg", "publishing volume",
"target-path", targetPath,
Expand All @@ -153,7 +130,11 @@ func (s *LinuxMountService) Publish(targetPath string, devicePath string, opts M
"encrypted", opts.EncryptionPassphrase != "",
)

return s.mounter.Mount(devicePath, targetPath, opts.FSType, mountOptions)
if opts.BlockVolume {
return s.mounter.Mount(devicePath, targetPath, opts.FSType, mountOptions)
}

return s.mounter.FormatAndMount(devicePath, targetPath, opts.FSType, mountOptions)
}

func (s *LinuxMountService) Unpublish(targetPath string) error {
Expand All @@ -178,71 +159,5 @@ func (s *LinuxMountService) Unpublish(targetPath string) error {
}

func (s *LinuxMountService) PathExists(path string) (bool, error) {
level.Debug(s.logger).Log(
"msg", "checking path existence",
"path", path,
)
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func (s *LinuxMountService) FormatDisk(disk string, fstype string) error {
level.Info(s.logger).Log(
"msg", "formatting disk",
"disk", disk,
"fstype", fstype,
)
switch fstype {
case "ext4":
_, _, err := command("mkfs.ext4", "-F", "-m0", disk)
return err
case "xfs":
_, _, err := command(
"mkfs.xfs",
"-i", "nrext64=0", // Compatibility with kernel that do not support nrext64
disk)
return err
case "btrfs":
_, _, err := command("mkfs.btrfs", disk)
return err
default:
return fmt.Errorf("unsupported disk format %s", fstype)
}
}

// see https://github.com/kubernetes/mount-utils/blob/master/mount_linux.go
func (s *LinuxMountService) DetectDiskFormat(disk string) (string, error) {
args := []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", disk}
output, exitCode, err := command("blkid", args...)
if exitCode == 2 {
return "", nil
}
if err != nil {
return "", err
}

fstypeRegex := regexp.MustCompile(`TYPE=(.*)`)
pttypeRegex := regexp.MustCompile(`PTTYPE=(.*)`)
fstype := ""
pttype := ""
fstypeMatch := fstypeRegex.FindStringSubmatch(output)
if fstypeMatch != nil {
fstype = fstypeMatch[1]
}
pttypeMatch := pttypeRegex.FindStringSubmatch(output)
if pttypeMatch != nil {
pttype = pttypeMatch[1]
}

if pttype != "" {
return "", fmt.Errorf("disk %s propably contains partitions", disk)
}

return fstype, nil
return mount.PathExists(path)
}
Loading

0 comments on commit 8147b51

Please sign in to comment.