Skip to content

Commit

Permalink
Add NFS mount driver (#38)
Browse files Browse the repository at this point in the history
* add nfs mount driver

* switch to unix.Mount for nfs mounting

* fix bad reference in nfs unmount

* fix bad reference in nfs unmount
  • Loading branch information
jlowellwofford authored Jul 23, 2021
1 parent ad57aa5 commit 65b06eb
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 0 deletions.
83 changes: 83 additions & 0 deletions internal/api/mount_nfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package api

import (
"fmt"
"net"
"strings"

"github.com/bensallen/rbd/pkg/mount"
"github.com/kraken-hpc/imageapi/models"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

func init() {
MountDrivers[models.MountKindNfs] = &MountDriverNFS{}
}

type MountDriverNFS struct {
log *logrus.Entry //
}

func (m *MountDriverNFS) Init(log *logrus.Entry) {
m.log = log
m.log.Trace("initialized")
}

func (m *MountDriverNFS) Mount(mnt *Mount) (ret *Mount, err error) {
l := m.log.WithField("operation", "mount")
if mnt.Nfs == nil {
l.Trace("attempted nfs mount with no mount definition")
return nil, ERRINVALDAT
}
l = l.WithFields(logrus.Fields{
"host": *mnt.Nfs.Host,
"path": *mnt.Nfs.Path,
})
// Resolve host
ips, err := net.LookupIP(*mnt.Nfs.Host)
if err != nil {
l.WithError(err).Debug("host resolution failed")
return nil, ERRINVALDAT
}
ip := ips[0] // should we be smarter about this?
// ok, we're good to attempt the mount
// make a mountpoint
flags := uintptr(0)
if mnt.Nfs.Ro != nil && *mnt.Nfs.Ro {
flags = unix.MS_RDONLY
}
version := "4.2"
if mnt.Nfs.Version != nil {
version = *mnt.Nfs.Version
}
mnt.Nfs.Options = append(mnt.Nfs.Options, fmt.Sprintf("addr=%s", ip.String()))
mnt.Nfs.Options = append(mnt.Nfs.Options, fmt.Sprintf("clientaddr=%s", ip.String()))
mnt.Nfs.Options = append(mnt.Nfs.Options, fmt.Sprintf("vers=%s", version))
// this doesn't work because u-root Mount is broken
//if err = mount.Mount(fmt.Sprintf("%s:%s", *mnt.Nfs.Host, *mnt.Nfs.Path), mnt.Mountpoint, "nfs", mnt.Nfs.MountOptions); err != nil {
if err = unix.Mount(fmt.Sprintf("%s:%s", *mnt.Nfs.Host, *mnt.Nfs.Path), mnt.Mountpoint, "nfs", flags, strings.Join(mnt.Nfs.Options, ",")); err != nil {
l.WithError(err).Error("failed to mount")
return nil, ERRFAIL
}
l.Info("successfully mounted")
return mnt, nil
}

func (m *MountDriverNFS) Unmount(mnt *Mount) (ret *Mount, err error) {
l := m.log.WithFields(logrus.Fields{
"operation": "unmount",
"id": mnt.ID,
"host": mnt.Nfs.Host,
"path": mnt.Nfs.Path,
})

// always lazy unmount. Good idea?
if err = mount.Unmount(mnt.Mountpoint, false, true); err != nil {
l.WithError(err).Error("unmount failed")
return nil, ERRFAIL
}
l.Info("successfully unmounted")
// garbage collection should do our cleanup
return mnt, nil
}
42 changes: 42 additions & 0 deletions models/mount.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions models/mount_nfs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 65b06eb

Please sign in to comment.