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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
"github.com/ceph/ceph-csi/pkg/util"
|
|
|
|
|
2019-02-26 16:57:24 +00:00
|
|
|
"k8s.io/klog"
|
2018-04-13 12:29:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-16 13:03:38 +00:00
|
|
|
volumeMounterFuse = "fuse"
|
|
|
|
volumeMounterKernel = "kernel"
|
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
|
|
|
|
|
|
|
// maps a mountpoint to PID of its FUSE daemon
|
|
|
|
fusePidMap = make(map[string]int)
|
|
|
|
fusePidMapMtx sync.Mutex
|
2019-02-27 19:29:20 +00:00
|
|
|
|
|
|
|
fusePidRx = regexp.MustCompile(`(?m)^ceph-fuse\[(.+)\]: starting fuse$`)
|
2018-08-14 14:48:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Load available ceph mounters installed on system into availableMounters
|
|
|
|
// Called from driver.go's Run()
|
|
|
|
func loadAvailableMounters() 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()
|
|
|
|
if err == nil {
|
|
|
|
kernelVersion, _, err := execCommand(context.TODO(), "uname", "-r")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vers := strings.Split(string(kernelVersion), ".")
|
|
|
|
majorVers, err := strconv.Atoi(vers[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
minorVers, err := strconv.Atoi(vers[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if majorVers >= 4 && minorVers >= 17 {
|
|
|
|
klog.Infof("loaded mounter: %s", volumeMounterKernel)
|
|
|
|
availableMounters = append(availableMounters, volumeMounterKernel)
|
|
|
|
} else {
|
|
|
|
klog.Infof("kernel version < 4.17 might not support quota feature, hence not loading kernel client")
|
|
|
|
}
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 04:43:23 +00:00
|
|
|
if fuseMounterProbe.Run() == nil {
|
|
|
|
klog.Infof("loaded mounter: %s", volumeMounterFuse)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
type volumeMounter interface {
|
2019-08-22 17:19:06 +00:00
|
|
|
mount(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *volumeOptions) error
|
2018-07-28 08:24:07 +00:00
|
|
|
name() string
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 14:48:30 +00:00
|
|
|
func newMounter(volOptions *volumeOptions) (volumeMounter, error) {
|
|
|
|
// 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
|
|
|
|
break
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chosenMounter == "" {
|
|
|
|
// Otherwise pick whatever is left
|
|
|
|
chosenMounter = availableMounters[0]
|
2019-08-25 04:43:23 +00:00
|
|
|
klog.Infof("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:
|
2018-08-14 14:48:30 +00:00
|
|
|
return &fuseMounter{}, nil
|
2019-01-16 13:03:38 +00:00
|
|
|
case volumeMounterKernel:
|
2018-08-14 14:48:30 +00:00
|
|
|
return &kernelMounter{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown mounter '%s'", chosenMounter)
|
|
|
|
}
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
type fuseMounter struct{}
|
|
|
|
|
2019-08-22 17:19:06 +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,
|
2018-07-31 05:41:14 +00:00
|
|
|
"-o", "nonempty",
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 09:47:12 +00:00
|
|
|
if volOptions.FuseMountOptions != "" {
|
|
|
|
args = append(args, ","+volOptions.FuseMountOptions)
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
if volOptions.FsName != "" {
|
|
|
|
args = append(args, "--client_mds_namespace="+volOptions.FsName)
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
_, stderr, err := execCommand(ctx, "ceph-fuse", args[:]...)
|
2018-04-13 12:29:54 +00:00
|
|
|
if err != nil {
|
2019-02-14 10:47:16 +00:00
|
|
|
return err
|
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
|
|
|
|
|
2019-02-27 19:29:20 +00:00
|
|
|
match := fusePidRx.FindSubmatch(stderr)
|
|
|
|
if len(match) != 2 {
|
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 {
|
|
|
|
return fmt.Errorf("failed to parse FUSE daemon PID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fusePidMapMtx.Lock()
|
|
|
|
fusePidMap[mountPoint] = pid
|
|
|
|
fusePidMapMtx.Unlock()
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
func (m *fuseMounter) mount(ctx context.Context, mountPoint string, cr *util.Credentials, 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
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func (m *fuseMounter) name() string { return "Ceph FUSE driver" }
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
type kernelMounter struct{}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
func mountKernel(ctx context.Context, mountPoint string, cr *util.Credentials, volOptions *volumeOptions) error {
|
|
|
|
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
|
|
|
}
|
2019-06-25 19:29:17 +00:00
|
|
|
optionsStr := fmt.Sprintf("name=%s,secretfile=%s", cr.ID, cr.KeyFile)
|
2019-05-28 19:03:18 +00:00
|
|
|
if volOptions.FsName != "" {
|
2019-06-10 06:48:41 +00:00
|
|
|
optionsStr += fmt.Sprintf(",mds_namespace=%s", volOptions.FsName)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2019-08-28 09:47:12 +00:00
|
|
|
if volOptions.KernelMountOptions != "" {
|
|
|
|
optionsStr += fmt.Sprintf(",%s", volOptions.KernelMountOptions)
|
|
|
|
}
|
2019-05-28 19:03:18 +00:00
|
|
|
args = append(args, "-o", optionsStr)
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
return execCommandErr(ctx, "mount", args[:]...)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
func (m *kernelMounter) mount(ctx context.Context, mountPoint string, cr *util.Credentials, 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
|
|
|
}
|
|
|
|
|
2018-07-28 08:24:07 +00:00
|
|
|
func (m *kernelMounter) name() string { return "Ceph kernel client" }
|
|
|
|
|
2019-08-22 17:19:06 +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 {
|
2018-07-28 08:24:07 +00:00
|
|
|
return fmt.Errorf("failed to bind-mount %s to %s: %v", from, to, err)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if readOnly {
|
2019-06-10 06:48:41 +00:00
|
|
|
mntOptionSli += ",remount"
|
2019-08-22 17:19:06 +00:00
|
|
|
if err := execCommandErr(ctx, "mount", "-o", mntOptionSli, to); err != nil {
|
2018-07-28 08:24:07 +00:00
|
|
|
return fmt.Errorf("failed read-only remount of %s: %v", to, err)
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:19:06 +00:00
|
|
|
func unmountVolume(ctx context.Context, mountPoint string) error {
|
|
|
|
if err := execCommandErr(ctx, "umount", mountPoint); err != nil {
|
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 {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Warningf(util.Log(ctx, "failed to find process %d: %v"), pid, err)
|
2019-02-26 16:57:24 +00:00
|
|
|
} else {
|
|
|
|
if _, err = p.Wait(); err != nil {
|
2019-08-22 17:19:06 +00:00
|
|
|
klog.Warningf(util.Log(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
|
|
|
}
|