Skip to content

Commit 60371ac

Browse files
committed
create subdirs for volumes
1 parent 5b52e3e commit 60371ac

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
all: deps compile
22

3-
VERSION=0.1.0
3+
VERSION=0.2.0
44

55
compile:
66
go build
@@ -38,7 +38,7 @@ deb: compile
3838
install -m 0644 docker-volume-moosefs.service obj/debian/lib/systemd/system
3939
fpm -C obj/debian --vendor MooseFS -m "contact@moosefs.com" -f \
4040
-s dir -t deb -n docker-volume-moosefs \
41-
--after-install files/post-install-systemd --version ${VERSION} . && \
41+
--version ${VERSION} . && \
4242
rm -fr obj/debian
4343

4444
clean:

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Plugin for MooseFS to create persistent volumes in Docker containers.
44

55
<img align="right" alt="MooseFS logo" src="https://moosefs.com/Content/Images/moosefs.png" />
66

7+
## Version history:
8+
9+
- 0.2.0 Creates new subdirectory for each volume in root e.g. /mnt/moosefs
10+
- 0.1.0 Mountpoint /mnt/moosefs is used as the volume
11+
712
## Preconditions
813

914
- **MooseFS** Storage Cluster has to be setup and running
@@ -69,10 +74,12 @@ make rpm
6974
Assuming we have **MooseFS** mounted in `/mnt/moosefs` we will create a volume labeled `mymoosefs`:
7075

7176
```
72-
docker volume create -d moosefs --name mymoosefs -o mountpoint=/mnt/moosefs
77+
docker volume create -d moosefs --name mymoosefs -o root=/mnt/moosefs
7378
```
7479

75-
Without specified mountpoint plugin will assume mounting in `/mnt/$NAME`, so **mymoosefs** should be mounted in `/mnt/myfs` to use short command `docker volume create -d moosefs --name mymoosefs`
80+
Without specified mountpoint plugin will assume mounting in `/mnt/moosefs/$NAME`, so **mymoosefs** will be mounted in `/mnt/moosefs/mymoosefs`
81+
82+
We can use shorter command: `docker volume create -d moosefs --name mymoosefs`
7683

7784
We can inspect created volume with following command:
7885

driver.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"path"
78
"path/filepath"
89
"sync"
@@ -16,16 +17,17 @@ import (
1617

1718
// A single volume instance
1819
type moosefsMount struct {
19-
name string
20-
mountpoint string
20+
name string
21+
path string
22+
root string
2123
}
2224

2325
type 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

3638
func (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

8294
func (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

89101
func (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 {
106118
func (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

114126
func (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+
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
var (
16-
mountpoint = flag.String("mountpoint", "/mnt/", "Host's base directory where volumes are created")
16+
root = flag.String("root", "/mnt/moosefs", "Host's base directory where volumes are created")
1717
verbose = flag.Bool("verbose", false, "Enable verbose logging")
1818
)
1919

@@ -29,7 +29,7 @@ func main() {
2929
u, _ := user.Lookup("root")
3030
gid, _ := strconv.Atoi(u.Gid)
3131

32-
d := newMooseFSDriver(*mountpoint)
32+
d := newMooseFSDriver(*root)
3333
h := volume.NewHandler(d)
3434
fmt.Println(h.ServeUnix("moosefs", gid))
3535
}

0 commit comments

Comments
 (0)