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 <idryomov@gmail.com>
(cherry picked from commit 9a61fa0c0a)
This commit is contained in:
Ilya Dryomov 2019-10-03 18:54:02 +02:00 committed by mergify[bot]
parent 4c87c5712b
commit 1a81316ca7

View File

@ -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
}
}
}