-
Notifications
You must be signed in to change notification settings - Fork 5
/
mounter.go
242 lines (201 loc) · 6.67 KB
/
mounter.go
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2018 The Kubernetes Authors.
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 main
import (
"fmt"
"os"
"strings"
"github.com/golang/glog"
"ecfs/log"
"github.com/elastifile/efaasclient/efaasapi"
"github.com/elastifile/emanage-go/src/emanage-client"
"github.com/elastifile/errors"
)
func mountNfs(args ...string) error {
out, err := execCommand("mount", args[:]...)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Mount failed. Output: %v", string(out)), 0)
}
glog.V(log.DEBUG).Infof("Command output: %v", string(out))
return nil
}
func getNfsAddress() (string, error) {
settings, _, err := GetPluginSettings()
if err != nil {
return "", errors.WrapPrefix(err, "Failed to get plugin settings", 0)
}
return settings[nfsAddress], nil
}
func getNfsExportEcfs(volId volumeHandleType) (nfsExport string, err error) {
nfsAddr, err := getNfsAddress()
if err != nil {
err = errors.WrapPrefix(err, fmt.Sprintf("Failed to get ECFS export for filesystem %v", volId), 0)
return
}
return fmt.Sprintf("%v:%v/%v", nfsAddr, string(volId), volumeExportName), nil
}
func getNfsExportEfaas(volId volumeHandleType) (nfsExport string, err error) {
client := newEfaasClient()
fs, err := client.GetFilesystemByName(efaasGetInstanceName(), string(volId))
if err != nil {
err = errors.WrapPrefix(err, fmt.Sprintf("Failed to get eFaaS filesystem %v", volId), 0)
return
}
if len(fs.Exports) == 0 {
err = errors.Errorf("eFaaS filesystem %v has no exports", volId)
return
}
nfsExport = fs.Exports[0].NfsMountPoint
return
}
func mountEcfs(mountPoint string, volId volumeHandleType, mountFlags []string) (err error) {
var nfsExport string
glog.V(log.INFO).Infof("ecfs: Mounting volume %v on %v", volId, mountPoint)
if IsEFAAS() {
nfsExport, err = getNfsExportEfaas(volId)
} else {
nfsExport, err = getNfsExportEcfs(volId)
}
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to get NFS export for volume %v", volId), 0)
}
if err = createMountPoint(mountPoint); err != nil {
return err
}
var args []string
if len(mountFlags) > 0 {
args = append(args, "-o", strings.Join(mountFlags, ","))
}
args = append(args,
"-vvv",
"-t", "nfs",
nfsExport,
mountPoint,
)
err = mountNfs(args...)
if err != nil {
if isErrorAlreadyMounted(err) && isWorkaround("b/153705643") {
glog.V(log.DEBUG).Infof("ecfs: Mount point %v is already mounted", mountPoint)
} else {
return errors.WrapPrefix(err, "Failed to mount ECFS export", 0)
}
}
return nil
}
// mountEmsSnapshot creates an EMS snapshot export and mounts it
func mountEmsSnapshot(mountPoint string, snapshot *emanage.Snapshot) error {
if err := createMountPoint(mountPoint); err != nil {
return errors.Wrap(err, 0)
}
glog.V(log.DETAILED_INFO).Infof("ecfs: Mounting snapshot %v on %v", snapshot.Name, mountPoint)
snapExportOptions := &volumeOptions{
UserMapping: emanage.UserMappingNone,
}
var emsClient emanageClient
snapshotExportPath, err := getSnapshotExportPath(emsClient.GetClient(), snapshot.ID)
if err != nil {
if isErrorDoesNotExist(err) { // Create export
_, err = createExportOnSnapshot(emsClient.GetClient(), snapshot, snapExportOptions)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to create export for snapshot %v (%v)",
snapshot.ID, snapshot.Name), 0)
}
snapshotExportPath, err = getSnapshotExportPath(emsClient.GetClient(), snapshot.ID)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to get newly created snapshot export path "+
"by snapshot id %v (%v)", snapshot.ID, snapshot.Name), 0)
}
} else {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to get snapshot export path by snapshot id %v (%v)",
snapshot.ID, snapshot.Name), 0)
}
}
nfsAddr, err := getNfsAddress()
if err != nil {
return errors.WrapPrefix(err, "Failed to mount ECFS export", 0)
}
args := []string{
"-vvv",
"-t", "nfs",
"-o", "ro,nolock,vers=3",
fmt.Sprintf("%v:%v", nfsAddr, snapshotExportPath),
mountPoint,
}
err = mountNfs(args...)
if err != nil {
return errors.WrapPrefix(err, "Failed to mount ECFS snapshot export", 0)
}
return nil
}
// mountEfaasSnapshot creates an eFaaS snapshot export and mounts it
func mountEfaasSnapshot(mountPoint string, snapName string) error {
if err := createMountPoint(mountPoint); err != nil {
return errors.Wrap(err, 0)
}
glog.V(log.DETAILED_INFO).Infof("ecfs: Mounting snapshot %v on %v", snapName, mountPoint)
client := newEfaasClient()
share, err := client.GetShare(efaasGetInstanceName(), snapName)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to get share for snapshot %v", snapName), 0)
}
exportPath := share.NfsMountPoint
if isWorkaround("'default' in export path") {
var fs efaasapi.DataContainer
fs, err = client.GetFilesystemBySnapshotName(efaasGetInstanceName(), snapName)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to get filesystem by snapshot name %v", snapName), 0)
}
exportPath = strings.Replace(exportPath, "default", fs.Name, 1)
}
args := []string{
"-vvv",
"-t", "nfs",
"-o", "ro,nolock,vers=3",
exportPath,
mountPoint,
}
err = mountNfs(args...)
if err != nil {
return errors.WrapPrefix(err, "Failed to mount ECFS snapshot export", 0)
}
return nil
}
func bindMount(from, to string, readOnly bool) error {
if err := execCommandAndValidate("mount", "--bind", from, to); err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("failed to bind-mount %s to %s", from, to), 0)
}
if readOnly {
if err := execCommandAndValidate("mount", "-o", "remount,ro,bind", to); err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("failed read-only remount of %s", to), 0)
}
}
return nil
}
func createMountPoint(mountPoint string) error {
return os.MkdirAll(mountPoint, 0750)
}
func unmount(mountPoint string) error {
return execCommandAndValidate("umount", mountPoint)
}
func unmountAndCleanup(mountPoint string) (err error) {
err = unmount(mountPoint)
if err != nil {
return errors.WrapPrefix(err, fmt.Sprintf("Failed to unmount %v", mountPoint), 0)
}
err = os.Remove(mountPoint)
if err != nil {
err = errors.WrapPrefix(err, fmt.Sprintf("Failed to delete mount dir %v", mountPoint), 0)
return
}
return
}