2018-04-13 12:29:54 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-04-13 12:29:54 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
package core
|
2018-04-13 12:29:54 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2019-01-16 13:17:29 +00:00
|
|
|
"errors"
|
2018-04-13 12:29:54 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-08-14 09:19:41 +00:00
|
|
|
"os/exec"
|
2019-02-27 19:29:20 +00:00
|
|
|
"regexp"
|
2019-02-26 16:57:24 +00:00
|
|
|
"strconv"
|
2019-06-06 12:18:11 +00:00
|
|
|
"strings"
|
2019-02-26 16:57:24 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2018-04-13 12:29:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-16 13:03:38 +00:00
|
|
|
volumeMounterFuse = "fuse"
|
|
|
|
volumeMounterKernel = "kernel"
|
2020-01-23 08:24:46 +00:00
|
|
|
netDev = "_netdev"
|
2018-04-13 12:29:54 +00:00
|
|
|
)
|
|
|
|
|
2018-08-14 14:48:30 +00:00
|
|
|
var (
|
|
|
|
availableMounters []string
|
2019-02-26 16:57:24 +00:00
|
|
|
|
2021-07-08 14:59:34 +00:00
|
|
|
// maps a mountpoint to PID of its FUSE daemon.
|
2019-02-26 16:57:24 +00:00
|
|
|
fusePidMap = make(map[string]int)
|
|
|
|
fusePidMapMtx sync.Mutex
|
2019-02-27 19:29:20 +00:00
|
|
|
|
|
|
|
fusePidRx = regexp.MustCompile(`(?m)^ceph-fuse\[(.+)\]: starting fuse$`)
|
2019-11-07 14:28:59 +00:00
|
|
|
|
2020-07-21 05:10:13 +00:00
|
|
|
// nolint:gomnd // numbers specify Kernel versions.
|
2020-06-22 07:09:45 +00:00
|
|
|
quotaSupport = []util.KernelVersion{
|
|
|
|
{
|
|
|
|
Version: 4,
|
|
|
|
PatchLevel: 17,
|
|
|
|
SubLevel: 0,
|
|
|
|
ExtraVersion: 0, Distribution: "",
|
|
|
|
Backport: false,
|
|
|
|
}, // standard 4.17+ versions
|
|
|
|
{
|
|
|
|
Version: 3,
|
|
|
|
PatchLevel: 10,
|
|
|
|
SubLevel: 0,
|
|
|
|
ExtraVersion: 1062,
|
|
|
|
Distribution: ".el7",
|
|
|
|
Backport: true,
|
|
|
|
}, // RHEL-7.7
|
2019-11-07 14:28:59 +00:00
|
|
|
}
|
2020-06-22 07:09:45 +00:00
|
|
|
)
|
2019-11-07 14:28:59 +00:00
|
|
|
|
2021-09-06 12:23:07 +00:00
|
|
|
func execCommandErr(ctx context.Context, program string, args ...string) error {
|
|
|
|
_, _, err := util.ExecCommand(ctx, program, args...)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-14 14:48:30 +00:00
|
|
|
// Load available ceph mounters installed on system into availableMounters
|
2020-07-19 12:21:03 +00:00
|
|
|
// Called from driver.go's Run().
|
2021-09-16 13:47:57 +00:00
|
|
|
func LoadAvailableMounters(conf *util.Config) error {
|
2019-01-28 19:55:10 +00:00
|
|
|
// #nosec
|
2018-08-14 14:48:30 +00:00
|
|
|
fuseMounterProbe := exec.Command("ceph-fuse", "--version")
|
2019-01-28 19:55:10 +00:00
|
|
|
// #nosec
|
2018-08-14 14:48:30 +00:00
|
|
|
kernelMounterProbe := exec.Command("mount.ceph")
|
|
|
|
|
2019-08-25 04:43:23 +00:00
|
|
|
err := kernelMounterProbe.Run()
|
2020-03-30 08:25:37 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLogMsg("failed to run mount.ceph %v", err)
|
2020-03-30 08:25:37 +00:00
|
|
|
} else {
|
2019-11-07 14:28:59 +00:00
|
|
|
// fetch the current running kernel info
|
2020-06-22 06:58:47 +00:00
|
|
|
release, kvErr := util.GetKernelVersion()
|
2020-06-17 18:30:09 +00:00
|
|
|
if kvErr != nil {
|
|
|
|
return kvErr
|
2019-08-25 04:43:23 +00:00
|
|
|
}
|
2019-10-10 10:15:44 +00:00
|
|
|
|
2020-06-22 07:09:45 +00:00
|
|
|
if conf.ForceKernelCephFS || util.CheckKernelSupport(release, quotaSupport) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DefaultLog("loaded mounter: %s", volumeMounterKernel)
|
2019-08-25 04:43:23 +00:00
|
|
|
availableMounters = append(availableMounters, volumeMounterKernel)
|
|
|
|
} else {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DefaultLog("kernel version < 4.17 might not support quota feature, hence not loading kernel client")
|
2019-08-25 04:43:23 +00:00
|
|
|
}
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 08:25:37 +00:00
|
|
|
err = fuseMounterProbe.Run()
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLogMsg("failed to run ceph-fuse %v", err)
|
2020-03-30 08:25:37 +00:00
|
|
|
} else {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DefaultLog("loaded mounter: %s", volumeMounterFuse)
|
2019-08-25 04:43:23 +00:00
|
|
|
availableMounters = append(availableMounters, volumeMounterFuse)
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(availableMounters) == 0 {
|
2019-01-16 13:17:29 +00:00
|
|
|
return errors.New("no ceph mounters found on system")
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
type VolumeMounter interface {
|
|
|
|
Mount(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *VolumeOptions) error
|
|
|
|
Name() string
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func NewMounter(volOptions *VolumeOptions) (VolumeMounter, error) {
|
2018-08-14 14:48:30 +00:00
|
|
|
// Get the mounter from the configuration
|
|
|
|
|
|
|
|
wantMounter := volOptions.Mounter
|
|
|
|
|
|
|
|
// Verify that it's available
|
|
|
|
|
|
|
|
var chosenMounter string
|
|
|
|
|
|
|
|
for _, availMounter := range availableMounters {
|
2019-08-25 04:43:23 +00:00
|
|
|
if availMounter == wantMounter {
|
|
|
|
chosenMounter = wantMounter
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-08-25 04:43:23 +00:00
|
|
|
break
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chosenMounter == "" {
|
|
|
|
// Otherwise pick whatever is left
|
|
|
|
chosenMounter = availableMounters[0]
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLogMsg("requested mounter: %s, chosen mounter: %s", wantMounter, chosenMounter)
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the mounter
|
|
|
|
|
|
|
|
switch chosenMounter {
|
2019-01-16 13:03:38 +00:00
|
|
|
case volumeMounterFuse:
|
2021-09-16 13:47:57 +00:00
|
|
|
return &FuseMounter{}, nil
|
2019-01-16 13:03:38 +00:00
|
|
|
case volumeMounterKernel:
|
2021-09-16 13:47:57 +00:00
|
|
|
return &KernelMounter{}, nil
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown mounter '%s'", chosenMounter)
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
type FuseMounter struct{}
|
2018-04-13 12:29:54 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func mountFuse(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *VolumeOptions) error {
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2018-04-13 12:29:54 +00:00
|
|
|
mountPoint,
|
2019-02-13 12:57:16 +00:00
|
|
|
"-m", volOptions.Monitors,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", util.CephConfigPath,
|
2019-06-25 19:29:17 +00:00
|
|
|
"-n", cephEntityClientPrefix + cr.ID, "--keyfile=" + cr.KeyFile,
|
2018-04-13 12:29:54 +00:00
|
|
|
"-r", volOptions.RootPath,
|
|
|
|
}
|
|
|
|
|
2020-09-21 12:04:25 +00:00
|
|
|
fmo := "nonempty"
|
2019-08-28 09:47:12 +00:00
|
|
|
if volOptions.FuseMountOptions != "" {
|
2020-09-21 12:04:25 +00:00
|
|
|
fmo += "," + strings.TrimSpace(volOptions.FuseMountOptions)
|
2019-08-28 09:47:12 +00:00
|
|
|
}
|
2020-09-21 12:04:25 +00:00
|
|
|
args = append(args, "-o", fmo)
|
2020-01-23 08:24:46 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
if volOptions.FsName != "" {
|
|
|
|
args = append(args, "--client_mds_namespace="+volOptions.FsName)
|
|
|
|
}
|
|
|
|
|
2020-07-22 12:11:41 +00:00
|
|
|
_, stderr, err := util.ExecCommand(ctx, "ceph-fuse", args[:]...)
|
2018-04-13 12:29:54 +00:00
|
|
|
if err != nil {
|
2021-04-08 08:54:46 +00:00
|
|
|
return fmt.Errorf("%w stderr: %s", err, stderr)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:57:24 +00:00
|
|
|
// Parse the output:
|
|
|
|
// We need "starting fuse" meaning the mount is ok
|
|
|
|
// and PID of the ceph-fuse daemon for unmount
|
|
|
|
|
2020-07-22 12:53:22 +00:00
|
|
|
match := fusePidRx.FindSubmatch([]byte(stderr))
|
2020-07-21 05:10:13 +00:00
|
|
|
// validMatchLength is set to 2 as match is expected
|
|
|
|
// to have 2 items, starting fuse and PID of the fuse daemon
|
|
|
|
const validMatchLength = 2
|
|
|
|
if len(match) != validMatchLength {
|
2019-02-14 10:47:16 +00:00
|
|
|
return fmt.Errorf("ceph-fuse failed: %s", stderr)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 19:29:20 +00:00
|
|
|
pid, err := strconv.Atoi(string(match[1]))
|
2019-02-26 16:57:24 +00:00
|
|
|
if err != nil {
|
2020-07-13 03:56:51 +00:00
|
|
|
return fmt.Errorf("failed to parse FUSE daemon PID: %w", err)
|
2019-02-26 16:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fusePidMapMtx.Lock()
|
|
|
|
fusePidMap[mountPoint] = pid
|
|
|
|
fusePidMapMtx.Unlock()
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func (m *FuseMounter) Mount(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx context.Context,
|
|
|
|
mountPoint string,
|
|
|
|
cr *util.Credentials,
|
2021-09-16 13:47:57 +00:00
|
|
|
volOptions *VolumeOptions) error {
|
2019-07-03 10:02:36 +00:00
|
|
|
if err := util.CreateMountPoint(mountPoint); err != nil {
|
2018-04-13 12:29:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
return mountFuse(ctx, mountPoint, cr, volOptions)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func (m *FuseMounter) Name() string { return "Ceph FUSE driver" }
|
2018-07-28 08:24:07 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
type KernelMounter struct{}
|
2018-04-13 12:29:54 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func mountKernel(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *VolumeOptions) error {
|
2019-08-22 17:19:06 +00:00
|
|
|
if err := execCommandErr(ctx, "modprobe", "ceph"); err != nil {
|
2018-04-13 12:29:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
args := []string{
|
2018-04-13 12:29:54 +00:00
|
|
|
"-t", "ceph",
|
|
|
|
fmt.Sprintf("%s:%s", volOptions.Monitors, volOptions.RootPath),
|
|
|
|
mountPoint,
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-06-17 09:13:47 +00:00
|
|
|
|
2019-06-25 19:29:17 +00:00
|
|
|
optionsStr := fmt.Sprintf("name=%s,secretfile=%s", cr.ID, cr.KeyFile)
|
2020-06-17 09:13:47 +00:00
|
|
|
mdsNamespace := ""
|
2019-05-28 19:03:18 +00:00
|
|
|
if volOptions.FsName != "" {
|
2020-06-17 09:13:47 +00:00
|
|
|
mdsNamespace = fmt.Sprintf("mds_namespace=%s", volOptions.FsName)
|
2020-01-23 08:24:46 +00:00
|
|
|
}
|
2020-06-17 09:13:47 +00:00
|
|
|
optionsStr = util.MountOptionsAdd(optionsStr, mdsNamespace, volOptions.KernelMountOptions, netDev)
|
2020-01-23 08:24:46 +00:00
|
|
|
|
2019-05-28 19:03:18 +00:00
|
|
|
args = append(args, "-o", optionsStr)
|
|
|
|
|
2021-04-08 08:54:46 +00:00
|
|
|
_, stderr, err := util.ExecCommand(ctx, "mount", args[:]...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w stderr: %s", err, stderr)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-04-08 08:54:46 +00:00
|
|
|
return err
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func (m *KernelMounter) Mount(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx context.Context,
|
|
|
|
mountPoint string,
|
|
|
|
cr *util.Credentials,
|
2021-09-16 13:47:57 +00:00
|
|
|
volOptions *VolumeOptions) error {
|
2019-07-03 10:02:36 +00:00
|
|
|
if err := util.CreateMountPoint(mountPoint); err != nil {
|
2018-04-13 12:29:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
return mountKernel(ctx, mountPoint, cr, volOptions)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func (m *KernelMounter) Name() string { return "Ceph kernel client" }
|
2018-07-28 08:24:07 +00:00
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func BindMount(ctx context.Context, from, to string, readOnly bool, mntOptions []string) error {
|
2019-06-06 12:18:11 +00:00
|
|
|
mntOptionSli := strings.Join(mntOptions, ",")
|
2019-08-22 17:19:06 +00:00
|
|
|
if err := execCommandErr(ctx, "mount", "-o", mntOptionSli, from, to); err != nil {
|
2020-12-08 13:20:22 +00:00
|
|
|
return fmt.Errorf("failed to bind-mount %s to %s: %w", from, to, err)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if readOnly {
|
2020-06-17 09:13:47 +00:00
|
|
|
mntOptionSli = util.MountOptionsAdd(mntOptionSli, "remount")
|
2019-08-22 17:19:06 +00:00
|
|
|
if err := execCommandErr(ctx, "mount", "-o", mntOptionSli, to); err != nil {
|
2020-12-08 13:20:22 +00:00
|
|
|
return fmt.Errorf("failed read-only remount of %s: %w", to, err)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
func UnmountVolume(ctx context.Context, mountPoint string) error {
|
2021-03-17 06:18:36 +00:00
|
|
|
if _, stderr, err := util.ExecCommand(ctx, "umount", mountPoint); err != nil {
|
|
|
|
err = fmt.Errorf("%w stderr: %s", err, stderr)
|
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf("umount: %s: not mounted", mountPoint)) ||
|
2019-12-17 09:07:46 +00:00
|
|
|
strings.Contains(err.Error(), "No such file or directory") {
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-02-26 16:57:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fusePidMapMtx.Lock()
|
|
|
|
pid, ok := fusePidMap[mountPoint]
|
|
|
|
if ok {
|
|
|
|
delete(fusePidMap, mountPoint)
|
|
|
|
}
|
|
|
|
fusePidMapMtx.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "failed to find process %d: %v", pid, err)
|
2019-02-26 16:57:24 +00:00
|
|
|
} else {
|
|
|
|
if _, err = p.Wait(); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "%d is not a child process: %v", pid, err)
|
2019-02-26 16:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|