aboutsummaryrefslogtreecommitdiff
path: root/pkg/driver/mounter_seaweedfs.go
blob: 51adfdb561ded8cab1ad617d97fb9eb965311a57 (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 (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"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))
	}

	// CacheDir 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")
	}

	// Handle volumeCapacity from controllerserver.go:51
	if value, ok := seaweedFs.volContext["volumeCapacity"]; ok{
		capacityMB := parseVolumeCapacity(value)
		args = append(args, fmt.Sprintf("-collectionQuotaMB=%d", capacityMB))
	}

	// Initial values for override-able args
	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,
	}

	// 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",
	}

	//	Merge volContext into argsMap with key-mapping
	for arg, value := range seaweedFs.volContext {
		if(arg == "volumeCapacity"){	// Ignore volumeCapacity, not the nicest solution like this :/
			continue
		}

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

		// 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()
	_ = os.RemoveAll(su.cacheDir)
	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 parseVolumeCapacity(volumeCapacity string) int64 {
	var capacity int64

	if vCap, err := strconv.ParseInt(volumeCapacity, 10, 64); err != nil {
		glog.Errorf("volumeCapacity %s can not be parsed to Int64, err is: %v", volumeCapacity, err)
	} else {
		capacity = vCap
	}

	capacityMB := capacity / 1024 / 1024
	return capacityMB
}