From 13cdb08e61fc49162c4cd9cb8fac02d5305e1df0 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Thu, 23 Feb 2023 17:39:09 +0100 Subject: [PATCH] rbd: cleanup passing `mkfs` arguments for NodeStageVolume Storing the default `mkfs` arguments in a map with key per filesystem type makes this a little more modular. It prepares th code for fetching the `mkfs` arguments from the VolumeContext. Signed-off-by: Niels de Vos --- internal/rbd/nodeserver.go | 40 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/internal/rbd/nodeserver.go b/internal/rbd/nodeserver.go index 359b3fdf0..68b0f0a37 100644 --- a/internal/rbd/nodeserver.go +++ b/internal/rbd/nodeserver.go @@ -101,6 +101,15 @@ var ( // xfsHasReflink is set by xfsSupportsReflink(), use the function when // checking the support for reflink. xfsHasReflink = xfsReflinkUnset + + mkfsDefaultArgs = map[string][]string{ + "ext4": {"-m0", "-Enodiscard,lazy_itable_init=1,lazy_journal_init=1"}, + "xfs": {"-K"}, + } + + mountDefaultOpts = map[string][]string{ + "xfs": {"nouuid"}, + } ) // parseBoolOption checks if parameters contain option and parse it. If it is @@ -756,7 +765,8 @@ func (ns *NodeServer) mountVolumeToStagePath( return err } - opt := []string{"_netdev"} + opt := mountDefaultOpts[fsType] + opt = append(opt, "_netdev") opt = csicommon.ConstructMountOptions(opt, req.GetVolumeCapability()) isBlock := req.GetVolumeCapability().GetBlock() != nil rOnly := "ro" @@ -771,34 +781,34 @@ func (ns *NodeServer) mountVolumeToStagePath( readOnly = true } - if fsType == "xfs" { - opt = append(opt, "nouuid") - } - if existingFormat == "" && !staticVol && !readOnly { - args := []string{} + args := mkfsDefaultArgs[fsType] + + // add extra arguments depending on the filesystem + mkfs := "mkfs." + fsType switch fsType { case "ext4": - args = []string{"-m0", "-Enodiscard,lazy_itable_init=1,lazy_journal_init=1"} if fileEncryption { args = append(args, "-Oencrypt") } - args = append(args, devicePath) case "xfs": - args = []string{"-K", devicePath} // always disable reflink // TODO: make enabling an option, see ceph/ceph-csi#1256 if ns.xfsSupportsReflink() { args = append(args, "-m", "reflink=0") } + case "": + // no filesystem type specified, just use "mkfs" + mkfs = "mkfs" } - if len(args) > 0 { - cmdOut, cmdErr := diskMounter.Exec.Command("mkfs."+fsType, args...).CombinedOutput() - if cmdErr != nil { - log.ErrorLog(ctx, "failed to run mkfs error: %v, output: %v", cmdErr, string(cmdOut)) - return cmdErr - } + // add device as last argument + args = append(args, devicePath) + cmdOut, cmdErr := diskMounter.Exec.Command(mkfs, args...).CombinedOutput() + if cmdErr != nil { + log.ErrorLog(ctx, "failed to run mkfs.%s (%v) error: %v, output: %v", fsType, args, cmdErr, string(cmdOut)) + + return cmdErr } }