/
btfs.go
365 lines (290 loc) · 9.4 KB
/
btfs.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package volume
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"bazil.org/fuse"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
docker "github.com/docker/docker/client"
"github.com/docker/go-plugins-helpers/volume"
"github.com/sonm-io/core/util/xdocker"
"go.uber.org/zap"
)
const (
BTFSDriverName = "btfs"
BTFSImage = "sonm/btfs@sha256:b64b4a7849aa742049b039aeabcaff0fa58876c00df9abdf0321d3fff31789a2"
DefaultDockerRootDirectory = volume.DefaultDockerRootDirectory
)
type BTFSDriver struct {
server *VolumeServer
log *zap.SugaredLogger
}
func NewBTFSDriver(options ...Option) (*BTFSDriver, error) {
server, err := NewVolumeServer(BTFSDriverName, options...)
if err != nil {
return nil, err
}
client, err := docker.NewEnvClient()
if err != nil {
return nil, fmt.Errorf("failed to create Docker client for BTFS volume driver: %v", err)
}
driver, err := NewBTFSDockerDriver(client, server.log)
if err != nil {
return nil, fmt.Errorf("failed to create BTFS Docker driver: %v", err)
}
go server.Serve(driver)
m := &BTFSDriver{
server: server,
log: server.log.With("driver", BTFSDriverName),
}
return m, nil
}
func (m *BTFSDriver) CreateVolume(name string, options map[string]string) (Volume, error) {
m.log.Debugw("handling Worker.CreateVolume request", zap.String("name", name), zap.Any("options", options))
return &BTFSVolume{Options: options}, nil
}
func (m *BTFSDriver) RemoveVolume(name string) error {
m.log.Debug("handling Worker.RemoveVolume request")
return nil
}
func (m *BTFSDriver) Close() error {
return m.server.Close()
}
type BTFSVolume struct {
Options map[string]string
}
func (m *BTFSVolume) Configure(mnt Mount, cfg *container.HostConfig) error {
cfg.Mounts = append(cfg.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: mnt.Source,
Target: mnt.Target,
ReadOnly: true,
Consistency: mount.ConsistencyDefault,
BindOptions: nil,
VolumeOptions: &mount.VolumeOptions{
NoCopy: false,
Labels: map[string]string{},
DriverConfig: &mount.Driver{
Name: BTFSDriverName,
Options: m.Options,
},
},
TmpfsOptions: nil,
})
return nil
}
func pullImage(ctx context.Context, client *docker.Client, ref string) error {
images, err := client.ImageList(ctx, types.ImageListOptions{All: true})
if err != nil {
return err
}
for _, summary := range images {
if summary.ID == BTFSImage {
return nil
}
}
body, err := client.ImagePull(ctx, BTFSImage, types.ImagePullOptions{All: false})
if err != nil {
return fmt.Errorf("failed to pull %s image: %v", ref, err)
}
if err = xdocker.DecodeImagePull(body); err != nil {
return fmt.Errorf("failed to pull %s image: %v", ref, err)
}
return nil
}
type BTFSDockerVolume struct {
Client *docker.Client
MagnetURI string
MountPoint string
ID string
Connections int
NetworkName string
NetworkID string
}
// BTFSDockerDriver is a Docker driver implementation for BTFS volume plugin.
type BTFSDockerDriver struct {
client *docker.Client
mountRootDir string
mu sync.Mutex
volumes map[string]*BTFSDockerVolume
log *zap.SugaredLogger
}
func NewBTFSDockerDriver(client *docker.Client, log *zap.SugaredLogger) (*BTFSDockerDriver, error) {
ctx := context.Background()
if err := pullImage(ctx, client, BTFSImage); err != nil {
return nil, err
}
m := &BTFSDockerDriver{
client: client,
mountRootDir: filepath.Join(DefaultDockerRootDirectory, BTFSDriverName, "mnt"),
volumes: map[string]*BTFSDockerVolume{},
log: log.With("driver", BTFSDriverName),
}
return m, nil
}
func (m *BTFSDockerDriver) Create(request *volume.CreateRequest) error {
m.log.Debugw("handling Volume.Create request", zap.Any("request", request))
uri, ok := request.Options["magnet"]
if !ok {
return fmt.Errorf("`magnet` URI is required")
}
m.mu.Lock()
defer m.mu.Unlock()
m.volumes[request.Name] = &BTFSDockerVolume{
Client: m.client,
MagnetURI: uri,
MountPoint: filepath.Join(m.mountRootDir, request.Name),
Connections: 0,
NetworkName: request.Options[OptionNetworkName],
NetworkID: request.Options[OptionNetworkID],
}
return nil
}
func (m *BTFSDockerDriver) List() (*volume.ListResponse, error) {
m.log.Debug("handling Volume.List request")
m.mu.Lock()
defer m.mu.Unlock()
volumes := make([]*volume.Volume, 0, len(m.volumes))
for id, v := range m.volumes {
volumes = append(volumes, &volume.Volume{
Name: id,
Mountpoint: v.MountPoint,
})
}
return &volume.ListResponse{Volumes: volumes}, nil
}
func (m *BTFSDockerDriver) Get(request *volume.GetRequest) (*volume.GetResponse, error) {
m.log.Debugw("handling Volume.Get request", zap.Any("request", request))
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.volumes[request.Name]
if !ok {
return &volume.GetResponse{}, fmt.Errorf("volume %s not found", request.Name)
}
return &volume.GetResponse{Volume: &volume.Volume{Name: request.Name, Mountpoint: v.MountPoint}}, nil
}
func (m *BTFSDockerDriver) Remove(request *volume.RemoveRequest) error {
m.log.Debugw("handling Volume.Remove request", zap.Any("request", request))
m.mu.Lock()
defer m.mu.Unlock()
if v, ok := m.volumes[request.Name]; ok {
m.log.Infof("removing `%s` volume on `%s`", request.Name, v.MountPoint)
delete(m.volumes, request.Name)
return nil
}
return fmt.Errorf("volume `%s` not found", request.Name)
}
func (m *BTFSDockerDriver) Path(request *volume.PathRequest) (*volume.PathResponse, error) {
m.log.Debugw("handling Volume.Path request", zap.Any("request", request))
m.mu.Lock()
defer m.mu.Unlock()
if v, ok := m.volumes[request.Name]; ok {
return &volume.PathResponse{Mountpoint: v.MountPoint}, nil
}
return nil, fmt.Errorf("volume `%s` not found", request.Name)
}
func (m *BTFSDockerDriver) Mount(request *volume.MountRequest) (*volume.MountResponse, error) {
m.log.Debugw("handling Volume.Mount request", zap.Any("request", request))
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.volumes[request.Name]
if !ok {
return nil, fmt.Errorf("volume `%s` not found", request.Name)
}
if v.Connections == 0 {
stat, err := os.Lstat(v.MountPoint)
switch {
case os.IsNotExist(err):
if err := os.MkdirAll(v.MountPoint, 0755); err != nil {
return nil, fmt.Errorf("failed to create %s directories for %s volume: %v", v.MountPoint, request.Name, err)
}
case err != nil:
return nil, fmt.Errorf("failed to perform lstat %s volume: %v", request.Name, err)
case stat != nil && !stat.IsDir():
return nil, fmt.Errorf("failed to create %s directiries for %s volume: already exist and it's not a directory", v.MountPoint, request.Name)
}
cfg := &container.Config{
Image: BTFSImage,
Labels: map[string]string{},
Volumes: map[string]struct{}{},
Cmd: []string{"/usr/bin/btfs", v.MagnetURI, "/root/mnt", "-f"},
}
hostConfig := &container.HostConfig{
CapAdd: []string{"SYS_ADMIN"},
Privileged: true,
SecurityOpt: []string{"apparmor:unconfined"},
PublishAllPorts: true,
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: v.MountPoint,
Target: "/root/mnt",
ReadOnly: false,
Consistency: mount.ConsistencyDefault,
BindOptions: &mount.BindOptions{
Propagation: mount.PropagationRShared,
},
VolumeOptions: nil,
TmpfsOptions: nil,
},
},
}
networkConfig := &network.NetworkingConfig{}
if len(v.NetworkName) > 0 && len(v.NetworkID) > 0 {
m.log.Debugf("configuring %s volume with %s network", request.Name, v.NetworkName)
networkConfig.EndpointsConfig = map[string]*network.EndpointSettings{
v.NetworkName: {
NetworkID: v.NetworkID,
},
}
}
ctx := context.Background()
response, err := m.client.ContainerCreate(ctx, cfg, hostConfig, networkConfig, "")
if err != nil {
return nil, fmt.Errorf("failed to create btfs container for %s volume: %v", request.Name, err)
}
if err := m.client.ContainerStart(ctx, response.ID, types.ContainerStartOptions{}); err != nil {
return nil, fmt.Errorf("failed to start btfs container for %s volume: %v", request.Name, err)
}
v.ID = response.ID
}
v.Connections++
return &volume.MountResponse{Mountpoint: v.MountPoint}, nil
}
func (m *BTFSDockerDriver) Unmount(request *volume.UnmountRequest) error {
m.log.Debugw("handling Volume.Unmount request", zap.Any("request", request))
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.volumes[request.Name]
if !ok {
return fmt.Errorf("volume `%s` not found", request.Name)
}
v.Connections--
if v.Connections <= 0 {
// Weird case when Docker calls "Unmount" before "Mount".
v.Connections = 0
ctx := context.Background()
if err := m.client.ContainerStop(ctx, v.ID, nil); err != nil {
return fmt.Errorf("failed to stop btfs container for %s volume: %v", request.Name, err)
}
if err := m.client.ContainerRemove(ctx, v.ID, types.ContainerRemoveOptions{}); err != nil {
return fmt.Errorf("failed to clean up btfs container for %s volume: %v", request.Name, err)
}
if err := fuse.Unmount(v.MountPoint); err != nil {
return fmt.Errorf("failed to unmount fuse for %s volume: %v", request.Name, err)
}
}
return nil
}
func (m *BTFSDockerDriver) Capabilities() *volume.CapabilitiesResponse {
return &volume.CapabilitiesResponse{
Capabilities: volume.Capability{
Scope: "local",
},
}
}