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

Commit a7819fe

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 48fc433 commit a7819fe

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

pkg/pmem-csi-driver/nodeserver.go

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,26 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
345345
return nil, status.Errorf(codes.Internal, "failed to get device details for volume id %q: %v", req.VolumeId, err)
346346
}
347347

348-
if err = ns.provisionDevice(device, requestedFsType); err != nil {
348+
// Check does devicepath already contain a filesystem?
349+
existingFsType, err := determineFilesystemType(device.Path)
350+
if err != nil {
349351
return nil, status.Error(codes.Internal, err.Error())
350352
}
351353

354+
// what to do if existing file system is detected;
355+
if existingFsType != "" {
356+
// Is existing filesystem type same as requested?
357+
if existingFsType == requestedFsType {
358+
klog.V(4).Infof("Skip mkfs as %v file system already exists on %v", existingFsType, device.Path)
359+
} else {
360+
return nil, status.Error(codes.AlreadyExists, "File system with different type exists")
361+
}
362+
} else {
363+
if err = ns.provisionDevice(device, requestedFsType); err != nil {
364+
return nil, status.Error(codes.Internal, err.Error())
365+
}
366+
}
367+
352368
// FIXME(avalluri): we shouldn't depend on volumecontext to determine the device mode,
353369
// instead PmemDeviceInfo should hold the device mode in which it was created.
354370
if params := req.GetVolumeContext(); params != nil {
@@ -480,44 +496,25 @@ func (ns *nodeServer) createEphemeralDevice(ctx context.Context, req *csi.NodePu
480496
// and mounts at given targetPath.
481497
func (ns *nodeServer) provisionDevice(device *pmdmanager.PmemDeviceInfo, fsType string) error {
482498
if fsType == "" {
483-
// Default to ext4 filesystem
499+
// Empty FsType means "unspecified" and we pick default, currently hard-coded to ext4
484500
fsType = defaultFilesystem
485501
}
486502

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

523520
return nil

0 commit comments

Comments
 (0)