From 5ac6b08c91e1fab0d375c7a203078968f20cc343 Mon Sep 17 00:00:00 2001 From: Jeremy Unruh Date: Mon, 3 Oct 2016 22:43:18 -0700 Subject: [PATCH] Initial support for NFS only to create directories within root share based on datavol name - Issue #72 --- Makefile | 2 +- netshare/drivers/driver.go | 5 +++++ netshare/drivers/mounts.go | 17 +++++++++++++++-- netshare/drivers/nfs.go | 13 +++++++++++++ netshare/drivers/util.go | 4 ++-- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index f2eaeec..baacdf3 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 0.20 +VERSION = 0.30 GO_FMT = gofmt -s -w -l . GO_XC = goxc -os="linux" -bc="linux,amd64" -tasks-="rmbin" diff --git a/netshare/drivers/driver.go b/netshare/drivers/driver.go index ceb1fc0..477ef78 100644 --- a/netshare/drivers/driver.go +++ b/netshare/drivers/driver.go @@ -32,7 +32,12 @@ func (v volumeDriver) Create(r volume.Request) volume.Response { if err := createDest(dest); err != nil { return volume.Response{Err: err.Error()} } + v.mountm.Create(r.Name, dest, r.Options) + //if v.mountm.GetOption(r.Name, ShareOpt) != "" && v.mountm.GetOptionAsBool(r.Name, CreateOpt) { + // log.Debugf("Create volume -> name: %s, creating option found, creating: %s", r.Name, r.Options) + // + //} return volume.Response{} } diff --git a/netshare/drivers/mounts.go b/netshare/drivers/mounts.go index 68918cb..54f223a 100644 --- a/netshare/drivers/mounts.go +++ b/netshare/drivers/mounts.go @@ -4,10 +4,12 @@ import ( "errors" log "github.com/Sirupsen/logrus" "github.com/docker/go-plugins-helpers/volume" + "strings" ) const ( ShareOpt = "share" + CreateOpt = "create" ) type mount struct { @@ -66,6 +68,14 @@ func (m *mountManager) GetOption(name, key string) string { return "" } +func (m *mountManager) GetOptionAsBool(name, key string) bool { + rv := strings.ToLower(m.GetOption(name, key)) + if rv == "yes" || rv == "true" { + return true + } + return false +} + func (m *mountManager) IsActiveMount(name string) bool { c, found := m.mounts[name] return found && c.connections > 0 @@ -88,12 +98,15 @@ func (m *mountManager) Add(name, hostdir string) { } } -func (m *mountManager) Create(name, hostdir string, opts map[string]string) { +func (m *mountManager) Create(name, hostdir string, opts map[string]string) *mount { c, found := m.mounts[name] if found && c.connections > 0 { c.opts = opts + return c } else { - m.mounts[name] = &mount{name: name, hostdir: hostdir, managed: true, opts: opts, connections: 0} + mnt := &mount{name: name, hostdir: hostdir, managed: true, opts: opts, connections: 0} + m.mounts[name] = mnt + return mnt } } diff --git a/netshare/drivers/nfs.go b/netshare/drivers/nfs.go index fd4eebc..ee61438 100644 --- a/netshare/drivers/nfs.go +++ b/netshare/drivers/nfs.go @@ -6,6 +6,7 @@ import ( "github.com/docker/go-plugins-helpers/volume" "os" "strings" + "path/filepath" ) const ( @@ -40,6 +41,8 @@ func (n nfsDriver) Mount(r volume.MountRequest) volume.Response { log.Debugf("Entering Mount: %v", r) n.m.Lock() defer n.m.Unlock() + + hostdir := mountpoint(n.root, r.Name) source := n.fixSource(r.Name, r.ID) @@ -63,6 +66,16 @@ func (n nfsDriver) Mount(r volume.MountRequest) volume.Response { return volume.Response{Err: err.Error()} } n.mountm.Add(r.Name, hostdir) + + if n.mountm.GetOption(r.Name, ShareOpt) != "" && n.mountm.GetOptionAsBool(r.Name, CreateOpt) { + log.Infof("Mount: Share and Create options enabled - using %s as sub-dir mount", r.Name) + datavol := filepath.Join(hostdir, r.Name) + if err := createDest(filepath.Join(hostdir, r.Name)); err != nil { + return volume.Response{Err: err.Error()} + } + hostdir = datavol + } + return volume.Response{Mountpoint: hostdir} } diff --git a/netshare/drivers/util.go b/netshare/drivers/util.go index 5c2114c..6e28a2a 100644 --- a/netshare/drivers/util.go +++ b/netshare/drivers/util.go @@ -25,8 +25,8 @@ func createDest(dest string) error { return nil } -func mountpoint(root, name string) string { - return filepath.Join(root, name) +func mountpoint(elem ...string) string { + return filepath.Join(elem...) } func run(cmd string) error {