aboutsummaryrefslogtreecommitdiff
path: root/pkg/driver/volume.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/driver/volume.go')
-rw-r--r--pkg/driver/volume.go68
1 files changed, 51 insertions, 17 deletions
diff --git a/pkg/driver/volume.go b/pkg/driver/volume.go
index 38d1308..ac0a80a 100644
--- a/pkg/driver/volume.go
+++ b/pkg/driver/volume.go
@@ -9,11 +9,12 @@ import (
"github.com/seaweedfs/seaweedfs/weed/pb/mount_pb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
+ "k8s.io/mount-utils"
)
type Volume struct {
VolumeId string
- TargetPath string
+ StagedPath string
mounter Mounter
unmounter Unmounter
@@ -30,31 +31,55 @@ func NewVolume(volumeID string, mounter Mounter) *Volume {
}
}
-func (vol *Volume) Publish(targetPath string) error {
+func (vol *Volume) Stage(stagingTargetPath string) error {
// check whether it can be mounted
- if isMnt, err := checkMount(targetPath); err != nil {
+ if isMnt, err := checkMount(stagingTargetPath); err != nil {
return err
} else if isMnt {
// try to unmount before mounting again
- _ = mountutil.Unmount(targetPath)
+ _ = mountutil.Unmount(stagingTargetPath)
}
- if u, err := vol.mounter.Mount(targetPath); err == nil {
- if vol.TargetPath != "" {
- if vol.TargetPath == targetPath {
- glog.Warningf("target path is already set to %s for volume %s", vol.TargetPath, vol.VolumeId)
+ if u, err := vol.mounter.Mount(stagingTargetPath); err == nil {
+ if vol.StagedPath != "" {
+ if vol.StagedPath == stagingTargetPath {
+ glog.Warningf("staged path is already set to %s for volume %s", vol.StagedPath, vol.VolumeId)
} else {
- glog.Warningf("target path is already set to %s and differs from %s for volume %s", vol.TargetPath, targetPath, vol.VolumeId)
+ glog.Warningf("staged path is already set to %s and differs from %s for volume %s", vol.StagedPath, stagingTargetPath, vol.VolumeId)
}
}
- vol.TargetPath = targetPath
+
+ vol.StagedPath = stagingTargetPath
vol.unmounter = u
+
return nil
} else {
return err
}
}
+func (vol *Volume) Publish(stagingTargetPath string, targetPath string, readOnly bool) error {
+ // check whether it can be mounted
+ if isMnt, err := checkMount(targetPath); err != nil {
+ return err
+ } else if isMnt {
+ // maybe already mounted?
+ return nil
+ }
+
+ // Use bind mount to create an alias of the real mount point.
+ mountOptions := []string{"bind"}
+ if readOnly {
+ mountOptions = append(mountOptions, "ro")
+ }
+
+ if err := mountutil.Mount(stagingTargetPath, targetPath, "", mountOptions); err != nil {
+ return err
+ }
+
+ return nil
+}
+
func (vol *Volume) Quota(sizeByte int64) error {
target := fmt.Sprintf("passthrough:///unix://%s", vol.localSocket)
dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
@@ -73,24 +98,33 @@ func (vol *Volume) Quota(sizeByte int64) error {
}
func (vol *Volume) Unpublish(targetPath string) error {
- glog.V(0).Infof("unmounting volume %s from %s", vol.VolumeId, targetPath)
+ // Try unmounting target path and deleting it.
+ if err := mount.CleanupMountPoint(targetPath, mountutil, true); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (vol *Volume) Unstage(stagingTargetPath string) error {
+ glog.V(0).Infof("unmounting volume %s from %s", vol.VolumeId, stagingTargetPath)
if vol.unmounter == nil {
- glog.Errorf("volume is not mounted: %s, path: %s", vol.VolumeId, targetPath)
+ glog.Errorf("volume is not mounted: %s, path: %s", vol.VolumeId, stagingTargetPath)
return nil
}
- if targetPath != vol.TargetPath {
- glog.Warningf("staging path %s differs for volume %s at %s", targetPath, vol.VolumeId, vol.TargetPath)
+ if stagingTargetPath != vol.StagedPath {
+ glog.Warningf("staging path %s differs for volume %s at %s", stagingTargetPath, vol.VolumeId, vol.StagedPath)
}
if err := vol.unmounter.Unmount(); err != nil {
- glog.Errorf("error unmounting volume during unstage: %s, err: %v", targetPath, err)
+ glog.Errorf("error unmounting volume during unstage: %s, err: %v", stagingTargetPath, err)
return err
}
- if err := os.Remove(targetPath); err != nil && !os.IsNotExist(err) {
- glog.Errorf("error removing staging path for volume %s at %s, err: %v", vol.VolumeId, targetPath, err)
+ if err := os.Remove(stagingTargetPath); err != nil && !os.IsNotExist(err) {
+ glog.Errorf("error removing staging path for volume %s at %s, err: %v", vol.VolumeId, stagingTargetPath, err)
return err
}