/
mount_attach.go
65 lines (58 loc) · 1.72 KB
/
mount_attach.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
package api
import (
"github.com/bensallen/rbd/pkg/mount"
"github.com/kraken-hpc/imageapi/models"
"github.com/sirupsen/logrus"
)
func init() {
MountDrivers[models.MountKindAttach] = &MountDriverAttach{}
}
type MountDriverAttach struct {
log *logrus.Entry //
}
func (m *MountDriverAttach) Init(log *logrus.Entry) {
m.log = log
m.log.Trace("initialized")
}
func (m *MountDriverAttach) Mount(mnt *Mount) (ret *Mount, err error) {
l := m.log.WithField("operation", "mount")
if mnt.Attach == nil {
l.Trace("attempted attach mount with no mount definition")
return nil, ErrInvalDat
}
// go-swagger should handle other validation that we need
var ma *Attach
if ma, err = API.Attachments.GetOrAttach((*Attach)(mnt.Attach.Attach)); err != nil {
l.WithError(err).Debug("GetOrAttach failed")
return nil, err
}
mnt.Attach.Attach = (*models.Attach)(ma)
defer func() {
if err != nil {
API.Store.RefAdd(mnt.Attach.Attach.ID, -1)
}
}()
l.WithField("attach_id", mnt.Attach.Attach.ID)
// ok, we're good to attempt the mount
// make a mountpoint
if err = mount.Mount(mnt.Attach.Attach.DeviceFile, mnt.Mountpoint, *mnt.Attach.FsType, mnt.Attach.MountOptions); err != nil {
l.WithError(err).Error("failed to mount")
return nil, ErrFail
}
return mnt, nil
}
func (m *MountDriverAttach) Unmount(mnt *Mount) (ret *Mount, err error) {
l := m.log.WithFields(logrus.Fields{
"operation": "unmount",
"id": mnt.ID,
"attach_id": mnt.Attach.Attach.ID,
})
// always lazy unmount. Good idea?
if err = mount.Unmount(mnt.Mountpoint, false, true); err != nil {
l.WithError(err).Error("unmount failed")
return nil, ErrFail
}
API.Store.RefAdd(mnt.Attach.Attach.ID, -1)
// garbage collection should do our cleanup
return mnt, nil
}