Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit e071b3b

Browse files
committed
nodeserver: Take check for existing fsType out of ProvisionDevice
This check is needed only if called from StageVolume, so lets make it in StageVolume, giving possibility to return API error codes that follow spec better.
1 parent fb93c47 commit e071b3b

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

pkg/pmem-csi-driver/nodeserver.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
344344
return nil, status.Error(codes.InvalidArgument, err.Error())
345345
}
346346

347+
// Check does devicepath already contain a filesystem?
348+
existingFsType, err := determineFilesystemType(device.Path)
349+
if err != nil {
350+
return nil, status.Error(codes.Internal, err.Error())
351+
}
352+
353+
// what to do if existing file system is detected;
354+
if existingFsType != "" {
355+
// Is existing filesystem type same as requested?
356+
if existingFsType == requestedFsType {
357+
klog.V(4).Infof("Skip mkfs as %v file system already exists on %v", existingFsType, device.Path)
358+
} else {
359+
return nil, status.Error(codes.AlreadyExists, "File system with different type exists")
360+
}
361+
}
362+
347363
if err = ns.provisionDevice(device, requestedFsType); err != nil {
348364
return nil, status.Error(codes.Internal, err.Error())
349365
}
@@ -478,44 +494,25 @@ func (ns *nodeServer) createEphemeralDevice(ctx context.Context, req *csi.NodePu
478494
// and mounts at given targetPath.
479495
func (ns *nodeServer) provisionDevice(device *pmdmanager.PmemDeviceInfo, fsType string) error {
480496
if fsType == "" {
481-
// Default to ext4 filesystem
497+
// Empty FsType means "unspecified" and we pick default, currently hard-coded to ext4
482498
fsType = defaultFilesystem
483499
}
484500

485-
// Check does devicepath already contain a filesystem?
486-
existingFsType, err := determineFilesystemType(device.Path)
487-
if err != nil {
488-
return err
489-
}
490-
491-
// what to do if existing file system is detected and is different from request;
492-
// forced re-format would lead to loss of previous data, so we refuse.
493-
if existingFsType != "" {
494-
// Is existing filesystem type same as requested?
495-
if existingFsType == fsType {
496-
klog.V(4).Infof("Skip mkfs as %v file system already exists on %v", existingFsType, device.Path)
497-
} else {
498-
return fmt.Errorf("File system with different type(%s) exists, whereas requested type is '%s'", existingFsType, fsType)
499-
}
501+
cmd := ""
502+
var args []string
503+
// hard-code block size to 4k to avoid smaller values and trouble to dax mount option
504+
if fsType == "ext4" {
505+
cmd = "mkfs.ext4"
506+
args = []string{"-b 4096", "-F", device.Path}
507+
} else if fsType == "xfs" {
508+
cmd = "mkfs.xfs"
509+
args = []string{"-b", "size=4096", "-f", device.Path}
500510
} else {
501-
// no existing file system, make fs
502-
// Empty FsType means "unspecified" and we pick default, currently hard-codes to ext4
503-
cmd := ""
504-
var args []string
505-
// hard-code block size to 4k to avoid smaller values and trouble to dax mount option
506-
if fsType == "ext4" {
507-
cmd = "mkfs.ext4"
508-
args = []string{"-b 4096", "-F", device.Path}
509-
} else if fsType == "xfs" {
510-
cmd = "mkfs.xfs"
511-
args = []string{"-b", "size=4096", "-f", device.Path}
512-
} else {
513-
return fmt.Errorf("Unsupported filesystem '%s'. Supported filesystems types: 'xfs', 'ext4'", fsType)
514-
}
515-
output, err := pmemexec.RunCommand(cmd, args...)
516-
if err != nil {
517-
return fmt.Errorf("mkfs failed: %s", output)
518-
}
511+
return fmt.Errorf("Unsupported filesystem '%s'. Supported filesystems types: 'xfs', 'ext4'", fsType)
512+
}
513+
output, err := pmemexec.RunCommand(cmd, args...)
514+
if err != nil {
515+
return fmt.Errorf("mkfs failed: %s", output)
519516
}
520517

521518
return nil

0 commit comments

Comments
 (0)