aboutsummaryrefslogtreecommitdiff
path: root/pkg/driver/volume.go
blob: fd1e18f3b9e29bc115001345f1ff06ef61dd8191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package driver

import (
	"context"
	"fmt"
	"os"

	"github.com/chrislusf/seaweedfs/weed/glog"
	"github.com/chrislusf/seaweedfs/weed/pb/mount_pb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"k8s.io/utils/mount"
)

type Volume struct {
	VolumeId string

	// volume's real mount point
	stagingTargetPath string

	// Target paths to which the volume has been published.
	// These paths are symbolic links to the real mount point.
	// So multiple pods using the same volume can share a mount.
	targetPaths map[string]bool

	mounter Mounter

	// unix socket used to manage volume
	localSocket string
}

func NewVolume(volumeID string, mounter Mounter) *Volume {
	return &Volume{
		VolumeId:    volumeID,
		mounter:     mounter,
		targetPaths: make(map[string]bool),
	}
}

func (vol *Volume) Stage(stagingTargetPath string) error {
	if vol.isStaged() {
		return nil
	}

	// check whether it can be mounted
	if notMnt, err := checkMount(stagingTargetPath); err != nil {
		return err
	} else if !notMnt {
		// maybe already mounted?
		return nil
	}

	if err := vol.mounter.Mount(stagingTargetPath); err != nil {
		return err
	}

	vol.stagingTargetPath = stagingTargetPath
	return nil
}

func (vol *Volume) Publish(targetPath string, readOnly bool) error {
	// check whether it can be mounted
	if notMnt, err := checkMount(targetPath); err != nil {
		return err
	} else if !notMnt {
		// 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")
	}

	mounter := mount.New("")
	if err := mounter.Mount(vol.stagingTargetPath, targetPath, "", mountOptions); err != nil {
		return nil
	}

	vol.targetPaths[targetPath] = true
	return nil
}

func (vol *Volume) Expand(sizeByte int64) error {
	if !vol.isStaged() {
		return nil
	}

	target := fmt.Sprintf("passthrough:///unix://%s", vol.getLocalSocket())
	dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())

	clientConn, err := grpc.Dial(target, dialOption)
	if err != nil {
		return err
	}
	defer clientConn.Close()

	client := mount_pb.NewSeaweedMountClient(clientConn)
	_, err = client.Configure(context.Background(), &mount_pb.ConfigureRequest{
		CollectionCapacity: sizeByte,
	})
	return err
}

func (vol *Volume) Unpublish(targetPath string) error {
	// Check whether the volume is published to the target path.
	if _, ok := vol.targetPaths[targetPath]; !ok {
		glog.Warningf("volume %s hasn't been published to %s", vol.VolumeId, targetPath)
		return nil
	}

	// Try unmounting target path and deleting it.
	mounter := mount.New("")
	if err := mount.CleanupMountPoint(targetPath, mounter, true); err != nil {
		return err
	}

	delete(vol.targetPaths, targetPath)
	return nil
}

func (vol *Volume) Unstage(_ string) error {
	if !vol.isStaged() {
		return nil
	}

	mountPoint := vol.stagingTargetPath
	glog.V(0).Infof("unmounting volume %s from %s", vol.VolumeId, mountPoint)

	if err := fuseUnmount(mountPoint); err != nil {
		return err
	}

	if err := os.Remove(mountPoint); err != nil && !os.IsNotExist(err) {
		return err
	}

	return nil
}

func (vol *Volume) isStaged() bool {
	return vol.stagingTargetPath != ""
}

func (vol *Volume) getLocalSocket() string {
	if vol.localSocket != "" {
		return vol.localSocket
	}

	socket := GetLocalSocket(vol.VolumeId)

	vol.localSocket = socket
	return socket
}