Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cephfs: do not run modprobe if support is compiled into the kernel #4378

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions internal/cephfs/mounter/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package mounter
import (
"context"
"fmt"
"os"
"strings"

"github.com/ceph/ceph-csi/internal/cephfs/store"
"github.com/ceph/ceph-csi/internal/util"
Expand All @@ -27,13 +29,47 @@ import (
const (
volumeMounterKernel = "kernel"
netDev = "_netdev"
kernelModule = "ceph"
)

type KernelMounter struct{}
// testErrorf can be set by unit test for enhanced error reporting.
var testErrorf = func(fmt string, args ...any) { /* do nothing */ }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testErrorf might not be the right name for non-test file and also fmt can be replaced to something else as its a core package name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand, fmt is just a string?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what i mean is that fmt is also a package name, we might get into a static check problem if we use the same name for some variable name as well.


func mountKernel(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *store.VolumeOptions) error {
if err := execCommandErr(ctx, "modprobe", "ceph"); err != nil {
return err
type KernelMounter interface {
Mount(
ctx context.Context,
mountPoint string,
cr *util.Credentials,
volOptions *store.VolumeOptions,
) error

Name() string
}

type kernelMounter struct {
// needsModprobe indicates that the ceph kernel module is not loaded in
// the kernel yet (or compiled into it)
needsModprobe bool
}

func NewKernelMounter() KernelMounter {
return &kernelMounter{
needsModprobe: !filesystemSupported(kernelModule),
}
}

func (m *kernelMounter) mountKernel(
ctx context.Context,
mountPoint string,
cr *util.Credentials,
volOptions *store.VolumeOptions,
) error {
if m.needsModprobe {
if err := execCommandErr(ctx, "modprobe", kernelModule); err != nil {
return err
}
Comment on lines +68 to +70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can optimize it by moving this modeprobe to NewKernelMounter so that we run it only once when driver starts not always WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a check to see if the driver is loaded when NewKernelMounter() is called. That sets m.needsModprobe. It is done so that NewKernelMounter() does not need to return an error, keeping other changes minimal.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i agree but this works byt when where a mountKernel is called and if needsModprobe not set we keep on running modeprobe, but running that can be avoided if we set needsModprobe to true on successful run of modprobe,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can take it as an enhancement as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, if modprobe succeeded, it should be set to true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is done now, please review again.


m.needsModprobe = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this needs to be set to true? also this is not of much use with the current code because mounter.New() is called for every NodeStage RPC call. we need to move this to Driver initialization time as it needs to run only one time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once the module is loaded, it will be listed in /proc/filesystems, so eventually modprobe is only run once

}

args := []string{
Expand Down Expand Up @@ -68,7 +104,7 @@ func mountKernel(ctx context.Context, mountPoint string, cr *util.Credentials, v
return err
}

func (m *KernelMounter) Mount(
func (m *kernelMounter) Mount(
ctx context.Context,
mountPoint string,
cr *util.Credentials,
Expand All @@ -78,7 +114,26 @@ func (m *KernelMounter) Mount(
return err
}

return mountKernel(ctx, mountPoint, cr, volOptions)
return m.mountKernel(ctx, mountPoint, cr, volOptions)
}

func (m *KernelMounter) Name() string { return "Ceph kernel client" }
func (m *kernelMounter) Name() string { return "Ceph kernel client" }

// filesystemSupported checks if the passed name of the filesystem is included
// in /proc/filesystems.
func filesystemSupported(fs string) bool {
// /proc/filesystems contains a list of all supported filesystems,
// either compiled into the kernel, or as loadable module.
data, err := os.ReadFile("/proc/filesystems")
if err != nil {
testErrorf("failed to read /proc/filesystems: %v", err)

return false
}

// The format of /proc/filesystems is one filesystem per line, an
// optional keyword ("nodev") followed by a tab and the name of the
// filesystem. Matching <tab>ceph<eol> for the ceph kernel module that
// supports CephFS.
return strings.Contains(string(data), "\t"+fs+"\n")
}
38 changes: 38 additions & 0 deletions internal/cephfs/mounter/kernel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 The Ceph-CSI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mounter

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilesystemSupported(t *testing.T) {
t.Parallel()

testErrorf = func(fmt string, args ...any) {
t.Errorf(fmt, args...)
}

// "proc" is always a supported filesystem, we detect supported
// filesystems by reading from it
assert.True(t, filesystemSupported("proc"))

// "nonefs" is a made-up name, and does not exist
assert.False(t, filesystemSupported("nonefs"))
}
2 changes: 1 addition & 1 deletion internal/cephfs/mounter/volumemounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func New(volOptions *store.VolumeOptions) (VolumeMounter, error) {
case volumeMounterFuse:
return &FuseMounter{}, nil
case volumeMounterKernel:
return &KernelMounter{}, nil
return NewKernelMounter(), nil
}

return nil, fmt.Errorf("unknown mounter '%s'", chosenMounter)
Expand Down
4 changes: 2 additions & 2 deletions internal/cephfs/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ func (ns *NodeServer) setMountOptions(
}
volOptions.FuseMountOptions = util.MountOptionsAdd(volOptions.FuseMountOptions, configuredMountOptions)
volOptions.FuseMountOptions = util.MountOptionsAdd(volOptions.FuseMountOptions, mountOptions...)
case *mounter.KernelMounter:
case mounter.KernelMounter:
configuredMountOptions = ns.kernelMountOptions
// override of kernelMountOptions are set
if kernelMountOptions != "" {
Expand All @@ -821,7 +821,7 @@ func (ns *NodeServer) setMountOptions(
if !csicommon.MountOptionContains(strings.Split(volOptions.FuseMountOptions, ","), readOnly) {
volOptions.FuseMountOptions = util.MountOptionsAdd(volOptions.FuseMountOptions, readOnly)
}
case *mounter.KernelMounter:
case mounter.KernelMounter:
if !csicommon.MountOptionContains(strings.Split(volOptions.KernelMountOptions, ","), readOnly) {
volOptions.KernelMountOptions = util.MountOptionsAdd(volOptions.KernelMountOptions, readOnly)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cephfs/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Test_setMountOptions(t *testing.T) {
{
name: "KernelMountOptions set in cluster-1 config and not set in CLI",
ns: &NodeServer{},
mnt: mounter.VolumeMounter(&mounter.KernelMounter{}),
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
volOptions: &store.VolumeOptions{
ClusterID: "cluster-1",
},
Expand All @@ -97,7 +97,7 @@ func Test_setMountOptions(t *testing.T) {
ns: &NodeServer{
kernelMountOptions: cliKernelMountOptions,
},
mnt: mounter.VolumeMounter(&mounter.KernelMounter{}),
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
volOptions: &store.VolumeOptions{
ClusterID: "cluster-1",
},
Expand All @@ -119,7 +119,7 @@ func Test_setMountOptions(t *testing.T) {
ns: &NodeServer{
kernelMountOptions: cliKernelMountOptions,
},
mnt: mounter.VolumeMounter(&mounter.KernelMounter{}),
mnt: mounter.VolumeMounter(mounter.NewKernelMounter()),
volOptions: &store.VolumeOptions{
ClusterID: "cluster-2",
},
Expand Down Expand Up @@ -162,7 +162,7 @@ func Test_setMountOptions(t *testing.T) {
if !strings.Contains(tc.volOptions.FuseMountOptions, tc.want) {
t.Errorf("Set FuseMountOptions = %v Required FuseMountOptions = %v", tc.volOptions.FuseMountOptions, tc.want)
}
case *mounter.KernelMounter:
case mounter.KernelMounter:
if !strings.Contains(tc.volOptions.KernelMountOptions, tc.want) {
t.Errorf("Set KernelMountOptions = %v Required KernelMountOptions = %v", tc.volOptions.KernelMountOptions, tc.want)
}
Expand Down