2015-04-07 17:22:23 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-04-07 17:22:23 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2016-07-13 14:06:24 +00:00
|
|
|
// diskManager interface and diskSetup/TearDown functions abstract commonly used procedures to setup a block volume
|
2016-03-23 05:12:21 +00:00
|
|
|
// rbd volume implements diskManager, calls diskSetup when creating a volume, and calls diskTearDown inside volume unmounter.
|
2015-04-07 17:22:23 +00:00
|
|
|
// TODO: consolidate, refactor, and share diskManager among iSCSI, GCE PD, and RBD
|
|
|
|
//
|
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2015-08-05 22:05:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2015-12-18 18:57:25 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2015-04-07 17:22:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Abstract interface to disk operations.
|
|
|
|
type diskManager interface {
|
|
|
|
MakeGlobalPDName(disk rbd) string
|
|
|
|
// Attaches the disk to the kubelet's host machine.
|
2016-03-23 05:12:21 +00:00
|
|
|
AttachDisk(disk rbdMounter) error
|
2015-04-07 17:22:23 +00:00
|
|
|
// Detaches the disk from the kubelet's host machine.
|
2016-03-23 05:12:21 +00:00
|
|
|
DetachDisk(disk rbdUnmounter, mntPath string) error
|
2016-08-18 20:50:59 +00:00
|
|
|
// Creates a rbd image
|
2016-11-18 20:58:56 +00:00
|
|
|
CreateImage(provisioner *rbdVolumeProvisioner) (r *v1.RBDVolumeSource, volumeSizeGB int, err error)
|
2016-08-18 20:50:59 +00:00
|
|
|
// Deletes a rbd image
|
|
|
|
DeleteImage(deleter *rbdVolumeDeleter) error
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// utility to mount a disk based filesystem
|
2017-06-21 07:13:36 +00:00
|
|
|
func diskSetUp(manager diskManager, b rbdMounter, volPath string, mounter mount.Interface, fsGroup *int64) error {
|
2015-07-24 09:20:42 +00:00
|
|
|
globalPDPath := manager.MakeGlobalPDName(*b.rbd)
|
2015-04-07 17:22:23 +00:00
|
|
|
// TODO: handle failed mounts here.
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
|
2015-04-07 17:22:23 +00:00
|
|
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
glog.Errorf("cannot validate mountpoint: %s", volPath)
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if !notMnt {
|
2015-04-07 17:22:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-24 09:20:42 +00:00
|
|
|
if err := manager.AttachDisk(b); err != nil {
|
2015-04-07 17:22:23 +00:00
|
|
|
glog.Errorf("failed to attach disk")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(volPath, 0750); err != nil {
|
|
|
|
glog.Errorf("failed to mkdir:%s", volPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Perform a bind mount to the full path to allow duplicate mounts of the same disk.
|
|
|
|
options := []string{"bind"}
|
2015-10-30 20:25:36 +00:00
|
|
|
if (&b).GetAttributes().ReadOnly {
|
2015-04-07 17:22:23 +00:00
|
|
|
options = append(options, "ro")
|
|
|
|
}
|
2017-02-21 18:19:48 +00:00
|
|
|
mountOptions := volume.JoinMountOptions(b.mountOptions, options)
|
|
|
|
err = mounter.Mount(globalPDPath, volPath, "", mountOptions)
|
2015-04-07 17:22:23 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to bind mount:%s", globalPDPath)
|
|
|
|
return err
|
|
|
|
}
|
2017-08-22 07:10:01 +00:00
|
|
|
glog.V(3).Infof("rbd: successfully bind mount %s to %s with options %v", globalPDPath, volPath, mountOptions)
|
2015-12-18 18:57:25 +00:00
|
|
|
|
|
|
|
if !b.ReadOnly {
|
|
|
|
volume.SetVolumeOwnership(&b, fsGroup)
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:22:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility to tear down a disk based filesystem
|
2016-03-23 05:12:21 +00:00
|
|
|
func diskTearDown(manager diskManager, c rbdUnmounter, volPath string, mounter mount.Interface) error {
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
|
2015-04-07 17:22:23 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("cannot validate mountpoint %s", volPath)
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if notMnt {
|
2015-04-07 17:22:23 +00:00
|
|
|
return os.Remove(volPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err := mount.GetMountRefs(mounter, volPath)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to get reference count %s", volPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mounter.Unmount(volPath); err != nil {
|
|
|
|
glog.Errorf("failed to umount %s", volPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If len(refs) is 1, then all bind mounts have been removed, and the
|
|
|
|
// remaining reference is the global mount. It is safe to detach.
|
|
|
|
if len(refs) == 1 {
|
|
|
|
mntPath := refs[0]
|
2015-07-24 09:20:42 +00:00
|
|
|
if err := manager.DetachDisk(c, mntPath); err != nil {
|
2015-04-07 17:22:23 +00:00
|
|
|
glog.Errorf("failed to detach disk from %s", mntPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, mntErr := mounter.IsLikelyNotMountPoint(volPath)
|
2015-04-07 17:22:23 +00:00
|
|
|
if mntErr != nil {
|
2015-04-16 23:49:53 +00:00
|
|
|
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
|
2015-04-07 17:22:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if notMnt {
|
2015-04-07 17:22:23 +00:00
|
|
|
if err := os.Remove(volPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|