-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdriver.go
202 lines (158 loc) · 4.28 KB
/
driver.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
package main
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"os"
"path"
"path/filepath"
"sync"
"syscall"
"github.com/davecgh/go-spew/spew"
"github.com/docker/go-plugins-helpers/volume"
)
// A single volume instance
type beegfsMount struct {
name string
path string
root string
}
type beegfsDriver struct {
mounts map[string]*beegfsMount
m *sync.Mutex
}
func newBeeGFSDriver(root string) beegfsDriver {
d := beegfsDriver{
mounts: make(map[string]*beegfsMount),
m: &sync.Mutex{},
}
return d
}
func (b beegfsDriver) Create(r *volume.CreateRequest) error {
var volumeRoot string
log.Infof("Create: %s, %v", r.Name, r.Options)
b.m.Lock()
defer b.m.Unlock()
// Handle options (unrecognized options are silently ignored):
// root: directory to create new volumes (this should correspond with
// beegfs-mounts.conf).
if optsRoot, ok := r.Options["root"]; ok {
volumeRoot = optsRoot
} else {
// Assume the default root
volumeRoot = *root
}
dest := filepath.Join(volumeRoot, r.Name)
if !isbeegfs(dest) {
emsg := fmt.Sprintf("Cannot create volume %s as it's not on a BeeGFS filesystem", dest)
log.Error(emsg)
return errors.New(emsg)
}
fmt.Printf("mounts: %d", len(b.mounts))
if _, ok := b.mounts[r.Name]; ok {
imsg := fmt.Sprintf("Cannot create volume %s, it already exists", dest)
log.Info(imsg)
return nil
}
volumePath := filepath.Join(volumeRoot, r.Name)
if err := createDest(dest); err != nil {
return err
}
b.mounts[r.Name] = &beegfsMount {
name: r.Name,
path: volumePath,
root: volumeRoot,
}
if *verbose {
spew.Dump(b.mounts)
}
return nil
}
func (b beegfsDriver) Remove(r *volume.RemoveRequest) error {
log.Infof("Remove: %s", r.Name)
b.m.Lock()
defer b.m.Unlock()
if _, ok := b.mounts[r.Name]; ok {
delete(b.mounts, r.Name)
}
return nil
}
func (b beegfsDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
log.Debugf("Path: %s", r.Name)
if _, ok := b.mounts[r.Name]; ok {
return &volume.PathResponse{Mountpoint: b.mounts[r.Name].path}, nil
}
return nil, nil
}
func (b beegfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
log.Infof("Mount: %s", r.Name)
dest := filepath.Join(b.mounts[r.Name].root, r.Name)
if !isbeegfs(dest) {
emsg := fmt.Sprintf("Cannot mount volume %s as it's not on a BeeGFS filesystem", dest)
log.Error(emsg)
return nil, errors.New(emsg)
}
if _, ok := b.mounts[r.Name]; ok {
return &volume.MountResponse{Mountpoint: b.mounts[r.Name].path}, nil
}
return nil, nil
}
func (b beegfsDriver) Unmount(r *volume.UnmountRequest) error {
log.Infof("Unmount: %s", r.Name)
return nil
}
func (b beegfsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
log.Infof("Get: %s", r.Name)
if v, ok := b.mounts[r.Name]; ok {
return &volume.GetResponse{
Volume: &volume.Volume{
Name: v.name,
Mountpoint: v.path,
},
}, nil
}
return nil, errors.New(fmt.Sprintf("volume %s unknown", r.Name))
}
func (b beegfsDriver) List() (*volume.ListResponse, error) {
log.Infof("List")
volumes := []*volume.Volume{}
for v := range b.mounts {
volumes = append(volumes, &volume.Volume{Name: b.mounts[v].name, Mountpoint: b.mounts[v].path})
}
return &volume.ListResponse{Volumes: volumes}, nil
}
func (d beegfsDriver) Capabilities() *volume.CapabilitiesResponse {
return &volume.CapabilitiesResponse{
Capabilities: volume.Capability{
Scope: "global",
},
}
}
// Check if the parent directory (where the volume will be created)
// is of type 'beegfs' using the BEEGFS_MAGIC value.
func isbeegfs(volumepath string) bool {
log.Debugf("isbeegfs() for %s", volumepath)
stat := syscall.Statfs_t{}
err := syscall.Statfs(path.Dir(volumepath), &stat)
if err != nil {
log.Errorf("Could not determine filesystem type for %s: %s", volumepath, err)
return false
}
log.Debugf("Type for %s: %d", volumepath, stat.Type)
// BEEGFS_MAGIC 0x19830326
return stat.Type == 428016422
}
func createDest(dest string) error {
fstat, err := os.Lstat(dest)
if os.IsNotExist(err) {
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
} else if err != nil {
return err
}
if fstat != nil && !fstat.IsDir() {
return fmt.Errorf("%v already exist and it's not a directory", dest)
}
return nil
}