@@ -3,6 +3,7 @@ package main
33import (
44 "errors"
55 "fmt"
6+ "os"
67 "path"
78 "path/filepath"
89 "sync"
@@ -16,16 +17,17 @@ import (
1617
1718// A single volume instance
1819type moosefsMount struct {
19- name string
20- mountpoint string
20+ name string
21+ path string
22+ root string
2123}
2224
2325type moosefsDriver struct {
2426 mounts map [string ]* moosefsMount
2527 m * sync.Mutex
2628}
2729
28- func newMooseFSDriver (mountpoint string ) moosefsDriver {
30+ func newMooseFSDriver (root string ) moosefsDriver {
2931 d := moosefsDriver {
3032 mounts : make (map [string ]* moosefsMount ),
3133 m : & sync.Mutex {},
@@ -34,33 +36,43 @@ func newMooseFSDriver(mountpoint string) moosefsDriver {
3436}
3537
3638func (d moosefsDriver ) Create (r * volume.CreateRequest ) error {
37- var volumeMountpoint string
39+ var volumeRoot string
3840
3941 d .m .Lock ()
4042 defer d .m .Unlock ()
4143
42- if optsMountpoint , ok := r .Options ["mountpoint " ]; ok {
43- volumeMountpoint = optsMountpoint
44+ if optsRoot , ok := r .Options ["root " ]; ok {
45+ volumeRoot = optsRoot
4446 } else {
45- // Assume the default mountpoint
46- volumeMountpoint = filepath . Join ( * mountpoint , r . Name )
47+ // Assume the default root
48+ volumeRoot = * root
4749 }
4850
49- if ! ismoosefs (volumeMountpoint ) {
50- emsg := fmt .Sprintf ("Cannot create volume %s as it's not a valid MooseFS mount" , volumeMountpoint )
51+ volumePath := filepath .Join (volumeRoot , r .Name )
52+
53+ if err := mkdir (volumePath ); err != nil {
54+ return err
55+ }
56+
57+ if ! ismoosefs (volumePath ) {
58+ emsg := fmt .Sprintf ("Cannot create volume %s as it's not a valid MooseFS mount" , volumePath )
5159 log .Error (emsg )
5260 return errors .New (emsg )
5361 }
5462
5563 if _ , ok := d .mounts [r .Name ]; ok {
56- emsg := fmt .Sprintf ("Cannot create volume %s, it already exists" , volumeMountpoint )
64+ emsg := fmt .Sprintf ("Cannot create volume %s, it already exists" , volumePath )
5765 log .Error (emsg )
5866 return errors .New (emsg )
5967 }
6068
69+ if err := mkdir (volumePath ); err != nil {
70+ return err
71+ }
6172 d .mounts [r .Name ] = & moosefsMount {
62- name : r .Name ,
63- mountpoint : volumeMountpoint ,
73+ name : r .Name ,
74+ path : volumePath ,
75+ root : volumeRoot ,
6476 }
6577
6678 if * verbose {
@@ -81,20 +93,20 @@ func (d moosefsDriver) Remove(r *volume.RemoveRequest) error {
8193
8294func (d moosefsDriver ) Path (r * volume.PathRequest ) (* volume.PathResponse , error ) {
8395 if _ , ok := d .mounts [r .Name ]; ok {
84- return & volume.PathResponse {Mountpoint : d .mounts [r .Name ].mountpoint }, nil
96+ return & volume.PathResponse {Mountpoint : d .mounts [r .Name ].path }, nil
8597 }
8698 return & volume.PathResponse {}, nil
8799}
88100
89101func (d moosefsDriver ) Mount (r * volume.MountRequest ) (* volume.MountResponse , error ) {
90- volumeMountpoint := d .mounts [r .Name ].mountpoint
91- if ! ismoosefs (volumeMountpoint ) {
92- emsg := fmt .Sprintf ("Cannot mount volume %s as it's not a valid MooseFS mount" , volumeMountpoint )
102+ volumePath := filepath . Join ( d .mounts [r .Name ].root , r . Name )
103+ if ! ismoosefs (volumePath ) {
104+ emsg := fmt .Sprintf ("Cannot mount volume %s as it's not a valid MooseFS mount" , volumePath )
93105 log .Error (emsg )
94106 return & volume.MountResponse {}, errors .New (emsg )
95107 }
96108 if _ , ok := d .mounts [r .Name ]; ok {
97- return & volume.MountResponse {Mountpoint : d .mounts [r .Name ].mountpoint }, nil
109+ return & volume.MountResponse {Mountpoint : d .mounts [r .Name ].path }, nil
98110 }
99111 return & volume.MountResponse {}, nil
100112}
@@ -106,15 +118,15 @@ func (d moosefsDriver) Unmount(r *volume.UnmountRequest) error {
106118func (d moosefsDriver ) Get (r * volume.GetRequest ) (* volume.GetResponse , error ) {
107119 if v , ok := d .mounts [r .Name ]; ok {
108120 return & volume.GetResponse {
109- Volume : & volume.Volume {Name : v .name , Mountpoint : v .mountpoint }}, nil
121+ Volume : & volume.Volume {Name : v .name , Mountpoint : v .path }}, nil
110122 }
111123 return & volume.GetResponse {}, fmt .Errorf ("volume %s unknown" , r .Name )
112124}
113125
114126func (d moosefsDriver ) List () (* volume.ListResponse , error ) {
115127 volumes := []* volume.Volume {}
116128 for v := range d .mounts {
117- volumes = append (volumes , & volume.Volume {Name : d .mounts [v ].name , Mountpoint : d .mounts [v ].mountpoint })
129+ volumes = append (volumes , & volume.Volume {Name : d .mounts [v ].name , Mountpoint : d .mounts [v ].path })
118130 }
119131 return & volume.ListResponse {Volumes : volumes }, nil
120132}
@@ -135,3 +147,21 @@ func ismoosefs(mountpoint string) bool {
135147 }
136148 return true
137149}
150+
151+ func mkdir (path string ) error {
152+ fstat , err := os .Lstat (path )
153+
154+ if os .IsNotExist (err ) {
155+ if err := os .MkdirAll (path , 0755 ); err != nil {
156+ return err
157+ }
158+ } else if err != nil {
159+ return err
160+ }
161+
162+ if fstat != nil && ! fstat .IsDir () {
163+ return fmt .Errorf ("%v already exist and it's not a directory" , path )
164+ }
165+
166+ return nil
167+ }
0 commit comments