aboutsummaryrefslogtreecommitdiff
path: root/pkg/driver/mounter_seaweedfs.go
blob: 56ba98b5da9a5fcb44d77ca3edc68030c44ca96f (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package driver

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/seaweedfs/seaweedfs-csi-driver/pkg/datalocality"
	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/util"
)

// Implements Mounter
type seaweedFsMounter struct {
	volumeID   string
	path       string
	collection string
	readOnly   bool
	driver     *SeaweedFsDriver
	volContext map[string]string
}

type seaweedFsUnmounter struct {
	unmounter Unmounter
	cacheDir  string
}

const (
	seaweedFsCmd = "weed"
)

func newSeaweedFsMounter(volumeID string, path string, collection string, readOnly bool, driver *SeaweedFsDriver, volContext map[string]string) (Mounter, error) {
	return &seaweedFsMounter{
		volumeID:   volumeID,
		path:       path,
		collection: collection,
		readOnly:   readOnly,
		driver:     driver,
		volContext: volContext,
	}, nil
}

func (seaweedFs *seaweedFsMounter) Mount(target string) (Unmounter, error) {
	glog.V(0).Infof("mounting %v %s to %s", seaweedFs.driver.filers, seaweedFs.path, target)

	var filers []string
	for _, address := range seaweedFs.driver.filers {
		filers = append(filers, string(address))
	}

	// CacheDirForRead should be always defined - we use temp dir in case it is not defined
	// we need to use predictable cache path, because we need to clean it up on unstage
	cacheDir := filepath.Join(seaweedFs.driver.CacheDir, seaweedFs.volumeID)

	// Final args
	args := []string{
		"-logtostderr=true",
		"mount",
		"-dirAutoCreate=true",
		"-umask=000",
		fmt.Sprintf("-dir=%s", target),
		fmt.Sprintf("-localSocket=%s", GetLocalSocket(seaweedFs.volumeID)),
		fmt.Sprintf("-cacheDir=%s", cacheDir),
	}

	if seaweedFs.readOnly {
		args = append(args, "-readOnly")
	}

	// Values for override-able args
	//  Whitelist for merging with volContext
	argsMap := map[string]string{
		"collection":         seaweedFs.collection,
		"filer":              strings.Join(filers, ","),
		"filer.path":         seaweedFs.path,
		"cacheCapacityMB":    fmt.Sprint(seaweedFs.driver.CacheCapacityMB),
		"concurrentWriters":  fmt.Sprint(seaweedFs.driver.ConcurrentWriters),
		"map.uid":            seaweedFs.driver.UidMap,
		"map.gid":            seaweedFs.driver.GidMap,
		"disk":               "",
		"dataCenter":         "",
		"replication":        "",
		"ttl":                "",
		"chunkSizeLimitMB":   "",
		"volumeServerAccess": "",
		"readRetryTime":      "",
	}

	// Handle DataLocality
	dataLocality := seaweedFs.driver.DataLocality
	// Try to override when set in context
	if dataLocalityStr, ok := seaweedFs.volContext["dataLocality"]; ok {
		// Convert to enum
		dataLocalityRes, ok := datalocality.FromString(dataLocalityStr)
		if !ok {
			glog.Warning("volumeContext 'dataLocality' invalid")
		} else {
			dataLocality = dataLocalityRes
		}
	}
	if err := CheckDataLocality(&dataLocality, &seaweedFs.driver.DataCenter); err != nil {
		return nil, err
	}
	// Settings based on type
	switch dataLocality {
	case datalocality.Write_preferLocalDc:
		argsMap["dataCenter"] = seaweedFs.driver.DataCenter
	}

	// volContext-parameter -> mount-arg
	parameterArgMap := map[string]string{
		"uidMap":    "map.uid",
		"gidMap":    "map.gid",
		"filerPath": "filer.path",
		// volumeContext has "diskType", but mount-option is "disk", converting for backwards compatability
		"diskType": "disk",
	}

	// Explicitly ignored volContext args e.g. handled somewhere else
	ignoreArgs := []string{
		"dataLocality",
	}

	//	Merge volContext into argsMap with key-mapping
	for arg, value := range seaweedFs.volContext {
		if in_arr(ignoreArgs, arg) {
			continue
		}

		// Check if key-mapping exists
		newArg, ok := parameterArgMap[arg]
		if ok {
			arg = newArg
		}

		// Check if arg can be applied
		if _, ok := argsMap[arg]; !ok {
			glog.Warningf("VolumeContext '%s' ignored", arg)
			continue
		}

		// Write to args-map
		argsMap[arg] = value
	}

	// Convert Args-Map to args
	for arg, value := range argsMap {
		if value != "" { // ignore empty values
			args = append(args, fmt.Sprintf("-%s=%s", arg, value))
		}
	}

	u, err := fuseMount(target, seaweedFsCmd, args)
	if err != nil {
		glog.Errorf("mount %v %s to %s: %s", seaweedFs.driver.filers, seaweedFs.path, target, err)
	}

	return &seaweedFsUnmounter{unmounter: u, cacheDir: cacheDir}, err
}

func (su *seaweedFsUnmounter) Unmount() error {
	err := su.unmounter.Unmount()
	err2 := os.RemoveAll(su.cacheDir)
	if err2 != nil {
		glog.Warningf("error removing cache from: %s, err: %v", su.cacheDir, err2)
	}
	return err
}

func GetLocalSocket(volumeID string) string {
	montDirHash := util.HashToInt32([]byte(volumeID))
	if montDirHash < 0 {
		montDirHash = -montDirHash
	}

	socket := fmt.Sprintf("/tmp/seaweedfs-mount-%d.sock", montDirHash)
	return socket
}

func in_arr(arr []string, val string) bool {
	for _, v := range arr {
		if val == v {
			return true
		}
	}
	return false
}