@@ -3,13 +3,13 @@ package main
33import (
44 "errors"
55 "fmt"
6- "os"
6+ "os"
77 "path"
88 "path/filepath"
99 "sync"
1010 "syscall"
1111
12- log "github.com/Sirupsen /logrus"
12+ log "github.com/sirupsen /logrus"
1313
1414 "github.com/davecgh/go-spew/spew"
1515 "github.com/docker/go-plugins-helpers/volume"
@@ -18,7 +18,7 @@ import (
1818// A single volume instance
1919type moosefsMount struct {
2020 name string
21- path string
21+ path string
2222 root string
2323}
2424
@@ -27,16 +27,21 @@ type moosefsDriver struct {
2727 m * sync.Mutex
2828}
2929
30- func newMooseFSDriver (root string ) moosefsDriver {
31- d := moosefsDriver {
30+ func newMooseFSDriver (root string ) ( * moosefsDriver , error ) {
31+ d := & moosefsDriver {
3232 mounts : make (map [string ]* moosefsMount ),
3333 m : & sync.Mutex {},
3434 }
35- return d
35+
36+ if err := d .loadState (); err != nil {
37+ return nil , err
38+ }
39+
40+ return d , nil
3641}
3742
3843func (d moosefsDriver ) Create (r * volume.CreateRequest ) error {
39- var volumeRoot string
44+ var volumeRoot string
4045
4146 d .m .Lock ()
4247 defer d .m .Unlock ()
@@ -45,15 +50,15 @@ func (d moosefsDriver) Create(r *volume.CreateRequest) error {
4550 volumeRoot = optsRoot
4651 } else {
4752 // Assume the default root
48- volumeRoot = * root
53+ volumeRoot = * root
4954 }
5055
51- volumePath := filepath .Join (volumeRoot , r .Name )
56+ volumePath := filepath .Join (volumeRoot , r .Name )
5257
53- if err := mkdir (volumePath ); err != nil {
58+ if err := mkdir (volumePath ); err != nil {
5459 return err
5560 }
56-
61+
5762 if ! ismoosefs (volumePath ) {
5863 emsg := fmt .Sprintf ("Cannot create volume %s as it's not a valid MooseFS mount" , volumePath )
5964 log .Error (emsg )
@@ -66,19 +71,22 @@ func (d moosefsDriver) Create(r *volume.CreateRequest) error {
6671 return errors .New (emsg )
6772 }
6873
69- if err := mkdir (volumePath ); err != nil {
70- return err
71- }
7274 d .mounts [r .Name ] = & moosefsMount {
7375 name : r .Name ,
74- path : volumePath ,
76+ path : volumePath ,
7577 root : volumeRoot ,
7678 }
7779
7880 if * verbose {
7981 spew .Dump (d .mounts )
8082 }
8183
84+ if err := d .saveState (); err != nil {
85+ // If we can't save state, remove the volume from memory
86+ delete (d .mounts , r .Name )
87+ return err
88+ }
89+
8290 return nil
8391}
8492
@@ -87,6 +95,9 @@ func (d moosefsDriver) Remove(r *volume.RemoveRequest) error {
8795 defer d .m .Unlock ()
8896 if _ , ok := d .mounts [r .Name ]; ok {
8997 delete (d .mounts , r .Name )
98+ if err := d .saveState (); err != nil {
99+ return err
100+ }
90101 }
91102 return nil
92103}
@@ -137,15 +148,26 @@ func (d moosefsDriver) Capabilities() *volume.CapabilitiesResponse {
137148 return & res
138149}
139150
140- // Check if MooseFS is mounted in mountpoint using the .masterinfo file
151+ // Check if path is under a MooseFS mount
141152func ismoosefs (mountpoint string ) bool {
142- stat := syscall.Statfs_t {}
143- err := syscall .Statfs (path .Join (mountpoint , ".masterinfo" ), & stat )
144- if err != nil {
145- log .Errorf ("Could not determine filesystem type for %s: %s" , mountpoint , err )
146- return false
147- }
148- return true
153+ // Get the parent directory until we find the mount point
154+ currentPath := mountpoint
155+ for currentPath != "/" {
156+ stat := syscall.Statfs_t {}
157+ err := syscall .Statfs (currentPath , & stat )
158+ if err != nil {
159+ log .Errorf ("Could not determine filesystem type for %s: %s" , currentPath , err )
160+ return false
161+ }
162+
163+ // Check if this is a FUSE filesystem (MooseFS uses FUSE)
164+ if stat .Type == 0x65735546 { // FUSE_SUPER_MAGIC
165+ return true
166+ }
167+
168+ currentPath = filepath .Dir (currentPath )
169+ }
170+ return false
149171}
150172
151173func mkdir (path string ) error {
0 commit comments