/
buse.go
404 lines (341 loc) · 9.43 KB
/
buse.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package buse
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
"syscall"
"github.com/sirupsen/logrus"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/cluster"
clustermanager "github.com/libopenstorage/openstorage/cluster/manager"
"github.com/libopenstorage/openstorage/pkg/correlation"
"github.com/libopenstorage/openstorage/volume"
"github.com/libopenstorage/openstorage/volume/drivers/common"
"github.com/pborman/uuid"
"github.com/portworx/kvdb"
)
const (
// Name of the driver
Name = "buse"
// Type of the driver
Type = api.DriverType_DRIVER_TYPE_BLOCK
// BuseDBKey for openstorage
BuseDBKey = "OpenStorageBuseKey"
// BuseMountPath mount path for openstorage
BuseMountPath = "/var/lib/openstorage/buse/"
)
// Implements the open storage volume interface.
type driver struct {
volume.IODriver
volume.StoreEnumerator
volume.StatsDriver
volume.QuiesceDriver
volume.CredsDriver
volume.CloudBackupDriver
volume.CloudMigrateDriver
volume.FilesystemTrimDriver
volume.FilesystemCheckDriver
buseDevices map[string]*buseDev
cl cluster.ClusterListener
}
type clusterListener struct {
cluster.NullClusterListener
}
// Implements the Device interface.
type buseDev struct {
file string
f *os.File
nbd *NBD
}
func (d *buseDev) ReadAt(b []byte, off int64) (n int, err error) {
return d.f.ReadAt(b, off)
}
func (d *buseDev) WriteAt(b []byte, off int64) (n int, err error) {
return d.f.WriteAt(b, off)
}
func copyFile(source string, dest string) (err error) {
sourcefile, err := os.Open(source)
if err != nil {
return err
}
defer sourcefile.Close()
destfile, err := os.Create(dest)
if err != nil {
return err
}
defer destfile.Close()
_, err = io.Copy(destfile, sourcefile)
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
}
}
return
}
// Init intialized the buse driver
func Init(params map[string]string) (volume.VolumeDriver, error) {
nbdInit()
inst := &driver{
IODriver: volume.IONotSupported,
StoreEnumerator: common.NewDefaultStoreEnumerator(Name,
kvdb.Instance()),
StatsDriver: volume.StatsNotSupported,
QuiesceDriver: volume.QuiesceNotSupported,
CredsDriver: volume.CredsNotSupported,
CloudBackupDriver: volume.CloudBackupNotSupported,
CloudMigrateDriver: volume.CloudMigrateNotSupported,
FilesystemTrimDriver: volume.FilesystemTrimNotSupported,
FilesystemCheckDriver: volume.FilesystemCheckNotSupported,
}
inst.buseDevices = make(map[string]*buseDev)
if err := os.MkdirAll(BuseMountPath, 0744); err != nil {
return nil, err
}
volumeInfo, err := inst.StoreEnumerator.Enumerate(
&api.VolumeLocator{},
nil,
)
if err == nil {
for _, info := range volumeInfo {
if info.Status == api.VolumeStatus_VOLUME_STATUS_NONE {
info.Status = api.VolumeStatus_VOLUME_STATUS_UP
inst.UpdateVol(info)
}
}
} else {
logrus.Println("Could not enumerate Volumes, ", err)
}
inst.cl = &clusterListener{}
c, err := clustermanager.Inst()
if err != nil {
logrus.Println("BUSE initializing in single node mode")
} else {
logrus.Println("BUSE initializing in clustered mode")
c.AddEventListener(inst.cl)
}
logrus.Println("BUSE initialized and driver mounted at: ", BuseMountPath)
return inst, nil
}
//
// These functions below implement the volume driver interface.
//
func (d *driver) String() string {
return Name
}
func (d *driver) Name() string {
return Name
}
func (d *driver) Type() api.DriverType {
return Type
}
func (d *driver) Version() (*api.StorageVersion, error) {
return &api.StorageVersion{
Driver: d.Name(),
Version: "1.0.0",
}, nil
}
// Status diagnostic information
func (d *driver) Status() [][2]string {
return [][2]string{}
}
func (d *driver) Create(
ctx context.Context,
locator *api.VolumeLocator,
source *api.Source,
spec *api.VolumeSpec,
) (string, error) {
volumeID := uuid.New()
volumeID = strings.TrimSuffix(volumeID, "\n")
if spec.Size == 0 {
return "", fmt.Errorf("Volume size cannot be zero: buse")
}
if spec.Format == api.FSType_FS_TYPE_NONE {
return "", fmt.Errorf("Missing volume format: buse")
}
// Create a file on the local buse path with this UUID.
buseFile := path.Join(BuseMountPath, volumeID)
f, err := os.Create(buseFile)
if err != nil {
logrus.Println(err)
return "", err
}
if err := f.Truncate(int64(spec.Size)); err != nil {
logrus.Println(err)
return "", err
}
bd := &buseDev{
file: buseFile,
f: f,
}
nbd := Create(bd, volumeID, int64(spec.Size))
bd.nbd = nbd
logrus.Infof("Connecting to NBD...")
dev, err := bd.nbd.Connect()
if err != nil {
logrus.Println(err)
return "", err
}
logrus.Infof("Formatting %s with %v", dev, spec.Format)
cmd := "/sbin/mkfs." + spec.Format.SimpleString()
o, err := exec.Command(cmd, dev).Output()
if err != nil {
logrus.Warnf("Failed to run command %v %v: %v", cmd, dev, o)
return "", err
}
logrus.Infof("BUSE mapped NBD device %s (size=%v) to block file %s", dev,
spec.Size, buseFile)
v := common.NewVolume(
volumeID,
spec.Format,
locator,
source,
spec,
)
v.DevicePath = dev
d.buseDevices[dev] = bd
err = d.CreateVol(v)
if err != nil {
return "", err
}
return v.Id, err
}
func (d *driver) Delete(ctx context.Context, volumeID string) error {
v, err := d.GetVol(volumeID)
if err != nil {
logrus.Println(err)
return err
}
bd, ok := d.buseDevices[v.DevicePath]
if !ok {
err = fmt.Errorf("Cannot locate a BUSE device for %s", v.DevicePath)
logrus.Println(err)
return err
}
// Clean up buse block file and close the NBD connection.
os.Remove(bd.file)
bd.f.Close()
bd.nbd.Disconnect()
logrus.Infof("BUSE deleted volume %v at NBD device %s", volumeID,
v.DevicePath)
if err := d.DeleteVol(volumeID); err != nil {
logrus.Println(err)
return err
}
return nil
}
func (d *driver) MountedAt(ctx context.Context, mountpath string) string {
return ""
}
func (d *driver) Mount(ctx context.Context, volumeID string, mountpath string, options map[string]string) error {
v, err := d.GetVol(volumeID)
if err != nil {
return fmt.Errorf("Failed to locate volume %q", volumeID)
}
if len(v.AttachPath) > 0 && len(v.AttachPath) > 0 {
return fmt.Errorf("Volume %q already mounted at %q", volumeID, v.AttachPath[0])
}
if err := syscall.Mount(v.DevicePath, mountpath, v.Spec.Format.SimpleString(), 0, ""); err != nil {
return fmt.Errorf("Failed to mount %v at %v: %v", v.DevicePath, mountpath, err)
}
logrus.Infof("BUSE mounted NBD device %s at %s", v.DevicePath, mountpath)
if v.AttachPath == nil {
v.AttachPath = make([]string, 1)
}
v.AttachPath[0] = mountpath
return d.UpdateVol(v)
}
func (d *driver) Unmount(ctx context.Context, volumeID string, mountpath string, options map[string]string) error {
v, err := d.GetVol(volumeID)
if err != nil {
return err
}
if len(v.AttachPath) == 0 || len(v.AttachPath[0]) == 0 {
return fmt.Errorf("Device %v not mounted", volumeID)
}
if err := syscall.Unmount(v.AttachPath[0], 0); err != nil {
return err
}
v.AttachPath = nil
return d.UpdateVol(v)
}
func (d *driver) Snapshot(volumeID string, readonly bool, locator *api.VolumeLocator, noRetry bool) (string, error) {
volIDs := make([]string, 1)
volIDs[0] = volumeID
vols, err := d.Inspect(volIDs)
if err != nil {
return "", nil
}
source := &api.Source{Parent: volumeID}
newVolumeID, err := d.Create(correlation.TODO(), locator, source, vols[0].Spec)
if err != nil {
return "", nil
}
// BUSE does not support snapshots, so just copy the block files.
err = copyFile(BuseMountPath+volumeID, BuseMountPath+newVolumeID)
if err != nil {
d.Delete(correlation.TODO(), newVolumeID)
return "", nil
}
return newVolumeID, nil
}
func (d *driver) Restore(volumeID string, snapID string) error {
if _, err := d.Inspect([]string{volumeID, snapID}); err != nil {
return err
}
// BUSE does not support restore, so just copy the block files.
return copyFile(BuseMountPath+snapID, BuseMountPath+volumeID)
}
func (d *driver) SnapshotGroup(groupID string, labels map[string]string, volumeIDs []string, deleteOnFailure bool) (*api.GroupSnapCreateResponse, error) {
return nil, volume.ErrNotSupported
}
func (d *driver) Set(volumeID string, locator *api.VolumeLocator, spec *api.VolumeSpec) error {
if spec != nil {
return volume.ErrNotSupported
}
v, err := d.GetVol(volumeID)
if err != nil {
return err
}
if locator != nil {
v.Locator = locator
}
return d.UpdateVol(v)
}
func (d *driver) Attach(ctx context.Context, volumeID string, attachOptions map[string]string) (string, error) {
// Nothing to do on attach.
return path.Join(BuseMountPath, volumeID), nil
}
func (d *driver) Detach(ctx context.Context, volumeID string, options map[string]string) error {
// Nothing to do on detach.
return nil
}
func (d *driver) Shutdown() {
logrus.Printf("%s Shutting down", Name)
syscall.Unmount(BuseMountPath, 0)
}
func (cl *clusterListener) Init(
self *api.Node,
clusterInfo *cluster.ClusterInfo,
) (cluster.FinalizeInitCb, error) {
return nil, nil
}
func (cl *clusterListener) Join(
self *api.Node,
initState *cluster.ClusterInitState,
) error {
return nil
}
func (cl *clusterListener) String() string {
return Name
}
func (d *driver) Catalog(volumeID, path, depth string) (api.CatalogResponse, error) {
return api.CatalogResponse{}, volume.ErrNotSupported
}
func (d *driver) VolService(volumeID string, vtreq *api.VolumeServiceRequest) (*api.VolumeServiceResponse, error) {
return nil, volume.ErrNotSupported
}