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-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"
|
|
|
|
"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")
|
|
|
|
|
|
|
|
if fuseMounterProbe.Run() == nil {
|
2019-01-16 13:03:38 +00:00
|
|
|
availableMounters = append(availableMounters, volumeMounterFuse)
|
2018-08-14 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kernelMounterProbe.Run() == nil {
|
2019-01-16 13:03:38 +00:00
|
|
|
availableMounters = append(availableMounters, volumeMounterKernel)
|
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-03-04 04:10:44 +00:00
|
|
|
mount(mountPoint string, cr *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
|
|
|
|
|
|
|
|
if wantMounter == "" {
|
|
|
|
wantMounter = DefaultVolumeMounter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it's available
|
|
|
|
|
|
|
|
var chosenMounter string
|
|
|
|
|
|
|
|
for _, availMounter := range availableMounters {
|
|
|
|
if chosenMounter == "" {
|
|
|
|
if availMounter == wantMounter {
|
|
|
|
chosenMounter = wantMounter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chosenMounter == "" {
|
|
|
|
// Otherwise pick whatever is left
|
|
|
|
chosenMounter = availableMounters[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-04 04:10:44 +00:00
|
|
|
func mountFuse(mountPoint string, cr *credentials, volOptions *volumeOptions) error {
|
2018-04-13 12:29:54 +00:00
|
|
|
args := [...]string{
|
|
|
|
mountPoint,
|
2019-02-13 12:57:16 +00:00
|
|
|
"-m", volOptions.Monitors,
|
2019-04-22 21:35:39 +00:00
|
|
|
"-c", util.CephConfigPath,
|
2019-02-14 10:47:16 +00:00
|
|
|
"-n", cephEntityClientPrefix + cr.id, "--key=" + cr.key,
|
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-02-14 10:47:16 +00:00
|
|
|
_, stderr, err := execCommand("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-03-04 04:10:44 +00:00
|
|
|
func (m *fuseMounter) mount(mountPoint string, cr *credentials, volOptions *volumeOptions) error {
|
2018-04-13 12:29:54 +00:00
|
|
|
if err := createMountPoint(mountPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:10:44 +00:00
|
|
|
return mountFuse(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-03-04 04:10:44 +00:00
|
|
|
func mountKernel(mountPoint string, cr *credentials, volOptions *volumeOptions) error {
|
2019-02-14 10:47:16 +00:00
|
|
|
if err := execCommandErr("modprobe", "ceph"); err != nil {
|
2018-04-13 12:29:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-14 10:47:16 +00:00
|
|
|
return execCommandErr("mount",
|
2018-04-13 12:29:54 +00:00
|
|
|
"-t", "ceph",
|
|
|
|
fmt.Sprintf("%s:%s", volOptions.Monitors, volOptions.RootPath),
|
|
|
|
mountPoint,
|
2019-02-14 10:47:16 +00:00
|
|
|
"-o", fmt.Sprintf("name=%s,secret=%s", cr.id, cr.key),
|
2018-04-13 12:29:54 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:10:44 +00:00
|
|
|
func (m *kernelMounter) mount(mountPoint string, cr *credentials, volOptions *volumeOptions) error {
|
2018-04-13 12:29:54 +00:00
|
|
|
if err := createMountPoint(mountPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:10:44 +00:00
|
|
|
return mountKernel(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" }
|
|
|
|
|
2018-04-13 12:29:54 +00:00
|
|
|
func bindMount(from, to string, readOnly bool) error {
|
2019-02-14 10:47:16 +00:00
|
|
|
if err := execCommandErr("mount", "--bind", 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-02-14 10:47:16 +00:00
|
|
|
if err := execCommandErr("mount", "-o", "remount,ro,bind", 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmountVolume(mountPoint string) error {
|
2019-02-26 16:57:24 +00:00
|
|
|
if err := execCommandErr("umount", mountPoint); err != nil {
|
|
|
|
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 {
|
|
|
|
klog.Warningf("failed to find process %d: %v", pid, err)
|
|
|
|
} else {
|
|
|
|
if _, err = p.Wait(); err != nil {
|
|
|
|
klog.Warningf("%d is not a child process: %v", pid, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-04-13 12:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMountPoint(root string) error {
|
|
|
|
return os.MkdirAll(root, 0750)
|
|
|
|
}
|