@@ -345,10 +345,26 @@ func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVol
345
345
return nil , status .Errorf (codes .Internal , "failed to get device details for volume id %q: %v" , req .VolumeId , err )
346
346
}
347
347
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 {
349
351
return nil , status .Error (codes .Internal , err .Error ())
350
352
}
351
353
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
+
352
368
// FIXME(avalluri): we shouldn't depend on volumecontext to determine the device mode,
353
369
// instead PmemDeviceInfo should hold the device mode in which it was created.
354
370
if params := req .GetVolumeContext (); params != nil {
@@ -480,44 +496,25 @@ func (ns *nodeServer) createEphemeralDevice(ctx context.Context, req *csi.NodePu
480
496
// and mounts at given targetPath.
481
497
func (ns * nodeServer ) provisionDevice (device * pmdmanager.PmemDeviceInfo , fsType string ) error {
482
498
if fsType == "" {
483
- // Default to ext4 filesystem
499
+ // Empty FsType means "unspecified" and we pick default, currently hard-coded to ext4
484
500
fsType = defaultFilesystem
485
501
}
486
502
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 }
502
512
} 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 )
521
518
}
522
519
523
520
return nil
0 commit comments