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-11-07 14:28:59 +00:00
|
|
|
"golang.org/x/sys/unix"
|
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
|
|
|
)
|
|
|
|
|
2019-11-07 14:28:59 +00:00
|
|
|
// Version checking the running kernel and comparing it to known versions that
|
|
|
|
// have support for quota. Distributors of enterprise Linux have backported
|
|
|
|
// quota support to previous versions. This function checks if the running
|
|
|
|
// kernel is one of the versions that have the feature/fixes backported.
|
|
|
|
//
|
|
|
|
// `uname -r` (or Uname().Utsname.Release has a format like 1.2.3-rc.vendor
|
|
|
|
// This can be slit up in the following components:
|
|
|
|
// - version (1)
|
|
|
|
// - patchlevel (2)
|
|
|
|
// - sublevel (3) - optional, defaults to 0
|
|
|
|
// - extraversion (rc) - optional, matching integers only
|
|
|
|
// - distribution (.vendor) - optional, match against whole `uname -r` string
|
|
|
|
//
|
|
|
|
// For matching multiple versions, the kernelSupport type contains a backport
|
|
|
|
// bool, which will cause matching
|
|
|
|
// version+patchlevel+sublevel+(>=extraversion)+(~distribution)
|
|
|
|
//
|
|
|
|
// In case the backport bool is false, a simple check for higher versions than
|
|
|
|
// version+patchlevel+sublevel is done.
|
|
|
|
func kernelSupportsQuota(release string) bool {
|
|
|
|
type kernelSupport struct {
|
|
|
|
version int
|
|
|
|
patchlevel int
|
|
|
|
sublevel int
|
|
|
|
extraversion int // prefix of the part after the first "-"
|
|
|
|
distribution string // component of full extraversion
|
|
|
|
backport bool // backports have a fixed version/patchlevel/sublevel
|
|
|
|
}
|
|
|
|
|
|
|
|
quotaSupport := []kernelSupport{
|
|
|
|
{4, 17, 0, 0, "", false}, // standard 4.17+ versions
|
|
|
|
{3, 10, 0, 1062, ".el7", true}, // RHEL-7.7
|
|
|
|
}
|
|
|
|
|
|
|
|
vers := strings.Split(strings.SplitN(release, "-", 2)[0], ".")
|
|
|
|
version, err := strconv.Atoi(vers[0])
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to parse version from %s: %v", release, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
patchlevel, err := strconv.Atoi(vers[1])
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to parse patchlevel from %s: %v", release, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sublevel := 0
|
|
|
|
if len(vers) >= 3 {
|
|
|
|
sublevel, err = strconv.Atoi(vers[2])
|
|
|
|
if err != nil {
|
|
|
|
klog.Errorf("failed to parse sublevel from %s: %v", release, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extra := strings.SplitN(release, "-", 2)
|
|
|
|
extraversion := 0
|
|
|
|
if len(extra) == 2 {
|
|
|
|
// ignore errors, 1st component of extraversion does not need to be an int
|
|
|
|
extraversion, err = strconv.Atoi(strings.Split(extra[1], ".")[0])
|
|
|
|
if err != nil {
|
|
|
|
// "go lint" wants err to be checked...
|
|
|
|
extraversion = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare running kernel against known versions
|
|
|
|
for _, kernel := range quotaSupport {
|
|
|
|
if !kernel.backport {
|
|
|
|
// deal with the default case(s), find >= match for version, patchlevel, sublevel
|
|
|
|
if version > kernel.version || (version == kernel.version && patchlevel > kernel.patchlevel) ||
|
|
|
|
(version == kernel.version && patchlevel == kernel.patchlevel && sublevel >= kernel.sublevel) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// specific backport, match distribution initially
|
|
|
|
if !strings.Contains(release, kernel.distribution) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// strict match version, patchlevel, sublevel, and >= match extraversion
|
|
|
|
if version == kernel.version && patchlevel == kernel.patchlevel &&
|
|
|
|
sublevel == kernel.sublevel && extraversion >= kernel.extraversion {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
klog.Errorf("kernel %s does not support quota", release)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-14 14:48:30 +00:00
|
|
|
// Load available ceph mounters installed on system into availableMounters
|
|
|
|
// Called from driver.go's Run()
|
2019-10-10 10:15:44 +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()
|
|
|
|
if err == nil {
|
2019-11-07 14:28:59 +00:00
|
|
|
// fetch the current running kernel info
|
|
|
|
utsname := unix.Utsname{}
|
|
|
|
err := unix.Uname(&utsname)
|
2019-08-25 04:43:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-07 14:28:59 +00:00
|
|
|
release := string(utsname.Release[:64])
|
2019-10-10 10:15:44 +00:00
|
|
|
|
2019-11-07 14:28:59 +00:00
|
|
|
if conf.ForceKernelCephFS || kernelSupportsQuota(release) {
|
2019-08-25 04:43:23 +00:00
|
|
|
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-12-17 09:07:46 +00:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf("exit status 32: umount: %s: not mounted", mountPoint)) ||
|
|
|
|
strings.Contains(err.Error(), "No such file or directory") {
|
|
|
|
return 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
|
|
|
}
|