Skip to content

Commit

Permalink
Merge pull request #765 from mowangdk/chore/add_xfs_cus_mkfsargs
Browse files Browse the repository at this point in the history
Chore: Add xfs custom mkfs args
  • Loading branch information
mowangdk authored May 9, 2023
2 parents e07c405 + 828afb8 commit e45b407
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
13 changes: 3 additions & 10 deletions pkg/disk/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,16 +1051,9 @@ func (ns *nodeServer) mountDeviceToGlobal(capability *csi.VolumeCapability, volu

// do format-mount or mount
diskMounter := &k8smount.SafeFormatAndMount{Interface: ns.k8smounter, Exec: utilexec.New()}
if len(mkfsOptions) > 0 && (fsType == "ext4" || fsType == "ext3") {
if err := utils.FormatAndMount(diskMounter, device, sourcePath, fsType, mkfsOptions, mountOptions, GlobalConfigVar.OmitFilesystemCheck); err != nil {
log.Log.Errorf("mountDeviceToGlobal: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, sourcePath, fsType, mkfsOptions, mountOptions, err.Error())
return status.Error(codes.Internal, err.Error())
}
} else {
if err := diskMounter.FormatAndMount(device, sourcePath, fsType, mountOptions); err != nil {
log.Log.Errorf("mountDeviceToGlobal: Device: %s, FormatAndMount error: %s", device, err.Error())
return status.Error(codes.Internal, err.Error())
}
if err := utils.FormatAndMount(diskMounter, device, sourcePath, fsType, mkfsOptions, mountOptions, GlobalConfigVar.OmitFilesystemCheck); err != nil {
log.Log.Errorf("mountDeviceToGlobal: FormatAndMount fail with mkfsOptions %s, %s, %s, %s, %s with error: %s", device, sourcePath, fsType, mkfsOptions, mountOptions, err.Error())
return status.Error(codes.Internal, err.Error())
}
return nil
}
Expand Down
43 changes: 27 additions & 16 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,42 +1005,53 @@ func FormatNewDisk(readOnly bool, source, fstype, target string, mkfsOptions, mo
return errors.New("failed to mount unformatted volume as read only")
}

// Disk is unformatted so format it.
args := []string{source}
// Use 'ext4' as the default
if len(fstype) == 0 {
fstype = "ext4"
}

if fstype == "ext4" || fstype == "ext3" {
args = []string{
"-F", // Force flag
"-m0", // Zero blocks reserved for super-user
source,
}
// add mkfs options
if len(mkfsOptions) != 0 {
args = []string{}
for _, opts := range mkfsOptions {
args = append(args, opts)
}
args = append(args, source)
args := mkfsDefaultArgs(fstype, source)

// add mkfs options
if len(mkfsOptions) != 0 {
args = []string{}
for _, opts := range mkfsOptions {
args = append(args, opts)
}
args = append(args, source)
}

log.Infof("Disk %q appears to be unformatted, attempting to format as type: %q with options: %v", source, fstype, args)
startT := time.Now()

pvName := filepath.Base(source)
_, err := diskMounter.Exec.Command("mkfs."+fstype, args...).CombinedOutput()
log.Infof("Disk format finished, pvName: %s elapsedTime: %+v ms", pvName, time.Now().Sub(startT).Milliseconds())
if err == nil {
// the disk has been formatted successfully try to mount it again.
log.Infof("Disk format successed, pvName: %s elapsedTime: %+v ms", pvName, time.Now().Sub(startT).Milliseconds())
return diskMounter.Interface.Mount(source, target, fstype, mountOptions)
}
log.Errorf("format of disk %q failed: type:(%q) target:(%q) options:(%q) error:(%v)", source, fstype, target, args, err)
return err
}

func mkfsDefaultArgs(fstype, source string) (args []string) {
// default args
if fstype == "ext4" || fstype == "ext3" {
args = []string{
"-F", // Force flag
"-m0", // Zero blocks reserved for super-user
source,
}
} else if fstype == "xfs" {
args = []string{
"-f",
source,
}
}
return
}

// GetDiskPtypePTtype uses 'blkid' to see if the given disk is unformatted
func GetDiskPtypePTtype(disk string) (fstype string, pttype string, err error) {
args := []string{"-p", "-s", "TYPE", "-s", "PTTYPE", "-o", "export", disk}
Expand Down

0 comments on commit e45b407

Please sign in to comment.