Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ ARG COMMIT_SHA
ARG BUILD_DATE
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-w -s -X github.com/scaleway/scaleway-csi/driver.driverVersion=${TAG} -X github.com/scaleway/scaleway-csi/driver.buildDate=${BUILD_DATE} -X github.com/scaleway/scaleway-csi/driver.gitCommit=${COMMIT_SHA} " -o scaleway-csi ./cmd/scaleway-csi

FROM alpine:3.18
RUN apk update && apk add --no-cache e2fsprogs e2fsprogs-extra xfsprogs xfsprogs-extra cryptsetup ca-certificates && update-ca-certificates
FROM alpine:3.15
RUN apk update && apk add --no-cache e2fsprogs e2fsprogs-extra xfsprogs xfsprogs-extra cryptsetup ca-certificates blkid && update-ca-certificates
WORKDIR /
COPY --from=builder /go/src/github.com/scaleway/scaleway-csi/scaleway-csi .
ENTRYPOINT ["/scaleway-csi"]
78 changes: 3 additions & 75 deletions driver/diskutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,11 @@ func (d *diskUtils) FormatAndMount(targetPath string, devicePath string, fsType
}

klog.V(4).Infof("Attempting to mount %s on %s with type %s", devicePath, targetPath, fsType)
err := d.MountToTarget(devicePath, targetPath, fsType, mountOptions)
if err != nil {
klog.V(4).Infof("Mount attempt failed, trying to format device %s with type %s", devicePath, fsType)
realFsType, fsErr := d.getDeviceType(devicePath)
if fsErr != nil {
return fsErr
}

if realFsType == "" {
fsErr = d.formatDevice(devicePath, fsType)
if fsErr != nil {
return fsErr
}
return d.MountToTarget(devicePath, targetPath, fsType, mountOptions)
}
return err
if err := d.kMounter.FormatAndMount(devicePath, targetPath, fsType, mountOptions); err != nil {
return fmt.Errorf("failed to optionnaly format and mount: %w", err)
}

return nil
}

Expand All @@ -218,66 +206,6 @@ func (d *diskUtils) MountToTarget(sourcePath, targetPath, fsType string, mountOp
return nil
}

func (d *diskUtils) formatDevice(devicePath string, fsType string) error {
if fsType == "" {
fsType = defaultFSType
}

mkfsPath, err := exec.LookPath("mkfs." + fsType)
if err != nil {
return err
}

mkfsArgs := []string{devicePath}
if fsType == "ext4" || fsType == "ext3" {
mkfsArgs = []string{
"-F", // Force mke2fs to create a filesystem
"-m0", // 0 blocks reserved for the super-user
devicePath,
}
}

return exec.Command(mkfsPath, mkfsArgs...).Run()
}

func (d *diskUtils) getDeviceType(devicePath string) (string, error) {
blkidPath, err := exec.LookPath("blkid")
if err != nil {
return "", err
}

blkidArgs := []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", devicePath}
blkidOutputBytes, err := exec.Command(blkidPath, blkidArgs...).Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 2 {
// From man page of blkid:
// If the specified token was not found, or no (specified) devices
// could be identified, or it is impossible to gather
// any information about the device identifiers
// or device content an exit code of 2 is returned.
return "", nil
}
}
return "", err
}

blkidOutput := string(blkidOutputBytes)
blkidOutputLines := strings.Split(blkidOutput, "\n")
for _, blkidLine := range blkidOutputLines {
if len(blkidLine) == 0 {
continue
}

blkidLineSplit := strings.Split(blkidLine, "=")
if blkidLineSplit[0] == "TYPE" && len(blkidLineSplit[1]) > 0 {
return blkidLineSplit[1], nil
}
}
// TODO real error???
return "", nil
}

func (d *diskUtils) GetDevicePath(volumeID string) (string, error) {
devicePath := path.Join(diskByIDPath, diskSCWPrefix+volumeID)
realDevicePath, err := filepath.EvalSymlinks(devicePath)
Expand Down
39 changes: 0 additions & 39 deletions driver/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package driver
import (
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -390,44 +389,6 @@ func (s *fakeHelper) MountToTarget(sourcePath, targetPath, fsType string, mountO
return nil
}

func (s *fakeHelper) getDeviceType(devicePath string) (string, error) {
blkidPath, err := exec.LookPath("blkid")
if err != nil {
return "", err
}

blkidArgs := []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", devicePath}
blkidOutputBytes, err := exec.Command(blkidPath, blkidArgs...).Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 2 {
// From man page of blkid:
// If the specified token was not found, or no (specified) devices
// could be identified, or it is impossible to gather
// any information about the device identifiers
// or device content an exit code of 2 is returned.
return "", nil
}
}
return "", err
}

blkidOutput := string(blkidOutputBytes)
blkidOutputLines := strings.Split(blkidOutput, "\n")
for _, blkidLine := range blkidOutputLines {
if len(blkidLine) == 0 {
continue
}

blkidLineSplit := strings.Split(blkidLine, "=")
if blkidLineSplit[0] == "TYPE" && len(blkidLineSplit[1]) > 0 {
return blkidLineSplit[1], nil
}
}
// TODO real error???
return "", nil
}

func (s *fakeHelper) GetDevicePath(volumeID string) (string, error) {
if _, ok := s.devices[path.Join(diskByIDPath, diskSCWPrefix+volumeID)]; ok {
return path.Join(diskByIDPath, diskSCWPrefix+volumeID), nil
Expand Down