From 9a61fa0c0a4e70a64dca7be72b6bb96a7d61726e Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Thu, 3 Oct 2019 18:54:02 +0200 Subject: [PATCH] Format ext4 with lazy_journal_init Skip zeroing of the journal on freshly created images. As only dynamic PVs are supported, it is fine to assume that existingFormat == "" image has never been mapped and written to before. lazy_itable_init is enabled by default, passing it for consistency with lazy_journal_init. Signed-off-by: Ilya Dryomov --- pkg/rbd/nodeserver.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/rbd/nodeserver.go b/pkg/rbd/nodeserver.go index 9da30ef7c..8732e322b 100644 --- a/pkg/rbd/nodeserver.go +++ b/pkg/rbd/nodeserver.go @@ -310,25 +310,38 @@ func getLegacyVolumeName(mountPath string) (string, error) { } func (ns *NodeServer) mountVolumeToStagePath(ctx context.Context, req *csi.NodeStageVolumeRequest, stagingPath, devicePath string) error { - // Publish Path fsType := req.GetVolumeCapability().GetMount().GetFsType() diskMounter := &mount.SafeFormatAndMount{Interface: ns.mounter, Exec: mount.NewOsExec()} + + // rbd images are thin-provisioned and return zeros for unwritten areas. A freshly created + // image will not benefit from discard and we also want to avoid as much unnecessary zeroing + // as possible. Open-code mkfs here because FormatAndMount() doesn't accept custom mkfs + // options. + // + // Note that "freshly" is very important here. While discard is more of a nice to have, + // lazy_journal_init=1 is plain unsafe if the image has been written to before and hasn't + // been zeroed afterwards (unlike the name suggests, it leaves the journal completely + // uninitialized and carries a risk until the journal is overwritten and wraps around for + // the first time). existingFormat, err := diskMounter.GetDiskFormat(devicePath) if err != nil { klog.Errorf(util.Log(ctx, "failed to get disk format for path %s, error: %v"), devicePath, err) return err } - if existingFormat == "" && (fsType == "ext4" || fsType == "xfs") { + // TODO: update this when adding support for static (pre-provisioned) PVs + if existingFormat == "" /* && !staticVol */ { args := []string{} if fsType == "ext4" { - args = []string{"-m0", "-Enodiscard", devicePath} + args = []string{"-m0", "-Enodiscard,lazy_itable_init=1,lazy_journal_init=1", devicePath} } else if fsType == "xfs" { args = []string{"-K", devicePath} } - _, err = diskMounter.Exec.Run("mkfs."+fsType, args...) - if err != nil { - klog.Errorf(util.Log(ctx, "failed to run mkfs, error: %v"), err) - return err + if len(args) > 0 { + _, err = diskMounter.Exec.Run("mkfs."+fsType, args...) + if err != nil { + klog.Errorf(util.Log(ctx, "failed to run mkfs, error: %v"), err) + return err + } } }