2015-03-13 21:31:13 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-13 21:31:13 +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 iscsi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:05:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2015-03-13 21:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Abstract interface to disk operations.
|
|
|
|
type diskManager interface {
|
|
|
|
MakeGlobalPDName(disk iscsiDisk) string
|
|
|
|
// Attaches the disk to the kubelet's host machine.
|
2015-07-24 08:55:56 +00:00
|
|
|
AttachDisk(b iscsiDiskBuilder) error
|
2015-03-13 21:31:13 +00:00
|
|
|
// Detaches the disk from the kubelet's host machine.
|
2015-07-24 08:55:56 +00:00
|
|
|
DetachDisk(disk iscsiDiskCleaner, mntPath string) error
|
2015-03-13 21:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// utility to mount a disk based filesystem
|
2015-07-24 08:55:56 +00:00
|
|
|
func diskSetUp(manager diskManager, b iscsiDiskBuilder, volPath string, mounter mount.Interface) error {
|
|
|
|
globalPDPath := manager.MakeGlobalPDName(*b.iscsiDisk)
|
2015-03-13 21:31:13 +00:00
|
|
|
// TODO: handle failed mounts here.
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
|
2015-03-13 21:31:13 +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-03-13 21:31:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-24 08:55:56 +00:00
|
|
|
if err := manager.AttachDisk(b); err != nil {
|
2015-03-13 21:31:13 +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.
|
2015-04-03 01:08:04 +00:00
|
|
|
options := []string{"bind"}
|
2015-07-24 08:55:56 +00:00
|
|
|
if b.readOnly {
|
2015-04-03 01:08:04 +00:00
|
|
|
options = append(options, "ro")
|
2015-03-13 21:31:13 +00:00
|
|
|
}
|
2015-04-03 01:08:04 +00:00
|
|
|
err = mounter.Mount(globalPDPath, volPath, "", options)
|
2015-03-13 21:31:13 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("failed to bind mount:%s", globalPDPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility to tear down a disk based filesystem
|
2015-07-24 08:55:56 +00:00
|
|
|
func diskTearDown(manager diskManager, c iscsiDiskCleaner, volPath string, mounter mount.Interface) error {
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, err := mounter.IsLikelyNotMountPoint(volPath)
|
2015-03-13 21:31:13 +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-03-13 21:31:13 +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
|
|
|
|
}
|
2015-04-03 01:08:04 +00:00
|
|
|
if err := mounter.Unmount(volPath); err != nil {
|
|
|
|
glog.Errorf("failed to unmount %s", volPath)
|
2015-03-13 21:31:13 +00:00
|
|
|
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 08:55:56 +00:00
|
|
|
if err := manager.DetachDisk(c, mntPath); err != nil {
|
2015-03-13 21:31:13 +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-03-13 21:31:13 +00:00
|
|
|
if mntErr != nil {
|
2015-04-16 23:49:53 +00:00
|
|
|
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
|
2015-03-13 21:31:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if notMnt {
|
2015-03-13 21:31:13 +00:00
|
|
|
if err := os.Remove(volPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|