-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
276 lines (231 loc) · 8.86 KB
/
mount.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cmd
import (
"fmt"
"net/http"
_ "net/http/pprof" // profiling server
"os"
"runtime/pprof"
"time"
fuse "bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
"github.com/Sirupsen/logrus"
"github.com/fatih/color"
"github.com/ovh/svfs/config"
"github.com/ovh/svfs/svfs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/xlucas/swift"
)
var (
configError error
debug bool
fs svfs.SVFS
srv *fusefs.Server
profAddr string
cpuProf string
memProf string
cfgFile string
device string
mountpoint string
)
func init() {
configError = config.LoadConfig()
// Logger
formatter := new(logrus.TextFormatter)
formatter.TimestampFormat = time.RFC3339
formatter.FullTimestamp = true
logrus.SetFormatter(formatter)
logrus.SetOutput(os.Stdout)
setFlags()
RootCmd.AddCommand(mountCmd)
}
// mountCmd represents the base command when called without any subcommands
var mountCmd = &cobra.Command{
Use: "mount --device name --mountpoint path",
Short: "Mount object storage as a device",
Long: "Mount object storage either from HubiC or a vanilla Swift access\n" +
"as a device at the given mountpoint.",
Run: func(cmd *cobra.Command, args []string) {
// Debug
if debug {
setDebug()
}
// Config validation
if configError != nil {
yellow := color.New(color.FgYellow).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
logrus.WithField("source", yellow("svfs")).Debugln(cyan("Skipping configuration : ", configError))
}
//Mandatory flags
cmd.MarkPersistentFlagRequired("device")
cmd.MarkPersistentFlagRequired("mountpoint")
// Use config file or ENV var if set
useConfiguration()
// Live profiling
if profAddr != "" {
go func() {
if err := http.ListenAndServe(profAddr, nil); err != nil {
logrus.Fatal(err)
}
}()
}
// CPU profiling
if cpuProf != "" {
createCPUProf(cpuProf)
defer pprof.StopCPUProfile()
}
// Check segment size
if err := checkOptions(); err != nil {
logrus.Fatal(err)
}
// Mount SVFS
c, err := fuse.Mount(mountpoint, mountOptions(device)...)
if err != nil {
goto Err
}
defer c.Close()
// Initialize SVFS
if err = fs.Init(); err != nil {
goto Err
}
// Serve SVFS
srv = fusefs.New(c, nil)
if err = srv.Serve(&fs); err != nil {
goto Err
}
// Check for mount errors
<-c.Ready
// Memory profiling
if memProf != "" {
createMemProf(memProf)
}
if err = c.MountError; err != nil {
goto Err
}
return
Err:
fuse.Unmount(mountpoint)
logrus.Fatal(err)
},
}
func setFlags() {
flags := mountCmd.PersistentFlags()
//Swift options
flags.StringVar(&svfs.SwiftConnection.AuthUrl, "os-auth-url", "https://auth.cloud.ovh.net/v2.0", "Authentification URL")
flags.StringVar(&svfs.TargetContainer, "os-container-name", "", "Container name")
flags.StringVar(&svfs.SwiftConnection.AuthToken, "os-auth-token", "", "Authentification token")
flags.StringVar(&svfs.SwiftConnection.UserName, "os-username", "", "Username")
flags.StringVar(&svfs.SwiftConnection.ApiKey, "os-password", "", "User password")
flags.StringVar(&svfs.SwiftConnection.Region, "os-region-name", "", "Region name")
flags.StringVar(&svfs.SwiftConnection.StorageUrl, "os-storage-url", "", "Storage URL")
flags.StringVar(&svfs.SwiftConnection.Tenant, "os-tenant-name", "", "Tenant name")
flags.IntVar(&svfs.SwiftConnection.AuthVersion, "os-auth-version", 0, "Authentification version, 0 = auto")
flags.DurationVar(&svfs.SwiftConnection.ConnectTimeout, "os-connect-timeout", 15*time.Second, "Swift connection timeout")
flags.DurationVar(&svfs.SwiftConnection.Timeout, "os-request-timeout", 5*time.Minute, "Swift operation timeout")
flags.Uint64Var(&svfs.SegmentSize, "os-segment-size", 256, "Swift segment size in MiB")
flags.StringVar(&svfs.StoragePolicy, "os-storage-policy", "", "Only show containers using this storage policy")
flags.StringVar(&swift.DefaultUserAgent, "user-agent", "svfs/"+svfs.Version, "Default User-Agent")
//HubiC options
flags.StringVar(&svfs.HubicAuthorization, "hubic-authorization", "", "hubiC authorization code")
flags.StringVar(&svfs.HubicRefreshToken, "hubic-refresh-token", "", "hubiC refresh token")
flags.BoolVar(&svfs.HubicTimes, "hubic-times", false, "Use file times set by hubiC synchronization clients")
// Permissions
flags.Uint64Var(&svfs.DefaultUID, "default-uid", 0, "Default UID (default 0)")
flags.Uint64Var(&svfs.DefaultGID, "default-gid", 0, "Default GID (default 0)")
flags.Uint64Var(&svfs.DefaultMode, "default-mode", 0700, "Default permissions")
flags.BoolVar(&svfs.AllowRoot, "allow-root", false, "Fuse allow-root option")
flags.BoolVar(&svfs.AllowOther, "allow-other", true, "Fuse allow_other option")
flags.BoolVar(&svfs.DefaultPermissions, "default-permissions", true, "Fuse default_permissions option")
flags.BoolVar(&svfs.ReadOnly, "read-only", false, "Read only access")
// Prefetch
flags.Uint64Var(&svfs.ListerConcurrency, "readdir-concurrency", 20, "Directory listing concurrency")
flags.BoolVar(&svfs.ExtraAttr, "readdir-extra-attributes", false, "Fetch extra attributes")
flags.UintVar(&svfs.BlockSize, "block-size", 4096, "Block size in bytes")
flags.UintVar(&svfs.ReadAheadSize, "readahead-size", 128, "Per file readhead size in KiB")
flags.IntVar(&svfs.TransferMode, "transfer-mode", 0, "Transfer optimizations mode")
// Cache Options
flags.DurationVar(&svfs.CacheTimeout, "cache-ttl", 1*time.Minute, "Cache timeout")
flags.Int64Var(&svfs.CacheMaxEntries, "cache-max-entries", -1, "Maximum overall entries allowed in cache")
flags.Int64Var(&svfs.CacheMaxAccess, "cache-max-access", -1, "Maximum access count to cached entries")
// Debug and profiling
flags.BoolVar(&debug, "debug", false, "Enable fuse debug log")
flags.StringVar(&profAddr, "profile-bind", "", "Profiling information will be served at this address")
flags.StringVar(&cpuProf, "profile-cpu", "", "Write cpu profile to this file")
flags.StringVar(&memProf, "profile-ram", "", "Write memory profile to this file")
// Mandatory flags
flags.StringVar(&device, "device", "", "Device name")
flags.StringVar(&mountpoint, "mountpoint", "", "Mountpoint")
mountCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// Bind cobra flags to viper flags
viper.BindPFlag("os_auth_url", mountCmd.PersistentFlags().Lookup("os-auth-url"))
viper.BindPFlag("os_username", mountCmd.PersistentFlags().Lookup("os-username"))
viper.BindPFlag("os_password", mountCmd.PersistentFlags().Lookup("os-password"))
viper.BindPFlag("os_tenant_name", mountCmd.PersistentFlags().Lookup("os-tenant-name"))
viper.BindPFlag("os_region_name", mountCmd.PersistentFlags().Lookup("os-region-name"))
viper.BindPFlag("os_auth_token", mountCmd.PersistentFlags().Lookup("os-auth-token"))
viper.BindPFlag("os_storage_url", mountCmd.PersistentFlags().Lookup("os-storage-url"))
viper.BindPFlag("hubic_auth", mountCmd.PersistentFlags().Lookup("hubic-authorization"))
viper.BindPFlag("hubic_token", mountCmd.PersistentFlags().Lookup("hubic-refresh-token"))
}
func mountOptions(device string) (options []fuse.MountOption) {
if svfs.AllowOther {
options = append(options, fuse.AllowOther())
}
if svfs.AllowRoot {
options = append(options, fuse.AllowRoot())
}
if svfs.DefaultPermissions {
options = append(options, fuse.DefaultPermissions())
}
if svfs.ReadOnly {
options = append(options, fuse.ReadOnly())
}
options = append(options, fuse.MaxReadahead(uint32(svfs.ReadAheadSize)))
options = append(options, fuse.Subtype("svfs"))
options = append(options, fuse.FSName(device))
return options
}
func checkOptions() error {
// Convert to MB
svfs.SegmentSize *= (1 << 20)
svfs.ReadAheadSize *= (1 << 10)
// Should not exceed swift maximum object size.
if svfs.SegmentSize > 5*(1<<30) {
return fmt.Errorf("Segment size can't exceed 5 GiB")
}
return nil
}
func setDebug() {
logrus.SetLevel(logrus.DebugLevel)
yellow := color.New(color.FgYellow).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
fuse.Debug = func(msg interface{}) {
logrus.WithField("source", yellow("fuse")).Debugln(blue(msg))
}
}
func createCPUProf(cpuProf string) {
f, err := os.Create(cpuProf)
if err != nil {
logrus.Fatal(err)
}
pprof.StartCPUProfile(f)
}
func createMemProf(memProf string) {
f, err := os.Create(memProf)
if err != nil {
logrus.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
func useConfiguration() {
svfs.HubicAuthorization = viper.GetString("hubic_auth")
svfs.HubicRefreshToken = viper.GetString("hubic_token")
svfs.SwiftConnection.AuthToken = viper.GetString("os_auth_token")
svfs.SwiftConnection.StorageUrl = viper.GetString("os_storage_url")
svfs.SwiftConnection.AuthUrl = viper.GetString("os_auth_url")
svfs.SwiftConnection.Tenant = viper.GetString("os_tenant_name")
svfs.SwiftConnection.UserName = viper.GetString("os_username")
svfs.SwiftConnection.ApiKey = viper.GetString("os_password")
svfs.SwiftConnection.Region = viper.GetString("os_region_name")
}